From f4b0d337ac9038a949e355b6b77e758dadfca03d Mon Sep 17 00:00:00 2001 From: Maciej Caderek Date: Sat, 12 Feb 2022 15:04:08 +0100 Subject: [PATCH] WIP --- src/player/Player.ts | 21 +++++++++++++++------ src/state.ts | 6 ++++-- src/ui/components/Moves.css | 5 +++++ src/ui/components/Moves.tsx | 11 +++++++++-- src/ui/components/Share.tsx | 4 ++-- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/player/Player.ts b/src/player/Player.ts index 62e3852..0f5136c 100644 --- a/src/player/Player.ts +++ b/src/player/Player.ts @@ -1,6 +1,7 @@ import { GameConfig } from "../types"; import Board from "../board/Board"; import Game from "../game/Game"; +import { setState } from "../state"; const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); @@ -22,6 +23,14 @@ class Player { this.config = { ...this.config, ...config }; } + getPosition() { + const position = this.game.getPosition(this.ply); + setState({ ply: position.ply }); + setState({ fen: position.fen }); + + return position; + } + async load(game: Game) { this.pause(); await this.firstRender; @@ -29,7 +38,7 @@ class Player { this.game = game; this.ply = 0; - await this.board.frame(this.game.getPosition(this.ply), this.game.header); + await this.board.frame(this.getPosition(), this.game.header); this.board.render(); } @@ -65,7 +74,7 @@ class Player { return; } - await this.board.frame(this.game.getPosition(ply), this.game.header); + await this.board.frame(this.getPosition(), this.game.header); this.board.render(); } @@ -78,28 +87,28 @@ class Player { this.ply = ply; - await this.board.frame(this.game.getPosition(ply), this.game.header); + await this.board.frame(this.getPosition(), this.game.header); this.board.render(); } async first() { this.ply = 0; - await this.board.frame(this.game.getPosition(this.ply), this.game.header); + await this.board.frame(this.getPosition(), this.game.header); this.board.render(); } async last() { this.ply = this.game.length - 1; - await this.board.frame(this.game.getPosition(this.ply), this.game.header); + await this.board.frame(this.getPosition(), this.game.header); this.board.render(); } async goto(ply: number) { this.ply = ply; - await this.board.frame(this.game.getPosition(this.ply), this.game.header); + await this.board.frame(this.getPosition(), this.game.header); this.board.render(); } } diff --git a/src/state.ts b/src/state.ts index aae1d2e..2571020 100644 --- a/src/state.ts +++ b/src/state.ts @@ -28,16 +28,18 @@ export type State = { board: BoardConfig; game: GameConfig; pgn: string | null; - fen: string | null; + fen: string; moves: string[]; + ply: number; }; const initialState: State = { board: boardConfig, game: gameConfig, pgn: null, - fen: null, + fen: "", moves: [], + ply: 0, }; const [state, setState] = createStore(initialState); diff --git a/src/ui/components/Moves.css b/src/ui/components/Moves.css index 50f5628..a979413 100644 --- a/src/ui/components/Moves.css +++ b/src/ui/components/Moves.css @@ -23,6 +23,11 @@ color: rgb(0, 173, 136); } +.move__ply--current { + color: #aaa; + background-color: #ffffff22; +} + .move__ply:hover { text-decoration: underline; cursor: pointer; diff --git a/src/ui/components/Moves.tsx b/src/ui/components/Moves.tsx index 0a7d632..289200c 100644 --- a/src/ui/components/Moves.tsx +++ b/src/ui/components/Moves.tsx @@ -3,6 +3,7 @@ import chunk_ from "@arrows/array/chunk_"; import { Handlers } from "../../types"; import Scrollable from "./reusable/Scrollable"; import "./Moves.css"; +import { state } from "../../state"; const Moves: Component<{ moves: readonly string[]; handlers: Handlers }> = ( props @@ -17,13 +18,19 @@ const Moves: Component<{ moves: readonly string[]; handlers: Handlers }> = (
{i() + 1}. props.handlers.goto(i() * 2 + 1)} > {white} props.handlers.goto(i() * 2 + 2)} > {black} diff --git a/src/ui/components/Share.tsx b/src/ui/components/Share.tsx index 8b5a85c..bbf2833 100644 --- a/src/ui/components/Share.tsx +++ b/src/ui/components/Share.tsx @@ -8,7 +8,7 @@ const Share: Component<{ handlers: Handlers }> = (props) => { return (