Created
April 14, 2022 00:52
-
-
Save ReiFan49/0ebe717b03e5329d2d1f7562829bae8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateWinnerBracket(playerCounts) { | |
let winnerRounds = Math.ceil(Math.log2(playerCounts)); | |
const seeds = Array.apply(null, {length: playerCounts}).map((x, i) => i + 1); | |
const rounds = []; | |
const expectedNext = []; | |
const expectedBuffer = []; | |
for(let seedPower = 1; seedPower <= winnerRounds; seedPower++) { | |
const round = {round: winnerRounds - seedPower + 1, matches: []}; | |
const availableSeeds = []; | |
[].push.apply(availableSeeds, seeds.filter(x => x > (1 << (seedPower - 1)) && x <= (1 << seedPower))); | |
[].push.apply(availableSeeds, seeds.filter(x => x <= (1 << (seedPower - 1))).slice(-(availableSeeds.length))); | |
[].push.apply(expectedNext, expectedNext.splice(0).reduce(function(x, y) { | |
if(availableSeeds.indexOf(y) >= 0) { | |
x.push(y); | |
} | |
return x; | |
}, [])); | |
availableSeeds.sort((x, y) => x - y); | |
const pairSum = 1 + (1 << (seedPower)); | |
const matchCount = expectedNext.length || 1 << (seedPower - 1); | |
for(let matchNum = 0; matchNum < matchCount; matchNum++) { | |
let seedWin, seedLose; | |
if (expectedNext.length <= 0) { | |
seedWin = availableSeeds.shift(); | |
} else { | |
seedWin = expectedNext.shift(); | |
} | |
seedLose = pairSum - seedWin; | |
[seedWin, seedLose].forEach(function(seedValue){ | |
if(availableSeeds.indexOf(seedValue) >= 0) | |
availableSeeds.splice(availableSeeds.indexOf(seedValue), 1); | |
expectedBuffer.push(seedValue); | |
}); | |
round.matches.push({matchID: [seedPower, matchNum], pair: [seedWin, seedLose]}); | |
console.log(seedPower, matchNum, [seedWin, seedLose]); | |
} | |
[].push.apply(expectedNext, expectedBuffer.splice(0)); | |
rounds.unshift(round); | |
} | |
return rounds; | |
} | |
function generateDoubleBracket(playerCounts, options){ | |
options = options || {}; | |
let winnerRounds = 1; | |
let loserRounds = 0; | |
if (options.split == "lower") { | |
winnerRounds += Math.floor(Math.log2(playerCounts)); | |
loserRounds += 1; | |
} else { | |
winnerRounds += Math.ceil(Math.log2(playerCounts)); | |
} | |
loserRounds += 2 * (1 + Math.max(0, winnerRounds - 3)); | |
const seeds = Array.apply(null, {length: playerCounts}).map((x, i) => i + 1); | |
const rounds = []; | |
console.log(playerCounts); | |
console.log(winnerRounds, "W", loserRounds, "L"); | |
generateWinnerBracket(playerCounts); | |
} | |
generateDoubleBracket(32); | |
generateDoubleBracket(44, {split: "upper"}); | |
generateDoubleBracket(44, {split: "lower"}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment