-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatcher.js
More file actions
1371 lines (1276 loc) · 41.3 KB
/
Matcher.js
File metadata and controls
1371 lines (1276 loc) · 41.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* # Matcher
* Copyright(c) 2020 Stefano Balietti
* MIT Licensed
*
* Class handling the creation of tournament schedules.
*
* http://www.nodegame.org
* ---
*/
(function(exports, node) {
var J = node.JSUS;
var Roler = node.Roler;
// Object containing methods to fetch a match in the requested format.
// Will be initialized later.
var fetchMatch;
exports.Matcher = Matcher;
// ## Static methods.
/**
* ### Matcher.bye
*
* Symbol used to complete matching when partner is missing
*
* @see Matcher.matches
*/
Matcher.bye = -1;
/**
* ### Matcher.missingId
*
* Symbol assigned to matching number without valid id
*
* @see Matcher.resolvedMatches
* @see Roler.missingId
*/
Matcher.missingId = 'bot';
/**
* ## Matcher.randomAssigner
*
* Assigns ids to positions randomly.
*
* @param {array} ids The ids to assign
*
* @return The sorted array
*
* @see JSUS.shuffle
*/
Matcher.randomAssigner = function(ids) {
return J.shuffle(ids);
};
/**
* ### Matcher.linearAssigner
*
* Assigns ids to positions linearly.
*
* @param {array} ids The ids to assign
*
* @return The sorted array
*/
Matcher.linearAssigner = function(ids) {
return J.clone(ids);
};
/**
* ## Matcher constructor
*
* Creates a new Matcher object
*
* @param {object} options Optional. Configuration options
*/
function Matcher(options) {
options = options || {};
/**
* ### Matcher.x
*
* The row-index of the last returned match by Matcher.getMatch
*
* @see Matcher.getMatch
*/
this.x = null;
/**
* ### Matcher.y
*
* The column-index of the last returned match by Matcher.getMatch
*
* @see Matcher.getMatch
*/
this.y = null;
/**
* ### Matcher.matches
*
* Nested array of matches (with position-numbers)
*
* Nests a new array for each round, and within each round
* individual matches are also array. For example:
*
* ```javascript
*
* // Matching array.
* [
*
* // First round.
* [ [ p1, p2 ], [ p3, p4 ], ... ],
*
* // Second round.
* [ [ p2, p3 ], [ p4, p1 ], ... ],
*
* // Further rounds.
* ];
* ```
*
* @see Matcher.setMatches
*/
this.matches = null;
/**
* ### Matcher.resolvedMatches
*
* Nested array of matches (with id-strings)
*
* Exactly Matcher.matches, but with with ids instead of numbers
*
* This method is used both by getMatch and getMatchObject (if
* a single match is requested).
*
* @see Matcher.matches
* @see Matcher.resolvedMatchesObj
* @see Matcher.resolvedMatchesById
* @see Matcher.setIds
* @see Matcher.setAssignerCb
* @see Matcher.match
*/
this.resolvedMatches = null;
/**
* ### Matcher.resolvedMatchesObj
*
* Array of maps id to partner, one map per round
*
* ```javascript
*
* // Matching array.
* [
*
* // First round.
* { p1: 'p2', p2: 'p1', p3: 'p4', p4: 'p3', ... },
*
* // Second round.
* { p2: 'p3', p3: 'p2', p4: 'p1', p1: 'p4', ... },
*
* // Further rounds.
* ];
* ```
*
* @see Matcher.resolvedMatches
* @see Matcher.resolvedMatchesById
* @see Matcher.setIds
* @see Matcher.match
*/
this.resolvedMatchesObj = null;
/**
* ### Matcher.resolvedMatchesById
*
* Maps ids to a sequence of matches
*
* ```javascript
*
* // Matching object.
* {
*
* // All rounds.
* p1: [ 'p2', 'p4', ... ],
* p2: [ 'p1', 'p3', ... ],
* p3: [ 'p4', 'p2', ... ],
* p4: [ 'p3', 'p1', ... ]
* ...
*
* };
* ```
*
* @see Matcher.resolvedMatches
* @see Matcher.resolvedMatchesObj
* @see Matcher.setIds
* @see Matcher.match
*/
this.resolvedMatchesById = null;
/**
* ### Matcher.ids
*
* Array ids to match
*
* @see Matcher.setIds
*/
this.ids = null;
/**
* ### Matcher.ids
*
* Array mapping each ordinal position to an id
*
* @see Matcher.ids
* @see Matcher.assignerCb
*/
this.assignedIds = null;
/**
* ### Matcher.idsMap
*
* Map ids to match
*
* @see Matcher.setIds
*/
this.idsMap = null;
/**
* ### Matcher.assignedIdsMap
*
* Map ids to ordinal position in matches
*
* @see Matcher.idsMap
* @see Matcher.assignedIds
*/
this.assignedIdsMap = null;
/**
* ### Matcher.assignerCb
*
* Callback that assigns ids to positions
*
* An assigner callback must take as input an array of ids,
* reorder them according to some criteria, and return it.
* The order of the items in the returned array will be used to
* match the numbers in the `matches` array.
*
* @see Matcher.ids
* @see Matcher.matches
* @see Matcher.assignedIds
*/
this.assignerCb = Matcher.randomAssigner;
/**
* ## Matcher.missingId
*
* An id used to replace missing players ids
*/
this.missingId = Matcher.missingId;
/**
* ## Matcher.missingId
*
* An id used by matching algorithms to complete unfinished matches
*/
this.bye = Matcher.bye;
/**
* ## Matcher.doObjLists
*
* Flag that obj lists should be created when `match` is invoked
*
* @see Matcher.resolvedMatchesObj
* @see Matcher.matcher
*/
this.doObjLists = true;
/**
* ## Matcher.doIdLists
*
* Flag that id lists should be created when `match` is invoked
*
* @see Matcher.resolvedMatchesById
* @see Matcher.matcher
*/
this.doIdLists = true;
/**
* ## Matcher.doRoles
*
* Flag that roles should be assigned when `match` is invoked
*
* Requires roles to be set, otherwise an error is thrown
*
* @see Matcher.roles
* @see Matcher.roler
* @see Matcher.matcher
*/
this.doRoles = false;
/**
* ## Matcher.roler
*
* Handles assigning roles to matches
*
* If null here, is initialized by `init` if doRoles is TRUE.
*
* @see Matcher.doRoles
* @see Matcher.init
*/
this.roler = options.roler || null;
/**
* ## Matcher.roles
*
* Roles map created if `doRoles` is TRUE
*
* @see Matcher.doRoles
* @see Matcher.roler
* @see Matcher.matcher
*/
this.roler = options.roler || null;
// Init.
this.init(options);
}
/**
* ### Matcher.init
*
* Inits the Matcher instance
*
* @param {object} options
*/
Matcher.prototype.init = function(options) {
options = options || {};
if (options.assignerCb) this.setAssignerCb(options.assignerCb);
if (options.ids) this.setIds(options.ids);
if (options.bye) this.bye = options.bye;
if (options.missingId) this.missingId = options.missingId;
if (null === options.x) this.x = null;
else if ('number' === typeof options.x) {
if (options.x < 0) {
throw new Error('Matcher.init: options.x cannot be negative.' +
'Found: ' + options.x);
}
this.x = options.x;
}
else if (options.x) {
throw new TypeError('Matcher.init: options.x must be number, ' +
'null or undefined. Found: ' + options.x);
}
if (null === options.y) this.y = null;
else if ('number' === typeof options.y) {
if (options.y < 0) {
throw new Error('Matcher.init: options.y cannot be negative.' +
'Found: ' + options.y);
}
this.y = options.y;
}
else if (options.y) {
throw new TypeError('Matcher.init: options.y must be number, ' +
'null or undefined. Found: ' + options.y);
}
if (options.doRoles || options.roles) {
if (!this.roler) this.roler = new Roler();
this.roler.init({
missingId: this.missingId,
roles: options.roles
});
this.doRoles = true;
}
else if ('undefined' !== typeof options.doRoles) {
this.doRoles = !!options.doRoles;
}
if ('undefined' !== typeof options.doObjLists) {
this.doObjLists = !!options.doObjLists;
}
if ('undefined' !== typeof options.doIdLists) {
this.doIdLists = !!options.doIdLists;
}
};
/**
* ### Matcher.generateMatches
*
* Creates a matches array according to the chosen scheduling algorithm
*
* Throws an error if the selected algorithm is not found.
*
* @param {string} alg The chosen algorithm. Available: 'roundrobin',
* 'random'
*
* @return {array} The array of matches
*/
Matcher.prototype.generateMatches = function(alg) {
var matches;
if ('string' !== typeof alg) {
throw new TypeError('Matcher.generateMatches: alg must be ' +
'string. Found: ' + alg);
}
alg = alg.toLowerCase();
if (alg === 'roundrobin' || alg === 'round_robin' ||
alg === 'random' || alg === 'random_pairs' ) {
matches = pairMatcher(alg, arguments[1], arguments[2]);
}
else {
throw new Error('Matcher.generateMatches: unknown algorithm: ' +
alg);
}
this.setMatches(matches);
return matches;
};
/**
* ### Matcher.setMatches
*
* Sets the matches for current instance
*
* Resets resolvedMatches and resolvedMatchesObj to null.
*
* @param {array} The array of matches
*
* @see this.matches
*/
Matcher.prototype.setMatches = function(matches) {
if (!J.isArray(matches) || !matches.length) {
throw new TypeError('Matcher.setMatches: matches must be a ' +
'non-empty array. Found: ' + matches);
}
this.matches = matches;
resetResolvedData(this);
};
/**
* ### Matcher.getMatches
*
* Returns the matches for current instance
*
* @return {array|null} The array of matches (NULL if not yet set)
*
* @see this.matches
*/
Matcher.prototype.getMatches = function() {
return this.matches;
};
/**
* ### Matcher.setIds
*
* Sets the ids to be used for the matches
*
* @param {array} ids Array containing the id of the matches
*
* @see Matcher.ids
* @see Matcher.idsMap
*/
Matcher.prototype.setIds = function(ids) {
var i, len;
if (!J.isArray(ids) || !ids.length) {
throw new TypeError('Matcher.setIds: ids must be a non-empty ' +
'array. Found: ' + ids);
}
// Keep track of all ids.
this.idsMap = {};
i = -1, len = ids.length;
for ( ; ++i < len ; ) {
// TODO: validate? Duplicated ids are fine?
this.idsMap[ids[i]] = true;
}
this.ids = ids;
resetResolvedData(this);
};
/**
* ### Matcher.getIds
*
* Returns the ids used to created the matching
*
* @return {array} ids Ids in use
*
* @see Matcher.ids
*/
Matcher.prototype.getIds = function() {
return this.ids;
};
/**
* ### Matcher.assignIds
*
* Calls the assigner callback to assign ids to positions
*
* Ids can be overwritten by parameter. If no ids are found,
* they will be automatically generated, provided that matches
* have been generated first.
*
* @param {array} ids Optional. Array containing the id of the matches
* to pass to Matcher.setIds
*
* @see Matcher.ids
* @see Matcher.setIds
* @see Matcher.assignedIds
* @see Matcher.assignedIdsMap
*/
Matcher.prototype.assignIds = function(ids) {
var i, len;
if ('undefined' !== typeof ids) this.setIds(ids);
if (!J.isArray(this.ids) || !this.ids.length) {
if (!J.isArray(this.matches) || !this.matches.length) {
throw new TypeError('Matcher.assignIds: no ids and no ' +
'matches found.');
}
this.ids = J.seq(0, this.matches.length -1, 1, function(i) {
return '' + i;
});
}
this.assignedIds = this.assignerCb(this.ids);
// Map all ids to its position.
this.assignedIdsMap = {};
i = -1, len = this.assignedIds.length;
for ( ; ++i < len ; ) {
this.assignedIdsMap[this.assignedIds[i]] = i;
}
};
/**
* ### Matcher.setAssignerCb
*
* Specify a callback to be used to assign existing ids to positions
*
* @param {function} cb The assigner cb
*
* @see Matcher.ids
* @see Matcher.matches
* @see Matcher.assignerCb
*/
Matcher.prototype.setAssignerCb = function(cb) {
if ('function' !== typeof cb) {
throw new TypeError('Matcher.setAssignerCb: cb must be ' +
'function. Found: ' + cb);
}
this.assignerCb = cb;
};
/**
* ### Matcher.match
*
* Substitutes the ids to the matches
*
* Populates the indexes:
*
* - `resolvedMatches`,
* - `resolvedMatchesObj`,
* - `resolvedMatchesById`
*
* If the matches array is not already set, an error is thrown.
*
* If the ids have not been assigned, it does automatic assignment.
*
* @param {boolean|array} assignIds Optional. A flag to force to
* re-assign existing ids, or an an array containing new ids to
* assign.
*
* @see Matcher.assignIds
* @see Matcher.resolvedMatchesObj
* @see Matcher.resolvedMatches
*
* TODO: creates two lists of matches with bots and without.
*/
Matcher.prototype.match = function(assignIds) {
var i, lenI, j, lenJ, pair;
var matched, matchedObj, matchedId, id1, id2;
var roles, rolesObj, idRolesObj, r1, r2;
if (!J.isArray(this.matches) || !this.matches.length) {
throw new Error('Matcher.match: no matches found');
}
// Assign/generate ids if not done before.
if (!this.assignedIds || assignIds) {
if (J.isArray(assignIds)) this.assignIds(assignIds);
else this.assignIds();
}
// Parse the matches array and creates two data structures
// where the absolute position becomes the player id.
i = -1, lenI = this.matches.length;
matched = new Array(lenI);
matchedObj = this.doObjLists ? new Array(lenI) : null;
matchedId = this.doIdLists ? {} : null;
if (this.doRoles) {
roles = new Array(lenI);
rolesObj = new Array(lenI);
idRolesObj = new Array(lenI);
}
else {
roles = null;
rolesObj = null;
idRolesObj = null;
}
for ( ; ++i < lenI ; ) {
j = -1, lenJ = this.matches[i].length;
matched[i] = new Array(lenJ);
if (this.doObjLists) matchedObj[i] = {};
if (this.doRoles) {
roles[i] = new Array(lenJ);
rolesObj[i] = new Array(lenJ);
idRolesObj[i] = new Array(lenJ);
}
for ( ; ++j < lenJ ; ) {
id1 = null, id2 = null;
pair = this.matches[i][j];
// Resolve matches.
id1 = importMatchItem(i, j,
pair[0],
this.assignedIds,
this.missingId);
id2 = importMatchItem(i, j,
pair[1],
this.assignedIds,
this.missingId);
// Create resolved matches:
// Array.
matched[i][j] = [id1, id2];
// Obj.
if (this.doObjLists) {
matchedObj[i][id1] = id2;
matchedObj[i][id2] = id1;
}
// By Id.
if (this.doIdLists) {
if (!matchedId[id1]) matchedId[id1] = new Array(lenI);
if (!matchedId[id2]) matchedId[id2] = new Array(lenI);
matchedId[id1][i] = id2;
matchedId[id2][i] = id1;
}
// Roles.
if (this.doRoles) {
roles[i][j] = this.roler.rolify(matched[i][j], i, j);
// TODO: this code is repeated in Roler.rolifyAll.
// make it one!
r1 = roles[i][j][0];
r2 = roles[i][j][1];
rolesObj[i][j] = {};
if (r1 !== r2) {
rolesObj[i][j][r1] = id1;
rolesObj[i][j][r2] = id2;
}
else {
rolesObj[i][j][r1] = [ id1, id2 ];
}
idRolesObj[i][j] = {};
idRolesObj[i][j][id1] = r1;
idRolesObj[i][j][id2] = r2;
}
}
}
// Substitute matching-structure.
this.resolvedMatches = matched;
this.resolvedMatchesObj = matchedObj;
this.resolvedMatchesById = matchedId;
this.roles = roles;
this.rolesObj = rolesObj;
if (this.doRoles) {
this.roler.setRolifiedMatches(roles, false);
this.roler.setRole2IdMatches(rolesObj, false);
this.roler.setId2RoleMatches(idRolesObj, false);
}
// Set getMatch indexes to 0.
this.x = null;
this.y = null;
};
/**
* ### Matcher.hasNext
*
* Returns TRUE if there is next match to be returned by getMatch
*
* @param {number} x Optional. The x-th round. Default: Matcher.x
* @param {number} y Optional. The y-th match within the x-th round
* Default: Matcher.y
*
* @return {bolean} TRUE, if there exists a next match
*
* @see Matcher.x
* @see Matcher.y
* @see Matcher.resolvedMatches
* @see hasOrGetNext
*/
Matcher.prototype.hasNext = function(x, y) {
return hasOrGetNext.call(this, 'hasNext', 0, x, y);
};
/**
* ### Matcher.getMatch
*
* Returns the next match, or the specified match
*
* @param {number} x Optional. The x-th round. Default: Matcher.x
* @param {number} y Optional. The y-th match within the x-th round.
* Default: Matcher.y
*
* @return {array} The next or requested match, or null if not found
*
* @see Matcher.x
* @see Matcher.y
* @see Matcher.resolvedMatches
* @see hasOrGetNext
*/
Matcher.prototype.getMatch = function(x, y) {
return hasOrGetNext.call(this, 'getMatch', 1, x, y);
};
/**
* ### Matcher.getMatchFor
*
* Returns the id/s of the next or the x-th match for the specified id
*
* If id lists are not generated (see `Matcher.doIdLists) an
* error is thrown.
*
* @param {string} id The id to get the matches for
* @param {number} x Optional. The x-th round. Default: Matcher.x
*
* @return {string|array} The next or requested match, or null if not found
*
* @see Matcher.x
* @see Matcher.y
* @see Matcher.doIdLists
* @see Matcher.resolvedMatches
* @see hasOrGetNext
*/
Matcher.prototype.getMatchFor = function(id, x) {
var out;
if ('string' !== typeof id) {
throw new TypeError('Matcher.getMatchFor: id must be string. ' +
'Found:' + id);
}
if (!this.resolvedMatchesById) {
throw new Error('Matcher.getMatchFor: no id-based matches found.');
}
out = this.resolvedMatchesById[id];
if (!out) return null;
if ('undefined' === typeof x) return out;
if ('number' === typeof x) {
if (x >= 0 && !isNaN(x)) return x > (out.length -1) ? null : out[x];
}
throw new TypeError('Matcher.getMatchFor: x must be a positive ' +
'number or undefined. Found: ' + x);
};
/**
* ### Matcher.getMatchObject
*
* Returns all the matches of the next or requested round as key-value pairs
*
* If object lists are not generated (see `Matcher.doObjLists) an
* error is thrown.
*
* @param {number} x Optional. The x-th round. Default: Matcher.x
* @param {number} y Optional. The y-th match within the x-th round.
* Default: Matcher.y
*
* @return {object|null} The next or requested match, or null if not found
*
* @see Matcher.x
* @see Matcher.y
* @see Matcher.doObjLists
* @see Matcher.resolvedMatchesObj
*/
Matcher.prototype.getMatchObject = function(x, y) {
if (!this.resolvedMatchesObj) {
throw new Error('Matcher.getMatchObject: no obj matches found.');
}
return hasOrGetNext.call(this, 'getMatchObject', 3, x, y);
};
/**
* ### Matcher.normalizeRound
*
* Returns the round index given the current number of matches
*
* For example, if the are only 10 matches repeated in cycle,
* but the game has 20 rounds, round 13th will have normalized
* round index equal to 3.
*
* Important! Matches are 0-based, but rounds are 1-based. This
* method takes care of it.
*
* @param {number} round The round to normalize
*
* @return {object} The next or requested match, or null if not found
*
* @see Matcher.x
* @see Matcher.matches
*/
Matcher.prototype.normalizeRound = function(round) {
if (!this.matches) {
throw new TypeError('Matcher.normalizeRound: no matches found.');
}
if ('number' !== typeof round || isNaN(round) || round < 1) {
throw new TypeError('Matcher.normalizeRound: round must be a ' +
'number > 0. Found: ' + round);
}
return (round-1) % this.matches.length;
};
/**
* ### Matcher.replaceId
*
* Replaces an id with a new one in all matches
*
* @param {string} oldId The id to be replaced
* @param {string} newId The replacing id
*
* @return {boolean} TRUE, if the oldId was found and replaced
*
* @see MatcherManager.replaceId
* @see Roler.replaceId
*/
Matcher.prototype.replaceId = function(oldId, newId) {
var m;
var i, len, j, lenJ, h, lenH;
var rowFound;
if ('string' !== typeof oldId) {
throw new TypeError('Matcher.replaceId: oldId should be string. ' +
'Found: ' + oldId);
}
if ('string' !== typeof newId || newId.trim() === '') {
throw new TypeError('Matcher.replaceId: newId should be a ' +
'non-empty string. Found: ' + newId);
}
// No id was assigned yet.
if (!this.resolvedMatches) return false;
// IdsMap.
m = this.idsMap[oldId];
if ('undefined' === typeof m) return false;
this.idsMap[newId] = true;
delete this.idsMap[oldId];
// Ids.
m = this.ids;
i = -1, len = m.length;
for ( ; ++i < len ; ) {
if (m[i] === oldId) {
m[i] = newId;
break;
}
}
// AssignedIds and AssignedIdsMap.
m = this.assignedIdsMap;
m[newId] = m[oldId];
delete m[oldId];
this.assignedIds[m[newId]] = newId;
// Update resolvedMatches.
m = this.resolvedMatches;
if (!m) return true;
i = -1, len = m.length;
for ( ; ++i < len ; ) {
j = -1, lenJ = m[i].length;
rowFound = false;
for ( ; ++j < lenJ ; ) {
h = -1, lenH = m[i][j].length;
for ( ; ++h < lenH ; ) {
if (m[i][j][h] === oldId) {
m[i][j][h] = newId;
rowFound = true;
break;
}
}
if (rowFound) break;
}
}
// Update resolvedMatchesObj.
m = this.resolvedMatchesObj;
i = -1, len = m.length;
for ( ; ++i < len ; ) {
for (j in m[i]) {
if (m[i].hasOwnProperty(j)) {
if (j === oldId) {
// Do the swap.
m[i][newId] = m[i][oldId];
m[i][m[i][oldId]] = newId;
delete m[i][oldId];
break;
}
}
}
}
// Update resolvedMatchesById.
m = this.resolvedMatchesById;
for (i in m) {
if (m.hasOwnProperty(i)) {
if (i === oldId) {
m[newId] = m[oldId];
delete m[oldId];
}
else {
lenJ = m[i].length;
// THIS OPTIMIZATION DOES NOT SEEM TO WORK.
// In fact, there might be more matches with the same
// partner in sequence.
// And also if === 1, it should be checked.
// if (lenJ == 1) {
// m[i][0] = newId;
// }
// else if (lenJ === 2) {
// if (m[i][0] === oldId) m[i][0] = newId;
// else m[i][1] = newId;
// }
// else {
j = -1;
for ( ; ++j < lenJ ; ) {
if (m[i][j] === oldId) {
m[i][j] = newId;
}
}
// }
}
}
}
return true;
};
/**
* ### Matcher.clear
*
* Clears the matcher as it would be a newly created object
*/
Matcher.prototype.clear = function() {
this.x = null;
this.y = null;
this.matches = null;
this.resolvedMatches = null;
this.resolvedMatchesObj = null;
this.ids = null;
this.assignedIds = null;
this.idsMap = null;
this.assignedIdsMap = null;
this.assignerCb = Matcher.randomAssigner;
this.missingId = Matcher.missingId;
this.bye = Matcher.bye;
};
// ## Helper methods.
/**
* ### importMatchItem
*
* Handles importing items from the matches array
*
* Items in matches array must be numbers or strings. If numbers
* they are translated into an id using the supplied map, otherwise
* they are considered as already an id.
*
* Items that are not numbers neither strings will throw an error.
*
* @param {number} i The row-id of the item
* @param {number} j The position in the row of the item
* @param {string|number} item The item to check
* @param {array} map The map of positions to ids
* @param {string} miss The id of number that cannot be resolved in map
*
* @return {string} The resolved id of the item
*/
function importMatchItem(i, j, item, map, miss) {
if ('number' === typeof item) {
return 'undefined' !== typeof map[item] ? map[item] : miss;
}
else if ('string' === typeof item) {
return item;
}
throw new TypeError('Matcher.match: items can be only string or ' +
'number. Found: ' + item + ' at position ' +
i + ',' + j);
}
/**
* ### resetResolvedData
*
* Resets resolved data of a matcher object