feat(multi): add game mode selection and surrender button for all_finish mode
This commit is contained in:
+13
-3
@@ -152,11 +152,11 @@ export function useMultiGame() {
|
||||
|
||||
// Room actions
|
||||
|
||||
async function createRoom(playerName: string, maxPlayers = 16, totalRounds = 3): Promise<{ error?: string }> {
|
||||
async function createRoom(playerName: string, maxPlayers = 16, totalRounds = 3, gameMode: "race" | "all_finish" = "race"): Promise<{ error?: string }> {
|
||||
const res = await fetch("/api/rooms", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ playerName, maxPlayers, totalRounds }),
|
||||
body: JSON.stringify({ playerName, maxPlayers, totalRounds, gameMode }),
|
||||
});
|
||||
const data = await res.json() as { room?: Room; playerId?: string; error?: string };
|
||||
if (!res.ok) return { error: data.error ?? "Erreur" };
|
||||
@@ -195,6 +195,16 @@ export function useMultiGame() {
|
||||
return {};
|
||||
}
|
||||
|
||||
async function surrender() {
|
||||
if (!room || !playerId) return;
|
||||
const res = await fetch(`/api/rooms/${room.code}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ action: "surrender", playerId }),
|
||||
});
|
||||
if (res.ok) setRoom((await res.json() as { room: Room }).room);
|
||||
}
|
||||
|
||||
async function nextRound() {
|
||||
if (!room || !playerId) return;
|
||||
const res = await fetch(`/api/rooms/${room.code}`, {
|
||||
@@ -247,7 +257,7 @@ export function useMultiGame() {
|
||||
return {
|
||||
room, playerId, html, title, loading, loadError,
|
||||
history, clicks: clicksDisplay, elapsed: timer.elapsed, countdown,
|
||||
createRoom, joinRoom, startGame, nextRound, resetGame, leave, navigate, restore,
|
||||
createRoom, joinRoom, startGame, nextRound, resetGame, leave, navigate, surrender, restore,
|
||||
retryLoad: () => title && loadArticle(title),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user