Skip to content

Commit e6ae7a6

Browse files
committed
Tournaments: minor style fixes
1 parent 0856529 commit e6ae7a6

File tree

2 files changed

+91
-121
lines changed

2 files changed

+91
-121
lines changed

tournaments/generator-elimination.js

+56-60
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,44 @@ function fixSingleChildNode(parentNode) {
2020
}
2121

2222
var value = parentNode.getValue();
23-
for (var key in value)
23+
for (var key in value) {
2424
delete value[key];
25+
}
2526
Object.merge(value, parentNode.removeChildAt(0).getValue());
2627
return parentNode;
2728
}
2829

2930
var Elimination = (function () {
3031
function Elimination(maxSubtrees) {
3132
maxSubtrees = maxSubtrees || 1;
32-
if (typeof maxSubtrees === 'string' && maxSubtrees.toLowerCase() === 'infinity')
33+
if (typeof maxSubtrees === 'string' && maxSubtrees.toLowerCase() === 'infinity') {
3334
maxSubtrees = Infinity;
34-
else if (typeof maxSubtrees !== 'number')
35+
} else if (typeof maxSubtrees !== 'number') {
3536
maxSubtrees = parseInt(maxSubtrees, 10);
36-
if (!maxSubtrees || maxSubtrees < 1)
37-
maxSubtrees = 1;
37+
}
38+
if (!maxSubtrees || maxSubtrees < 1) maxSubtrees = 1;
3839

3940
this.maxSubtrees = maxSubtrees;
4041
this.isBracketFrozen = false;
4142
this.tree = null;
4243
this.users = new Map();
4344

44-
if (nameMap[maxSubtrees])
45+
if (nameMap[maxSubtrees]) {
4546
this.name = nameMap[maxSubtrees] + " " + this.name;
46-
else if (maxSubtrees === Infinity)
47+
} else if (maxSubtrees === Infinity) {
4748
this.name = "N-" + this.name;
48-
else
49+
} else {
4950
this.name = maxSubtrees + "-tuple " + this.name;
51+
}
5052
}
5153

5254
Elimination.prototype.name = "Elimination";
5355
Elimination.prototype.isDrawingSupported = false;
5456

5557
Elimination.prototype.addUser = function (user) {
56-
if (this.isBracketFrozen)
57-
return 'BracketFrozen';
58+
if (this.isBracketFrozen) return 'BracketFrozen';
5859

59-
if (this.users.has(user))
60-
return 'UserAlreadyAdded';
60+
if (this.users.has(user)) return 'UserAlreadyAdded';
6161
this.users.set(user, {});
6262

6363
if (!this.tree) {
@@ -88,23 +88,22 @@ var Elimination = (function () {
8888
}
8989
};
9090
Elimination.prototype.removeUser = function (user) {
91-
if (this.isBracketFrozen)
92-
return 'BracketFrozen';
91+
if (this.isBracketFrozen) return 'BracketFrozen';
9392

94-
if (!this.users.has(user))
95-
return 'UserNotAdded';
93+
if (!this.users.has(user)) return 'UserNotAdded';
9694
this.users.delete(user);
9795

9896
var targetNode;
99-
for (var n = 0; n < this.tree.currentLayerLeafNodes.length && !targetNode; ++n)
97+
for (var n = 0; n < this.tree.currentLayerLeafNodes.length && !targetNode; ++n) {
10098
if (this.tree.currentLayerLeafNodes[n].getValue().user === user) {
10199
targetNode = this.tree.currentLayerLeafNodes[n];
102100
this.tree.currentLayerLeafNodes.splice(n, 1);
103101
}
102+
}
104103
if (targetNode) {
105-
if (this.users.size === 0)
104+
if (this.users.size === 0) {
106105
this.tree = null;
107-
else if (this.tree.nextLayerLeafNodes.length === 0) {
106+
} else if (this.tree.nextLayerLeafNodes.length === 0) {
108107
this.tree.nextLayerLeafNodes = this.tree.currentLayerLeafNodes;
109108

110109
var parentNode = targetNode.getParent();
@@ -125,33 +124,36 @@ var Elimination = (function () {
125124
return;
126125
}
127126

128-
for (var n = 0; n < this.tree.nextLayerLeafNodes.length && !targetNode; ++n)
127+
for (var n = 0; n < this.tree.nextLayerLeafNodes.length && !targetNode; ++n) {
129128
if (this.tree.nextLayerLeafNodes[n].getValue().user === user) {
130129
targetNode = this.tree.nextLayerLeafNodes[n];
131130
this.tree.nextLayerLeafNodes.splice(n, 1);
132131
}
132+
}
133133
var parentNode = targetNode.getParent();
134134
parentNode.removeChild(targetNode);
135135
this.tree.nextLayerLeafNodes.splice(this.tree.nextLayerLeafNodes.indexOf(parentNode.getChildAt(0)), 1);
136136
this.tree.currentLayerLeafNodes.push(fixSingleChildNode(parentNode));
137137
};
138138
Elimination.prototype.replaceUser = function (user, replacementUser) {
139-
if (!this.users.has(user))
140-
return 'UserNotAdded';
139+
if (!this.users.has(user)) return 'UserNotAdded';
141140

142-
if (this.users.has(replacementUser))
143-
return 'UserAlreadyAdded';
141+
if (this.users.has(replacementUser)) return 'UserAlreadyAdded';
144142

145143
this.users.delete(user);
146144
this.users.set(user, {});
147145

148146
var targetNode;
149-
for (var n = 0; n < this.tree.currentLayerLeafNodes.length && !targetNode; ++n)
150-
if (this.tree.currentLayerLeafNodes[n].getValue().user === user)
147+
for (var n = 0; n < this.tree.currentLayerLeafNodes.length && !targetNode; ++n) {
148+
if (this.tree.currentLayerLeafNodes[n].getValue().user === user) {
151149
targetNode = this.tree.currentLayerLeafNodes[n];
152-
for (var n = 0; n < this.tree.nextLayerLeafNodes.length && !targetNode; ++n)
153-
if (this.tree.nextLayerLeafNodes[n].getValue().user === user)
150+
}
151+
}
152+
for (var n = 0; n < this.tree.nextLayerLeafNodes.length && !targetNode; ++n) {
153+
if (this.tree.nextLayerLeafNodes[n].getValue().user === user) {
154154
targetNode = this.tree.nextLayerLeafNodes[n];
155+
}
156+
}
155157
targetNode.getValue().user = replacementUser;
156158
};
157159
Elimination.prototype.getUsers = function (remaining) {
@@ -174,9 +176,9 @@ var Elimination = (function () {
174176
frame.toNode.children.push(node);
175177

176178
var fromNodeValues = frame.fromNode.getValue();
177-
if (frame.fromNode.isLeaf())
179+
if (frame.fromNode.isLeaf()) {
178180
node.team = fromNodeValues.user || null;
179-
else {
181+
} else {
180182
node.state = fromNodeValues.state || 'unavailable';
181183
if (node.state === 'finished') {
182184
node.team = fromNodeValues.user;
@@ -210,11 +212,9 @@ var Elimination = (function () {
210212
var queue = [{node: this.tree.tree, depth: 0}];
211213
while (queue.length > 0) {
212214
var frame = queue.shift();
213-
if (frame.node.isLeaf() || frame.node.getValue().onLoseNode)
214-
continue;
215+
if (frame.node.isLeaf() || frame.node.getValue().onLoseNode) continue;
215216

216-
if (!matchesByDepth[frame.depth])
217-
matchesByDepth[frame.depth] = [];
217+
if (!matchesByDepth[frame.depth]) matchesByDepth[frame.depth] = [];
218218
matchesByDepth[frame.depth].push(frame.node);
219219

220220
queue.push({node: frame.node.getChildAt(0), depth: frame.depth + 1});
@@ -229,8 +229,7 @@ var Elimination = (function () {
229229
newTree.currentLayerLeafNodes.push(newTree.tree);
230230

231231
for (var m in matchesByDepth) {
232-
if (m === '0')
233-
continue;
232+
if (m === '0') continue;
234233
var n = 0;
235234
for (; n < matchesByDepth[m].length - 1; n += 2) {
236235
// Replace old leaf with:
@@ -294,63 +293,59 @@ var Elimination = (function () {
294293
};
295294

296295
Elimination.prototype.disqualifyUser = function (user) {
297-
if (!this.isBracketFrozen)
298-
return 'BracketNotFrozen';
296+
if (!this.isBracketFrozen) return 'BracketNotFrozen';
299297

300-
if (!this.users.has(user))
301-
return 'UserNotAdded';
298+
if (!this.users.has(user)) return 'UserNotAdded';
302299

303300
this.users.get(user).isDisqualified = true;
304301

305302
// The user either has a single available battle or no available battles
306303
var match = null;
307304
var result;
308305
this.tree.tree.traverse(function (node) {
309-
if (node.getValue().state === 'available')
306+
if (node.getValue().state === 'available') {
310307
if (node.getChildAt(0).getValue().user === user) {
311308
match = [user, node.getChildAt(1).getValue().user];
312309
result = 'loss';
313310
} else if (node.getChildAt(1).getValue().user === user) {
314311
match = [node.getChildAt(0).getValue().user, user];
315312
result = 'win';
316313
}
314+
}
317315

318316
return !match;
319317
});
320318
if (match) {
321319
var error = this.setMatchResult(match, result);
322-
if (error)
320+
if (error) {
323321
throw new Error("Unexpected " + error + " from setMatchResult([" + match.join(", ") + "], " + result + ")");
322+
}
324323
}
325324
};
326325
Elimination.prototype.getUserBusy = function (user) {
327-
if (!this.isBracketFrozen)
328-
return 'BracketNotFrozen';
326+
if (!this.isBracketFrozen) return 'BracketNotFrozen';
329327

330-
if (!this.users.has(user))
331-
return 'UserNotAdded';
328+
if (!this.users.has(user)) return 'UserNotAdded';
332329
return this.users.get(user).isBusy;
333330
};
334331
Elimination.prototype.setUserBusy = function (user, isBusy) {
335-
if (!this.isBracketFrozen)
336-
return 'BracketNotFrozen';
332+
if (!this.isBracketFrozen) return 'BracketNotFrozen';
337333

338-
if (!this.users.has(user))
339-
return 'UserNotAdded';
334+
if (!this.users.has(user)) return 'UserNotAdded';
340335
this.users.get(user).isBusy = isBusy;
341336
};
342337

343338
Elimination.prototype.getAvailableMatches = function () {
344-
if (!this.isBracketFrozen)
345-
return 'BracketNotFrozen';
339+
if (!this.isBracketFrozen) return 'BracketNotFrozen';
346340

347341
var matches = [];
348342
this.tree.tree.traverse(function (node) {
349343
if (node.getValue().state === 'available') {
350344
var userA = node.getChildAt(0).getValue().user;
351345
var userB = node.getChildAt(1).getValue().user;
352-
if (!this.users.get(userA).isBusy && !this.users.get(userB).isBusy)
346+
if (!this.users.get(userA).isBusy && !this.users.get(userB).isBusy) {
353347
matches.push([userA, userB]);
348+
}
354349
}
355350
}, this);
356351
return matches;
@@ -366,8 +361,9 @@ var Elimination = (function () {
366361
this.tree.tree.traverse(function (node) {
367362
if (node.getValue().state === 'available' &&
368363
node.getChildAt(0).getValue().user === match[0] &&
369-
node.getChildAt(1).getValue().user === match[1])
364+
node.getChildAt(1).getValue().user === match[1]) {
370365
targetNode = node;
366+
}
371367
return !targetNode;
372368
});
373369
if (!targetNode) return 'InvalidMatch';
@@ -430,8 +426,9 @@ var Elimination = (function () {
430426
else if (this.users.get(userB).isDisqualified)
431427
error = this.setMatchResult([userA, userB], 'win');
432428

433-
if (error)
429+
if (error) {
434430
throw new Error("Unexpected " + error + " from setMatchResult([" + userA + ", " + userB + "], ...)");
431+
}
435432
}
436433
}
437434
};
@@ -441,20 +438,19 @@ var Elimination = (function () {
441438
};
442439

443440
Elimination.prototype.getResults = function () {
444-
if (!this.isTournamentEnded())
445-
return 'TournamentNotEnded';
441+
if (!this.isTournamentEnded()) return 'TournamentNotEnded';
446442

447443
var results = [];
448444
var currentNode = this.tree.tree;
449445
for (var n = 0; n < this.maxSubtrees; ++n) {
450446
results.push([currentNode.getValue().user]);
451447
currentNode = currentNode.getChildAt(currentNode.getValue().result === 'loss' ? 0 : 1);
452-
if (!currentNode)
453-
break;
448+
if (!currentNode) break;
454449
}
455450

456-
if (this.users.size - 1 === this.maxSubtrees && currentNode)
451+
if (this.users.size - 1 === this.maxSubtrees && currentNode) {
457452
results.push([currentNode.getValue().user]);
453+
}
458454

459455
return results;
460456
};

0 commit comments

Comments
 (0)