Files
pk-lab-xhs/utils/bracket.js
T
codex-bot 350aae797f
Verify mini program and deploy preview / verify-deploy (push) Failing after 34s
feat: launch PK testing application
2026-09-18 21:50:25 +08:00

45 lines
1.3 KiB
JavaScript

function create(candidates) {
return {
round: 1,
pool: candidates.slice(),
pairIndex: 0,
winners: [],
eliminated: [],
decisions: []
};
}
function current(state) {
if (state.pool.length === 1) return null;
return [state.pool[state.pairIndex], state.pool[state.pairIndex + 1] || null];
}
function choose(state, winner) {
var pair = current(state);
if (!pair || !pair[1]) throw new Error('invalid bracket pair');
var loser = pair[0].id === winner.id ? pair[1] : pair[0];
state.winners.push(winner);
state.eliminated.unshift(loser);
state.decisions.push({ round: state.round, left_id: pair[0].id, right_id: pair[1].id, winner_id: winner.id });
state.pairIndex += 2;
if (state.pairIndex < state.pool.length) {
if (!state.pool[state.pairIndex + 1]) {
state.winners.push(state.pool[state.pairIndex]);
state.pairIndex += 1;
}
if (state.pairIndex < state.pool.length) return false;
}
state.pool = state.winners;
state.winners = [];
state.pairIndex = 0;
state.round += 1;
return state.pool.length === 1;
}
function ranking(state) {
return state.pool.concat(state.eliminated).map(function (item) { return item.id; });
}
module.exports = { create: create, current: current, choose: choose, ranking: ranking };