-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
373 lines (316 loc) · 17.7 KB
/
script.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
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
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
Array.prototype.diff = function (a) {
return this.filter(function (i) { return a.indexOf(i) < 0; });
};
Array.prototype.getUnique = function () {
var u = {}, a = [];
for (var i = 0, l = this.length; i < l; ++i) {
if (u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
angular.module('kick', ["firebase", "ngAnimate"])
.controller('kickeria', function ($scope, $http, $firebaseArray) {
$scope.showPredictions = false;
$scope.fixedPositions = true;
$scope.showQuality = true;
$scope.showExperimentalStats = false;
$scope.fairMatch = true;
$scope.orderByField = "Highscore.NumWins";
$scope.reverseSort = true;
$scope.showOdds = true;
$scope.options = [{ Id: "new", Description: "New tournament" },
{ Id: "T1", Description: "Day 1 (8-Apr-2016)" },
{ Id: "T2", Description: "Day 2 (28-Apr-2016)" },
{ Id: "t3", Description: "Day 3 (2-Jun-2016)" },
{ Id: "t4", Description: "Day 4 (29-Jun-2016)" },
{ Id: "t5", Description: "Day 5 (11-Aug-2016)" },
{ Id: "tall", Description: "All (1-4)" }];
$scope.selectedTournament = $scope.options[0];
var margin = 0.1;
$scope.tournamentChange = function () {
var firebaseUrl = 'todo: enter url here' ;
var myDataRef = new Firebase(firebaseUrl+'tournaments/' + $scope.selectedTournament.Id + "/games");
var myDataRefPlayers = new Firebase(firebaseUrl+'tournaments/' + $scope.selectedTournament.Id + "/players");
var prevUsedPlayerRef = new Firebase(firebaseUrl+'tournaments/' + $scope.selectedTournament.Id + "/usedplayers");
$scope.activeplayers = $firebaseArray(myDataRefPlayers);
$scope.games = $firebaseArray(myDataRef);
$scope.previouslyUsed = $firebaseArray(prevUsedPlayerRef);
$scope.games.$loaded().then(updateHighscore);
$scope.scoreChanged = function (w) {
$scope.games.$save(w);
updateHighscore();
};
$scope.isDisabled = $scope.selectedTournament.Id != "new";
};
$scope.addplayer = function () {
if ($scope.newplayer){
if($scope.activeplayers.filter(function(f){ return f.Name == $scope.newplayer;}).length > 0){
alert('Player already added');
}
else{
$scope.activeplayers.$add({ Name: $scope.newplayer, IsActive: true });
$scope.newplayer = "";
}
}
else
alert("Enter player name!")
}
$scope.changeActive = function (player) {
$scope.activeplayers.$save(player);
}
function initHS(){
return { NumGames: 0, NumWins: 0, NumZeros: 0, NumEarned:0, Trueskill: { Mu: initMu, Sigma: initSigma }, AttackTrueskill: { Mu: initMu, Sigma: initSigma }, DefenseTrueskill: { Mu: initMu, Sigma: initSigma } };
}
function gamequality(p1,p2,p3,p4){
if( p1.Highscore === undefined || p1.Highscore.Trueskill === undefined)
p1.Highscore = initHS();
if(p2.Highscore === undefined || p2.Highscore.Trueskill === undefined)
p2.Highscore = initHS();
if(p3.Highscore === undefined ||p3.Highscore.Trueskill === undefined)
p3.Highscore = initHS();
if(p4.Highscore === undefined ||p4.Highscore.Trueskill === undefined)
p4.Highscore = initHS();
var team1StdSq = sq(p1.Highscore.Trueskill.Sigma) +sq(p2.Highscore.Trueskill.Sigma);
var team2StdSq = sq(p3.Highscore.Trueskill.Sigma) +sq(p4.Highscore.Trueskill.Sigma);
var team1MeanSum = p1.Highscore.Trueskill.Mu + p2.Highscore.Trueskill.Mu;
var team2MeanSum = p3.Highscore.Trueskill.Mu + p4.Highscore.Trueskill.Mu;
var sqrtPart = Math.sqrt((4*betaSq)/(4*betaSq+team1StdSq + team2StdSq));
var expPart = Math.exp((-1*(sq(team1MeanSum-team2MeanSum))) / (2*(4*betaSq + team1StdSq + team2StdSq)));
return sqrtPart * expPart;
}
function probTeam1(p1,p2,p3,p4){
var m = p1.Highscore.Trueskill.Mu+p2.Highscore.Trueskill.Mu - p3.Highscore.Trueskill.Mu-p4.Highscore.Trueskill.Mu;
var s = Math.sqrt(sq(p1.Highscore.Trueskill.Sigma)+sq(p2.Highscore.Trueskill.Sigma)+sq(p3.Highscore.Trueskill.Sigma)+sq(p4.Highscore.Trueskill.Sigma))+tauSq;
return GaussianCumulativeTo(m/s);
}
var updateHighscore = function () {
var result=[];
var allMentionedPlayers = [];
$scope.games.forEach(function (game) {
allMentionedPlayers.push(game.Player1, game.Player2, game.Player3, game.Player4);
});
var ump = allMentionedPlayers.filter(onlyUnique);
var missingPlayers = ump.diff($scope.activeplayers.map(function (p) { return p.Name; }));
missingPlayers.forEach(function (m) {
$scope.activeplayers.$add({ Name: m, IsActive: true });
});
var activeplayers = $scope.activeplayers;
activeplayers.forEach(function (player) { player.Highscore = initHS() });
var totalGames = 0;
var totalZeros = 0;
var friendly = [];
var hostile = [];
/* True skill, leaderboard etc. */
$scope.games.forEach(function (g) {
totalGames = totalGames + 1;
var player1 = activeplayers.filter(function (p) { return p.Name == g.Player1; })[0];
var player2 = activeplayers.filter(function (p) { return p.Name == g.Player2; })[0];
var player3 = activeplayers.filter(function (p) { return p.Name == g.Player3; })[0];
var player4 = activeplayers.filter(function (p) { return p.Name == g.Player4; })[0];
g.ProbTeam1 = probTeam1(player1,player2,player3,player4);
g.ProbTeam2 = 1-g.ProbTeam1;
g.GameQuality = gamequality(player1,player2,player3,player4);
g.Odds1 = Math.min(Math.max((1-margin) / g.ProbTeam1, 1.01), 50);
g.Odds2 = Math.min(Math.max((1-margin) / (1-g.ProbTeam1), 1.01), 50);
if (g.ScoreTeam1 !== undefined && g.ScoreTeam2 !== undefined) {
g.Player1Old = player1.Highscore.Trueskill;
g.Player2Old = player2.Highscore.Trueskill;
g.Player3Old = player3.Highscore.Trueskill;
g.Player4Old = player4.Highscore.Trueskill;
var newTrueskills = calcTrueskill(player1.Highscore.Trueskill, player2.Highscore.Trueskill, player3.Highscore.Trueskill, player4.Highscore.Trueskill, g.ScoreTeam1 > g.ScoreTeam2, Math.abs(g.ScoreTeam1-g.ScoreTeam2));
player1.Highscore.Trueskill = newTrueskills[0];
player2.Highscore.Trueskill = newTrueskills[1];
player3.Highscore.Trueskill = newTrueskills[2];
player4.Highscore.Trueskill = newTrueskills[3];
var newAttackDefenseTrueskills = calcTrueskill(player1.Highscore.AttackTrueskill, player2.Highscore.DefenseTrueskill, player3.Highscore.AttackTrueskill, player4.Highscore.DefenseTrueskill, g.ScoreTeam1 > g.ScoreTeam2, Math.abs(g.ScoreTeam1-g.ScoreTeam2));
player1.Highscore.AttackTrueskill = newAttackDefenseTrueskills[0];
player2.Highscore.DefenseTrueskill = newAttackDefenseTrueskills[1];
player3.Highscore.AttackTrueskill = newAttackDefenseTrueskills[2];
player4.Highscore.DefenseTrueskill = newAttackDefenseTrueskills[3];
g.Player1Diff = player1.Highscore.Trueskill.Diff;
g.Player2Diff = player2.Highscore.Trueskill.Diff;
g.Player3Diff = player3.Highscore.Trueskill.Diff;
g.Player4Diff = player4.Highscore.Trueskill.Diff;
friendly.push({ Name: g.Player1, Ally: g.Player2 });
friendly.push({ Name: g.Player2, Ally: g.Player1 });
friendly.push({ Name: g.Player3, Ally: g.Player4 });
friendly.push({ Name: g.Player4, Ally: g.Player3 });
hostile.push({ Name: g.Player1, Enemy: g.Player3 });
hostile.push({ Name: g.Player1, Enemy: g.Player4 });
hostile.push({ Name: g.Player2, Enemy: g.Player3 });
hostile.push({ Name: g.Player2, Enemy: g.Player4 });
hostile.push({ Name: g.Player3, Enemy: g.Player1 });
hostile.push({ Name: g.Player3, Enemy: g.Player2 });
hostile.push({ Name: g.Player4, Enemy: g.Player1 });
hostile.push({ Name: g.Player4, Enemy: g.Player2 });
if (g.ScoreTeam1 > g.ScoreTeam2) {
// winners
player1.Highscore.NumGames++;
player1.Highscore.NumWins++;
player2.Highscore.NumGames++;
player2.Highscore.NumWins++;
// losers
player3.Highscore.NumGames++;
player4.Highscore.NumGames++;
if (g.ScoreTeam2 == 0) {
player3.Highscore.NumZeros++;
player4.Highscore.NumZeros++;
totalZeros += 2;
player1.Highscore.NumEarned++;
player2.Highscore.NumEarned++;
}
}
if (g.ScoreTeam1 < g.ScoreTeam2) {
// losers
player1.Highscore.NumGames++;
player2.Highscore.NumGames++;
// winners
player3.Highscore.NumGames++;
player3.Highscore.NumWins++;
player4.Highscore.NumGames++;
player4.Highscore.NumWins++;
if (g.ScoreTeam1 == 0) {
player1.Highscore.NumZeros++;
player2.Highscore.NumZeros++;
totalZeros += 2;
player3.Highscore.NumEarned++;
player4.Highscore.NumEarned++;
}
}
if (g.ScoreTeam1 === g.ScoreTeam2) {
alert("Error, cannot have the same score!");
}
}
});
var hs = activeplayers.map(function (player) {
return { Name: player.Name, Highscore: player.Highscore };
});
// hs.sort(function(a,b){return a.Highscore.NumWins-b.Highscore.NumWins;});
// hs.reverse();
$scope.highScore = hs;
$scope.highScore.totalGames = totalGames;
$scope.highScore.totalZeros = totalZeros;
var line1 = [{ Friends: "Friends/Enemies", Enemies: "Friends/Enemies" }];
activeplayers.forEach(function (p) {
line1.push({ Friends: p.Name, Enemies: p.Name });
})
var friends = [line1];
activeplayers.forEach(function (p1) {
var list = [{ Friends: p1.Name, Enemies: p1.Name }];
activeplayers.forEach(function (p2) {
var f = friendly.filter(function (f) { return f.Name == p1.Name && f.Ally == p2.Name; }).length;
var e = hostile.filter(function (f) { return f.Name == p1.Name && f.Enemy == p2.Name; }).length;
list.push({ Friends: f, Enemies: e });
})
friends.push(list);
})
$scope.friends = friends;
};
var previouslyUsed = [];
$scope.newmatch = function () {
// alert('Only host can shuffle maps');
// return;
var matchup = [];
var seeded = [];
var doReseed = false;
var allplayers = $scope.activeplayers.filter(function (p) { return p.IsActive; }).map(function (a) { return a.Name; });
if (allplayers.length >= 4) {
while (matchup.length < 4) {
var pool = allplayers.diff(matchup).diff(previouslyUsed);
// var pool = allplayers.diff(matchup).diff($scope.previouslyUsed);
if (pool.length > 0) {
var rnd = Math.floor((Math.random() * pool.length));
matchup.push(pool[rnd]);
}
else {
if (matchup.length > 0) {
seeded = matchup.slice();
doReseed = true;
}
previouslyUsed = [];
// $scope.previouslyUsed = [];
}
}
previouslyUsed.push(matchup[0], matchup[1], matchup[2], matchup[3]);
// $scope.previouslyUsed.push(matchup[0], matchup[1], matchup[2], matchup[3]);
if (doReseed) {
previouslyUsed = previouslyUsed.diff(seeded);
// $scope.previouslyUsed = $scope.previouslyUsed.diff(seeded);
}
var player1 = $scope.activeplayers.filter(function (p) { return p.Name == matchup[0]; })[0];
var player2 = $scope.activeplayers.filter(function (p) { return p.Name == matchup[1]; })[0];
var player3 = $scope.activeplayers.filter(function (p) { return p.Name == matchup[2]; })[0];
var player4 = $scope.activeplayers.filter(function (p) { return p.Name == matchup[3]; })[0];
// matchmaking
var g1 = gamequality(player1,player2,player3,player4);
var g2 = gamequality(player1,player3,player2,player4);
var g3 = gamequality(player1,player4,player3,player2);
var p1 = probTeam1(player1,player2,player3,player4);
var p2 = probTeam1(player1,player3,player2,player4);
var p3 = probTeam1(player1,player4,player3,player2);
console.log(player1.Name,player2.Name,player3.Name,player4.Name);
console.log(g1,g2,g3);
if($scope.fairMatch){
if(g1 >= g2 && g1 >= g3) { // g1 is best
$scope.games.$add({
Player1: player1.Name,
Player2: player2.Name,
Player3: player3.Name,
Player4: player4.Name,
GameQuality: g1,
ProbTeam1: p1,
Odds1: Math.min(Math.max((1-margin) / p1, 1.0), 50),
Odds2: Math.min(Math.max((1-margin) / (1-p1), 1.0), 50)
});
}
else if (g2 >= g1 && g2 >= g3) { // g2 is best
$scope.games.$add({
Player1: player1.Name,
Player2: player3.Name,
Player3: player2.Name,
Player4: player4.Name,
GameQuality: g2,
ProbTeam1: p2,
Odds1: Math.min(Math.max((1-margin) / p2, 1.0), 50),
Odds2: Math.min(Math.max((1-margin) / (1-p2), 1.0), 50)
});
}
else{ // so, g3 is the best..
$scope.games.$add({
Player1: player1.Name,
Player2: player4.Name,
Player3: player3.Name,
Player4: player2.Name,
GameQuality: g3,
ProbTeam1: p3,
Odds1: Math.min(Math.max((1-margin) / p3, 1.0), 50),
Odds2: Math.min(Math.max((1-margin) / (1-p3), 1.0), 50)
});
}
}
else{
$scope.games.$add({
Player1: player1.Name,
Player2: player2.Name,
Player3: player3.Name,
Player4: player4.Name,
GameQuality: g1,
ProbTeam1: p1,
Odds1: Math.min(Math.max((1-margin) / p1, 1.0), 50),
Odds2: Math.min(Math.max((1-margin) / (1-p1), 1.0), 50)
});
}
}
else {
alert("Need >= 4 active players!")
}
}
$scope.tournamentChange();
});