|
|
@ -1,6 +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 } from "../../shared/datatypes.ts"; |
|
|
|
import { getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; |
|
|
|
import { ButtonValues, getButtonValues, getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; |
|
|
|
|
|
|
|
|
|
|
|
const getSubmitAttributes = (key: string, targetState: BoardgameStateType) => ({ |
|
|
|
const getSubmitAttributes = (key: string, targetState: BoardgameStateType) => ({ |
|
|
|
type: "submit" as const, |
|
|
|
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) => { |
|
|
|
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 buttonValues = getButtonValues(gameState); |
|
|
|
|
|
|
|
|
|
|
|
const gameClasses = getDisplayStates(gameState, currentOutcome); |
|
|
|
const gameClasses = getDisplayStates(gameState, currentOutcome); |
|
|
|
const gameActiveClassNames = Object.entries(gameClasses) |
|
|
|
const gameActiveClassNames = Object.entries(gameClasses) |
|
|
|
.filter(([, value]) => value) |
|
|
|
.filter(([, value]) => value) |
|
|
|
.map(([name]) => name); |
|
|
|
.map(([name]) => name); |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<board-game track={key} class={gameActiveClassNames.join(" ")}> |
|
|
|
<board-game track={key} className={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> |
|
|
@ -64,34 +75,34 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul |
|
|
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-x-disabled"> |
|
|
|
<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 {...getGenericButtonAttributes("autoplayer-x-enable", { key, buttonValues })}> |
|
|
|
Make computer play for X. |
|
|
|
Make computer play for X. |
|
|
|
</button> |
|
|
|
</button> |
|
|
|
</p> |
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-x-enabled"> |
|
|
|
<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 {...getGenericButtonAttributes("autoplayer-x-disable", { key, buttonValues })}> |
|
|
|
Make them manually. |
|
|
|
Make them manually. |
|
|
|
</button> |
|
|
|
</button> |
|
|
|
</p> |
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-o-disabled"> |
|
|
|
<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 {...getGenericButtonAttributes("autoplayer-o-enable", { key, buttonValues })}> |
|
|
|
Make computer play for O. |
|
|
|
Make computer play for O. |
|
|
|
</button> |
|
|
|
</button> |
|
|
|
</p> |
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
|
|
<p class="when-autoplayer-o-enabled"> |
|
|
|
<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 {...getGenericButtonAttributes("autoplayer-o-disable", { key, buttonValues })}> |
|
|
|
Make them manually. |
|
|
|
Make them manually. |
|
|
|
</button> |
|
|
|
</button> |
|
|
|
</p> |
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
|
|
<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-not-in-progress">Start game</span> |
|
|
|
<span class="when-game-in-progress">Restart game</span> |
|
|
|
<span class="when-game-in-progress">Restart game</span> |
|
|
|
</button> |
|
|
|
</button> |
|
|
|