-
Notifications
You must be signed in to change notification settings - Fork 1
/
baseball.js
492 lines (393 loc) · 11 KB
/
baseball.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
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
var di = 0,
gi = 0,
isDude = true,
teamName;
teamName=l("currentTeam")||"Squids";
var teams=["Squids","Space Trash"];
function setCurDude(newIdx) {
di = newIdx;
l("di", newIdx);
l("isDude", true);
isDude = true;
resetHiLiteList();
hiLitePlayer(true, di, "curBatter");
hiLitePlayer(false, gi, "nextBatter");
}
function setCurGal(newIdx) {
gi = newIdx;
l("gi", newIdx);
isDude = false;
l("isDude", false);
resetHiLiteList();
hiLitePlayer(false, gi, "curBatter");
hiLitePlayer(true, di, "nextBatter");
}
function idx(e, someChildEl) { //get index of someChildEl in e
return Array.prototype.indexOf.call(e.childNodes, someChildEl);
}
function get(elemId) { return document.getElementById(elemId); }
function html(elemId, newHtml) {
//console.log(get(elemId));
//if (get(elemId!=="")){
if (newHtml !== undefined) {
get(elemId).innerHTML = newHtml;
} else {
return get(elemId).innerHTML;
}
/*}else{
alert(elemId + " is null");
}*/
}
function hide(elemId) {
get(elemId).setAttribute("class", "hide");
}
function show(elemId) { get(elemId).setAttribute("class", ""); }
function toggle(elemId) { if(get(elemId).classList.contains("hide")){
show(elemId);
}else{
hide(elemId);
}
}
function resetHiLiteList() {
var dl = get("dudesList").childNodes;
var gl = get("galsList").childNodes;
for (i = 0; i < dl.length; i++) {
dl[i].setAttribute("class", "");
}
for (i = 0; i < gl.length; i++) {
gl[i].setAttribute("class", "");
}
}
function hiLitePlayer(isDude, idx, hiLiteClass) {
var dl = get("dudesList").childNodes;
var gl = get("galsList").childNodes;
if (isDude) {
dl[idx].setAttribute("class", hiLiteClass);
//gl[nextIdx].setAttribute("class", hiLiteClass);
} else {
gl[idx].setAttribute("class", hiLiteClass);
//dl[nextIdx].setAttribute("class", hiLiteClass);
}
}
//UPDAYEEEE
var teamFn = doT.template(html("teamTmpl"));
var curBatter = doT.template(html("battingTmpl2"));
var peopleList = doT.template(html("selectButtons"));
var playerListTmpl= doT.template(html("playersTmpl"));
var linkSongTmplFn=doT.template(html("linkSongTmpl"));
var players=[];
players=jsonStore("players")||[];
function addPlayer(playerName){
playerName=playerName.trim();
if (playerName!==""&&players.indexOf(playerName)===-1){
players.push(playerName);
jsonStore("players",players);
//refresh the order setup lists
peoplesList('dude');
peoplesList('gal');
}
}
function removePlayer(playerName) {
playerName=playerName.trim();
if (players.indexOf(playerName)!==-1){
console.log("removing at ",players.indexOf(playerName));
players.splice(players.indexOf(playerName), 1);
jsonStore("players",players);
//refresh the order setup lists
peoplesList('dude');
peoplesList('gal');
}
}
function setPlayerSong(playerName,songName){
music[playerName]=songName;
//now i need to like save this to localstorage
jsonStore("music",music);
}
function updatePlayer(dataSet,playerName,value){
if (dataSet==="delaySong"){
delaySong[playerName]=value;
jsonStore("delaySong",delaySong);
}else if(dataSet==="musicStartsAt"){
musicStartsAt[playerName]=value;
jsonStore("musicStartsAt",musicStartsAt );
}
}
var Team;
var music=jsonStore("music")||{};
//REMEMBER -- everything is hosted from virtual directory "/bs"
musicStartsAt=jsonStore("musicStartsAt")||{};
delaySong=jsonStore("delaySong")||{};
function exportConfig(){
//ok do we handle multiple teams?
var backupCurTeamName=teamName;
var result=[];
teams.forEach(t=>{
teamName=t;
if (jsonStore("players")){ //if we dont have players then there is nothing to backup
result.push({
team:t,
players:jsonStore("players"),
music:jsonStore("music"),
musicStartsAt:jsonStore("musicStartsAt"),
delaySong:jsonStore("delaySong"),
});
}
});
saveToClipboard(JSON.stringify(result));
teamName=backupCurTeamName;
}
function importTeamData(){
var backupCurTeamName=teamName;
//ok lets get the js from the textarea.
var idata=get("importTeamData").value;
console.log(idata);
var x=JSON.parse(idata);
var fields=["players","music","delaySong","musicStartsAt"];
x.forEach(t=>{
console.log(t.team);
teamName=t.team;
fields.forEach(f=>{
console.log(f);
if (t[f]){ //if we have some data for our current field
jsonStore(f,t[f]); //save it
}
})
})
teamName=backupCurTeamName;
//loadTeam(teamName);
location.reload();
}
function saveToClipboard(text){
navigator.clipboard.writeText(text).then(function() {
/* clipboard successfully set */
alert("copied to clipboard!");
}, function() {
/* clipboard write failed */
alert("FAILED to copy to clipboard! adding textarea");
var x = document.createElement("TEXTAREA");
var t = document.createTextNode(text);
x.appendChild(t);
document.body.appendChild(x);
});
}
function queryCache(){
var url = [];
caches.open('mysite-static-v5').then(function (cache){
cache.keys().then(function(keys){
return Promise.all(
keys.map(function(k){url.push(k.url); return k.url} )
)
}).then(function(u){ cacheList(url);})
})
}
var availableSongs=[];
function cacheList(Items) {
for (var i = 0; i < Items.length; i++) {
if (['mp3', 'mp4', 'ogg'].indexOf(Items[i].split(".").pop()) > -1) {
// console.log(Items[i]);
availableSongs.push(Items[i].split("/").pop());
}
}
availableSongs=availableSongs.sort();
}
queryCache();
console.log(availableSongs);
function peoplesList(sex) {
var p = {};
p.sex = sex;
p.people = players.sort();//Object.keys(music).sort();
html(sex, peopleList(p));
}
function addToList(sex, name) {
var curSexList = get(sex + 's');
if (curSexList.value.replace(' ', '') === '') {
curSexList.value = curSexList.value + name;
} else {
curSexList.value = curSexList.value + ',' + name;
}
}
function loadVoices() {
var voices = speechSynthesis.getVoices();
var voiceshtml = "<select id='voice'>";
for (var i = 0; i < voices.length; i++) {
voiceshtml += "<option value=" + i + ">" + voices[i].name + "</option>";
// console.log("Voice " + i.toString() + ' ' + voices[i].name + ' ' + voices[i].uri);
}
voiceshtml += "</select>";
html("voices", voiceshtml);
}
function saveTeam() {
l("dudes", get("dudes").value);
l("gals", get("gals").value);
}
function loadTeam(teamId) {
peoplesList('dude');
peoplesList('gal');
//first off we are looking for our list of dudes and gals to populate the active state
if (l("dudes") != "") {
get("dudes").value = l("dudes");
}
if (l("gals") != "") {
//alert(l("dudes"));
get("gals").value = l("gals");
}
if (l("isDude") != "") {
if (l("isDude") == "true") {
isDude = true;
} else {
isDude = false;
}
if (l("di") != "") {
di = parseInt(l("di"));
}
if (l("gi") != "") {
gi = parseInt(l("gi"));
}
}
Team = loadPlayers(teamId);
var battingsonghtml = "";
html("main", teamFn(Team));
loadVoices();
resetHiLiteList();
if (isDude) {
hiLitePlayer(true, di, "curBatter");
hiLitePlayer(false, gi, "nextBatter");
} else {
hiLitePlayer(true, di, "nextBatter");
hiLitePlayer(false, gi, "curBatter");
}
}
var loadPlayers = function (teamId) {
team = {};
//reset globals
var dudes = get("dudes").value.split(",");
team.dudes = [];
team.gals = [];
for (d in dudes) {
team.dudes.push({
name: dudes[d]
});
}
var gals = get("gals").value.split(",");
for (g in gals) {
team.gals.push({
name: gals[g]
});
}
curSong = team.dudes[0].song;
team.name = "Squids";
team.isCoed = true;
return team;
}
var sound;
var itsbegun = false;
var curSong;
function nextBatter() {
var hasDelay = false;
resetHiLiteList()
var curSongName;
if (curSong) {
curSong.pause();
curSong.currentTime = 0;
}
var next3 = {};
if (di === Team.dudes.length) { di = 0; }
if (gi === Team.gals.length) { gi = 0; }
if (isDude) {
hiLitePlayer(true, di, "curBatter");
hiLitePlayer(false, gi, "nextBatter");
next3.up = Team.dudes[di].name;
next3.onDeck = Team.gals[gi].name;
next3.inHole = Team.dudes[di + 1] != undefined ? Team.dudes[di + 1].name : Team.dudes[0].name;
curSong = new Audio('songs/'+ music[Team.dudes[di].name]+("#t="+musicStartsAt[Team.dudes[di].name]||0)); //new Howl({ urls: ["battingmusic/" + music[Team.dudes[di].name]], buffer: true, volume: 0.7 });
curSongName = music[Team.dudes[di].name];
hasDelay = delaySong[Team.dudes[di].name];
di = di + 1;
isDude = false;
} else {
hiLitePlayer(false, gi, "curBatter");
hiLitePlayer(true, di, "nextBatter");
next3.up = Team.gals[gi].name;
curSong = new Audio('songs/'+ music[Team.gals[gi].name]+("#t="+musicStartsAt[Team.gals[gi].name]||0)); //new Howl({ urls: ["battingmusic/" + music[Team.gals[gi].name]], buffer: true, volume: 0.7 });
hasDelay = delaySong[Team.gals[gi].name];
curSongName = music[Team.gals[gi].name];
next3.onDeck = Team.dudes[di].name;
next3.inHole = Team.gals[gi + 1] != undefined ? Team.gals[gi + 1].name : Team.gals[0].name;
gi = gi + 1;
isDude = true;
}
l("isDude", isDude);
l("gi", gi);
l("di", di);
html("curBattingTrack", curBatter(next3));
if (sound) {
sound.pause();
}
voiceSay("now batting " + next3.up + " ,," + next3.onDeck + " awn deck ", function (e) {
if (musicStartsAt[next3.up] !== undefined) {
curSong.currentTime = musicStartsAt[next3.up];
}
curSong.play(
function () {
window.setTimeout(function () {
if (musicStartsAt[next3.up] !== undefined) {
curSong.currentTime(musicStartsAt[next3.up]);
console.log(curSong.currentTime ());
}
}, 50);
}
);
html('nowPlaying', curSongName.replace(/%20/g," "));
}, hasDelay);
}
function voiceSay(say, callback, delay) {
var e = document.getElementById("voice");
var strVoice = e.options[e.selectedIndex].value;
//console.log(strVoice);
var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[parseInt(strVoice)]; // Note: some voices don't support altering params
msg.voiceURI = 'native';
msg.volume = 1; // 0 to 1
msg.rate = .6; // 0.1 to 10
//msg.pitch = 2; //0 to 2
msg.text = say,
msg.lang = 'en-US';
if (callback) {
if (delay) {
msg.onend = function () { callback(e) };
} else {
callback(e)
}
}
speechSynthesis.speak(msg);
}
function initVoices() {
//var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
var watch = setInterval(function () {
console.log("gettin voices");
// Load all voices available
var voices = speechSynthesis.getVoices();
if (voices.length !== 0) {
for (var i = 0; i < voices.length; i++) {
voiceshtml += "<option value=" + i + ">" + voices[i].name + "</option>";
}
clearInterval(watch);
voiceSay("Softball Robo Coach");
}
}, 10);
var voiceshtml = "<select id='voice'>";
for (var i = 0; i < voices.length; i++) {
voiceshtml += "<option value=" + i + ">" + voices[i].name + "</option>";
// console.log("Voice " + i.toString() + ' ' + voices[i].name + ' ' + voices[i].uri);
}
voiceshtml += "</select>";
html("voices", voiceshtml);
}
function playWATC() {
curSong.pause();
curSong = curSong = new Audio("songs/Queen - We Are The Champions.mp3#t=38");
curSong.play(function () {
});
}