diff --git a/src/backend/app.ts b/src/backend/app.ts index 1f4f353..d3fbe73 100644 --- a/src/backend/app.ts +++ b/src/backend/app.ts @@ -1,7 +1,7 @@ import path from "node:path"; import bodyParser from "body-parser"; import express from "express"; -import { mainPageHandler } from "./main.tsx"; +import { mainPageHandler } from "./main/index.tsx"; import { createProgressiveForm } from "./progressive-form.ts"; export const createApp = () => { diff --git a/src/backend/components/boardgame.tsx b/src/backend/components/boardgame.tsx new file mode 100644 index 0000000..5e29329 --- /dev/null +++ b/src/backend/components/boardgame.tsx @@ -0,0 +1,51 @@ +import { sequence } from "../../shared/array-utils.ts"; +import { BoardgameStateType, SquareState, formatSquareState } from "../../shared/datatypes.ts"; + +const getCellHtml = (key: string, state: BoardgameStateType, row: number, column: number) => { + if (!state.board) { + return ( + + {" "} + + ); + } + + const squareState = state.board.get(row, column); + const nextGameState = squareState === SquareState.Unoccupied ? state.withMove(row, column) : state; + return ( + + {formatSquareState(squareState)} + + ); +}; + +export const getBoardgameHtml = (key: string, state: BoardgameStateType) => { + return ( + + + Current player: {state.currentPlayerName} + + + + {sequence(state.rows).map((row) => ( + + {sequence(state.columns).map((column) => ( + {getCellHtml(key, state, row, column)} + ))} + + ))} + + + + + Start game + + + + ); +}; diff --git a/src/backend/main/boardgame-handler.ts b/src/backend/main/boardgame-handler.ts new file mode 100644 index 0000000..7438017 --- /dev/null +++ b/src/backend/main/boardgame-handler.ts @@ -0,0 +1,19 @@ +import type { Request, Response } from "express"; +import { rewriteQueryParamsWith, safeGetQueryValue } from "../utils.ts"; +import { BoardgameState } from "../../shared/boardgame-state.ts"; +import { getBoardgameHtml } from "../components/boardgame.tsx"; + +// Returns nothing if query parameter is uninitialized and a redirect was made, +// or component if query parameter is initialized. +export const handleBoardgame = (req: Request, res: Response, key: string) => { + const serializedState = safeGetQueryValue(req, key); + const state = BoardgameState.fromSerialized(serializedState); + + if (!state) { + const newState = BoardgameState.createWithoutBoard(3, 3); + rewriteQueryParamsWith(req, res, { [key]: newState.serialize() }); + return; + } + + return getBoardgameHtml(key, state); +}; diff --git a/src/backend/main.tsx b/src/backend/main/index.tsx similarity index 89% rename from src/backend/main.tsx rename to src/backend/main/index.tsx index 366965a..da90ab0 100644 --- a/src/backend/main.tsx +++ b/src/backend/main/index.tsx @@ -1,11 +1,18 @@ import type { RequestHandler } from "express"; -import { safeGetQueryValue, sendHtml } from "./utils.ts"; +import { safeGetQueryValue, sendHtml } from "../utils.ts"; +import { handleBoardgame } from "./boardgame-handler.ts"; export const mainPageHandler: RequestHandler = (req, res) => { const counters = { a: parseInt(safeGetQueryValue(req, "a") ?? "0", 10), b: parseInt(safeGetQueryValue(req, "b") ?? "0", 10), }; + + const board1 = handleBoardgame(req, res, "tictactoe1"); + if (!board1) { + return; + } + sendHtml( res, @@ -60,6 +67,7 @@ export const mainPageHandler: RequestHandler = (req, res) => { ))} + {board1}
Current player: {state.currentPlayerName}