Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

game-config.ts

119 lines3.8 KB

1/**
2 * Shared game configuration used by the frontend and Postgres writer.
3 *
4 * This is the SINGLE SOURCE OF TRUTH for game-related constants.
5 * Both src/lib/config.ts and server/store.ts import from here.
6 */
7​
8// =============================================================================
9// Game End Conditions
10// =============================================================================
11​
12/**
13 * Game end mode:
14 * - "board_complete": Game ends when all boxes are claimed, leader wins
15 * - "first_to_score": Game ends when a team reaches the target score
16 */
17export type GameEndMode = `board_complete` | `first_to_score`
18​
19/**
20 * Current game end mode.
21 * Change this to test different end conditions.
22 */
23export const GAME_END_MODE: GameEndMode = `board_complete`
24​
25/**
26 * Target score for "first_to_score" mode.
27 * Game ends when any team reaches this number of boxes.
28 * Only used when GAME_END_MODE is "first_to_score".
29 */
30export const FIRST_TO_SCORE_TARGET = 1000
31​
32// =============================================================================
33// Game Grid Configuration
34// =============================================================================
35​
36/**
37 * Grid dimensions (number of boxes in each direction).
38 * Total boxes = W * H = 1,000,000
39 */
40export const GRID_WIDTH = 1000
41export const GRID_HEIGHT = 1000
42​
43/**
44 * Total number of horizontal edges: W * (H + 1)
45 */
46export const HORIZ_EDGE_COUNT = GRID_WIDTH * (GRID_HEIGHT + 1)
47​
48/**
49 * Total number of vertical edges: H * (W + 1)
50 */
51export const VERT_EDGE_COUNT = GRID_HEIGHT * (GRID_WIDTH + 1)
52​
53/**
54 * Total number of edges in the game.
55 */
56export const TOTAL_EDGE_COUNT = HORIZ_EDGE_COUNT + VERT_EDGE_COUNT
57​
58/**
59 * Total number of boxes in the game.
60 */
61export const TOTAL_BOX_COUNT = GRID_WIDTH * GRID_HEIGHT
62​
63// =============================================================================
64// Quota Configuration (Shared: Server + Client)
65// =============================================================================
66//
67// HOW QUOTA WORKS:
68// - Each player has a token bucket with MAX_QUOTA tokens (default: 8)
69// - Drawing a line costs 1 token
70// - Tokens refill at a rate of 1 per QUOTA_REFILL_INTERVAL_MS (default: 3s)
71// - BONUS: Completing a box grants bonus tokens!
72// - Complete 1 box: get QUOTA_BONUS_SINGLE tokens back (refund + bonus)
73// - Complete 2 boxes with one line: get QUOTA_BONUS_DOUBLE tokens back
74// This mimics the "extra turn" mechanic from classic Dots & Boxes.
75// - The Postgres writer is authoritative; client tracks optimistically
76// for instant UI feedback, then syncs with server response.
77//
78// =============================================================================
79​
80/**
81 * Maximum number of tokens (lines) a player can have.
82 */
83export const MAX_QUOTA = 8
84​
85/**
86 * Time interval for refilling one quota token (in ms).
87 * At 3000ms, a player gets 1 token every 3 seconds.
88 */
89export const QUOTA_REFILL_INTERVAL_MS = 3000
90​
91/**
92 * Bonus tokens granted for completing 1 box with a single move.
93 * This includes the refund for the move spent plus a bonus.
94 */
95export const QUOTA_BONUS_SINGLE = 2
96​
97/**
98 * Bonus tokens granted for completing 2 boxes with a single move.
99 * This includes the refund for the move spent plus a bonus.
100 */
101export const QUOTA_BONUS_DOUBLE = 4
102​
103/**
104 * Timing leeway in ms for quota consumption (server-side).
105 * Allows moves if a refill would happen within this window.
106 * Accounts for client/server clock differences and network latency.
107 */
108export const QUOTA_TIMING_LEEWAY_MS = 500
109​
110/**
111 * LocalStorage key for persisting quota state (client-side optimistic UI).
112 */
113export const QUOTA_STORAGE_KEY = `boxes:quota`
114​
115/**
116 * LocalStorage key for storing the game start timestamp.
117 * Used to detect game resets and clear stale client data.
118 */
119export const GAME_START_TIMESTAMP_KEY = `boxes:game-start`