76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import { api, unwrap } from "../api/client";
|
|
import { qk } from "../api/queryKeys";
|
|
import type { FactionRead, MatchCreate, MatchFinish, MatchRead } from "../domain/types";
|
|
|
|
export function useMatch(matchId: number | null) {
|
|
return useQuery({
|
|
queryKey: matchId ? qk.match(matchId) : ["match", "none"],
|
|
enabled: matchId != null,
|
|
queryFn: async () =>
|
|
unwrap(
|
|
await api.GET("/api/matches/{match_id}", {
|
|
params: { path: { match_id: matchId as number } },
|
|
}),
|
|
),
|
|
});
|
|
}
|
|
|
|
export function useRandomizeFaction() {
|
|
return useMutation({
|
|
mutationFn: async (args: { group_id: number; exclude_faction_ids: number[] }): Promise<FactionRead> => {
|
|
const res = unwrap(await api.POST("/api/matches/randomize-faction", { body: args }));
|
|
return res.faction;
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Этап 1: старт партии (игроки + фракции, без мест). */
|
|
export function useStartMatch() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (body: MatchCreate): Promise<MatchRead> =>
|
|
unwrap(await api.POST("/api/matches", { body })),
|
|
onSuccess: (m) => {
|
|
qc.invalidateQueries({ queryKey: qk.groupMatches(m.group_id) });
|
|
qc.invalidateQueries({ queryKey: qk.home });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Этап 2: завершение партии (места, причина победы). */
|
|
export function useFinishMatch() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (args: { matchId: number; body: MatchFinish }): Promise<MatchRead> =>
|
|
unwrap(
|
|
await api.POST("/api/matches/{match_id}/finish", {
|
|
params: { path: { match_id: args.matchId } },
|
|
body: args.body,
|
|
}),
|
|
),
|
|
onSuccess: (m) => {
|
|
qc.invalidateQueries({ queryKey: qk.match(m.id) });
|
|
qc.invalidateQueries({ queryKey: qk.groupMatches(m.group_id) });
|
|
qc.invalidateQueries({ queryKey: qk.groupStats(m.group_id) });
|
|
qc.invalidateQueries({ queryKey: qk.leaderboard });
|
|
qc.invalidateQueries({ queryKey: qk.home });
|
|
qc.invalidateQueries({ queryKey: qk.myStats });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteMatch() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (matchId: number) =>
|
|
unwrap(
|
|
await api.DELETE("/api/matches/{match_id}", {
|
|
params: { path: { match_id: matchId } },
|
|
}),
|
|
),
|
|
onSuccess: () => qc.invalidateQueries(),
|
|
});
|
|
}
|