From 014ab9fa702fd37bca12dc49051db5d0a2feef29 Mon Sep 17 00:00:00 2001 From: apetitco Date: Sat, 20 Dec 2025 00:45:49 +0100 Subject: [PATCH] (tic-tac-toe): Laid the foundations for tic tac toe/database interactions --- src/@shared/src/database/init.sql | 9 ++++ src/@shared/src/database/mixin/tictactoe.ts | 55 +++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/@shared/src/database/mixin/tictactoe.ts diff --git a/src/@shared/src/database/init.sql b/src/@shared/src/database/init.sql index 2406225..f2d2af4 100644 --- a/src/@shared/src/database/init.sql +++ b/src/@shared/src/database/init.sql @@ -23,3 +23,12 @@ CREATE TABLE IF NOT EXISTS blocked ( CREATE UNIQUE INDEX IF NOT EXISTS idx_blocked_user_pair ON blocked(user, blocked); +CREATE TABLE IF NOT EXISTS tictactoe ( + id INTEGER PRIMARY KEY NOT NULL, + player1 TEXT NOT NULL, + player2 TEXT NOT NULL, + outcome TEXT NOT NULL, + + FOREIGN KEY(player1) REFERENCES user(id) + FOREIGN KEY(player2) REFERENCES user(id) +); diff --git a/src/@shared/src/database/mixin/tictactoe.ts b/src/@shared/src/database/mixin/tictactoe.ts new file mode 100644 index 0000000..5fb7c5c --- /dev/null +++ b/src/@shared/src/database/mixin/tictactoe.ts @@ -0,0 +1,55 @@ +import { isNullish } from '@shared/utils'; +import type { Database } from './_base'; +import { UserId } from './user'; + +// describe every function in the object +export interface ITicTacToeDb extends Database { + normalFunction(id: TemplateId): TemplateData | undefined, + asyncFunction(id: TemplateId): Promise, +}; + +export const TicTacToeImpl: Omit = { + /** + * @brief Write the outcome of the specified game to the database. + * + * @param gameId The game we want to write the outcome of. + * + */ + setGameOutcome(this: ITicTacToeDb, id: GameId): void { + // Find a way to retrieve the outcome of the game. + this.prepare('INSERT INTO tictactoe (game, outcome) VALUES (@id, "draw" /* replace w/ game outcome */)').run({ id }); + }, + /** + * whole function description + * + * @param id the argument description + * + * @returns what does the function return ? + */ +// async asyncFunction(this: ITemplateDb, id: TemplateId): Promise { +// void id; +// return undefined; +// }, +}; + +export type TicTacToeId = number & { readonly __brand: unique symbol }; + +export type TemplateData = { + readonly id: TicTacToeId; + readonly player1: string; + readonly player2: string; + readonly outcome: string; +}; + +// this function will be able to be called from everywhere +// export async function freeFloatingExportedFunction(): Promise { +// return false; +// } + +// this function will never be able to be called outside of this module +// async function privateFunction(): Promise { +// return undefined; +// } + +// silence warnings +void privateFunction;