-
Notifications
You must be signed in to change notification settings - Fork 4
/
home.js
363 lines (337 loc) · 9.74 KB
/
home.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
var Database = InitDatabase();
var DEMO_LEAGUEID = '-KdIiWEUj7_toD3MKMO_';
var DEMO_LEAGUE_OBJ = false;
var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;
var lastMonthEst = new Date(Date.now() - (4 * WEEK));
var lastMonth = new Date(lastMonthEst.getFullYear(), lastMonthEst.getMonth()).getTime();
var lastMonthStr = moment(lastMonth).format('MMMM');
var demoWeeks = [
{from: lastMonth, to: lastMonth + WEEK},
{from: lastMonth + WEEK, to: lastMonth + (2 * WEEK)}
];
window.scrollToId = (id) => {
var el = document.getElementById(id);
el.scrollIntoView({
block: 'start',
behavior: 'smooth'
});
}
// To speed up score loading
Database.getLeague({
leagueid: DEMO_LEAGUEID,
from: Date.now() - WEEK,
to: Date.now()
}).then(data => {
DEMO_LEAGUE_OBJ = data;
}).catch(console.error);
var searchByAddress = document.getElementById('search-by-address');
searchByAddress.addEventListener('keypress', e => {
if(e.charCode === 13){
searchForYourAlderman();
}
});
var searchByButton = document.getElementById('search-by-button');
searchByButton.addEventListener('click', e => {
searchForYourAlderman();
});
var searchError = document.getElementById('search-error');
function searchForYourAlderman(){
searchError.style.display = 'none';
var addr = searchByAddress.value;
findMyWard(addr).then(res => {
if(res){
showYourAlderman(res);
}
}).catch(err => {
searchError.style.display = 'block';
searchError.innerText = err;
});
}
function getAldermanByWard(num){
var pnum = '' + num;
while(pnum.length < 4){
pnum = '0' + pnum;
}
var pid = 'playerid' + pnum
var player = PLAYER_MAP[pid];
player.pid = pid;
return player;
}
var yourAlderman = document.getElementById('your-alderman');
var aldSub = document.getElementById('your-ward');
var aldScore = document.getElementById('your-alderman-score');
var wardNum = document.getElementById('your-ward-number');
var breakDown = document.getElementById('ward-score-breakdown');
var yourAldHist = document.getElementById('your-alderman-historical');
yourAldHist.dataset.from = demoWeeks[0].to - (4 * WEEK);
yourAldHist.dataset.to = demoWeeks[0].to;
function showYourAlderman(res){
var alderman = getAldermanByWard(res.WARD);
yourAlderman.innerText = alderman.name;
aldSub.innerText = 'Alderman of Ward ' + alderman.ward;
wardNum.innerText = res.WARD;
yourAldHist.dataset.pid = alderman.pid;
yourAldHist.innerText = 'View scores for ' + alderman.name;
Database.getPlayer({
leagueid: DEMO_LEAGUEID,
playerid: alderman.pid,
from: Date.now() - WEEK,
to: Date.now()
}, DEMO_LEAGUE_OBJ).then(data => {
var sum = 0;
breakDown.innerHTML = '';
for(var sid in data.scores){
var pts = data.scores[sid];
sum += pts;
var dataset = Database.Scoring.DATASET_NAMES[sid];
var scoreStr = pts + ' points on ' + dataset;
var li = document.createElement('li');
li.innerText = scoreStr;
breakDown.appendChild(li);
}
aldScore.innerText = sum;
var tli = document.createElement('li');
tli.innerText = 'For a grand total of ' + sum + ' points.';
breakDown.appendChild(tli);
window.scrollToId('alderman-result');
}).catch(console.error);
}
var monthSpans = document.getElementsByClassName('insert-month');
var visitorTable = document.getElementById('visitor-roster');
var robotTable = document.getElementById('robot-roster');
for(var m = 0; m < monthSpans.length; m++){
var mSpan = monthSpans[m];
mSpan.innerText = lastMonthStr;
}
var pastWeek = document.getElementById('insert-past-week');
pastWeek.innerText = moment(demoWeeks[0].from).format('M/D');
var nextWeek = document.getElementById('insert-next-week');
nextWeek.innerText = moment(demoWeeks[1].from).format('M/D');
var demoRosters = false;
Database.getAllPlayers({
leagueid: DEMO_LEAGUEID,
from: demoWeeks[0].from,
to: demoWeeks[0].to
}, DEMO_LEAGUE_OBJ).then(playerMap => {
var rosters = getRandomRosterPair();
var starters = {
visitor: 0,
robot: 0
}
var callbackMap = {};
for(var pid in playerMap){
var player = playerMap[pid];
player.starter = false;
if(pid in rosters.visitor){
if(starters.visitor < 3){
player.starter = true;
starters.visitor++;
}
rosters.visitor[pid] = player;
}
if(pid in rosters.robot){
if(starters.robot < 3){
player.starter = true;
starters.robot++;
}
rosters.robot[pid] = player;
}
var action = getPlayerAction(player);
callbackMap[pid] = action;
}
demoRosters = rosters;
showDemoRosters(callbackMap);
}).catch(console.error);
function getPlayerAction(player){
var action = {
fn: e => {
var event = new CustomEvent('roster-click', {
detail: demoRosters.visitor[player.playerid]
});
window.dispatchEvent(event);
}
};
return action;
}
function getRandomRosterPair(){
var visitor = {};
var robot = {};
for(var r = 1; r < 11; r++){
var player = getAldermanByWard(r);
if(r < 6){
visitor[player.pid] = player;
}
else{
robot[player.pid] = player;
}
}
return {visitor: visitor, robot: robot};
}
var scout = ScoutingReport();
scout.initializeReport('.scout-anchor');
function renderRosterTable(table, roster, callback, opt){
var options = opt || {};
table.innerHTML = '';
var thead = document.createElement('thead');
var th1 = document.createElement('th');
th1.innerText = 'Alderman';
var th2 = document.createElement('th');
th2.innerText = 'Score';
var th3 = document.createElement('th');
th3.innerText = 'Lineup';
thead.appendChild(th1);
thead.appendChild(th2);
thead.appendChild(th3);
table.appendChild(thead);
var list = Object.keys(roster).map(pid => {
return roster[pid];
}).sort((a, b) => {
if(a.starter === b.starter){
//return scout.sumScores(b.scores) - scout.sumScores(a.scores);
return a.name.localeCompare(b.name);
}
else{
if(a.starter){
return -1;
}
else{
return 1;
}
}
});
var lineupSum = 0;
list.forEach((player, idx) => {
if(player.starter){
lineupSum += scout.sumScores(player.scores);
}
if(idx === 3){
var br = document.createElement('tr');
var b1 = document.createElement('td');
b1.innerText = '';
var b2 = document.createElement('td');
b2.innerText = lineupSum;
var b3 = document.createElement('td');
b3.innerText = 'Total';
br.appendChild(b1);
br.appendChild(b2);
br.appendChild(b3);
table.appendChild(br);
}
var pid = player.playerid;
var tr = document.createElement('tr');
var td1 = document.createElement('td');
td1.innerText = player.name;
//if(!options.locked){
td1 = scout.attachReport(td1, player.playerid, {
from: demoWeeks[0].to - (4 * WEEK),
to: demoWeeks[0].to
});
//}
var td2 = document.createElement('td');
td2.innerText = scout.sumScores(player.scores);
var td3 = document.createElement('td');
if(options.locked){
td3.innerText = player.starter ? 'Starting' : 'Benched';
}
else{
if(selectedPlayer === player.playerid){
td3.innerText = player.starter ? 'Benching' : 'Starting';
}
else if(!player.hidden){
var btn = document.createElement('button');
btn.addEventListener('click', callback[pid].fn);
btn.innerText = player.starter ? 'Bench' : 'Start';
td3.appendChild(btn);
}
}
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
table.appendChild(tr);
});
return {
teamScore: lineupSum
}
}
var selectedPlayer = false;
function showDemoRosters(callbackMap){
window.addEventListener('roster-click', e => {
var player = e.detail;
if(selectedPlayer){
var selPlayer = demoRosters.visitor[selectedPlayer];
var thisPlayer = demoRosters.visitor[player.playerid];
selPlayer.starter = !selPlayer.starter;
thisPlayer.starter = !thisPlayer.starter;
selectedPlayer = false;
for(var pid in demoRosters.visitor){
demoRosters.visitor[pid].hidden = false;
}
}
else{
selectedPlayer = player.playerid;
for(var pid in demoRosters.visitor){
var otherPlayer = demoRosters.visitor[pid];
if(otherPlayer.starter === player.starter){
demoRosters.visitor[pid].hidden = true;
}
}
}
renderRosterTable(visitorTable, demoRosters.visitor, callbackMap);
});
renderRosterTable(visitorTable, demoRosters.visitor, callbackMap);
renderRosterTable(robotTable, demoRosters.robot, {}, {
locked: true
});
}
var visitorResult = document.getElementById('visitor-result');
var robotResult = document.getElementById('robot-result');
var submitRoster = document.getElementById('submit-roster');
var gameOver = false;
submitRoster.addEventListener('click', e => {
if(!gameOver){
renderRosterTable(visitorTable, demoRosters.visitor, {}, {
locked: true
});
gameOver = true;
}
Database.getAllPlayers({
leagueid: DEMO_LEAGUEID,
from: demoWeeks[1].from,
to: demoWeeks[1].to
}, DEMO_LEAGUE_OBJ).then(playerMap => {
var rosters = {
visitor: {},
robot: {}
};
for(var vid in demoRosters.visitor){
demoRosters.visitor[vid].scores = playerMap[vid].scores;
}
for(var rid in demoRosters.robot){
demoRosters.robot[rid].scores = playerMap[rid].scores;
}
var visitorRes = renderRosterTable(visitorResult, demoRosters.visitor, {}, {
locked: true
});
var robotRes = renderRosterTable(robotResult, demoRosters.robot, {}, {
locked: true
});
var p = document.getElementById('demo-outcome');
if(visitorRes.teamScore > robotRes.teamScore){
p.innerText = 'Way to go! Your team won ' + visitorRes.teamScore + ' to ' + robotRes.teamScore + '!';
}
if(visitorRes.teamScore < robotRes.teamScore){
p.innerText = 'Too bad. Your team lost ' + visitorRes.teamScore + ' to ' + robotRes.teamScore + '!';
}
window.scrollToId('game-result');
}).catch(console.error);
});
var playGameBtns = document.getElementsByClassName('play-game');
for(var p = 0; p < playGameBtns.length; p++){
var btn = playGameBtns[p];
btn.addEventListener('click', e => {
window.open('https://bit.ly/fantasycivicsform');
});
}