diff --git a/src/actions/index.ts b/src/actions/index.ts index fc4caab..437ffa6 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -1,5 +1,6 @@ import { defineAction, z } from 'astro:actions'; import { db, eq, Game, Player, PlayerInGame, Scores, sql } from 'astro:db'; +import { getMaxRounds } from 'src/utils/game'; import { MAX_PLAYERS, MIN_PLAYERS } from '../constants'; import { createHumanId } from '../utils/id'; @@ -81,6 +82,26 @@ export const server = { .where(eq(Scores.id, `${gameId}-${playerId}-${round}`)), ), ); + const maxRounds = getMaxRounds(playerIds.length); + + if (round === maxRounds) { + await db + .update(Game) + .set({ endedAt: new Date() }) + .where(eq(Game.id, gameId)); + } + }, + }), + endGame: defineAction({ + accept: 'form', + input: z.object({ + gameId: z.string(), + }), + handler: async ({ gameId }) => { + await db + .update(Game) + .set({ endedAt: new Date() }) + .where(eq(Game.id, gameId)); }, }), }; diff --git a/src/db.ts b/src/db.ts index 749178e..6042eee 100644 --- a/src/db.ts +++ b/src/db.ts @@ -61,15 +61,12 @@ export async function getScoreCard( } const delta = calculateScoreDelta(score); + const total = delta ? (runningTotals[player.id] || 0) + delta : null; - if (!delta) { - return score; + if (total) { + runningTotals[player.id] = total; } - const total = (runningTotals[player.id] || 0) + delta; - - runningTotals[player.id] = total; - return { ...score, delta, total }; }); }); diff --git a/src/pages/game/[gameId]/index.astro b/src/pages/game/[gameId]/index.astro index a41ea1f..96b6af4 100644 --- a/src/pages/game/[gameId]/index.astro +++ b/src/pages/game/[gameId]/index.astro @@ -14,6 +14,10 @@ if (!game) { return Astro.redirect('/404'); } +if (game.endedAt) { + return Astro.redirect(`/game/${game.id}/scores`); +} + const scores = await getScores(gameId); if (scores.length === 0) { diff --git a/src/pages/game/[gameId]/round/[round]/bids.astro b/src/pages/game/[gameId]/round/[round]/bids.astro index 09325c8..dd1ca2c 100644 --- a/src/pages/game/[gameId]/round/[round]/bids.astro +++ b/src/pages/game/[gameId]/round/[round]/bids.astro @@ -35,7 +35,7 @@ if (result && !result.error) { const players = await getPlayers(gameId, round); const scores = await getScores(gameId, round); -const maxRounds = getMaxRounds(players); +const maxRounds = getMaxRounds(players.length); const description = 'Each player predicts how many tricks they will take this round.'; diff --git a/src/pages/game/[gameId]/round/[round]/dealer.astro b/src/pages/game/[gameId]/round/[round]/dealer.astro index 59b2b51..d868ad7 100644 --- a/src/pages/game/[gameId]/round/[round]/dealer.astro +++ b/src/pages/game/[gameId]/round/[round]/dealer.astro @@ -28,7 +28,7 @@ if (!game) { const players = await getPlayers(gameId, round); const dealer = players[players.length - 1]!; -const maxRounds = getMaxRounds(players); +const maxRounds = getMaxRounds(players.length); const description = `${dealer.name} deals ${round} ${pluralize({ singular: 'card', plural: 'cards' }, round)}.`; --- diff --git a/src/pages/game/[gameId]/round/[round]/tricks.astro b/src/pages/game/[gameId]/round/[round]/tricks.astro index 7f44daa..5275398 100644 --- a/src/pages/game/[gameId]/round/[round]/tricks.astro +++ b/src/pages/game/[gameId]/round/[round]/tricks.astro @@ -27,16 +27,19 @@ if (!game) { return Astro.redirect('/404'); } +const players = await getPlayers(gameId, round); +const scores = await getScores(gameId, round); +const maxRounds = getMaxRounds(players.length); + const result = Astro.getActionResult(actions.updateTricks); if (result && !result.error) { + if (round === maxRounds || round > maxRounds) { + return Astro.redirect(`/game/${gameId}/scores`); + } return Astro.redirect(`/game/${gameId}/round/${round + 1}/dealer`); } -const players = await getPlayers(gameId, round); -const scores = await getScores(gameId, round); -const maxRounds = getMaxRounds(players); - const description = 'Count how many tricks each player managed to take.'; --- diff --git a/src/pages/game/[gameId]/scores.astro b/src/pages/game/[gameId]/scores.astro index b5af597..f3ca05e 100644 --- a/src/pages/game/[gameId]/scores.astro +++ b/src/pages/game/[gameId]/scores.astro @@ -1,4 +1,6 @@ --- +import { actions } from 'astro:actions'; + import Layout from '../../../layouts/Standard.astro'; import { getGame, getPlayers, getScoreCard } from '../../../db'; @@ -53,6 +55,10 @@ const scoreCard = await getScoreCard(gameId, players); } +
+ + +