Skip to content

Commit

Permalink
Add logic to end game
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Aug 7, 2024
1 parent 0cee439 commit 1ae7113
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 20 deletions.
21 changes: 21 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
},
}),
};
9 changes: 3 additions & 6 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
});
});
Expand Down
4 changes: 4 additions & 0 deletions src/pages/game/[gameId]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/game/[gameId]/round/[round]/bids.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
2 changes: 1 addition & 1 deletion src/pages/game/[gameId]/round/[round]/dealer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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)}.`;
---
Expand Down
11 changes: 7 additions & 4 deletions src/pages/game/[gameId]/round/[round]/tricks.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
---

Expand Down
7 changes: 7 additions & 0 deletions src/pages/game/[gameId]/scores.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import { actions } from 'astro:actions';
import Layout from '../../../layouts/Standard.astro';
import { getGame, getPlayers, getScoreCard } from '../../../db';
Expand Down Expand Up @@ -53,6 +55,10 @@ const scoreCard = await getScoreCard(gameId, players);
}
</tbody>
</table>
<form method="POST" action={actions.endGame}>
<input type="hidden" name="gameId" value={gameId} />
<button class="button destructive" type="submit">End game</button>
</form>
</Layout>

<style>
Expand All @@ -61,6 +67,7 @@ const scoreCard = await getScoreCard(gameId, players);
table-layout: fixed;
border-collapse: collapse;
text-align: center;
margin-bottom: var(--layout-gutter);
}

thead {
Expand Down
5 changes: 0 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ import type { Game, Scores } from 'astro:db';
export type GameId = (typeof Game.$inferSelect)['id'];
export type Round = number;
export type Score = typeof Scores.$inferSelect;
export type Player = {
id: string;
name: string;
position: number;
};
6 changes: 3 additions & 3 deletions src/utils/game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Player, Score } from '../types';
import type { Score } from '../types';
import {
NUMBER_OF_CARDS,
POINTS_CORRECT,
Expand All @@ -19,6 +19,6 @@ export function calculateScoreDelta(score: Score | null | undefined) {
return -1 * Math.abs(score.tricks - score.bid) * POINTS_PER_TRICK;
}

export function getMaxRounds(players: Player[]) {
return NUMBER_OF_CARDS / players.length;
export function getMaxRounds(numberOfPlayers: number) {
return NUMBER_OF_CARDS / numberOfPlayers;
}

0 comments on commit 1ae7113

Please sign in to comment.