@gamepark/rules-api
Version:
API to implement the rules of a board game
150 lines • 7.07 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MaterialRulesPart = void 0;
var Rules_1 = require("../../Rules");
var items_1 = require("../items");
var memory_1 = require("../memory");
var moves_1 = require("../moves");
/**
* When you implement the rules of a game using {@link MaterialRules}, the rules are split into small parts.
* This is the base class to implement one part of the rules.
* The constructor cannot be changed as the class is instantiated by {@link MaterialRules}.
*/
var MaterialRulesPart = /** @class */ (function (_super) {
__extends(MaterialRulesPart, _super);
function MaterialRulesPart() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.startPlayerTurn = moves_1.MaterialMoveBuilder.startPlayerTurn;
_this.startSimultaneousRule = moves_1.MaterialMoveBuilder.startSimultaneousRule;
_this.startRule = moves_1.MaterialMoveBuilder.startRule;
_this.customMove = moves_1.MaterialMoveBuilder.customMove;
_this.endGame = moves_1.MaterialMoveBuilder.endGame;
return _this;
}
/**
* Helper function to get a {@link Material} instance to work on some item type in the game.
* @param type Type of material
* @returns {Material} the material instance
*/
MaterialRulesPart.prototype.material = function (type) {
return new items_1.Material(type, this.game.items[type]);
};
/**
* This function is called immediately before an {@link ItemMove} is played.
* @param _move The move which is going to be played
* @param _context Context of execution
* @returns {MaterialMove[]} Any consequences that should automatically be played after the move
*/
MaterialRulesPart.prototype.beforeItemMove = function (_move, _context) {
return [];
};
/**
* This function is called immediately after an {@link ItemMove} is played.
* @param _move The move which has just been played
* @param _context Context of execution
* @returns {MaterialMove[]} Any consequences that should automatically be played after the move
*/
MaterialRulesPart.prototype.afterItemMove = function (_move, _context) {
return [];
};
/**
* This function is called immediately after the {@link RuleMove} that started this rules step was played
* @param _move The move which has just been played
* @param _previousRule The step of the rules immediately before this one started
* @param _context Context of execution
* @returns {MaterialMove[]} Any consequences that should automatically be played after the move
*/
MaterialRulesPart.prototype.onRuleStart = function (_move, _previousRule, _context) {
return [];
};
/**
* This function is called just before a {@link RuleMove} that leave this rules step is played.
*
* BEWARE: any consequences returned here will happen inside the next rule step. Usually we only clean the memory here.
*
* @param _move The move which is going to be played
* @param _context Context of execution
* @returns {MaterialMove[]} Any consequences that should automatically be played after the move
*/
MaterialRulesPart.prototype.onRuleEnd = function (_move, _context) {
return [];
};
/**
* This function is called when a {@link CustomMove} is played.
* @param _move The move
* @param _context Context of execution
* @returns {MaterialMove[]} Any consequences that should automatically be played after the move
*/
MaterialRulesPart.prototype.onCustomMove = function (_move, _context) {
return [];
};
/**
* @deprecated replace this.rules().[the function] with: this.[the function]
*/
MaterialRulesPart.prototype.rules = function () {
return moves_1.MaterialMoveBuilder;
};
/**
* 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
*/
MaterialRulesPart.prototype.getMemory = function (player) {
return player === undefined ? new memory_1.GameMemory(this.game) : new memory_1.PlayerMemory(this.game, player);
};
/**
* Save a new value inside the memory.
* @param key The key to index the memorized value.
* @param value Any JSON serializable value to store, or a function that takes previous stored value and returns the new value to store.
* @param player optional, if we need to memorize a different value for each player.
*/
MaterialRulesPart.prototype.memorize = function (key, value, player) {
return this.getMemory(player).memorize(key, value);
};
/**
* Retrieve the value memorized under a given key.
* Shortcut for this.game.memory[key] or this.game.memory[key][player]
*
* @param key Key under which the memory is store. Usually a value of a numeric enum named "Memory".
* @param player optional, if we need to memorize a different value for each player.
*/
MaterialRulesPart.prototype.remind = function (key, player) {
return this.getMemory(player).remind(key);
};
/**
* Delete a value from the memory
* @param key Key of the value to delete
* @param player optional, if we need to memorize a different value for each player.
*/
MaterialRulesPart.prototype.forget = function (key, player) {
this.getMemory(player).forget(key);
};
/**
* @deprecated because of the lifecycle of this method, the game state must never be modified inside it. However, it is a very common mistake, so
* this method should not be used in the material rules parts: use {@link play} method to return the automatic moves.
*/
MaterialRulesPart.prototype.getAutomaticMoves = function () {
return [];
};
return MaterialRulesPart;
}(Rules_1.Rules));
exports.MaterialRulesPart = MaterialRulesPart;
//# sourceMappingURL=MaterialRulesPart.js.map