less inline styles, more CSS

main
Inga 🏳‍🌈 5 days ago
parent c48825ef56
commit 15b63eefcf
  1. 35
      src/backend/components/boardgame.tsx
  2. 6
      src/frontend/components/board-game.ts
  3. 12
      src/frontend/static/style.css
  4. 6
      src/shared/display.ts

@ -1,15 +1,6 @@
import { sequence } from "../../shared/array-utils.ts"; import { sequence } from "../../shared/array-utils.ts";
import { BoardgameStateType, CurrentOutcome, GameRules, Player } from "../../shared/datatypes.ts"; import { BoardgameStateType, CurrentOutcome, GameRules, Player } from "../../shared/datatypes.ts";
import { ClassNames, getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; import { getCellDisplayData, getDisplayStates } from "../../shared/display.ts";
const getClassAndDisplayAttributes = (
className: ClassNames,
gameState: BoardgameStateType,
currentOutcome: CurrentOutcome,
) => ({
class: className,
style: getDisplayStates(gameState, currentOutcome)[className] ? {} : { display: "none" },
});
const getSubmitAttributes = (key: string, targetState: BoardgameStateType) => ({ const getSubmitAttributes = (key: string, targetState: BoardgameStateType) => ({
type: "submit" as const, type: "submit" as const,
@ -41,9 +32,13 @@ const getCellHtml = ({
export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rules: GameRules) => { export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rules: GameRules) => {
const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided; 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 ( return (
<board-game track={key}> <board-game track={key} class={gameActiveClassNames.join(" ")}>
<form method="post" is="progressive-form"> <form method="post" is="progressive-form">
<p> <p>
Current player: <span class="current-player-name">{gameState.currentPlayerName}</span> Current player: <span class="current-player-name">{gameState.currentPlayerName}</span>
@ -62,33 +57,33 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul
</table> </table>
<p> <p>
<span {...getClassAndDisplayAttributes("outcome-winx", gameState, currentOutcome)}>Player X won</span> <span class="when-outcome-winx">Player X won</span>
<span {...getClassAndDisplayAttributes("outcome-wino", gameState, currentOutcome)}>Player O won</span> <span class="when-outcome-wino">Player O won</span>
<span {...getClassAndDisplayAttributes("outcome-draw", gameState, currentOutcome)}>Draw</span> <span class="when-outcome-draw">Draw</span>
</p> </p>
<p {...getClassAndDisplayAttributes("autoplayer-x-disabled", gameState, currentOutcome)}> <p class="when-autoplayer-x-disabled">
Currently X moves are made manually.{" "} Currently X moves are made manually.{" "}
<button class="autoplayer-x-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.X))}> <button class="autoplayer-x-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.X))}>
Make computer play for X. Make computer play for X.
</button> </button>
</p> </p>
<p {...getClassAndDisplayAttributes("autoplayer-x-enabled", gameState, currentOutcome)}> <p class="when-autoplayer-x-enabled">
Currently X moves are made by computer.{" "} Currently X moves are made by computer.{" "}
<button class="autoplayer-x-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.X))}> <button class="autoplayer-x-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.X))}>
Make them manually. Make them manually.
</button> </button>
</p> </p>
<p {...getClassAndDisplayAttributes("autoplayer-o-disabled", gameState, currentOutcome)}> <p class="when-autoplayer-o-disabled">
Currently O moves are made manually.{" "} Currently O moves are made manually.{" "}
<button class="autoplayer-o-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.O))}> <button class="autoplayer-o-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.O))}>
Make computer play for O. Make computer play for O.
</button> </button>
</p> </p>
<p {...getClassAndDisplayAttributes("autoplayer-o-enabled", gameState, currentOutcome)}> <p class="when-autoplayer-o-enabled">
Currently O moves are made by computer.{" "} Currently O moves are made by computer.{" "}
<button class="autoplayer-o-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.O))}> <button class="autoplayer-o-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.O))}>
Make them manually. Make them manually.
@ -97,8 +92,8 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul
<p> <p>
<button type="submit" class="game-start" name={key} value={gameState.withEmptyBoard().serialize()}> <button type="submit" class="game-start" name={key} value={gameState.withEmptyBoard().serialize()}>
<span {...getClassAndDisplayAttributes("start-new-game", gameState, currentOutcome)}>Start game</span> <span class="when-game-not-in-progress">Start game</span>
<span {...getClassAndDisplayAttributes("start-clear-game", gameState, currentOutcome)}>Restart game</span> <span class="when-game-in-progress">Restart game</span>
</button> </button>
</p> </p>
</form> </form>

@ -27,10 +27,8 @@ export class BoardGameComponent extends HTMLElement {
const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided; const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided;
const displayStates = getDisplayStates(gameState, currentOutcome); const displayStates = getDisplayStates(gameState, currentOutcome);
for (const [className, shouldDisplay] of Object.entries(displayStates)) { for (const [className, shouldEnable] of Object.entries(displayStates)) {
for (const element of this.querySelectorAll(`.${className}`)) { this.classList.toggle(className, shouldEnable);
(element as HTMLElement).style.display = shouldDisplay ? "" : "none";
}
} }
for (const playerNameElement of this.querySelectorAll(".current-player-name")) { for (const playerNameElement of this.querySelectorAll(".current-player-name")) {

@ -6,3 +6,15 @@
li form { li form {
display: inline; 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;
}

@ -8,12 +8,10 @@ export const getDisplayStates = (gameState: BoardgameStateType, currentOutcome:
"autoplayer-x-disabled": !gameState.autoPlayers.has(Player.X), "autoplayer-x-disabled": !gameState.autoPlayers.has(Player.X),
"autoplayer-o-enabled": gameState.autoPlayers.has(Player.O), "autoplayer-o-enabled": gameState.autoPlayers.has(Player.O),
"autoplayer-o-disabled": !gameState.autoPlayers.has(Player.O), "autoplayer-o-disabled": !gameState.autoPlayers.has(Player.O),
"start-new-game": !gameState.board, "game-in-progress": !!gameState.board,
"start-clear-game": gameState.board, "game-not-in-progress": !gameState.board,
}); });
export type ClassNames = keyof ReturnType<typeof getDisplayStates>;
export const getCellDisplayData = ({ export const getCellDisplayData = ({
gameState, gameState,
currentOutcome, currentOutcome,

Loading…
Cancel
Save