feat: add animated choice modes and bracket editor
Verify mini program and deploy preview / verify-deploy (push) Successful in 50s
Verify mini program and deploy preview / verify-deploy (push) Successful in 50s
This commit is contained in:
+35
-31
@@ -1,44 +1,48 @@
|
||||
function buildMatches(pool) {
|
||||
var matches = [];
|
||||
for (var index = 0; index < pool.length; index += 2) {
|
||||
matches.push({ left: pool[index], right: pool[index + 1] || null, winner: pool[index + 1] ? null : pool[index] });
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
function create(candidates) {
|
||||
return {
|
||||
round: 1,
|
||||
pool: candidates.slice(),
|
||||
pairIndex: 0,
|
||||
winners: [],
|
||||
eliminated: [],
|
||||
decisions: []
|
||||
};
|
||||
var pool = candidates.slice();
|
||||
return { round: 1, pool: pool, matches: buildMatches(pool), eliminated: [], decisions: [] };
|
||||
}
|
||||
|
||||
function current(state) {
|
||||
if (state.pool.length === 1) return null;
|
||||
return [state.pool[state.pairIndex], state.pool[state.pairIndex + 1] || null];
|
||||
var match = state.matches.filter(function (item) { return item.right && !item.winner; })[0];
|
||||
return match ? [match.left, match.right] : null;
|
||||
}
|
||||
|
||||
function advance(state) {
|
||||
if (state.matches.some(function (item) { return !item.winner; })) return false;
|
||||
state.pool = state.matches.map(function (item) { return item.winner; });
|
||||
if (state.pool.length === 1) return true;
|
||||
state.round += 1;
|
||||
state.matches = buildMatches(state.pool);
|
||||
return false;
|
||||
}
|
||||
|
||||
function chooseAt(state, matchIndex, winner) {
|
||||
var match = state.matches[matchIndex];
|
||||
if (!match || !match.right || match.winner) throw new Error('invalid bracket match');
|
||||
if (winner.id !== match.left.id && winner.id !== match.right.id) throw new Error('winner is not in match');
|
||||
var loser = match.left.id === winner.id ? match.right : match.left;
|
||||
match.winner = winner;
|
||||
state.eliminated.unshift(loser);
|
||||
state.decisions.push({ round: state.round, left_id: match.left.id, right_id: match.right.id, winner_id: winner.id });
|
||||
return advance(state);
|
||||
}
|
||||
|
||||
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;
|
||||
var index = state.matches.findIndex(function (item) { return item.right && !item.winner; });
|
||||
return chooseAt(state, index, winner);
|
||||
}
|
||||
|
||||
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 };
|
||||
|
||||
module.exports = { create: create, current: current, choose: choose, chooseAt: chooseAt, ranking: ranking };
|
||||
|
||||
Reference in New Issue
Block a user