UNPKG

preferans-rating-js

Version:
73 lines 2.37 kB
#!/usr/bin/env node 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const math = require("mathjs"); const prefRatingPlayer_1 = require("./prefRatingPlayer"); const MAGIC = 2.8; const _calcD = (p1, p2, bula) => Number(math.round(math .chain(p1.score) .subtract(p2.score) .divide(bula) .done(), 3)); const _calculateDs = (p1, p2, p3, bula) => ({ a12: _calcD(p1, p2, bula), a13: _calcD(p1, p3, bula), a23: _calcD(p2, p3, bula), }); const _calcT = (p1, p2) => Number(math.round(math .chain(p2.rating) .subtract(p1.rating) .multiply(MAGIC) .divide(100) .done(), 3)); const _calculateTs = (p1, p2, p3) => ({ a12: _calcT(p1, p2), a13: _calcT(p1, p3), a23: _calcT(p2, p3), }); const _calcN = (p1, p2, bula) => (p1.score > p2.score ? bula : p1.score < p2.score ? -bula : 0) / 100; const _calculateNs = (p1, p2, p3, bula) => ({ a12: _calcN(p1, p2, bula), a13: _calcN(p1, p3, bula), a23: _calcN(p2, p3, bula), }); const _calcC = (c1, c2) => Number(math.round(math .chain(c1) .add(c2) .divide(2) .done())); const _calculateChanges = (p1, p2, p3, bula) => { const D = _calculateDs(p1, p2, p3, bula); const T = _calculateTs(p1, p2, p3); const N = _calculateNs(p1, p2, p3, bula); const C12 = D.a12 + T.a12 + N.a12; const C13 = D.a13 + T.a13 + N.a13; const C23 = D.a23 + T.a23 + N.a23; return { c1: _calcC(C12, C13), c2: _calcC(-C12, C23), c3: _calcC(-C13, -C23), }; }; class PrefRating { constructor(p1, p2, p3, bula) { this._p1 = new prefRatingPlayer_1.default(p1.username, p1.rating, p1.score); this._p2 = new prefRatingPlayer_1.default(p2.username, p2.rating, p2.score); this._p3 = new prefRatingPlayer_1.default(p3.username, p3.rating, p3.score); this._bula = bula; const { c1, c2, c3 } = _calculateChanges(this._p1, this._p2, this._p3, this._bula); this._p1.change = c1; this._p2.change = c2; this._p3.change = c3; } get rating() { return { bula: this._bula, p1: this._p1.json, p2: this._p2.json, p3: this._p3.json, }; } } exports.default = PrefRating; //# sourceMappingURL=prefRating.js.map