UNPKG

@real_one_chess_king/game-logic

Version:
138 lines 5.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleKillAffect = handleKillAffect; exports.handleTransformAffect = handleTransformAffect; exports.handleMoveAffect = handleMoveAffect; exports.handleSpawnAffect = handleSpawnAffect; exports.reverseAffects = reverseAffects; const piece_builder_1 = require("../piece/piece-builder"); const affect_types_1 = require("./affect.types"); const affect_utils_1 = require("./affect.utils"); function handleKillAffect(affect, cells, killed) { const { from } = affect; if ((0, affect_utils_1.isKillAffect)(affect)) { const [fromX, fromY] = from; const killedPiece = cells[fromY][fromX].popPiece(); if (!killedPiece) { throw new Error(`Invalid affect kill cordinate ${fromX}, ${fromY}`); } killed.push(killedPiece); } } function handleTransformAffect(affect, cells, boardMeta) { if ((0, affect_utils_1.isTransformationAffect)(affect)) { const { from, sourcePieceType, destPieceType } = affect; if (!sourcePieceType) { throw new Error("Invalid move: transformation affect should have sourcePieceType"); } if (!destPieceType) { throw new Error("Invalid move: transformation affect should have destPieceType"); } const [fromX, fromY] = from; const transformed = cells[fromY][fromX].popPiece(); if (!transformed) { throw new Error(`Invalid affect transformation cordinate ${fromX}, ${fromY}. Cell is empty`); } const newPieceMeta = boardMeta.pieceMeta.find((pieceMeta) => pieceMeta.color === transformed.color && pieceMeta.type === destPieceType); if (!newPieceMeta) { throw new Error(`Invalid move: no meta for piece ${transformed.color} ${destPieceType} in meta storage`); } const newPiece = (0, piece_builder_1.buildPieceByMeta)(newPieceMeta); cells[fromY][fromX].putPiece(newPiece); } } function handleMoveAffect(affect, cells // metaStorage: MetaStorage ) { if ((0, affect_utils_1.isMoveAffect)(affect)) { checkAffectToAttribute(affect); const { from, to } = affect; const [fromX, fromY] = from; const [toX, toY] = to; const pieceMovedByAffect = cells[fromY][fromX].popPiece(); if (!pieceMovedByAffect) { throw new Error(`Invalid move: no piece at from coordinate X:${fromX} y:${fromY}`); } if (!cells[toY][toX].isEmpty()) { throw new Error("Invalid move: affect moved piece to not empty cell"); } cells[toY][toX].putPiece(pieceMovedByAffect); } } // currently it's purely for reverting kill function handleSpawnAffect(affect, cells, killed) { if ((0, affect_utils_1.isSpawnAffect)(affect)) { const [fromX, fromY] = affect.from; const spawnedPiece = killed.pop(); if (!spawnedPiece) { throw new Error("Invalid move: no piece to spawn"); } cells[fromY][fromX].putPiece(spawnedPiece); } } function checkAffectFromAttribute(affect) { if (!affect.from) { throw new Error("From attribute is not provided for affect"); } } function checkAffectToAttribute(affect) { if (!affect.to) { throw new Error("To attribute is not provided for affect"); } } function reverseKillAffect(affect) { checkAffectFromAttribute(affect); return { type: affect_types_1.AffectType.spawn, from: affect.from, // spawnedPiece: killed.reverse()[i], }; } function reverseSpawnAffect(affect) { checkAffectFromAttribute(affect); return { type: affect_types_1.AffectType.kill, from: affect.from, }; } function reverseMoveAffect(affect) { checkAffectFromAttribute(affect); checkAffectToAttribute(affect); return { type: affect_types_1.AffectType.move, from: affect.to, to: affect.from, }; } function reverseTransformationAffect(affect) { checkAffectFromAttribute(affect); return { type: affect_types_1.AffectType.transformation, from: affect.from, sourcePieceType: affect.destPieceType, destPieceType: affect.sourcePieceType, // pieceTypesForTransformation: affect.pieceTypesForTransformation, // before: true, }; } function reverseAffects(affects) { return affects.toReversed().map((affect) => { if ((0, affect_utils_1.isKillAffect)(affect)) { return reverseKillAffect(affect); } else if ((0, affect_utils_1.isMoveAffect)(affect)) { return reverseMoveAffect(affect); } else if ((0, affect_utils_1.isSpawnAffect)(affect)) { return reverseSpawnAffect(affect); } else if ((0, affect_utils_1.isTransformationAffect)(affect)) { return reverseTransformationAffect(affect); } else { throw new Error("Invalid affect type"); } }); } //# sourceMappingURL=affect.js.map