UNPKG

preferans-paper-js

Version:
109 lines 4.26 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const pref_paper_column_1 = require("./pref.paper.column"); const pref_paper_entry_hat_1 = require("./pref.paper.entry.hat"); const pref_paper_entry_refa_1 = require("./pref.paper.entry.refa"); const pref_paper_position_1 = require("./enums/pref.paper.position"); const pref_paper_entry_number_1 = require("./pref.paper.entry.number"); const _getLastNonZeroValue = (vals) => { const nums = _.filter(vals, (v) => { if (v.isNumber) { const n = v; return 0 !== n.value; } return false; }); const last = _.last(nums); return last.value; }; const _validAddHat = (last, val) => val < 0 && last + val <= 0; const _shouldAddHat = (vals, last, val) => (_getLastNonZeroValue(vals) < 0 ? false : _validAddHat(last, val)); const _validAddHatCrossed = (last, val) => val > 0 && last + val >= 0; const _shouldAddHatCrossed = (vals, last, val) => (_getLastNonZeroValue(vals) > 0 ? false : _validAddHatCrossed(last, val)); const _isUnplayedRefaForPosition = (v, position) => { if (v.isRefa) { const r = v; switch (position) { case pref_paper_position_1.default.LEFT: return !r.leftPlayed; case pref_paper_position_1.default.MIDDLE: return !r.middlePlayed; case pref_paper_position_1.default.RIGHT: return !r.rightPlayed; } } return false; }; class PrefPaperColumnMiddle extends pref_paper_column_1.default { constructor(value) { if (!pref_paper_column_1.default.isValidValue(value)) throw new Error('PrefPaperColumnMiddle::constructor:Value is not valid ' + value + '. Value must be larger than 0 and even.'); super(); this._initialValue = value; this.reset(); } reset() { this._value = this._initialValue; this._values = [new pref_paper_entry_number_1.default(this._initialValue)]; return this; } addValue(value) { const entry = this._makeNumberEntry(value); this._value = entry.value; this._processHats(value); if (0 !== this._value) this._values.push(entry); return this; } addValueRepealed(value) { const entry = this._makeNumberEntry(value); entry.repealed = true; this._values.push(entry); return this; } addRefa() { this._values.push(new pref_paper_entry_refa_1.default()); return this; } markPlayedRefaPassed(position) { return this._markPlayedRefa(position, false); } markPlayedRefaFailed(position) { return this._markPlayedRefa(position, true); } hasOpenRefa(position = pref_paper_position_1.default.MIDDLE) { return this._getOpenRefasCount(position) > 0; } _markPlayedRefa(position, failed) { const index = _.findIndex(this._values, (i) => _isUnplayedRefaForPosition(i, position)); if (index < 0) throw new Error('PrefPaperColumnMiddle::markPlayedRefa:There are no open refas for that position: ' + position); const r = this._values[index]; if (failed) r.setPlayedFailed(position); else r.setPlayedPassed(position); this._values[index] = r; return this; } _getOpenRefasCount(position) { return _.size(_.filter(this._values, (i) => _isUnplayedRefaForPosition(i, position))); } _makeNumberEntry(value) { return new pref_paper_entry_number_1.default(this._value + value); } _processHats(value) { if (_shouldAddHat(this._values, this._value, value)) { this._values.push(new pref_paper_entry_hat_1.default()); } else if (_shouldAddHatCrossed(this._values, this._value, value)) { const hat = new pref_paper_entry_hat_1.default(); hat.crossed = true; this._values.push(hat); } return this; } } exports.default = PrefPaperColumnMiddle; //# sourceMappingURL=pref.paper.column.middle.js.map