|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
import { sequence } from "../../shared/array-utils.ts"; |
|
|
|
|
import { BoardgameStateType, CurrentOutcome, GameRules, Player } from "../../shared/datatypes.ts"; |
|
|
|
|
import { getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; |
|
|
|
|
import { BoardgameStateType, CurrentOutcome, GameRules } from "../../shared/datatypes.ts"; |
|
|
|
|
import { ButtonValues, getButtonValues, getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; |
|
|
|
|
|
|
|
|
|
const getSubmitAttributes = (key: string, targetState: BoardgameStateType) => ({ |
|
|
|
|
type: "submit" as const, |
|
|
|
@ -30,15 +30,26 @@ const getCellHtml = ({ |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const getGenericButtonAttributes = ( |
|
|
|
|
kind: keyof ButtonValues, |
|
|
|
|
{ key, buttonValues }: { key: string; buttonValues: ButtonValues }, |
|
|
|
|
) => ({ |
|
|
|
|
...getSubmitAttributes(key, buttonValues[kind]), |
|
|
|
|
className: kind, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rules: GameRules) => { |
|
|
|
|
const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided; |
|
|
|
|
|
|
|
|
|
const buttonValues = getButtonValues(gameState); |
|
|
|
|
|
|
|
|
|
const gameClasses = getDisplayStates(gameState, currentOutcome); |
|
|
|
|
const gameActiveClassNames = Object.entries(gameClasses) |
|
|
|
|
.filter(([, value]) => value) |
|
|
|
|
.map(([name]) => name); |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<board-game track={key} class={gameActiveClassNames.join(" ")}> |
|
|
|
|
<board-game track={key} className={gameActiveClassNames.join(" ")}> |
|
|
|
|
<form method="post" is="progressive-form"> |
|
|
|
|
<p> |
|
|
|
|
Current player: <span class="current-player-name">{gameState.currentPlayerName}</span> |
|
|
|
@ -64,34 +75,34 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul |
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-x-disabled"> |
|
|
|
|
Currently X moves are made manually.{" "} |
|
|
|
|
<button class="autoplayer-x-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.X))}> |
|
|
|
|
<button {...getGenericButtonAttributes("autoplayer-x-enable", { key, buttonValues })}> |
|
|
|
|
Make computer play for X. |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-x-enabled"> |
|
|
|
|
Currently X moves are made by computer.{" "} |
|
|
|
|
<button class="autoplayer-x-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.X))}> |
|
|
|
|
<button {...getGenericButtonAttributes("autoplayer-x-disable", { key, buttonValues })}> |
|
|
|
|
Make them manually. |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-o-disabled"> |
|
|
|
|
Currently O moves are made manually.{" "} |
|
|
|
|
<button class="autoplayer-o-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.O))}> |
|
|
|
|
<button {...getGenericButtonAttributes("autoplayer-o-enable", { key, buttonValues })}> |
|
|
|
|
Make computer play for O. |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-o-enabled"> |
|
|
|
|
Currently O moves are made by computer.{" "} |
|
|
|
|
<button class="autoplayer-o-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.O))}> |
|
|
|
|
<button {...getGenericButtonAttributes("autoplayer-o-disable", { key, buttonValues })}> |
|
|
|
|
Make them manually. |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p> |
|
|
|
|
<button type="submit" class="game-start" name={key} value={gameState.withEmptyBoard().serialize()}> |
|
|
|
|
<button {...getGenericButtonAttributes("game-start", { key, buttonValues })}> |
|
|
|
|
<span class="when-game-not-in-progress">Start game</span> |
|
|
|
|
<span class="when-game-in-progress">Restart game</span> |
|
|
|
|
</button> |
|
|
|
|