UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

42 lines 1.77 kB
import { hasHiddenInformation } from '../HiddenInformation'; import { hasSecretInformation } from '../SecretInformation'; import { applyAutomaticMoves } from './automatic-moves.util'; import { hasRandomMove } from './random.util'; export function playActionWithViews(rules, move, playerId, recipients, id) { if (hasRandomMove(rules)) { move = rules.randomize(move, playerId); } const actionWithView = { action: { id, playerId, move, consequences: [] }, views: [] }; // Prepare action view for each player for (const recipient of recipients) { actionWithView.views.push({ recipient, action: { id, playerId, move: getMoveView(rules, move, recipient), consequences: [] } }); } // Prepare action view for spectators actionWithView.views.push({ action: { id, playerId, move: getMoveView(rules, move), consequences: [] } }); const consequences = rules.play(JSON.parse(JSON.stringify(move))); applyAutomaticMoves(rules, consequences, move => { actionWithView.action.consequences.push(move); for (const view of actionWithView.views) { view.action.consequences.push(getMoveView(rules, move, view.recipient)); } }); return actionWithView; } export function getMoveView(rules, move, playerId) { if (hasSecretInformation(rules) && rules.getPlayerMoveView && playerId !== undefined) { return JSON.parse(JSON.stringify(rules.getPlayerMoveView(move, playerId))); } else if (hasHiddenInformation(rules)) { return JSON.parse(JSON.stringify(rules.getMoveView(move))); } else { return move; } } export function isSecretAction(action) { return Array.isArray(action.secrets); } //# sourceMappingURL=action-view.util.js.map