UNPKG

@real_one_chess_king/game-logic

Version:
124 lines 3.93 kB
import { Color } from "./color"; import { reverseColor } from "./color"; import { MovesTree } from "./moves-tree/moves-tree"; import { TurnType } from "./turn"; import { serializeCoordinate } from "./moves-tree"; import { Timer } from "./timer"; export class Player { pkey; nickName; constructor(pkey, nickName) { this.pkey = pkey; this.nickName = nickName; } } export class Game { white; black; board; globalRules; treeLength; timeStart; timeLeft; movesTree; timers; _nextTurnColor; turns = []; result = null; timeEnd = null; constructor(white, black, board, globalRules, treeLength, timeStart, timeLeft) { this.white = white; this.black = black; this.board = board; this.globalRules = globalRules; this.treeLength = treeLength; this.timeStart = timeStart; this.timeLeft = timeLeft; this._nextTurnColor = Color.white; this.movesTree = new MovesTree(this.board, this.turns, this.globalRules, this.treeLength, this._nextTurnColor); this.timers = { [Color.white]: new Timer(this.timeLeft[Color.white], () => this.onTimeEnd(Color.white)), [Color.black]: new Timer(this.timeLeft[Color.black], () => this.onTimeEnd(Color.black)), }; } get nextTurnColor() { return this._nextTurnColor; } updateGameNextTurn() { this._nextTurnColor = reverseColor(this._nextTurnColor); } onTimeEnd = (color) => { this.result = reverseColor(color); this.timeEnd = new Date().toISOString(); this.externalOnTimeEnd(); }; externalOnTimeEnd = () => { }; startTimer(externalOnTimeStart, externalOnTimeEnd) { const expectedStart = new Date(this.timeStart); const beforeStart = expectedStart.getTime() - new Date().getTime(); setTimeout(() => { this.timers[Color.white].start(); externalOnTimeStart(); }, beforeStart); this.externalOnTimeEnd = externalOnTimeEnd; } getActionsForCoordinate(coordinate) { const root = this.movesTree.getRoot(); const actions = []; const fromKey = serializeCoordinate(coordinate); const movements = root.movements[fromKey]; if (!movements) { return actions; } for (const toKey in movements) { actions.push(movements[toKey].affects); } return actions; } processTurn(turn) { const { color, type } = turn; if (this._nextTurnColor !== color) { throw new Error("Not your turn"); } if (type === TurnType.Move) { this.timers[color].pause(); this.turns.push(turn); this.movesTree.processTurn(turn); } else { // this.turns.push(turn); // this.board.cast(color, from, to); } this.updateGameNextTurn(); const freshRoot = this.movesTree.getRoot(); if (Object.keys(freshRoot.movements).length === 0) { if (freshRoot.underCheck) { this.result = reverseColor(color); } else { this.result = "draw"; } this.timeEnd = new Date().toISOString(); } else { this.timers[reverseColor(color)].start(); } return this.result; } // returns meta board for color, it hides opponent private data, hold minimal data getBoardMeta() { return this.board.getMeta(); } getNewGameInfoForColor(color) { return { players: { [Color.white]: this.white, [Color.black]: this.black, }, yourColor: color, timeStart: this.timeStart, timeLeft: this.timeLeft, }; } } //# sourceMappingURL=game.js.map