|
|
@ -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> |
|
|
|