From c1164c62f22a8fae4a22f402d7adb7c4acf602af Mon Sep 17 00:00:00 2001 From: Llewellyn Brunsdon-Haland Date: Tue, 27 Feb 2024 16:39:15 +0000 Subject: [PATCH] changes to sort non scorers last --- js/src/highjump.js | 40 ++++++++++++++++++++++++++++------------ js/test/highjump.spec.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/js/src/highjump.js b/js/src/highjump.js index 62e3483..b6e93cb 100644 --- a/js/src/highjump.js +++ b/js/src/highjump.js @@ -39,7 +39,9 @@ function Jumper(kwds) { 'category', 'OPEN', 'order', - 1 + 1, + 'non_scorer', + false ]; const options = _options || {}; @@ -207,6 +209,7 @@ function HighJumpCompetition() { this.inJumpOff = false; this.actions = []; // log for replay purposes. this.state = 'scheduled'; + this.sortNonScorersLast = false; }, addJumper(kwds) { @@ -351,22 +354,35 @@ function HighJumpCompetition() { return cmpKeys(a, b); }, - _rankj() { + _rankj(verbose = false) { // sort them + const self = this; const rankj = this.rankedJumpers; const rankjlen = rankj.length; let i; - // console.log('rankj before', rankj); - + if (verbose) { + console.log('rankj before', rankj); + } for (i = 0; i < rankjlen; i++) rankj[i]._oldPos = i; rankj.sort(function (a, b) { - var r; - - // console.log(a.first_name, b.first_name); - r = cmpKeys([a.rankingKey, a._oldPos], [b.rankingKey, b._oldPos]); - - // console.log('result', r); + var r, + aKeys = [a.rankingKey, a._oldPos], + bKeys = [b.rankingKey, b._oldPos]; + + if (self.sortNonScorersLast) { + if (a.non_scorer) { + aKeys = [[3, ...a.rankingKey.splice(1)], a._oldPos]; + } else if (b.non_scorer) { + bKeys = [[3, ...b.rankingKey.splice(1)], b._oldPos]; + } + r = cmpKeys(aKeys, bKeys); + } else { + r = cmpKeys(aKeys, bKeys); + } + if (verbose) { + console.log(a.first_name, aKeys, b.first_name, bKeys, 'result', r); + } return r; }); @@ -392,11 +408,11 @@ function HighJumpCompetition() { return rankj; }, - _rank() { + _rank(verbose = false) { var nc; // Determine who is winning - const rankj = this._rankj(); + const rankj = this._rankj(verbose); if (rankj.length === 0) return; diff --git a/js/test/highjump.spec.js b/js/test/highjump.spec.js index 6787a3a..7436baf 100644 --- a/js/test/highjump.spec.js +++ b/js/test/highjump.spec.js @@ -558,5 +558,38 @@ describe('Given an instance of Athlib.HighJumpCompetition', function(){ it("test athlete with no jumps is eliminated", ()=>{ expect(emanuoil_karalis.eliminated).to.be.equal(true) }) + }); + + describe('test ranking order of result with non scorer',function(){ + // add disqualification + BELGRADE_INDOOR_HJ.push(["1", 1, 1999, "Llewellyn", "BRUNSDON-HALAND", "SWE", "M", "", "", "", "", "", "", "", "", "", "", "DNS", ""]) + const c = Athlib.HighJumpCompetition.fromMatrix(BELGRADE_INDOOR_HJ); + c.state = 'finished' // pretend we simulated a HJ competition + // console.log(c) + + c.sortNonScorersLast = true + + // ["1", 1, 148, "Armand", "DUPLANTIS", "SWE", "M", "", "", "", "", "o", "", "", "o", "o", "xxo", 6.19, ""], + c.jumpers[0].non_scorer = true + + + + c._rank(true) + var rank_aths = c.rankedJumpers + + var armand_duplantis = rank_aths.find(ath => ath.bib === "148") + + it("test armand_duplantis placed last but infront of any disqualifications",()=>{ expect(rank_aths[2]).deep.to.be.equal(armand_duplantis) }); + + it("test ordering correct",()=>{ expect(rank_aths.map(r => r.bib)).deep.to.be.equal(["152", "153", "148", "150", "151", "149", "1999"]) }); + + // it("test correct calc rankingKey", ()=>{ expect(emanuoil_karalis.rankingKey).deep.to.be.equal([ 3, -0, 0, 0 ]) }) + + // it("test athlete is eliminated", ()=>{ expect(wenwen_chen.eliminated).to.be.equal(true) }) + + // // athletes with no jump will be set as eliminted once the state is set to 'finished' + // it("test athlete with no jumps is eliminated", ()=>{ expect(emanuoil_karalis.eliminated).to.be.equal(true) }) + + }); });