@gamepark/rules-api
Version:
API to implement the rules of a board game
140 lines • 6.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MaterialGameSetup = void 0;
var utils_1 = require("../utils");
var items_1 = require("./items");
var memory_1 = require("./memory");
var moves_1 = require("./moves");
/**
* Helper class to implement {@link GameSetup} when using the {@link MaterialRules} approach.
*/
var MaterialGameSetup = /** @class */ (function () {
function MaterialGameSetup() {
/**
* The game setup state we are working on
* @protected
*/
this.game = { players: [], items: {}, memory: {} };
}
Object.defineProperty(MaterialGameSetup.prototype, "rules", {
/**
* Get an instance of the rules of the game
*/
get: function () {
return new this.Rules(this.game);
},
enumerable: false,
configurable: true
});
/**
* Entry point for {@link GameSetup}
* @param options Options of the game
* @param tutorial Initial tutorial state if any
* @returns the initial state of the game
*/
MaterialGameSetup.prototype.setup = function (options, tutorial) {
this.game = { players: getPlayerIds(options), items: {}, memory: {}, tutorial: tutorial };
this.setupMaterial(options);
this.start(options);
return this.game;
};
Object.defineProperty(MaterialGameSetup.prototype, "players", {
/**
* @returns array of the player ids (shortcut for this.game.players)
*/
get: function () {
return this.game.players;
},
enumerable: false,
configurable: true
});
/**
* Override this function if you need to set up some material before the game starts. Called by {@link setup}.
* @param _options Options of the game
*/
MaterialGameSetup.prototype.setupMaterial = function (_options) {
};
/**
* Help function to execute a move immediately on the game state.
* When the game is on, the moves should never be played directly: the framework takes care of playing the moves when necessary
* (on te server, on the clients, during replays...).
* However, during the setup, the moves must be played immediately in the game state to provide the initial game state to the framework.
* This help function allows to easily play a {@link MaterialMove}, using the rules provided in {@link Rules}, including the consequences.
*
* @param move The MaterialMove to play
* @protected
*/
MaterialGameSetup.prototype.playMove = function (move) {
if ((0, utils_1.hasRandomMove)(this.rules)) {
move = this.rules.randomize(move);
}
var consequences = this.rules.play(JSON.parse(JSON.stringify(move)));
(0, utils_1.applyAutomaticMoves)(this.rules, consequences);
};
/**
* Helper function to manipulate the items of the game. See {@link Material}.
* @param type The type of Material we want to work on
* @returns a Material instance to manipulate all the material of that type in current game state.
*/
MaterialGameSetup.prototype.material = function (type) {
var _this = this;
return new items_1.Material(type, this.game.items[type], function (move) { return _this.playMove(move); });
};
/**
* Utility function to access the memory tool for the game or on player.
* this.game.memory can be used to store any data that is not available through the state of the material, or current rule.
*
* @param player Optional, identifier of the player if we want to manipulate a specific player's memory
* @returns {@link GameMemory} or {@link PlayerMemory} utility
* @protected
*/
MaterialGameSetup.prototype.getMemory = function (player) {
return player === undefined ? new memory_1.GameMemory(this.game) : new memory_1.PlayerMemory(this.game, player);
};
/**
* Helper function to memorize some information that does not fit in an item or the rules state, in the game state
* @param key Key under which the memory is store. Usually a value of a numeric enum named "Memory".
* @param value Value to memorize
* @param player optional, if we need to memorize a different value for each player.
*/
MaterialGameSetup.prototype.memorize = function (key, value, player) {
return this.getMemory(player).memorize(key, value);
};
/**
* Helper function to play a {@link StartPlayerTurn} move on the game setup state.
* @param id Rule id to start
* @param player Player that starts the game (default value: this.game.players[0])
*/
MaterialGameSetup.prototype.startPlayerTurn = function (id, player) {
if (player === void 0) { player = this.game.players[0]; }
this.playMove(moves_1.MaterialMoveBuilder.startPlayerTurn(id, player));
};
/**
* Helper function to play a {@link StartSimultaneousRule} move on the game setup state.
* @param id Rule id to start
* @param players Players that are active (all the players by default)
*/
MaterialGameSetup.prototype.startSimultaneousRule = function (id, players) {
this.playMove(moves_1.MaterialMoveBuilder.startSimultaneousRule(id, players));
};
/**
* Helper function to play a {@link StartRule} move on the game setup state.
* @param id Rule id to start
*/
MaterialGameSetup.prototype.startRule = function (id) {
this.playMove(moves_1.MaterialMoveBuilder.startRule(id));
};
return MaterialGameSetup;
}());
exports.MaterialGameSetup = MaterialGameSetup;
function getPlayerIds(options) {
var _a;
if (Array.isArray(options.players)) {
return options.players.map(function (player, index) { var _a; return (_a = player.id) !== null && _a !== void 0 ? _a : index + 1; });
}
else {
var numberOfPlayers = (_a = options.players) !== null && _a !== void 0 ? _a : 2;
return Array.from(Array(numberOfPlayers).keys()).map(function (index) { return (index + 1); });
}
}
//# sourceMappingURL=MaterialGameSetup.js.map