UNPKG

@real_one_chess_king/game-logic

Version:
129 lines 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Game = exports.Player = void 0; const color_1 = require("./color"); const color_2 = require("./color"); const moves_tree_1 = require("./moves-tree/moves-tree"); const turn_1 = require("./turn"); const moves_tree_2 = require("./moves-tree"); const timer_1 = require("./timer"); class Player { pkey; nickName; constructor(pkey, nickName) { this.pkey = pkey; this.nickName = nickName; } } exports.Player = Player; 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_1.Color.white; this.movesTree = new moves_tree_1.MovesTree(this.board, this.turns, this.globalRules, this.treeLength, this._nextTurnColor); this.timers = { [color_1.Color.white]: new timer_1.Timer(this.timeLeft[color_1.Color.white], () => this.onTimeEnd(color_1.Color.white)), [color_1.Color.black]: new timer_1.Timer(this.timeLeft[color_1.Color.black], () => this.onTimeEnd(color_1.Color.black)), }; } get nextTurnColor() { return this._nextTurnColor; } updateGameNextTurn() { this._nextTurnColor = (0, color_2.reverseColor)(this._nextTurnColor); } onTimeEnd = (color) => { this.result = (0, color_2.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_1.Color.white].start(); externalOnTimeStart(); }, beforeStart); this.externalOnTimeEnd = externalOnTimeEnd; } getActionsForCoordinate(coordinate) { const root = this.movesTree.getRoot(); const actions = []; const fromKey = (0, moves_tree_2.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 === turn_1.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 = (0, color_2.reverseColor)(color); } else { this.result = "draw"; } this.timeEnd = new Date().toISOString(); } else { this.timers[(0, color_2.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_1.Color.white]: this.white, [color_1.Color.black]: this.black, }, yourColor: color, timeStart: this.timeStart, timeLeft: this.timeLeft, }; } } exports.Game = Game; //# sourceMappingURL=game.js.map