From 15b63eefcf5f35d38d36350ff25876aa90eb293e Mon Sep 17 00:00:00 2001 From: Inga Date: Tue, 19 Nov 2024 17:36:25 +0000 Subject: [PATCH] less inline styles, more CSS --- src/backend/components/boardgame.tsx | 35 ++++++++++++--------------- src/frontend/components/board-game.ts | 6 ++--- src/frontend/static/style.css | 16 ++++++++++-- src/shared/display.ts | 6 ++--- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/backend/components/boardgame.tsx b/src/backend/components/boardgame.tsx index 3836758..5b62ef0 100644 --- a/src/backend/components/boardgame.tsx +++ b/src/backend/components/boardgame.tsx @@ -1,15 +1,6 @@ import { sequence } from "../../shared/array-utils.ts"; import { BoardgameStateType, CurrentOutcome, GameRules, Player } from "../../shared/datatypes.ts"; -import { ClassNames, getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; - -const getClassAndDisplayAttributes = ( - className: ClassNames, - gameState: BoardgameStateType, - currentOutcome: CurrentOutcome, -) => ({ - class: className, - style: getDisplayStates(gameState, currentOutcome)[className] ? {} : { display: "none" }, -}); +import { getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; const getSubmitAttributes = (key: string, targetState: BoardgameStateType) => ({ type: "submit" as const, @@ -41,9 +32,13 @@ const getCellHtml = ({ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rules: GameRules) => { const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided; + const gameClasses = getDisplayStates(gameState, currentOutcome); + const gameActiveClassNames = Object.entries(gameClasses) + .filter(([, value]) => value) + .map(([name]) => name); return ( - +

Current player: {gameState.currentPlayerName} @@ -62,33 +57,33 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul

- Player X won - Player O won - Draw + Player X won + Player O won + Draw

-

+

Currently X moves are made manually.{" "}

-

+

Currently X moves are made by computer.{" "}

-

+

Currently O moves are made manually.{" "}

-

+

Currently O moves are made by computer.{" "}

diff --git a/src/frontend/components/board-game.ts b/src/frontend/components/board-game.ts index 8f475d4..916ea3f 100644 --- a/src/frontend/components/board-game.ts +++ b/src/frontend/components/board-game.ts @@ -27,10 +27,8 @@ export class BoardGameComponent extends HTMLElement { const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided; const displayStates = getDisplayStates(gameState, currentOutcome); - for (const [className, shouldDisplay] of Object.entries(displayStates)) { - for (const element of this.querySelectorAll(`.${className}`)) { - (element as HTMLElement).style.display = shouldDisplay ? "" : "none"; - } + for (const [className, shouldEnable] of Object.entries(displayStates)) { + this.classList.toggle(className, shouldEnable); } for (const playerNameElement of this.querySelectorAll(".current-player-name")) { diff --git a/src/frontend/static/style.css b/src/frontend/static/style.css index 72f4c57..124d5cf 100644 --- a/src/frontend/static/style.css +++ b/src/frontend/static/style.css @@ -4,5 +4,17 @@ } li form { - display: inline; -} \ No newline at end of file + display: inline; +} + +board-game:not(.outcome-winx) .when-outcome-winx, +board-game:not(.outcome-wino) .when-outcome-wino, +board-game:not(.outcome-draw) .when-outcome-draw, +board-game:not(.autoplayer-x-enabled) .when-autoplayer-x-enabled, +board-game:not(.autoplayer-x-disabled) .when-autoplayer-x-disabled, +board-game:not(.autoplayer-o-enabled) .when-autoplayer-o-enabled, +board-game:not(.autoplayer-o-disabled) .when-autoplayer-o-disabled, +board-game:not(.game-in-progress) .when-game-in-progress, +board-game:not(.game-not-in-progress) .when-game-not-in-progress { + display: none; +} diff --git a/src/shared/display.ts b/src/shared/display.ts index 3eaa5be..00e8a2e 100644 --- a/src/shared/display.ts +++ b/src/shared/display.ts @@ -8,12 +8,10 @@ export const getDisplayStates = (gameState: BoardgameStateType, currentOutcome: "autoplayer-x-disabled": !gameState.autoPlayers.has(Player.X), "autoplayer-o-enabled": gameState.autoPlayers.has(Player.O), "autoplayer-o-disabled": !gameState.autoPlayers.has(Player.O), - "start-new-game": !gameState.board, - "start-clear-game": gameState.board, + "game-in-progress": !!gameState.board, + "game-not-in-progress": !gameState.board, }); -export type ClassNames = keyof ReturnType; - export const getCellDisplayData = ({ gameState, currentOutcome,