(tic-tac-toe): Laid the foundations for tic tac toe/database interactions
This commit is contained in:
parent
7922f6eac1
commit
b757cb6bb9
2 changed files with 64 additions and 0 deletions
|
|
@ -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)
|
||||
);
|
||||
|
|
|
|||
55
src/@shared/src/database/mixin/tictactoe.ts
Normal file
55
src/@shared/src/database/mixin/tictactoe.ts
Normal file
|
|
@ -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<TemplateData | undefined>,
|
||||
};
|
||||
|
||||
export const TicTacToeImpl: Omit<ITicTacToeDb, keyof Database> = {
|
||||
/**
|
||||
* @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<TemplateData | undefined> {
|
||||
// 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<boolean> {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// this function will never be able to be called outside of this module
|
||||
// async function privateFunction(): Promise<string | undefined> {
|
||||
// return undefined;
|
||||
// }
|
||||
|
||||
// silence warnings
|
||||
void privateFunction;
|
||||
Loading…
Add table
Add a link
Reference in a new issue