@gamepark/rules-api
Version:
API to implement the rules of a board game
50 lines • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSecretAction = exports.getMoveView = exports.playActionWithViews = void 0;
var HiddenInformation_1 = require("../HiddenInformation");
var SecretInformation_1 = require("../SecretInformation");
var automatic_moves_util_1 = require("./automatic-moves.util");
var random_util_1 = require("./random.util");
function playActionWithViews(rules, move, playerId, recipients, id) {
if ((0, random_util_1.hasRandomMove)(rules)) {
move = rules.randomize(move, playerId);
}
var actionWithView = {
action: { id: id, playerId: playerId, move: move, consequences: [] },
views: []
};
// Prepare action view for each player
for (var _i = 0, recipients_1 = recipients; _i < recipients_1.length; _i++) {
var recipient = recipients_1[_i];
actionWithView.views.push({ recipient: recipient, action: { id: id, playerId: playerId, move: getMoveView(rules, move, recipient), consequences: [] } });
}
// Prepare action view for spectators
actionWithView.views.push({ action: { id: id, playerId: playerId, move: getMoveView(rules, move), consequences: [] } });
var consequences = rules.play(JSON.parse(JSON.stringify(move)));
(0, automatic_moves_util_1.applyAutomaticMoves)(rules, consequences, function (move) {
actionWithView.action.consequences.push(move);
for (var _i = 0, _a = actionWithView.views; _i < _a.length; _i++) {
var view = _a[_i];
view.action.consequences.push(getMoveView(rules, move, view.recipient));
}
});
return actionWithView;
}
exports.playActionWithViews = playActionWithViews;
function getMoveView(rules, move, playerId) {
if ((0, SecretInformation_1.hasSecretInformation)(rules) && rules.getPlayerMoveView && playerId !== undefined) {
return JSON.parse(JSON.stringify(rules.getPlayerMoveView(move, playerId)));
}
else if ((0, HiddenInformation_1.hasHiddenInformation)(rules)) {
return JSON.parse(JSON.stringify(rules.getMoveView(move)));
}
else {
return move;
}
}
exports.getMoveView = getMoveView;
function isSecretAction(action) {
return Array.isArray(action.secrets);
}
exports.isSecretAction = isSecretAction;
//# sourceMappingURL=action-view.util.js.map