-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
768 lines (689 loc) · 26 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Aaron's Solace Dragon Boat Demo!</title>
<link rel="shortcut icon" type="image/png" href="gfx/favicon.png"/>
<script src="lib/paho-mqtt-min.js" type="text/javascript"></script>
<script src="lib/fabric.min.js" type="text/javascript"></script>
<script src="lib/shared.js" type="text/javascript"></script>
<script src="lib/fontfaceobserver.js"></script>
<style>
@import url('https://fonts.googleapis.com/css?family=Luckiest+Guy');
@import url('https://fonts.googleapis.com/css?family=Share+Tech+Mono&display=swap');
body {
background: linear-gradient( #0033cc, #2299aa);
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
const URL_TO_DISPLAY = "sg.solace.com/db";
// load some fonts!
loadAndUse("Luckiest Guy");
loadAndUse("Share Tech Mono");
// audio to be used later
var audioRow;
var audioTrumpet;
var audioCheer;
var audioAwww;
// how big is my screen?
var w = window.innerWidth - 10;
var h = window.innerHeight - 18; // subtract some for no scrollbars
// build the canvas
var canvas = new fabric.Canvas('c', {
width: w,
height: h,
});
canvas.selection = false; // disable group selection
///rect.set('selectable', false); // make object unselectable
const logLength = 15;
var logs = [];
// used to log information to both the console as well as on-screen
function writeConsole(message) {
console.log(message);
if (logs.length >= logLength) {
logs.shift();
}
logs.push(""+message);
// don't log to screen once the race has started
if (state !== "race" && state !== "end") {
setLogText();
}
}
function drawLogo(mode) {
fabric.Image.fromURL('gfx/SolLogo.png', function (img) {
img.scale(0.5).set('top', 10).set('left', w - 200);
canvas.add(img);
});
if (mode == 'meet') {
fabric.Image.fromURL('gfx/qr.png', function (img) {
img.scale(0.8).set('top', h-130).set('left', w-130);
canvas.add(img);
});
}
}
var screenText;
function setScreenText(words, size) {
var fontSize = 100;
// if (size !== undefined) fontSize = size;
if (size) fontSize = size;
if (screenText) canvas.remove(screenText);
screenText = new fabric.Text(words, {
fontFamily: 'Luckiest Guy',
//fontFamily: 'Arial',
//left: 0,
//top: 0,
fontSize: fontSize,
stroke: 'black',
strokeWidth: 3,
fill: 'white',
selectable: false,
// width: w,
textAlign: 'center',
});
canvas.add(screenText);
screenText.center();
}
var latencyText = "";
function setLatencyText(val) {
var fontSize = 32;
if (latencyText !== "") canvas.remove(latencyText);
latencyText = new fabric.Text("Latency: " + val + "ms", {
fontFamily: 'Luckiest Guy',
left: 10,
top: h - 40,
fontSize: fontSize,
stroke: 'black',
//strokeWidth: 2,
fill: 'white',
selectable: false,
});
canvas.add(latencyText);
}
var logText;
function setLogText() {
const fontSize = 24;
if (logText) {
canvas.remove(logText);
}
logText = new fabric.Text(logs.join('\n'), {
fontFamily: 'monospace',// 'Share Tech Mono',
fontWeight: 800,
left: 10,
top: 10,
fontSize: fontSize,
stroke: 'black',
strokeWidth: 0.25,
fill: 'white',
selectable: false,
});
canvas.add(logText);
}
function loadAndUse(font) {
var myfont = new FontFaceObserver(font)
myfont.load().then(function () {
// when font is loaded, use it.
//canvas.requestRenderAll();
}).catch(function (e) {
writeConsole.log(e)
alert('font loading failed ' + font);
});
}
// every app gets its own unique ID, which will be used for it "inbox" subscription
var uniqueID = "" + (Math.random() + 1).toString(36).substring(2, 10); // 8 "random" [0-9a-z]
var meetID;
// grab the URL parameters, everything after the '&'
var params = window.location.search.substring(1).split('&')[0]; // first element of split
audioCheer = new Audio('audio/cheer.mp3');
audioTrumpet = new Audio('audio/winners-trumpet.mp3'); // load the trumpet sound
var meet;
if (params !== "") { // if there's something there...
mode = 'meet'; // this is a race controller
meetID = uniqueID;//
//audioTrumpet = new Audio('audio/winners-trumpet.mp3'); // load the trumpet sound
} else {
mode = 'boat'; // else, we're a boat/player
audioRow = new Audio('audio/row.mp3');
audioAwww = new Audio('audio/sad.mp3');
}
drawLogo(mode);
canvas.on('mouse:down', function (options) {
click();
});
//meetID = "mwtk4nfk";
var isMobile = detectMobile(); // from shared.js
// let's do some logging...
writeConsole("UniqueID: " + uniqueID);
if (mode === 'meet') {
writeConsole("MeetID: " + meetID);
}
writeConsole("Is phone? " + isMobile);
writeConsole("Mode: '" + mode + "'");
// setup, prep (players can join, teams get chosen), starting in 5 seconds, racing, end of race
// MEET: setup, prep, marks, race, end
// BOAT: setup, prep, marks, race, end
var state = "setup";
var meetName = "";
if (mode === 'meet') {
// assign here with a input box
//meetName = "Dev Jam race!";
meetName = prompt("Please enter the race name", "Dev Jam Race!");
writeConsole("Race name: "+meetName);
//var numTeams = prompt("How many teams do you want? [2-4]", "4");
//writeConsole("Teams: "+numTeams);
}
var teams = ["pink", "green", "yellow", "orange"];
boatImg = {};
for (color in teams) {
var img = new Image();
img.src = "gfx/boat" + color + ".png";
boatImg[teams[color]] = img;
}
var raceID; // per-race unique ID
// meet vars:
var curPlayers = {};
var totalClicksByPlayer = {};
var teamPointer = 0;
var curTeams = {};
for (color in teams) {
curTeams[teams[color]] = new Set();
}
var notReadyPlayers;
var nextPlayers = new Set();
// boat vars:
var myTeam;
var isConnected = false;
writeConsole("Connecting...");
var client = getClientConnection(uniqueID,onMessageArrived,onConnectionLost,onConnect,mode);
// called when the client connects
function onConnect() {
writeConsole("Connected to "+client.host+"!");
if (mode === 'meet') { // this is the race controller
client.subscribe("boat/meet/" + meetID + "/#"); // is meet catch-all
client.subscribe("boat/meet/query");
meetSwitchToPrep();
} else { // this is a player (boat)
client.subscribe("boat/player/" + uniqueID + "/#"); // subscribe to his unique topics
var message = new Paho.MQTT.Message(uniqueID);
message.destinationName = "boat/meet/query";
sendMessage(message); // find out what meets there are
writeConsole("Just published looking for races");
}
client.subscribe("boat/timing/" + uniqueID);
if (isMobile) {
} else {
}
isConnected = true;
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
isConnected = false;
if (responseObject.errorCode !== 0) {
writeConsole("Connection Lost! " + responseObject.errorMessage);
}
setLatencyText("--"); // to show no connectivity
}
// called when a message arrives
function onMessageArrived(msg) {
if (msg.destinationName == "boat/timing/" + uniqueID) {
updateLatency(msg.payloadString);
} else {
if (mode === 'meet') {
meetMessageArrived(msg);
} else {
writeConsole("-> [MSG RECV] topic='"+msg.destinationName+"'; payload='"+msg.payloadString+"'");
boatMessageArrived(msg);
}
}
}
function sendMessage(msg) {
if (mode === 'boat') {
writeConsole("<- [MSG SENT] topic='"+msg.destinationName+"'; payload='"+msg.payloadString+"'");
}
client.send(msg);
}
// called when a message arrives
function meetMessageArrived(msg) {
var topic = msg.destinationName;
if (topic === "boat/meet/query") { // always reply with our details
// QUERY to find out my race ID and NAME
var payload = { meetID: meetID, meetName: meetName };
var reply = new Paho.MQTT.Message(JSON.stringify(payload));
reply.destinationName = "boat/player/" + msg.payloadString + "/query";
sendMessage(reply); // reply to the player
} else if (topic === "boat/meet/" + meetID + "/join") { // always reply with our details
// JOIN request, to join this race
switch (state) {
// just getting ready, add them in!
case "prep":
assignPlayer(msg.payloadString);
sendPrepMessage(msg.payloadString);
setScreenText("" + Object.keys(curPlayers).length + " Joined");
break;
// otherwise, wait to the next race
default:
nextPlayers.add(msg.payloadString);
var meetDeets = { meetID: meetID, meetName: meetName };
var reply = new Paho.MQTT.Message(JSON.stringify(meetDeets));
reply.destinationName = "boat/player/" + msg.payloadString + "/join";
sendMessage(reply);
writeConsole("Added " + msg.payloadString + " to next race.");
break;
}
} else if (topic === "boat/meet/" + meetID + "/prep") { // for remote control
meetSwitchToPrep();
} else if (topic === "boat/meet/" + meetID + "/marks") { // for remote control, instead of clicking
meetOnYourMarks();
} else if (topic === "boat/meet/" + raceID + "/ready") { // from the player saying they're good to go!
playerReady(msg.payloadString);
} else if (topic.startsWith("boat/meet/" + raceID + "/row")) {
if (state !== "race") return;
var levels = topic.split("/");
var team = levels[4];
var uniqueID = levels[5];
pointsNeeded[team]--;
totalClicksByPlayer[uniqueID]++;
// is the race won? game over?
if (pointsNeeded[team] === 0) {
changeState("end");
writeConsole(team + " WON THE RACE");
audioCheer.play();
// ok, sort all the players by total # of clicks
var ps = [];
for (var key in Object.keys(curPlayers)) {
var player = Object.keys(curPlayers)[key];
ps.push({ name: player, val: totalClicksByPlayer[player] });
}
ps.sort(function (a, b) {
return b.val - a.val;
});
console.log(ps);
// sort the teams by their ratios to determine who won
var ratios = meetCalcRatios();
var ratiosA = [];
for (var key in Object.keys(ratios)) {
var t = teams[key];
ratiosA.push({ team: t, val: ratios[t] });
}
ratiosA.sort(function (a, b) {
return b.val - a.val;
});
console.log(ratiosA);
meetBroadcastStats();
setScreenText(team + ' Team Wins!!', 120);
var endMsg;
endMsg = { winningTeam: team, ranking: ps };
//if (ps.length >= 3) endMsg = { team: team, winners: [ps[0].name, ps[1].name, ps[2].name] };
//else if (ps.length >= 2) endMsg = { team: team, winners: [ps[0].name, ps[1].name] };
//else endMsg = { team: team, winners: [ps[0].name] };
console.log(endMsg);
var msg = new Paho.MQTT.Message(JSON.stringify(endMsg));
msg.destinationName = "boat/race/" + raceID + "/end";
sendMessage(msg);
var final = "Most clicks: " + ps[0].val;
if (ps.length >= 3) final += "\n2nd most: " + ps[1].val;
if (ps.length >= 4) final += "\n3rd most: " + ps[2].val;
if (ps.length >= 2) final += "\nWorst clicker: " + ps[ps.length - 1].val;
setTimeout(setScreenText, 5000, final);
//setTimeout(meetSwitchToPrep, 10000);
}
}
}
function prv_changeState(newState) {
state = newState;
writeConsole("Switching state from '" + state + "' to '" + newState + "'");
}
// controls the state engine, used by both modes: 'meet' and 'boat'
// states are: [setup, prep, marks, race, end]
// this would be much better using a proper state machine
function changeState(newState) {
switch (newState) {
case "setup": // you should never switch into setup: starts this way... or go from end->prep
break;
case "prep":
if (state == "setup" || state === "end" || state === "marks") {
prv_changeState(newState);
return true;
}
break;
case "marks":
if (state === "prep" && (mode === 'meet' && Object.keys(curPlayers).length > 0)) {
prv_changeState(newState);
return true;
}
break;
case "race":
if (state === "marks") {
if (mode === 'meet' && Object.keys(curPlayers).length > 0) {
prv_changeState(newState);
return true;
} else {
writeConsole("No active players!! Going back to prep mode");
meetSwitchToPrep();
return false;
}
}
break;
case "end":
if (state === "race") {
prv_changeState(newState);
return true;
}
break;
default:
writeConsole("Unknown state transition: " + newState);
}
writeConsole("State switch from " + state + " to " + newState + " failed!");
return false;
}
function meetSwitchToPrep() {
if (changeState("prep")) {
setScreenText('Waiting for Players!');
// add some temporary text to show the URL
var urlText = new fabric.Text(URL_TO_DISPLAY, {
fontFamily: 'monospace',
fontWeight: '900',
top: 3 * h / 5,
fontSize: 100,
stroke: 'black',
strokeWidth: 3,
fill: 'white',
selectable: false,
});
canvas.add(urlText);
urlText.centerH();
// generate a new raceID for this upcoming race
raceID = "" + (Math.random() + 1).toString(36).substring(2, 10); // 8 "random" [0-9a-z]
// all the players that have been waiting to join, add them to this new race and inform them
meetCopyPlayers();
}
}
const marksTime = 8000
// when starting the race..!
function meetOnYourMarks() {
if (changeState("marks")) { // make sure we're allowed to change state
client.subscribe("boat/meet/" + raceID + "/#"); // add a special race subscription
notReadyPlayers = new Set(); // make an empty set...
for (var key in Object.keys(curPlayers)) {
var player = Object.keys(curPlayers)[key]; // add every player into this empty set
notReadyPlayers.add(player);
}
var ratios = meetCalcRatios();
var reply = new Paho.MQTT.Message("GET READY!!!! 5 seconds!");
reply.destinationName = "boat/race/" + raceID + "/marks";
sendMessage(reply);
setTimeout(meetStartRace, marksTime); // start the cound down!
setTimeout(meetFinalPrep, marksTime - 2500); // 2500ms before the start, make sure the teams are good
setTimeout(setScreenText, marksTime - 2000, 'Ready?');
setTimeout(setScreenText, marksTime - 1000, 'Set...');
setTimeout(setScreenText, marksTime, 'GO!!', 150);
setTimeout(setScreenText, marksTime + 100, '');
writeConsole("GET READY!");
setScreenText('On your marks!');
audioTrumpet.play();
}
}
function playerReady(player) {
notReadyPlayers.delete(player);
writeConsole("Player " + player + " on team " + curPlayers[player] + " reports ready!");
}
var pointsNeeded = {};
var totalPointsNeeded = {};
// whoever hasn't reported ready, kick them off, then define team sizes + race length
function meetFinalPrep() {
for (let player of notReadyPlayers) {
var team = curPlayers[player];
writeConsole("Removing inactive player " + player + " from team " + team);
delete curPlayers[player];
curTeams[team].delete(player);
}
// all the players left are actually in the race
for (var key in Object.keys(curPlayers)) {
var player = Object.keys(curPlayers)[key];
totalClicksByPlayer[player] = 0;
}
for (var team in teams) {
// need 100 clicks from each player
var totalStrokes = 100;
pointsNeeded[teams[team]] = totalStrokes * curTeams[teams[team]].size + 1;
totalPointsNeeded[teams[team]] = totalStrokes * curTeams[teams[team]].size + 1;
}
initBoats();
//boatBroadcastStats();
}
const updateInterval = 500;
// The race has begun!
function meetStartRace() {
if (changeState("race")) {
writeConsole("Race has started");
// send out the position stats every 500ms
setTimeout(meetBroadcastStatsLoop, updateInterval);
}
}
function meetBroadcastStatsLoop() {
// as long as the race is on, keep looping
if (state !== "race") return;
meetBroadcastStats();
setTimeout(meetBroadcastStatsLoop, updateInterval);
}
// returns an object with teams and how far they are from starting line to finish line
// e.g.: { "pink":0.2, "green":0.7 }
function meetCalcRatios() {
var ratios = {};
for (let k in teams) {
var team = teams[k];
ratios[team] = 1 - (pointsNeeded[team] / totalPointsNeeded[team]);
}
return ratios;
}
function meetBroadcastStats() {
var ratios = meetCalcRatios();
var msg = new Paho.MQTT.Message(JSON.stringify(ratios));
msg.destinationName = "boat/race/" + raceID + "/stats";
sendMessage(msg);
drawBoats(ratios);
// console.log(ratios);
}
function resetCanvas() {
canvas.clear();
drawLogo(mode);
}
function click() {
if (mode === 'boat') {
row();
} else { // meet mode
if (state === 'prep') {
meetOnYourMarks();
}
}
}
function assignPlayer(player) {
var team = teams[(teamPointer++) % 4];
curPlayers[player] = team;
curTeams[team].add(player);
return team;
}
// this is called
// anybody waiting for the next race to start, move them over and inform them
function meetCopyPlayers() {
for (let player of nextPlayers) {
assignPlayer(player);
sendPrepMessage(player);
}
}
// this tells the players/boats what the next race ID is going to be, and to listen to that topic
function sendPrepMessage(player) {
var color = curPlayers[player];
var raceDeets = { meetID: meetID, meetName: meetName, team: color, raceID: raceID };
var reply = new Paho.MQTT.Message(JSON.stringify(raceDeets));
reply.destinationName = "boat/player/" + player + "/prep";
sendMessage(reply);
//console.log(reply);
writeConsole("Added " + player + " to team " + color + "!");
}
// SHARED FUNCTIONS! //////////////////////////////////////////////////////////////////////
var boats = [];
function initBoats() {
canvas.remove(logText);
resetCanvas();
for (var i = 0; i < 4; i++) {
var img = boatImg[teams[i]];
var iw = img.width;
var ih = img.height;
// gotta make this look good on both landscape and portrait
var yScale = (h / 6) / ih;
var xScale = (w / 3) / iw;
var scale = Math.min(xScale, yScale);
if (mode === 'boat' && teams[i] === myTeam) scale = scale *= 1.25;;
var x = 0;
var y = (((2 + 2 * i) * h) / 10) - (ih * scale * 0.5);
var imgInstance = new fabric.Image(img, {
left: x,
top: y,
selectable: false,
});
imgInstance.scale(scale);
boats[i] = imgInstance;
canvas.add(boats[i]);
}
}
// this is used by both the meet and boats
function drawBoats(pos) {
for (var i = 0; i < 4; i++) {
var img = boatImg[teams[i]];
var iw = img.width;
var ih = img.height;
var yScale = (h / 8) / ih;
var xScale = (w / 3) / iw;
var scale = Math.min(xScale, yScale);
// the players will see their own boats 25% larger than the others
if (mode === 'boat' && teams[i] === myTeam) scale = scale *= 1.25;
var x = pos[teams[i]] * (w - (iw * scale));
//var y = (((2 + 2 * i) * h) / 10) - (ih * scale * 0.5);
boats[i].animate('left', x, {
onChange: canvas.renderAll.bind(canvas),
duration: updateInterval - 50,
easing: fabric.util.ease.easeOutQuad,
});
}
}
// BOAT-SPECIFIC FUNCTIONS! //////////////////////////////////////////////////////////////////////
function boatMessageArrived(msg) {
var topic = msg.destinationName;
if (topic === "boat/player/" + uniqueID + "/query") {
// QUERY response received, should add to list of meets to choose from
var payload = JSON.parse(msg.payloadString)
if (meetName != "") return;
var asdf = confirm("Do you want to join the race called: \""+payload.meetName+"\"?");
if (!asdf) return;
// THIS PART A HACK, WE SHOULD IDEALLY ADD TO A LIST OF RACES, NOT JUST REPLY
writeConsole("Ok, heard back from '" + payload.meetName + "', going to join it");
var reply = new Paho.MQTT.Message(uniqueID);
reply.destinationName = "boat/meet/" + payload.meetID + "/join";
sendMessage(reply); // join this meet!
writeConsole("Have requested to join race");
} else if (topic == "boat/player/" + uniqueID + "/join") {
// JOIN response received, means we can add ourselves to this meet
var payload = JSON.parse(msg.payloadString)
if (meetName != "") {
writeConsole("Already joined a meet (" + meetName + "), can't join " + payload.name);
return;
}
meetName = payload.meetName;
meetID = payload.meetID;
writeConsole("Successfully joined '" + meetName + "' meet, standby for next race!");
setScreenText("Standby for\nnext race!");
} else if (topic == "boat/player/" + uniqueID + "/prep") {
var payload = JSON.parse(msg.payloadString)
writeConsole("Going to start a race soon! Get ready! I'm on the " + payload.team + " team!");
meetName = payload.meetName;
meetID = payload.meetID;
raceID = payload.raceID;
myTeam = payload.team;
client.subscribe("boat/race/" + raceID + "/#");
var img = boatImg[myTeam];
var imgInstance = new fabric.Image(img, {
selectable: false,
});
imgInstance.scale(1.5);
canvas.add(imgInstance);
imgInstance.centerV();
imgInstance.centerH();
setScreenText("You're on\n" + myTeam + " team!");
} else if (topic === "boat/race/" + raceID + "/marks") {
// the race is starting... need to confirm I'm ready
writeConsole(msg.payloadString);
var reply = new Paho.MQTT.Message(uniqueID);
reply.destinationName = "boat/meet/" + raceID + "/ready";
sendMessage(reply);
setTimeout(boatStartRace, marksTime);
setTimeout(initBoats, marksTime - 2500); // 2500ms before the start, make sure the teams are good
setScreenText('On your marks!');
setTimeout(setScreenText, marksTime - 2000, 'Ready?');
setTimeout(setScreenText, marksTime - 1000, 'Set...');
setTimeout(setScreenText, marksTime, 'GO!!', 150);
setTimeout(setScreenText, marksTime + 500, '');
// buzz the phone, if it allows it...
var vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;
audioTrumpet.play();
if (vibrate) {
// writeConsole("VAIBRATTE!");
navigator.vibrate(1000);
}
} else if (topic === "boat/race/" + raceID + "/stats") {
drawBoats(JSON.parse(msg.payloadString));
} else if (topic === "boat/race/" + raceID + "/end") {
state = "end";
var payload = JSON.parse(msg.payloadString)
writeConsole(payload);
audioRow.pause();
audioRow.currentTime = 0;
if (payload.winningTeam === myTeam) {
audioCheer.play();
setScreenText("Your Team Won!!");
} else {
audioAwww.play();
setScreenText("Your team lost! :-(");
}
var ranking = payload['ranking'];
for (var i=0;i<ranking.length;i++) {
if (ranking[i]['name'] == uniqueID) {
setTimeout(setScreenText, 5000, "You ranked\n#"+(i+1)+" out of "+ranking.length+"\nfor rows!");
}
}
}
}
// boat function
function boatStartRace() {
if (state === 'meet') return;
state = "race";
writeConsole("GO!!!!");
}
// boat function
function row() {
if (state !== "race") return;
var msg = new Paho.MQTT.Message(myTeam);
msg.destinationName = "boat/meet/" + raceID + "/row/" + myTeam + "/" + uniqueID;
sendMessage(msg);
audioRow.pause();
audioRow.currentTime = 0;
audioRow.play();
}
// LATENCY TIMING FUNCTIONS! //////////////////////////////////////////////////////////////////////
// anonymous function to send latency/timing messages every second
setInterval(function () {
if (!isConnected) return;
var timestamp = "" + Date.now();
var message = new Paho.MQTT.Message(timestamp);
message.destinationName = "boat/timing/" + uniqueID;
client.send(message);
}, 1000);
// when a timing message is received, update the displayed text
function updateLatency(ts) {
var delta = Date.now() - ts;
setLatencyText(delta.toFixed(0));
}
</script>
</body>
</html>