@@ -20,44 +20,44 @@ function fixSingleChildNode(parentNode) {
20
20
}
21
21
22
22
var value = parentNode . getValue ( ) ;
23
- for ( var key in value )
23
+ for ( var key in value ) {
24
24
delete value [ key ] ;
25
+ }
25
26
Object . merge ( value , parentNode . removeChildAt ( 0 ) . getValue ( ) ) ;
26
27
return parentNode ;
27
28
}
28
29
29
30
var Elimination = ( function ( ) {
30
31
function Elimination ( maxSubtrees ) {
31
32
maxSubtrees = maxSubtrees || 1 ;
32
- if ( typeof maxSubtrees === 'string' && maxSubtrees . toLowerCase ( ) === 'infinity' )
33
+ if ( typeof maxSubtrees === 'string' && maxSubtrees . toLowerCase ( ) === 'infinity' ) {
33
34
maxSubtrees = Infinity ;
34
- else if ( typeof maxSubtrees !== 'number' )
35
+ } else if ( typeof maxSubtrees !== 'number' ) {
35
36
maxSubtrees = parseInt ( maxSubtrees , 10 ) ;
36
- if ( ! maxSubtrees || maxSubtrees < 1 )
37
- maxSubtrees = 1 ;
37
+ }
38
+ if ( ! maxSubtrees || maxSubtrees < 1 ) maxSubtrees = 1 ;
38
39
39
40
this . maxSubtrees = maxSubtrees ;
40
41
this . isBracketFrozen = false ;
41
42
this . tree = null ;
42
43
this . users = new Map ( ) ;
43
44
44
- if ( nameMap [ maxSubtrees ] )
45
+ if ( nameMap [ maxSubtrees ] ) {
45
46
this . name = nameMap [ maxSubtrees ] + " " + this . name ;
46
- else if ( maxSubtrees === Infinity )
47
+ } else if ( maxSubtrees === Infinity ) {
47
48
this . name = "N-" + this . name ;
48
- else
49
+ } else {
49
50
this . name = maxSubtrees + "-tuple " + this . name ;
51
+ }
50
52
}
51
53
52
54
Elimination . prototype . name = "Elimination" ;
53
55
Elimination . prototype . isDrawingSupported = false ;
54
56
55
57
Elimination . prototype . addUser = function ( user ) {
56
- if ( this . isBracketFrozen )
57
- return 'BracketFrozen' ;
58
+ if ( this . isBracketFrozen ) return 'BracketFrozen' ;
58
59
59
- if ( this . users . has ( user ) )
60
- return 'UserAlreadyAdded' ;
60
+ if ( this . users . has ( user ) ) return 'UserAlreadyAdded' ;
61
61
this . users . set ( user , { } ) ;
62
62
63
63
if ( ! this . tree ) {
@@ -88,23 +88,22 @@ var Elimination = (function () {
88
88
}
89
89
} ;
90
90
Elimination . prototype . removeUser = function ( user ) {
91
- if ( this . isBracketFrozen )
92
- return 'BracketFrozen' ;
91
+ if ( this . isBracketFrozen ) return 'BracketFrozen' ;
93
92
94
- if ( ! this . users . has ( user ) )
95
- return 'UserNotAdded' ;
93
+ if ( ! this . users . has ( user ) ) return 'UserNotAdded' ;
96
94
this . users . delete ( user ) ;
97
95
98
96
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 ) {
100
98
if ( this . tree . currentLayerLeafNodes [ n ] . getValue ( ) . user === user ) {
101
99
targetNode = this . tree . currentLayerLeafNodes [ n ] ;
102
100
this . tree . currentLayerLeafNodes . splice ( n , 1 ) ;
103
101
}
102
+ }
104
103
if ( targetNode ) {
105
- if ( this . users . size === 0 )
104
+ if ( this . users . size === 0 ) {
106
105
this . tree = null ;
107
- else if ( this . tree . nextLayerLeafNodes . length === 0 ) {
106
+ } else if ( this . tree . nextLayerLeafNodes . length === 0 ) {
108
107
this . tree . nextLayerLeafNodes = this . tree . currentLayerLeafNodes ;
109
108
110
109
var parentNode = targetNode . getParent ( ) ;
@@ -125,33 +124,36 @@ var Elimination = (function () {
125
124
return ;
126
125
}
127
126
128
- for ( var n = 0 ; n < this . tree . nextLayerLeafNodes . length && ! targetNode ; ++ n )
127
+ for ( var n = 0 ; n < this . tree . nextLayerLeafNodes . length && ! targetNode ; ++ n ) {
129
128
if ( this . tree . nextLayerLeafNodes [ n ] . getValue ( ) . user === user ) {
130
129
targetNode = this . tree . nextLayerLeafNodes [ n ] ;
131
130
this . tree . nextLayerLeafNodes . splice ( n , 1 ) ;
132
131
}
132
+ }
133
133
var parentNode = targetNode . getParent ( ) ;
134
134
parentNode . removeChild ( targetNode ) ;
135
135
this . tree . nextLayerLeafNodes . splice ( this . tree . nextLayerLeafNodes . indexOf ( parentNode . getChildAt ( 0 ) ) , 1 ) ;
136
136
this . tree . currentLayerLeafNodes . push ( fixSingleChildNode ( parentNode ) ) ;
137
137
} ;
138
138
Elimination . prototype . replaceUser = function ( user , replacementUser ) {
139
- if ( ! this . users . has ( user ) )
140
- return 'UserNotAdded' ;
139
+ if ( ! this . users . has ( user ) ) return 'UserNotAdded' ;
141
140
142
- if ( this . users . has ( replacementUser ) )
143
- return 'UserAlreadyAdded' ;
141
+ if ( this . users . has ( replacementUser ) ) return 'UserAlreadyAdded' ;
144
142
145
143
this . users . delete ( user ) ;
146
144
this . users . set ( user , { } ) ;
147
145
148
146
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 ) {
151
149
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 ) {
154
154
targetNode = this . tree . nextLayerLeafNodes [ n ] ;
155
+ }
156
+ }
155
157
targetNode . getValue ( ) . user = replacementUser ;
156
158
} ;
157
159
Elimination . prototype . getUsers = function ( remaining ) {
@@ -174,9 +176,9 @@ var Elimination = (function () {
174
176
frame . toNode . children . push ( node ) ;
175
177
176
178
var fromNodeValues = frame . fromNode . getValue ( ) ;
177
- if ( frame . fromNode . isLeaf ( ) )
179
+ if ( frame . fromNode . isLeaf ( ) ) {
178
180
node . team = fromNodeValues . user || null ;
179
- else {
181
+ } else {
180
182
node . state = fromNodeValues . state || 'unavailable' ;
181
183
if ( node . state === 'finished' ) {
182
184
node . team = fromNodeValues . user ;
@@ -210,11 +212,9 @@ var Elimination = (function () {
210
212
var queue = [ { node : this . tree . tree , depth : 0 } ] ;
211
213
while ( queue . length > 0 ) {
212
214
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 ;
215
216
216
- if ( ! matchesByDepth [ frame . depth ] )
217
- matchesByDepth [ frame . depth ] = [ ] ;
217
+ if ( ! matchesByDepth [ frame . depth ] ) matchesByDepth [ frame . depth ] = [ ] ;
218
218
matchesByDepth [ frame . depth ] . push ( frame . node ) ;
219
219
220
220
queue . push ( { node : frame . node . getChildAt ( 0 ) , depth : frame . depth + 1 } ) ;
@@ -229,8 +229,7 @@ var Elimination = (function () {
229
229
newTree . currentLayerLeafNodes . push ( newTree . tree ) ;
230
230
231
231
for ( var m in matchesByDepth ) {
232
- if ( m === '0' )
233
- continue ;
232
+ if ( m === '0' ) continue ;
234
233
var n = 0 ;
235
234
for ( ; n < matchesByDepth [ m ] . length - 1 ; n += 2 ) {
236
235
// Replace old leaf with:
@@ -294,63 +293,59 @@ var Elimination = (function () {
294
293
} ;
295
294
296
295
Elimination . prototype . disqualifyUser = function ( user ) {
297
- if ( ! this . isBracketFrozen )
298
- return 'BracketNotFrozen' ;
296
+ if ( ! this . isBracketFrozen ) return 'BracketNotFrozen' ;
299
297
300
- if ( ! this . users . has ( user ) )
301
- return 'UserNotAdded' ;
298
+ if ( ! this . users . has ( user ) ) return 'UserNotAdded' ;
302
299
303
300
this . users . get ( user ) . isDisqualified = true ;
304
301
305
302
// The user either has a single available battle or no available battles
306
303
var match = null ;
307
304
var result ;
308
305
this . tree . tree . traverse ( function ( node ) {
309
- if ( node . getValue ( ) . state === 'available' )
306
+ if ( node . getValue ( ) . state === 'available' ) {
310
307
if ( node . getChildAt ( 0 ) . getValue ( ) . user === user ) {
311
308
match = [ user , node . getChildAt ( 1 ) . getValue ( ) . user ] ;
312
309
result = 'loss' ;
313
310
} else if ( node . getChildAt ( 1 ) . getValue ( ) . user === user ) {
314
311
match = [ node . getChildAt ( 0 ) . getValue ( ) . user , user ] ;
315
312
result = 'win' ;
316
313
}
314
+ }
317
315
318
316
return ! match ;
319
317
} ) ;
320
318
if ( match ) {
321
319
var error = this . setMatchResult ( match , result ) ;
322
- if ( error )
320
+ if ( error ) {
323
321
throw new Error ( "Unexpected " + error + " from setMatchResult([" + match . join ( ", " ) + "], " + result + ")" ) ;
322
+ }
324
323
}
325
324
} ;
326
325
Elimination . prototype . getUserBusy = function ( user ) {
327
- if ( ! this . isBracketFrozen )
328
- return 'BracketNotFrozen' ;
326
+ if ( ! this . isBracketFrozen ) return 'BracketNotFrozen' ;
329
327
330
- if ( ! this . users . has ( user ) )
331
- return 'UserNotAdded' ;
328
+ if ( ! this . users . has ( user ) ) return 'UserNotAdded' ;
332
329
return this . users . get ( user ) . isBusy ;
333
330
} ;
334
331
Elimination . prototype . setUserBusy = function ( user , isBusy ) {
335
- if ( ! this . isBracketFrozen )
336
- return 'BracketNotFrozen' ;
332
+ if ( ! this . isBracketFrozen ) return 'BracketNotFrozen' ;
337
333
338
- if ( ! this . users . has ( user ) )
339
- return 'UserNotAdded' ;
334
+ if ( ! this . users . has ( user ) ) return 'UserNotAdded' ;
340
335
this . users . get ( user ) . isBusy = isBusy ;
341
336
} ;
342
337
343
338
Elimination . prototype . getAvailableMatches = function ( ) {
344
- if ( ! this . isBracketFrozen )
345
- return 'BracketNotFrozen' ;
339
+ if ( ! this . isBracketFrozen ) return 'BracketNotFrozen' ;
346
340
347
341
var matches = [ ] ;
348
342
this . tree . tree . traverse ( function ( node ) {
349
343
if ( node . getValue ( ) . state === 'available' ) {
350
344
var userA = node . getChildAt ( 0 ) . getValue ( ) . user ;
351
345
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 ) {
353
347
matches . push ( [ userA , userB ] ) ;
348
+ }
354
349
}
355
350
} , this ) ;
356
351
return matches ;
@@ -366,8 +361,9 @@ var Elimination = (function () {
366
361
this . tree . tree . traverse ( function ( node ) {
367
362
if ( node . getValue ( ) . state === 'available' &&
368
363
node . getChildAt ( 0 ) . getValue ( ) . user === match [ 0 ] &&
369
- node . getChildAt ( 1 ) . getValue ( ) . user === match [ 1 ] )
364
+ node . getChildAt ( 1 ) . getValue ( ) . user === match [ 1 ] ) {
370
365
targetNode = node ;
366
+ }
371
367
return ! targetNode ;
372
368
} ) ;
373
369
if ( ! targetNode ) return 'InvalidMatch' ;
@@ -430,8 +426,9 @@ var Elimination = (function () {
430
426
else if ( this . users . get ( userB ) . isDisqualified )
431
427
error = this . setMatchResult ( [ userA , userB ] , 'win' ) ;
432
428
433
- if ( error )
429
+ if ( error ) {
434
430
throw new Error ( "Unexpected " + error + " from setMatchResult([" + userA + ", " + userB + "], ...)" ) ;
431
+ }
435
432
}
436
433
}
437
434
} ;
@@ -441,20 +438,19 @@ var Elimination = (function () {
441
438
} ;
442
439
443
440
Elimination . prototype . getResults = function ( ) {
444
- if ( ! this . isTournamentEnded ( ) )
445
- return 'TournamentNotEnded' ;
441
+ if ( ! this . isTournamentEnded ( ) ) return 'TournamentNotEnded' ;
446
442
447
443
var results = [ ] ;
448
444
var currentNode = this . tree . tree ;
449
445
for ( var n = 0 ; n < this . maxSubtrees ; ++ n ) {
450
446
results . push ( [ currentNode . getValue ( ) . user ] ) ;
451
447
currentNode = currentNode . getChildAt ( currentNode . getValue ( ) . result === 'loss' ? 0 : 1 ) ;
452
- if ( ! currentNode )
453
- break ;
448
+ if ( ! currentNode ) break ;
454
449
}
455
450
456
- if ( this . users . size - 1 === this . maxSubtrees && currentNode )
451
+ if ( this . users . size - 1 === this . maxSubtrees && currentNode ) {
457
452
results . push ( [ currentNode . getValue ( ) . user ] ) ;
453
+ }
458
454
459
455
return results ;
460
456
} ;
0 commit comments