This commit is contained in:
Maciej Caderek
2022-02-11 22:22:12 +01:00
parent d08080543d
commit db40aa2939
6 changed files with 51 additions and 7 deletions

View File

@@ -70,7 +70,21 @@ class Game {
loadFEN(fen: string) { loadFEN(fen: string) {
this.game = new Chess(fen); 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) { private getPlacement(fen: string) {

View File

@@ -112,8 +112,21 @@ const main = async () => {
changePiecesStyle(style: PiecesStyle) { changePiecesStyle(style: PiecesStyle) {
board.setPiecesStyle(style); 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 * RENDER
**/ **/

View File

@@ -27,9 +27,9 @@ class Player {
await this.firstRender; await this.firstRender;
this.game = game; 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(); this.board.render();
} }
@@ -53,7 +53,7 @@ class Player {
async prev() { async prev() {
const ply = this.ply - 1; const ply = this.ply - 1;
if (ply < -1) { if (ply < -1 || (ply < 0 && this.config.titleScreen === false)) {
return; return;
} }

View File

@@ -27,12 +27,16 @@ const gameConfig: GameConfig = {
export type State = { export type State = {
board: BoardConfig; board: BoardConfig;
game: GameConfig; game: GameConfig;
pgn: string | null;
fen: string | null;
moves: string[]; moves: string[];
}; };
const initialState: State = { const initialState: State = {
board: boardConfig, board: boardConfig,
game: gameConfig, game: gameConfig,
pgn: null,
fen: null,
moves: [], moves: [],
}; };

View File

@@ -201,4 +201,6 @@ export type Handlers = {
goto(ply: number): void; goto(ply: number): void;
changeBoardStyle: (style: BoardStyle) => void; changeBoardStyle: (style: BoardStyle) => void;
changePiecesStyle: (style: PiecesStyle) => void; changePiecesStyle: (style: PiecesStyle) => void;
loadPGN: (pgn: string) => Promise<void>;
loadFEN: (fen: string) => Promise<void>;
}; };

View File

@@ -1,7 +1,11 @@
import { Component } from "solid-js"; import { Component, createSignal } from "solid-js";
import { Handlers } from "../../types";
import "./Load.css"; import "./Load.css";
const Load: Component = () => { const Load: Component<{ handlers: Handlers }> = (props) => {
const [fen, setFEN] = createSignal("");
const [pgn, setPGN] = createSignal("");
return ( return (
<div class="load"> <div class="load">
<input <input
@@ -10,8 +14,15 @@ const Load: Component = () => {
name="load-fen" name="load-fen"
placeholder="PASTE FEN..." placeholder="PASTE FEN..."
spellcheck={false} spellcheck={false}
value={fen()}
onInput={(e) => setFEN(e.currentTarget.value)}
/> />
<button class="load__fen-btn">LOAD FEN</button> <button
class="load__fen-btn"
onClick={() => props.handlers.loadFEN(fen())}
>
LOAD FEN
</button>
<textarea <textarea
class="load__pgn-input" class="load__pgn-input"
name="load-pgn" name="load-pgn"