PageSourceSearch

https://topdowntabletop.com/shared/tournament-standings.js

js topdowntabletop.com collected 2026-10-08 09:16:34 UTC 12,787 bytes, 312 lines download raw bytes

1/* GENERATED FILE -- do not edit.
2 * Source: packages/shared/src/tournament/standings.ts
3 * Regenerate: npm run tokens
4 *
5 * Browser twin of the Dice Throne tournament standings/tiebreaker module,
6 * COMPILED from the real TypeScript source (see formats.js's banner for why).
7 * Exposed as window.TDTT.tournamentStandings.
8 *
9 * standings.ts's one runtime import (bracketFinish, from bracket.ts) is
10 * rewritten from a require() call to window.TDTT.bracket -- this twin reads
11 * the bracket twin off the global instead of bundling a copy, so bracket.js
12 * MUST be loaded before tournament-standings.js on any page that uses both.
13 */
14(function () {
15  'use strict';
16  var exports = {};
17"use strict";
18var __assign = (this && this.__assign) || function () {
19    __assign = Object.assign || function(t) {
20        for (var s, i = 1, n = arguments.length; i < n; i++) {
21            s = arguments[i];
22            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
23                t[p] = s[p];
24        }
25        return t;
26    };
27    return __assign.apply(this, arguments);
28};
29var __values = (this && this.__values) || function(o) {
30    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
31    if (m) return m.call(o);
32    if (o && typeof o.length === "number") return {
33        next: function () {
34            if (o && i >= o.length) o = void 0;
35            return { value: o && o[i++], done: !o };
36        }
37    };
38    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
39};
40var __read = (this && this.__read) || function (o, n) {
41    var m = typeof Symbol === "function" && o[Symbol.iterator];
42    if (!m) return o;
43    var i = m.call(o), r, ar = [], e;
44    try {
45        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
46    }
47    catch (error) { e = { error: error }; }
48    finally {
49        try {
50            if (r && !r.done && (m = i["return"])) m.call(i);
51        }
52        finally { if (e) throw e.error; }
53    }
54    return ar;
55};
56var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
57    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
58        if (ar || !(i in from)) {
59            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
60            ar[i] = from[i];
61        }
62    }
63    return to.concat(ar || Array.prototype.slice.call(from));
64};
65Object.defineProperty(exports, "__esModule", { value: true });
66exports.rankStandings = rankStandings;
67exports.eliminationStandings = eliminationStandings;
68exports.describeTiebreakers = describeTiebreakers;
69var bracket_js_1 = window.TDTT.bracket;
70function sortStableDesc(items, key) {
71    return items
72        .map(function (item, idx) { return ({ item: item, idx: idx }); })
73        .sort(function (a, b) { return key(b.item) - key(a.item) || a.idx - b.idx; })
74        .map(function (entry) { return entry.item; });
75}
76function bucketConsecutive(items, key) {
77    var buckets = [];
78    var i = 0;
79    while (i < items.length) {
80        var j = i + 1;
81        while (j < items.length && key(items[j]) === key(items[i]))
82            j++;
83        buckets.push(items.slice(i, j));
84        i = j;
85    }
86    return buckets;
87}
88function healthDiffByPlayer(matches) {
89    var e_1, _a, e_2, _b;
90    var _c, _d;
91    var diffs = new Map();
92    var add = function (id, delta) {
93        var _a;
94        diffs.set(id, ((_a = diffs.get(id)) !== null && _a !== void 0 ? _a : 0) + delta);
95    };
96    try {
97        for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
98            var match = matches_1_1.value;
99            if (match.player2Id == null)
100                continue;
101            var games = match.games && match.games.length > 0
102                ? match.games
103                : [
104                    {
105                        player1HealthRemaining: match.player1HealthRemaining,
106                        player2HealthRemaining: match.player2HealthRemaining,
107                    },
108                ];
109            try {
110                for (var games_1 = (e_2 = void 0, __values(games)), games_1_1 = games_1.next(); !games_1_1.done; games_1_1 = games_1.next()) {
111                    var game = games_1_1.value;
112                    var h1 = (_c = game.player1HealthRemaining) !== null && _c !== void 0 ? _c : 0;
113                    var h2 = (_d = game.player2HealthRemaining) !== null && _d !== void 0 ? _d : 0;
114                    add(match.player1Id, h1 - h2);
115                    add(match.player2Id, h2 - h1);
116                }
117            }
118            catch (e_2_1) { e_2 = { error: e_2_1 }; }
119            finally {
120                try {
121                    if (games_1_1 && !games_1_1.done && (_b = games_1.return)) _b.call(games_1);
122                }
123                finally { if (e_2) throw e_2.error; }
124            }
125        }
126    }
127    catch (e_1_1) { e_1 = { error: e_1_1 }; }
128    finally {
129        try {
130            if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
131        }
132        finally { if (e_1) throw e_1.error; }
133    }
134    return diffs;
135}
136function h2hScore(id, groupIds, completedMatches) {
137    var e_3, _a;
138    var points = 0;
139    try {
140        for (var completedMatches_1 = __values(completedMatches), completedMatches_1_1 = completedMatches_1.next(); !completedMatches_1_1.done;
140 completedMatches_1_1 = completedMatches_1.next()) {
141            var match = completedMatches_1_1.value;
142            if (match.player2Id == null)
143                continue;
144            if (match.player1Id !== id && match.player2Id !== id)
145                continue;
146            var oppId = match.player1Id === id ? match.player2Id : match.player1Id;
147            if (oppId === id || !groupIds.has(oppId))
148                continue;
149            if (match.isDraw)
150                points += 1;
151            else if (match.winnerId === id)
152                points += 3;
153        }
154    }
155    catch (e_3_1) { e_3 = { error: e_3_1 }; }
156    finally {
157        try {
158            if (completedMatches_1_1 && !completedMatches_1_1.done && (_a = completedMatches_1.return)) _a.call(completedMatches_1);
159        }
160        finally { if (e_3) throw e_3.error; }
161    }
162    return points;
163}
164function tiebreakerScore(tb, player, groupIds, completedMatches) {
165    switch (tb) {
166        case 'wins':
167            return player.wins;
168        case 'buchholz':
169            return player.buchholz;
170        case 'median_buchholz':
171            return player.medianBuchholz;
172        case 'health_diff':
173            return player.healthDiff;
174        case 'h2h':
175            return h2hScore(player.id, groupIds, completedMatches);
176    }
177}
178function rankByTiebreakers(group, tiebreakers, completedMatches) {
179    if (group.length <= 1 || tiebreakers.length === 0)
180        return __spreadArray([], __read(group), false);
181    var _a = __read(tiebreakers), tb = _a[0], rest = _a.slice(1);
182    var groupIds = new Set(group.map(function (p) { return p.id; }));
183    var scoreOf = new Map(group.map(function (p) { return [p.id, tiebreakerScore(tb, p, groupIds, completedMatches)]; }));
184    var sorted = sortStableDesc(group, function (p) { return scoreOf.get(p.id); });
185    var buckets = bucketConsecutive(sorted, function (p) { return scoreOf.get(p.id); });
186    return buckets.flatMap(function (bucket) { return rankByTiebreakers(bucket, rest, completedMatches); });
187}
188function rankStandings(players, completedMatches, tiebreakers) {
189    var e_4, _a;
190    var eligible = players.filter(function (p) { return !p.dropped && p.approved !== false; });
191    var eligibleIds = new Set(eligible.map(function (p) { return p.id; }));
192    var matchPointsById = new Map();
193    try {
194        for (var players_1 = __values(players), players_1_1 = players_1.next(); !players_1_1.done; players_1_1 = players_1.next()) {
195            var p = players_1_1.value;
196            matchPointsById.set(p.id, eligibleIds.has(p.id) ? p.wins * 3 + p.draws : p.matchPoints);
197        }
198    }
199    catch (e_4_1) { e_4 = { error: e_4_1 }; }
200    finally {
201        try {
202            if (players_1_1 && !players_1_1.done && (_a = players_1.return)) _a.call(players_1);
203        }
204        finally { if (e_4) throw e_4.error; }
205    }
206    var healthDiffs = healthDiffByPlayer(completedMatches);
207    var computed = eligible.map(function (p) {
208        var _a;
209        var oppPoints = p.opponents
210            .map(function (oppId) { return matchPointsById.get(oppId); })
211            .filter(function (points) { return points !== undefined; });
212        var buchholz = oppPoints.reduce(function (sum, points) { return sum + points; }, 0);
213        var medianBuchholz = buchholz;
214        if (oppPoints.length >= 3) {
215            var sortedOppPoints = __spreadArray([], __read(oppPoints), false).sort(function (a, b) { return a - b; });
216            medianBuchholz = sortedOppPoints.slice(1, -1).reduce(function (sum, points) { return sum + points; }, 0);
217        }
218        return {
219            id: p.id,
220            wins: p.wins,
221            matchPoints: matchPointsById.get(p.id),
222            buchholz: buchholz,
223            medianBuchholz: medianBuchholz,
224            healthDiff: (_a = healthDiffs.get(p.id)) !== null && _a !== void 0 ? _a : 0,
225        };
226    });
227    var sortedByPoints = sortStableDesc(computed, function (p) { return p.matchPoints; });
228    var pointGroups = bucketConsecutive(sortedByPoints, function (p) { return p.matchPoints; });
229    var ranked = pointGroups.flatMap(function (group) {
230        return rankByTiebreakers(group, tiebreakers, completedMatches);
231    });
232    return ranked.map(function (p, i) { return ({
233        id: p.id,
234        rank: i + 1,
235        matchPoints: p.matchPoints,
236        wins: p.wins,
237        buchholz: p.buchholz,
238        medianBuchholz: p.medianBuchholz,
239        healthDiff: p.healthDiff,
240    }); });
241}
242function eliminationStandings(bracket, preCut) {
243    var e_5, _a, e_6, _b;
244    var _c;
245    var preCutRank = new Map(preCut.map(function (p, idx) { return [p.playerId, idx]; }));
246    var rankOf = function (playerId) { var _a; return (_a = preCutRank.get(playerId)) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER; };
247    var entrantIds = new Set();
248    try {
249        for (var _d = __values((_c = bracket.rounds[0]) !== null && _c !== void 0 ? _c : []), _e = _d.next(); !_e.done; _e = _d.next()) {
250            var match = _e.value;
251            if (match.p1 != null)
252                entrantIds.add(match.p1);
253            if (match.p2 != null)
254                entrantIds.add(match.p2);
255        }
256    }
257    catch (e_5_1) { e_5 = { error: e_5_1 }; }
258    finally {
259        try {
260            if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
261        }
262        finally { if (e_5) throw e_5.error; }
263    }
264    var decided = new Map((0, bracket_js_1.bracketFinish)(bracket).map(function (entry) { return [entry.playerId, entry]; }));
265    var alive = [];
266    var finished = [];
267    try {
268        for (var entrantIds_1 = __values(entrantIds), entrantIds_1_1 = entrantIds_1.next(); !entrantIds_1_1.done; entrantIds_1_1 = entrantIds_1.next()) {
269            var playerId = entrantIds_1_1.value;
270            var entry = decided.get(playerId);
271            if (entry) {
272                finished.push({
273                    playerId: playerId,
274                    place: entry.place,
275                    finishRound: entry.finishRound,
276                    alive: false,
277                    rank: 0,
278                });
279            }
280            else {
281                alive.push({ playerId: playerId, place: null, finishRound: null, alive: true, rank: 0 });
282            }
283        }
284    }
285    catch (e_6_1) { e_6 = { error: e_6_1 }; }
286    finally {
287        try {
288            if (entrantIds_1_1 && !entrantIds_1_1.done && (_b = entrantIds_1.return)) _b.call(entrantIds_1);
289        }
290        finally { if (e_6) throw e_6.error; }
291    }
292    alive.sort(function (a, b) { return rankOf(a.playerId) - rankOf(b.playerId); });
293    finished.sort(function (a, b) {
294        if (a.place !== b.place)
295            return a.place - b.place;
296        return rankOf(a.playerId) - rankOf(b.playerId);
297    });
298    return __spreadArray(__spreadArray([], __read(alive), false), __read(finished), false).map(function (entry, i) { return (__assign(__assign({}, entry), { rank: i + 1 })); });
299}
300var TIEBREAKER_DESCRIPTIONS = {
301    h2h: 'Head-to-head',
302    median_buchholz: "Median Buchholz (opponents' strength, best and worst dropped)",
303    buchholz: "Buchholz (opponents' strength)",
304    health_diff: 'Health difference',
305    wins: 'Total wins',
306};
307function describeTiebreakers(list) {
308    return list.map(function (tb) { return TIEBREAKER_DESCRIPTIONS[tb]; });
309}
310  window.TDTT = window.TDTT || {};
311  window.TDTT.tournamentStandings = exports;
312})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.