From 07090070efbedca2fa4724545b2542f138706e98 Mon Sep 17 00:00:00 2001 From: sirDonovan Date: Mon, 26 Oct 2015 11:52:12 -0500 Subject: [PATCH] Tournaments: randomize seeds in elimination Generate the bracket when the tournament starts rather than during signups to randomize seeds --- tournaments/generator-elimination.js | 113 +++++++-------------------- tournaments/index.js | 1 + 2 files changed, 30 insertions(+), 84 deletions(-) diff --git a/tournaments/generator-elimination.js b/tournaments/generator-elimination.js index c330b1e456..07a4dc71df 100644 --- a/tournaments/generator-elimination.js +++ b/tournaments/generator-elimination.js @@ -10,21 +10,6 @@ const nameMap = { // Feel free to add more }; -function fixSingleChildNode(parentNode) { - if (parentNode.getParent()) { - var newNode = parentNode.removeChildAt(0); - parentNode.getParent().replaceChild(newNode, parentNode); - return newNode; - } - - var value = parentNode.getValue(); - for (var key in value) { - delete value[key]; - } - Object.merge(value, parentNode.removeChildAt(0).getValue()); - return parentNode; -} - var Elimination = (function () { function Elimination(maxSubtrees) { maxSubtrees = maxSubtrees || 1; @@ -57,81 +42,12 @@ var Elimination = (function () { if (this.users.has(user)) return 'UserAlreadyAdded'; this.users.set(user, {}); - - if (!this.tree) { - this.tree = { - tree: new TreeNode(null, {user: user}), - currentLayerLeafNodes: [], - nextLayerLeafNodes: [] - }; - this.tree.currentLayerLeafNodes.push(this.tree.tree); - return; - } - - var targetNode = this.tree.currentLayerLeafNodes.shift(); - - var newNode = new TreeNode(null, {user: targetNode.getValue().user}); - this.tree.nextLayerLeafNodes.push(newNode); - targetNode.addChild(newNode); - - newNode = new TreeNode(null, {user: user}); - this.tree.nextLayerLeafNodes.push(newNode); - targetNode.addChild(newNode); - - delete targetNode.getValue().user; - - if (this.tree.currentLayerLeafNodes.length === 0) { - this.tree.currentLayerLeafNodes = this.tree.nextLayerLeafNodes; - this.tree.nextLayerLeafNodes = []; - } }; Elimination.prototype.removeUser = function (user) { if (this.isBracketFrozen) return 'BracketFrozen'; if (!this.users.has(user)) return 'UserNotAdded'; this.users.delete(user); - - var targetNode; - for (var n = 0; n < this.tree.currentLayerLeafNodes.length && !targetNode; ++n) { - if (this.tree.currentLayerLeafNodes[n].getValue().user === user) { - targetNode = this.tree.currentLayerLeafNodes[n]; - this.tree.currentLayerLeafNodes.splice(n, 1); - } - } - if (targetNode) { - if (this.users.size === 0) { - this.tree = null; - } else if (this.tree.nextLayerLeafNodes.length === 0) { - this.tree.nextLayerLeafNodes = this.tree.currentLayerLeafNodes; - - var parentNode = targetNode.getParent(); - parentNode.removeChild(targetNode); - this.tree.nextLayerLeafNodes.splice(this.tree.nextLayerLeafNodes.indexOf(parentNode.getChildAt(0)), 1); - this.tree.currentLayerLeafNodes = [fixSingleChildNode(parentNode)]; - } else { - var newNode = this.tree.nextLayerLeafNodes.pop(); - - var parentNode = newNode.getParent(); - parentNode.removeChild(newNode); - this.tree.nextLayerLeafNodes.splice(this.tree.nextLayerLeafNodes.indexOf(parentNode.getChildAt(0)), 1); - this.tree.currentLayerLeafNodes.push(fixSingleChildNode(parentNode)); - - targetNode.getParent().replaceChild(newNode, targetNode); - this.tree.currentLayerLeafNodes.push(newNode); - } - return; - } - - for (var n = 0; n < this.tree.nextLayerLeafNodes.length && !targetNode; ++n) { - if (this.tree.nextLayerLeafNodes[n].getValue().user === user) { - targetNode = this.tree.nextLayerLeafNodes[n]; - this.tree.nextLayerLeafNodes.splice(n, 1); - } - } - var parentNode = targetNode.getParent(); - parentNode.removeChild(targetNode); - this.tree.nextLayerLeafNodes.splice(this.tree.nextLayerLeafNodes.indexOf(parentNode.getChildAt(0)), 1); - this.tree.currentLayerLeafNodes.push(fixSingleChildNode(parentNode)); }; Elimination.prototype.replaceUser = function (user, replacementUser) { if (!this.users.has(user)) return 'UserNotAdded'; @@ -163,6 +79,35 @@ var Elimination = (function () { return users; }; + Elimination.prototype.generateBracket = function () { + this.getUsers().randomize().forEach(function (user) { + if (!this.tree) { + this.tree = { + tree: new TreeNode(null, {user: user}), + currentLayerLeafNodes: [], + nextLayerLeafNodes: [] + }; + this.tree.currentLayerLeafNodes.push(this.tree.tree); + return; + } + var targetNode = this.tree.currentLayerLeafNodes.shift(); + + var newNode = new TreeNode(null, {user: targetNode.getValue().user}); + this.tree.nextLayerLeafNodes.push(newNode); + targetNode.addChild(newNode); + + newNode = new TreeNode(null, {user: user}); + this.tree.nextLayerLeafNodes.push(newNode); + targetNode.addChild(newNode); + + delete targetNode.getValue().user; + + if (this.tree.currentLayerLeafNodes.length === 0) { + this.tree.currentLayerLeafNodes = this.tree.nextLayerLeafNodes; + this.tree.nextLayerLeafNodes = []; + } + }, this); + }; Elimination.prototype.getBracketData = function () { var rootNode = {children: []}; if (this.tree) { diff --git a/tournaments/index.js b/tournaments/index.js index 8bd608940e..43273da502 100644 --- a/tournaments/index.js +++ b/tournaments/index.js @@ -365,6 +365,7 @@ Tournament = (function () { return false; } + if (this.generator.generateBracket) this.generator.generateBracket(); this.generator.freezeBracket(); this.availableMatches = new Map();