UNPKG

preferans-score-js

Version:
182 lines 6.99 kB
#!/usr/bin/env node 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const preferans_paper_js_1 = require("preferans-paper-js"); const prefScoreHandGame_1 = require("./prefScoreHandGame"); const prefScoreHandRefa_1 = require("./prefScoreHandRefa"); class PrefScore { constructor(p1username, p2username, p3username, bula, refas = Infinity) { this._hands = new Map(); this._bula = bula; this._refas = refas; this._usedRefas = 0; this._p1username = p1username; this._p2username = p2username; this._p3username = p3username; this._p1score = -this._bula * 10; this._p2score = -this._bula * 10; this._p3score = -this._bula * 10; this._p1 = new preferans_paper_js_1.default('p1', bula); this._p2 = new preferans_paper_js_1.default('p2', bula); this._p3 = new preferans_paper_js_1.default('p3', bula); } addRefaHand() { if (!this._hasUnusedRefas()) throw new Error('PrefScore::addRefaHand:No more unused refas!'); const hand = new prefScoreHandRefa_1.default(); const index = lodash_1.size(this._hands) + 1; hand.index = index; this._hands.set(index, hand); this._usedRefas++; return this._processHand(hand); } addPlayedHand(value, main, left, right) { const hand = new prefScoreHandGame_1.default(value, main, left, right); const index = lodash_1.size(this._hands) + 1; hand.index = index; this._hands.set(index, hand); return this._processHand(hand); } repealHand(index) { if (index > 0) { const hand = this._hands.get(index); if (hand) { hand.repealed = true; this._hands.set(index, hand); return this._recalculate(); } } throw new Error('PrefPapers::repealHand:Hand not found with index ' + index); } hasUnplayedRefa(designation) { return this.getPaper(designation).hasUnplayedRefa(); } get username1() { return this._p1username; } set username1(name1) { this._p1username = name1; } get username2() { return this._p2username; } set username2(name2) { this._p2username = name2; } get username3() { return this._p3username; } set username3(name3) { this._p3username = name3; } get handCount() { let cnt = 0; for (const m of this._hands) { if (m[1].refa) cnt++; else { const h = m[1]; if (!h.repealed) cnt++; } } return cnt; } get mini() { return { p1: { score: this._p1score, paper: this._p1.mini }, p2: { score: this._p2score, paper: this._p2.mini }, p3: { score: this._p3score, paper: this._p3.mini } }; } get json() { return { p1: { score: this._p1score, paper: this._p1.json }, p2: { score: this._p2score, paper: this._p2.json }, p3: { score: this._p3score, paper: this._p3.json } }; } getPaper(designation) { if (this._p1.designation === designation) return this._p1; if (this._p2.designation === designation) return this._p2; return this._p3; } _recalculate() { this._p1.reset(); this._p2.reset(); this._p3.reset(); this._p1score = -this._bula * 10; this._p2score = -this._bula * 10; this._p3score = -this._bula * 10; for (const hand of this._hands) this._processHand(hand[1]); return this; } _hasUnusedRefas() { if (Infinity === this._refas) return true; return this._refas - this._usedRefas > 0; } _processHand(hand) { if (hand.refa) return this._processNewRefa(); const playHand = hand; const main = playHand.main; const left = playHand.left; const right = playHand.right; const value = playHand.value; const mainPaper = this.getPaper(main.designation); const leftPaper = this.getPaper(left.designation); const rightPaper = this.getPaper(right.designation); if (playHand.repealed) { mainPaper.processAsMainRepealed(value, main.designation, main.failed); if (left.followed) leftPaper.processAsFollowerRepealed(value, left.designation, left.tricks, left.failed, main.designation); if (right.followed) rightPaper.processAsFollowerRepealed(value, right.designation, right.tricks, right.failed, main.designation); } else { mainPaper.processAsMain(value, main.designation, main.failed); if (left.followed) leftPaper.processAsFollower(value, left.designation, left.tricks, left.failed, main.designation); if (right.followed) rightPaper.processAsFollower(value, right.designation, right.tricks, right.failed, main.designation); let mainScore = this._getScoreByDesignation(main.designation); let leftScore = this._getScoreByDesignation(left.designation); let rightScore = this._getScoreByDesignation(right.designation); mainScore = mainPaper.left + mainPaper.right - mainPaper.middle * 10 - leftPaper.right - rightPaper.left; leftScore = leftPaper.left + leftPaper.right - leftPaper.middle * 10 - rightPaper.right - mainPaper.left; rightScore = rightPaper.left + rightPaper.right - rightPaper.middle * 10 - mainPaper.right - leftPaper.left; this._setScoreByDesignation(main.designation, mainScore); this._setScoreByDesignation(left.designation, leftScore); this._setScoreByDesignation(right.designation, rightScore); } return this; } _getScoreByDesignation(designation) { if (this._p1.designation === designation) return this._p1score; if (this._p2.designation === designation) return this._p2score; return this._p3score; } _setScoreByDesignation(designation, score) { if (this._p1.designation === designation) this._p1score = score; else if (this._p2.designation === designation) this._p2score = score; else this._p3score = score; } _processNewRefa() { this._p1.addNewRefa(); this._p2.addNewRefa(); this._p3.addNewRefa(); return this; } } exports.default = PrefScore; //# sourceMappingURL=prefScore.js.map