Skip to content

Commit

Permalink
Group games into leagues
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Aug 11, 2024
1 parent 97c641a commit 1b6798b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
6 changes: 1 addition & 5 deletions db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { db, Game, Player, PlayerInGame, Scores } from 'astro:db';

import { createId } from '../src/utils/id';

const games = [
{ id: 'KVMGQ' },
// TODO: Add complete game data
{ id: 'MFFRA', endedAt: new Date() },
];
const games = [{ id: 'KVMGQ' }];

const players = [
{ id: createId(), name: 'Apple' },
Expand Down
34 changes: 29 additions & 5 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { defineAction, z } from 'astro:actions';
import { db, eq, Game, Player, PlayerInGame, Scores, sql } from 'astro:db';
import { getMaxRounds } from 'src/utils/game';
import {
db,
eq,
Game,
League,
Player,
PlayerInGame,
Scores,
sql,
} from 'astro:db';
import { inDaysFromNow } from 'src/utils/date';

import { MAX_PLAYERS, MIN_PLAYERS } from '../constants';
import { LEAGUE_COOKIE, MAX_PLAYERS, MIN_PLAYERS } from '../constants';
import { getMaxRounds } from '../utils/game';
import { createHumanId } from '../utils/id';

export const server = {
Expand All @@ -12,12 +22,26 @@ export const server = {
playerIds: z.array(z.string()).min(MIN_PLAYERS).max(MAX_PLAYERS),
playerNames: z.array(z.string()).min(MIN_PLAYERS).max(MAX_PLAYERS),
}),
handler: async ({ playerIds, playerNames }) => {
handler: async ({ playerIds, playerNames }, context) => {
let leagueId: string;
if (context.cookies.has(LEAGUE_COOKIE)) {
leagueId = context.cookies.get(LEAGUE_COOKIE)!.value;
} else {
leagueId = createHumanId();
await db.insert(League).values({ id: leagueId });
}

context.cookies.set(LEAGUE_COOKIE, leagueId, {
path: '/',
sameSite: 'strict',
expires: inDaysFromNow(90),
});

const players = playerIds
.map((id, index) => ({ id, name: playerNames[index] as string }))
.filter((player) => Boolean(player.name));

const game = { id: createHumanId() };
const game = { id: createHumanId(), leagueId };
const playersInGame = players.map((player, index) => ({
id: `${game.id}-${player.id}`,
gameId: game.id,
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const MAX_PLAYERS = 6;
// TODO: Make configurable per game?
export const POINTS_CORRECT = 20;
export const POINTS_PER_TRICK = 10;

export const LEAGUE_COOKIE = 'wizard-league';
10 changes: 8 additions & 2 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { createArray, shiftArray } from './utils/array';
import { calculateScoreDelta } from './utils/game';
import type { GameId } from './types';

export function getGames() {
return db.select().from(Game).orderBy(Game.endedAt);
export function getGames(leagueId: string | undefined) {
return leagueId
? db
.select()
.from(Game)
.where(eq(Game.leagueId, leagueId))
.orderBy(Game.endedAt)
: [];
}

export function getGame(gameId: GameId) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import Layout from '../layouts/Standard.astro';
import GameCard from '../components/GameCard.astro';
import { getGames } from '../db';
import { MAX_PLAYERS, MIN_PLAYERS } from '../constants';
import { LEAGUE_COOKIE, MAX_PLAYERS, MIN_PLAYERS } from '../constants';
const title = 'Wizard Scorekeeper';
const description =
'Keep track of the scores in the trick-taking card game Wizard for three to six players';
const games = await getGames();
const games = await getGames(Astro.cookies.get(LEAGUE_COOKIE)?.value);
const hasActiveGame = games.some(game => !game.endedAt)
---

Expand Down
5 changes: 5 additions & 0 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ const dateTimeFormat = new Intl.DateTimeFormat(LOCALE, {
export function formatDate(date: Date): string {
return dateTimeFormat.format(date);
}

export function inDaysFromNow(days: number) {
const today = new Date();
return new Date(today.getTime() + days * 24 * 60 * 60 * 1000);
}

0 comments on commit 1b6798b

Please sign in to comment.