diff --git a/src/game/Game.ts b/src/game/Game.ts index b9aa100..6ac03b3 100644 --- a/src/game/Game.ts +++ b/src/game/Game.ts @@ -70,7 +70,21 @@ class Game { loadFEN(fen: string) { this.game = new Chess(fen); - this.positions = []; + this.positions = [ + { + ply: 0, + move: null, + end: 0, + fen, + check: false, + mate: false, + turn: this.game.turn(), + material: this.materialInfo(this.game.board()), + placement: this.getPlacement(this.game.fen()), + last: true, + }, + ]; + return this; } private getPlacement(fen: string) { diff --git a/src/main.tsx b/src/main.tsx index 0e5dbae..5eeb381 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -112,8 +112,21 @@ const main = async () => { changePiecesStyle(style: PiecesStyle) { board.setPiecesStyle(style); }, + async loadPGN(pgn: string) { + const game = new Game().loadPGN(pgn); + setState({ pgn, fen: null, moves: game.getMoves() }); + await player.load(game); + }, + async loadFEN(fen: string) { + const game = new Game().loadFEN(fen); + setState({ pgn: null, fen, moves: game.getMoves() }); + await player.load(game); + }, }; + // @ts-ignore + window.handlers = handlers; + /** * RENDER **/ diff --git a/src/player/Player.ts b/src/player/Player.ts index 21ccdd2..62e3852 100644 --- a/src/player/Player.ts +++ b/src/player/Player.ts @@ -27,9 +27,9 @@ class Player { await this.firstRender; this.game = game; - this.ply = -1; + this.ply = 0; - await this.board.titleFrame(this.game.header); + await this.board.frame(this.game.getPosition(this.ply), this.game.header); this.board.render(); } @@ -53,7 +53,7 @@ class Player { async prev() { const ply = this.ply - 1; - if (ply < -1) { + if (ply < -1 || (ply < 0 && this.config.titleScreen === false)) { return; } diff --git a/src/state.ts b/src/state.ts index e9e5881..aae1d2e 100644 --- a/src/state.ts +++ b/src/state.ts @@ -27,12 +27,16 @@ const gameConfig: GameConfig = { export type State = { board: BoardConfig; game: GameConfig; + pgn: string | null; + fen: string | null; moves: string[]; }; const initialState: State = { board: boardConfig, game: gameConfig, + pgn: null, + fen: null, moves: [], }; diff --git a/src/types.ts b/src/types.ts index 2379352..607e4ab 100644 --- a/src/types.ts +++ b/src/types.ts @@ -201,4 +201,6 @@ export type Handlers = { goto(ply: number): void; changeBoardStyle: (style: BoardStyle) => void; changePiecesStyle: (style: PiecesStyle) => void; + loadPGN: (pgn: string) => Promise; + loadFEN: (fen: string) => Promise; }; diff --git a/src/ui/components/Load.tsx b/src/ui/components/Load.tsx index 237f65a..ee5146b 100644 --- a/src/ui/components/Load.tsx +++ b/src/ui/components/Load.tsx @@ -1,7 +1,11 @@ -import { Component } from "solid-js"; +import { Component, createSignal } from "solid-js"; +import { Handlers } from "../../types"; import "./Load.css"; -const Load: Component = () => { +const Load: Component<{ handlers: Handlers }> = (props) => { + const [fen, setFEN] = createSignal(""); + const [pgn, setPGN] = createSignal(""); + return (
{ name="load-fen" placeholder="PASTE FEN..." spellcheck={false} + value={fen()} + onInput={(e) => setFEN(e.currentTarget.value)} /> - +