forked from CreaturePhil/Showdown-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgenerator-round-robin.js
202 lines (172 loc) · 5.88 KB
/
generator-round-robin.js
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
var RoundRobin = (function () {
function RoundRobin(isDoubles) {
this.isDoubles = !!isDoubles;
this.isBracketFrozen = false;
this.users = [];
this.isUsersBusy = null;
this.matches = null;
this.userScores = null;
this.pendingMatches = 0;
if (isDoubles) this.name = "Double " + this.name;
}
RoundRobin.prototype.name = "Round Robin";
RoundRobin.prototype.isDrawingSupported = true;
RoundRobin.prototype.addUser = function (user) {
if (this.isBracketFrozen) return 'BracketFrozen';
if (this.users.indexOf(user) >= 0) return 'UserAlreadyAdded';
this.users.push(user);
};
RoundRobin.prototype.removeUser = function (user) {
if (this.isBracketFrozen) return 'BracketFrozen';
var userIndex = this.users.indexOf(user);
if (userIndex < 0) return 'UserNotAdded';
this.users.splice(userIndex, 1);
};
RoundRobin.prototype.replaceUser = function (user, replacementUser) {
var userIndex = this.users.indexOf(user);
if (userIndex < 0) return 'UserNotAdded';
if (this.users.indexOf(replacementUser) >= 0) return 'UserAlreadyAdded';
this.users[userIndex] = replacementUser;
};
RoundRobin.prototype.getUsers = function () {
return this.users.slice(0);
};
RoundRobin.prototype.getBracketData = function () {
var data = {};
data.type = 'table';
data.tableHeaders = {
cols: this.users.slice(0),
rows: this.users.slice(0)
};
data.tableContents = this.users.map(function (userA, row) {
return this.users.map(function (userB, col) {
if (!this.isDoubles && col >= row) return null;
if (userA === userB) return null;
var cell = {};
if (!this.isBracketFrozen) {
cell.state = 'unavailable';
} else {
var match = this.matches[row][col];
cell.state = match.state;
if (match.state === 'finished') {
cell.result = match.result;
cell.score = match.score.slice(0);
}
}
return cell;
}, this);
}, this);
data.scores = this.users.map(function (user, u) {
return this.isBracketFrozen ? this.userScores[u] : 0;
}, this);
return data;
};
RoundRobin.prototype.freezeBracket = function () {
this.isBracketFrozen = true;
this.isUsersBusy = this.users.map(function () { return false; });
this.matches = this.users.map(function (userA, row) {
return this.users.map(function (userB, col) {
if (!this.isDoubles && col >= row) return null;
if (userA === userB) return null;
++this.pendingMatches;
return {state: 'available'};
}, this);
}, this);
this.userScores = this.users.map(function () { return 0; });
};
RoundRobin.prototype.disqualifyUser = function (user) {
if (!this.isBracketFrozen) return 'BracketNotFrozen';
var userIndex = this.users.indexOf(user);
if (userIndex < 0) return 'UserNotAdded';
this.matches[userIndex].forEach(function (match, col) {
if (!match || match.state !== 'available') return;
match.state = 'finished';
match.result = 'loss';
match.score = [0, 1];
++this.userScores[col];
--this.pendingMatches;
}, this);
this.matches.forEach(function (challenges, row) {
var match = challenges[userIndex];
if (!match || match.state !== 'available') return;
match.state = 'finished';
match.result = 'win';
match.score = [1, 0];
++this.userScores[row];
--this.pendingMatches;
}, this);
};
RoundRobin.prototype.getUserBusy = function (user) {
if (!this.isBracketFrozen) return 'BracketNotFrozen';
var userIndex = this.users.indexOf(user);
if (userIndex < 0) return 'UserNotAdded';
return this.isUsersBusy[userIndex];
};
RoundRobin.prototype.setUserBusy = function (user, isBusy) {
if (!this.isBracketFrozen) return 'BracketNotFrozen';
var userIndex = this.users.indexOf(user);
if (userIndex < 0) return 'UserNotAdded';
this.isUsersBusy[userIndex] = isBusy;
};
RoundRobin.prototype.getAvailableMatches = function () {
if (!this.isBracketFrozen) return 'BracketNotFrozen';
var matches = [];
this.matches.forEach(function (challenges, row) {
challenges.forEach(function (match, col) {
if (!match) return;
if (match.state === 'available' && !this.isUsersBusy[row] && !this.isUsersBusy[col]) {
matches.push([this.users[row], this.users[col]]);
}
}, this);
}, this);
return matches;
};
RoundRobin.prototype.setMatchResult = function (match, result, score) {
if (!this.isBracketFrozen) return 'BracketNotFrozen';
if (!(result in {win:1, loss:1, draw:1})) return 'InvalidMatchResult';
var userIndexA = this.users.indexOf(match[0]);
var userIndexB = this.users.indexOf(match[1]);
if (userIndexA < 0 || userIndexB < 0) return 'UserNotAdded';
match = this.matches[userIndexA][userIndexB];
if (!match || match.state !== 'available') return 'InvalidMatch';
var virtualScore;
if (result === 'win') {
virtualScore = [1, 0];
} else if (result === 'loss') {
virtualScore = [0, 1];
} else {
virtualScore = [0.5, 0.5];
}
if (!score) score = virtualScore;
match.state = 'finished';
match.result = result;
match.score = score.slice(0);
this.userScores[userIndexA] += virtualScore[0];
this.userScores[userIndexB] += virtualScore[1];
--this.pendingMatches;
};
RoundRobin.prototype.isTournamentEnded = function () {
return this.isBracketFrozen && this.pendingMatches === 0;
};
RoundRobin.prototype.getResults = function () {
if (!this.isTournamentEnded()) return 'TournamentNotEnded';
var sortedScores = this.userScores.map(function (score, userIndex) {
return {userIndex: userIndex, score: score};
}).sort(function (a, b) { return b.score - a.score; });
var results = [];
var currentScore = sortedScores[0].score;
var currentRank = [];
results.push(currentRank);
sortedScores.forEach(function (score) {
if (score.score < currentScore) {
currentScore = score.score;
currentRank = [];
results.push(currentRank);
}
currentRank.push(this.users[score.userIndex]);
}, this);
return results;
};
return RoundRobin;
})();
exports.RoundRobin = RoundRobin;