-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.html
1039 lines (881 loc) · 38.1 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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!----
Copyright (C) 2024 Merten Dahlkemper
Contact: merten.nikolay <at> gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
--->
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#loginForm { display: flex; flex-direction: column; max-width: 300px; }
select, input, button { margin-bottom: 10px; padding: 5px; }
#gameContent { display: none; }
#ticketOptions { margin-top: 20px; border: 1px solid #ccc; padding: 10px; }
.ticket-option { margin-bottom: 10px; }
#debug { margin-top: 20px; border: 1px solid red; padding: 10px; }
#map { height: 600px}
</style>
</head>
<body>
<h1>Switzerland Travel Game</h1>
<div id="loginSection">
<h2>Login</h2>
<form id="loginForm">
<select id="teamSelect">
<option value="Team A">Team A</option>
<option value="Team B">Team B</option>
</select>
<input type="password" id="accessCode" placeholder="Access Code">
<input type="submit" value="Login">
</form>
<div id="loginError" style="color: red;"></div>
</div>
<div id="gameContent">
<h2 id="teamInfo"></h2>
<div id="map"></div>
<div id="ticketSection" style="display:block;">
<h3>Draw Tickets</h3>
<div id="availableTickets"></div>
<p><b>Attention:</b> Once you click the following button, you immediately spend <b>15 Coins</b> and you have to choose at least one of the 5 Tickets (or less if less than 5 tickets are left in the stack).</p>
<button onclick="drawTickets()">Draw Tickets</button>
</div>
<div id="ticketOptionsSection" style="display:none;">
<h3>Select Tickets</h3>
<p> You have to select at least one of the following tickets:</p>
<div id="ticketList"></div>
<button onclick="confirmTicketSelection()">Confirm Selection</button>
</div>
<div id="claimInitiation" style="display:block;">
<h3>Claim a Route</h3>
<button id="initiateClaimButton" onclick="initiateClaimRoute()">Set Start Location for the claim</button>
<select id="claimStationDropdown" style="display:none;"></select>
<button id="selectStationButton" onclick="selectStartStation()" style="display:none;">Select Station</button>
<div id="startStation" style="display:none;"></div>
<button id="startClaimButton" onclick="startClaim()" style="display:none;">Start the claim</button>
</div>
<div id="claimSection" style="display:none;">
<h3>Finish the route claim</h3>
<div id="startStationFinish" style="display:block;"></div>
<button id="finishClaimButton" onclick="finishClaimRoute()" style="display:block;">Finish Claim</button>
<button id="abortClaimButton" onclick="abortClaim()" style="display:block;">Abort Claim</button>
<select id="routeDropdown" style="display:none;"></select>
<input type="text" id="trainNumberInput" placeholder="Enter train number" style="display:none;">
<button id="claimRouteButton" onclick="claimSelectedRoute()" style="display:none;">Claim Route</button>
</div>
<div id="challengeSection" style="display:block;">
<h3>Draw a challenge</h3>
<p><b>Attention:</b> Once you draw challenges, you will be provided with three random challenge options. If none of them suits you, you can cancel, but it will cost you 20% of your current Coins.</p>
<button onclick="initiateChallenge()">Set Challenge Location</button>
<select id="challengeStationDropdown" style="display:none;"></select>
<button id="selectLocationButton" onclick="selectChallengeLocation()" style="display:none;">Select Location</button>
<div id="challengeLocation" style="display:none;"></div>
<button id="drawChallengeButton" onclick="drawChallenges()" style="display:none;">Draw Challenges</button>
</div>
<div id="challengeOptionsSection" style="display:none;">
<h3>Select the challenge</h3>
<p> Choose one of the following challenges you want to try. If you cancel, it will cost you 20 % of your current Coins.</p>
<div id="challengeLocationFinish" style="display:block;"></div>
<div id="challengeList"></div>
<button onclick="confirmChallengeSelection()">Confirm</button>
<button onclick="cancelChallengeSelection()">Cancel</button>
</div>
<div id="challengeCompletionSection" style="display:none;">
<h3>Complete the challenge</h3>
<p> You are trying the following challenge. Confirm if you complete the challenge and don't forget to send proof to the other team.</p>
<p> If you want to cancel, it will cost you 20 % of your current Coins.</p>
<div id="challengeText"></div>
<div id="challengeLocationCompletion" style="display:none;"></div>
<button onclick="confirmChallengeCompletion()">Confirm</button>
<button onclick="cancelChallengeSelection()">Cancel</button>
</div>
<div id="gameStatus"></div>
<div id="teamTickets"></div>
<div id="allChallengeList"></div>
</div>
<script>
let currentTeamId = '';
let currentTicketOptions = [];
var map;
var data = {};
var stations = {};
var routes = {};
var gameStatus;
let markers = [];
let routeLayers = [];
let ticketMarkers = [];
var teamTickets ={};
let selectedTickets = [];
let selectedChallenges = [];
var globalChallenges = {};
let pollingInterval;
const ticketColors = ['#FF5733', '#33FF57', '#3357FF', '#FF33F1', '#33FFF1'];
//let startStationId;
document.getElementById('loginForm').onsubmit = function(e) {
e.preventDefault();
const teamId = document.getElementById('teamSelect').value;
const accessCode = document.getElementById('accessCode').value;
google.script.run
.withSuccessHandler(function(isValid) {
if (isValid) {
currentTeamId = teamId;
console.log("Current Team Id:",currentTeamId)
document.getElementById('loginSection').style.display = 'none';
document.getElementById('gameContent').style.display = 'block';
document.getElementById('teamInfo').textContent = 'Your Team: ' + teamId;
console.log("These are the stations: ",stations);
//stations = data.stations;
//routes = data.routes;
createChallengeList();
//console.log("Challenges", globalChallenges)//,Object.keys(globalChallenges))
map = initializeMap();
updateDisplay(data);
//console.log("One specific challenge",allChallenges["CH02"])
//updateGameStatusDisplay();
//updateTicketDisplay();
} else {
document.getElementById('loginError').textContent = 'Invalid access code';
}
})
.validateTeamAccess(teamId, accessCode);
};
function initializeMap(){
console.log('Initializing map...');
map = L.map('map').setView([46.8182, 8.2275], 8);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap contributors'
}).addTo(map);
console.log('Map initialized');
//var marker = L.marker([46.82, 8.23]).addTo(map);
//marker.bindPopup(`<b>Center of Switzerland`);
addStationsAndRoutes(stations, map);
updateMap(routes);
return map;
};
function getChallengesWithDelay(callback) {
setTimeout(function() {
if (Object.keys(globalChallenges).length > 0) {
callback(globalChallenges);
} else {
console.log("Challenges not loaded yet, retrying...");
getChallengesWithDelay(callback);
}
}, 100); // 100ms delay
}
function addStationsAndRoutes(stations, map) {
//stations = data.stations;
console.log('Data received:', stations);
//routes = data.routes;
//var marker = L.marker([46.95, 7.44]).addTo(map);
//marker.bindPopup(`<b>Bern</b><br>Canton: Bern<br>Altitude: 551 m`);
getChallengesWithDelay(function(challenges){
// Add stations
Object.values(stations).forEach(station => {
const lat = station.lat;
const lon = station.lon;
let possibleGeneralChallengeIds = station.possibleGeneralChallenges
let possibleLocalChallengeIds = station.possibleSpecialChallenges
var possibleChallenges = "";
if(possibleLocalChallengeIds[0]){
possibleLocalChallengeIds.forEach(challenge =>{
//console.log("Challenges:",challenges[challenge])
possibleChallenges += challenges[challenge].title+"; "
})
}
possibleGeneralChallengeIds.forEach(challenge =>{
possibleChallenges += challenges[challenge].title+"; "
})
let marker = L.circleMarker([lat, lon], {
radius: 5,
fillColor: "#808080",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
}).addTo(map);
marker.bindPopup(`<b>${station.name}</b><br>Canton: ${station.canton}<br>Altitude: ${station.altitude} m<br>Possible Challenges: ${possibleChallenges}
`);
})
});
}
function createChallengeList(){
var challengeListElement = document.getElementById("allChallengeList");
challengeListElement.innerHTML = "<h3>List of all challenges</h3>"
google.script.run
.withSuccessHandler(function(response){
let challenges = response[0];
challenges.forEach(challenge=>{
challengeListElement.innerHTML += `
<p><b>${challenge.title}</b> (${challenge.region}) for ${challenge.reward} Coins:<br> ${challenge.text}<br><br></p>
`;
})
globalChallenges = response[1];
console.log("Challenges loaded:", Object.keys(globalChallenges));
})
.createListOfChallenges();
}
//Ticket functions
function drawTickets() {
document.getElementById('ticketSection').style.display = 'none';
document.getElementById('claimInitiation').style.display = 'none';
document.getElementById('challengeSection').style.display = 'none';
document.getElementById('ticketOptionsSection').style.display = 'block';
google.script.run
.withSuccessHandler(displayTicketOptions)
.generateTicketOptions(currentTeamId);
}
function displayTicketOptions(tickets) {
if(tickets.message){
let message = tickets.message;
if (message === "Not enough coins to draw tickets") {
alert("You don't have enough coins to draw new tickets (at least 15 are needed). Complete a challenge before you can draw tickets.");
cancelTicketSelection();
return;
}
else if (message === "No tickets left") {
alert("There are no tickets left.");
cancelTicketSelection();
return;
}
}
let ticketList = document.getElementById('ticketList');
ticketList.innerHTML = '';
tickets.forEach((ticket, index) => {
let ticketDiv = document.createElement('div');
let color = ticketColors[index];
ticketDiv.innerHTML = `
<input type="checkbox" id="ticket${index}" onchange="toggleTicket(${index})">
<label for="ticket${index}" style="color:${color}">${ticket.from.name} to ${ticket.to.name} (${ticket.points} points)"</label>
`;
ticketList.appendChild(ticketDiv);
addTicketToMap(ticket, color, index);
selectedTickets[index] = ticket;
});
}
function addTicketToMap(ticket, color, index) {
//let color = ticketColors[index];
let startMarker = L.circleMarker([ticket.from.lat, ticket.from.lon], {
color: color,
fillColor: color,
fillOpacity: 0.8,
radius: 8
}).addTo(map);
startMarker.bindPopup(`Ticket ${index + 1} Start: ${ticket.from.name}`);
let endMarker = L.circleMarker([ticket.to.lat, ticket.to.lon], {
color: color,
fillColor: color,
fillOpacity: 0.8,
radius: 8
}).addTo(map);
endMarker.bindPopup(`Ticket ${index + 1} End: ${ticket.to.name}`);
let line = L.polyline([
[ticket.from.lat, ticket.from.lon],
[ticket.to.lat, ticket.to.lon]
], {color: color, dashArray: '5, 5', weight: 5}).addTo(map);
line.bindPopup(`<b>Ticket ${index + 1}:<br><b>${ticket.from.name} (${ticket.from.canton})</b> to ${ticket.to.name} (${ticket.to.canton})</b><br>Points: ${ticket.points}`);
ticketMarkers.push(startMarker, endMarker, line);
}
function toggleTicket(index) {
selectedTickets[index].selected = document.getElementById(`ticket${index}`).checked;
}
function confirmTicketSelection() {
let selectedTicketData = selectedTickets.filter(ticket => ticket.selected);
console.log("Selected Tickets: ",selectedTicketData)
if (selectedTicketData.length < 1){
alert ("Please select at least one ticket.")
return;
}
google.script.run
.withSuccessHandler(updateDisplay)
.saveSelectedTickets(currentTeamId, selectedTicketData);
cancelTicketSelection();
}
function cancelTicketSelection() {
document.getElementById('ticketOptionsSection').style.display = 'none';
document.getElementById('ticketSection').style.display = 'block';
document.getElementById('claimInitiation').style.display = 'block';
document.getElementById('challengeSection').style.display = 'block';
ticketMarkers.forEach(marker => map.removeLayer(marker));
ticketMarkers = [];
updateTicketDisplay();
}
//Route related functions
function updateMap(routes) {
console.log("Updating map with routes:", routes);
// Clear existing routes
if (typeof routeLayers === 'undefined') {
routeLayers = [];
}
routeLayers.forEach(layer => map.removeLayer(layer));
routeLayers = [];
// Add updated routes
Object.values(routes).forEach(route => {
let from = stations[route.from];
let to = stations[route.to];
let color = getRouteColor(route.claimedBy);
let line = L.polyline([[from.lat, from.lon], [to.lat, to.lon]], {color: color, weight: 5}).addTo(map);
line.bindPopup(`<b>${from.name} (${route.fromCanton}) to ${to.name} (${route.toCanton})</b><br>Cost: ${route.cost} Coins<br>Reward: ${route.points} Points<br>Claimed by: ${route.claimedBy || 'Unclaimed'}`);
routeLayers.push(line);
});
}
function getRouteColor(claimedBy) {
if (!claimedBy) return 'blue';
return claimedBy === currentTeamId ? 'green' : 'red';
}
function getUserLocation() {
return new Promise((resolve, reject) => {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
position => {
resolve({
lat: position.coords.latitude,
lon: position.coords.longitude
});
},
error => {
reject(error);
}
);
} else {
reject(new Error("Geolocation is not supported by this browser."));
}
});
}
function findNearestStation(userLat, userLon, stations) {
let nearestStation = null;
let shortestDistance = Infinity;
for (let stationId in stations) {
let station = stations[stationId];
let distance = calculateDistance(userLat, userLon, station.lat, station.lon);
if (distance < shortestDistance) {
shortestDistance = distance;
nearestStation = station;
}
}
return nearestStation;
}
function calculateDistance(lat1, lon1, lat2, lon2) {
// Haversine formula to calculate distance between two points on Earth
const R = 6371; // Radius of the Earth in km
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLon = (lon2 - lon1) * Math.PI / 180;
const a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
const distance = R * c; // Distance in km
return distance;
}
// Claim related functions
function initiateClaimRoute() {
getUserLocation()
.then(location => {
const nearestStation = findNearestStation(location.lat, location.lon, stations);
if (nearestStation && calculateDistance(location.lat, location.lon, nearestStation.lat, nearestStation.lon) <= 2) {
setStartStationDisplay(nearestStation);
} else {
displayStationDropdown("claim");
}
})
.catch(error => {
console.error("Error getting user location:", error);
displayStationDropdown("claim");
});
}
function setStartStationDisplay(station){
setStartStationDiv(station.id)
document.getElementById('startStation').style.display = 'block';
document.getElementById('startClaimButton').style.display = 'block';
}
function setStartStationDiv(stationId){
let startStationElement = document.getElementById('startStation');
let station = stations[stationId]
let locationText = `${station.name} (${station.canton}, ${station.altitude} m)`
startStationElement.innerHTML = `The claim will be started in ${locationText}`;
startStationElement.dataset.stationId = station.id;
document.getElementById('startStationFinish').innerHTML = `The claim has been started in ${locationText}. To finish or abort the claim, please click one of the buttons below.`
}
function displayStationDropdown(section) {
if(section === "claim"){
var dropdown = document.getElementById('claimStationDropdown');
var confirmButton = document.getElementById('selectStationButton');
}
else if (section === "challenge"){
var dropdown = document.getElementById('challengeStationDropdown');
var confirmButton = document.getElementById('selectLocationButton');
}
dropdown.innerHTML = '';
let stationArray = Object.values(stations).map(station => ({
id: station.id,
name: station.name,
canton: station.canton,
altitude: station.altitude
}));
// Sort the array alphabetically by station name
stationArray.sort((a, b) => a.name.localeCompare(b.name));
// Create and append sorted options
stationArray.forEach(station => {
let option = document.createElement('option');
option.value = station.id;
option.text = `${station.name} (${station.canton}, ${station.altitude} m)`;
dropdown.appendChild(option);
});
dropdown.style.display = 'block';
confirmButton.style.display = 'block';
}
function selectStartStation() {
let stationId = document.getElementById('claimStationDropdown').value;
setStartStationDisplay(stations[stationId]);
}
function startClaim(){
let stationId = document.getElementById('startStation').dataset.stationId;
let station = stations[stationId];
document.getElementById('finishClaimButton').style.display = 'block';
document.getElementById('abortClaimButton').style.display = 'block';
document.getElementById('claimInitiation').style.display = 'none';
google.script.run
.withSuccessHandler(updateDisplay)
.withFailureHandler(handleError)
.initiateClaimInGameStatus(currentTeamId, station);
// Send email to other team
google.script.run
.withFailureHandler(handleError)
.sendClaimInitiationEmail(currentTeamId, station);
}
function handleError(error) {
console.error("Error:", error);
alert("An error occurred: " + error.message);
}
function abortClaim(){
google.script.run
.withSuccessHandler(updateDisplay)
.withFailureHandler(handleError)
.abortClaim(currentTeamId);
// Send email to other team
google.script.run
.withFailureHandler(handleError)
.sendClaimAbortionEmail(currentTeamId);
}
function finishClaimRoute() {
var startStationId = document.getElementById('startStation').dataset.stationId;
console.log("Start Station Id:", startStationId)
let possibleRoutes = getPossibleRoutes(startStationId);
console.log("These routes are possible:", possibleRoutes)
displayRouteDropdown(possibleRoutes);
document.getElementById('trainNumberInput').style.display = 'block';
}
function getPossibleRoutes(startStationId) {
console.log("These are the data for the station:", stations[startStationId])
var getPossibleRouteIds = stations[startStationId].possibleRoutes;
var possibleRoutes = [];
getPossibleRouteIds.forEach(routeId => possibleRoutes.push(routes[routeId]));
return possibleRoutes;
}
function displayRouteDropdown(possibleRoutes) {
let dropdown = document.getElementById('routeDropdown');
dropdown.innerHTML = '';
possibleRoutes.forEach(route => {
let option = document.createElement('option');
option.value = route.id;
option.text = `${route.fromName} to ${route.toName} (${route.cost} Coins, ${route.points} Points)`;
dropdown.appendChild(option);
});
dropdown.style.display = 'block';
document.getElementById('claimRouteButton').style.display = 'block';
}
function claimSelectedRoute() {
let trainNumber = document.getElementById('trainNumberInput').value;
if (!trainNumber) {
alert("Please enter the train number.");
return;
}
document.getElementById('claimSection').style.display = 'none';
document.getElementById('claimInitiation').style.display = 'block';
document.getElementById('claimStationDropdown').style.display = 'none';
document.getElementById('selectStationButton').style.display = 'none';
document.getElementById('startStation').style.display = 'none';
document.getElementById('startClaimButton').style.display = 'none'
let routeId = document.getElementById('routeDropdown').value;
var startStationId = document.getElementById('startStation').dataset.stationId;
let route = routes[routeId];
gameStatus[currentTeamId].latestAction = "Route claimed"
google.script.run
.withSuccessHandler(updateDisplay)
.withFailureHandler(handleError)
.claimRoute(currentTeamId, route,startStationId, trainNumber);
}
//Challenge related functions
function initiateChallenge() {
getUserLocation()
.then(location => {
const nearestStation = findNearestStation(location.lat, location.lon, stations);
if (nearestStation) {
setChallengeLocation(nearestStation);
} else {
displayStationDropdown("challenge");
}
})
.catch(error => {
console.error("Error getting user location:", error);
displayStationDropdown("challenge");
});
}
function selectChallengeLocation() {
let stationId = document.getElementById('challengeStationDropdown').value;
setChallengeLocation(stations[stationId]);
}
function setChallengeLocationDiv(station){
let challengeLocationElement = document.getElementById('challengeLocation');
let locationText = `${station.name} (${station.canton}, ${station.altitude} m)`
challengeLocationElement.innerHTML = `The challenge will take place in ${locationText}`;
challengeLocationElement.dataset.stationId = station.id;
document.getElementById('challengeLocationFinish').innerHTML = `You are doing a challenge in ${locationText}.`
}
function setChallengeLocation(station){
setChallengeLocationDiv(station);
document.getElementById('challengeLocation').style.display = 'block';
document.getElementById('drawChallengeButton').style.display = 'block';
}
function drawChallenges(){
let stationId = document.getElementById('challengeLocation').dataset.stationId
let station = stations[stationId]
document.getElementById('ticketSection').style.display = 'none';
document.getElementById('claimInitiation').style.display = 'none';
document.getElementById('challengeSection').style.display = 'none';
document.getElementById('challengeOptionsSection').style.display = 'block';
google.script.run
.withSuccessHandler(displayChallengeOptions)
.provideChallengeOptions(currentTeamId,station);
}
function displayChallengeOptions(challenges) {
let challengeList = document.getElementById('challengeList');
challengeList.innerHTML = '';
challenges.forEach((challenge, index) => {
let challengeDiv = document.createElement('div');
//let color = ticketColors[index];
challengeDiv.innerHTML = `
<input type="radio" id="challenge${index}" onchange="toggleChallenge(${index})">
<label for="challenge${index}">${challenge.title} (${challenge.region}, ${challenge.reward} Coins)</label>
`;
challengeList.appendChild(challengeDiv);
selectedChallenges[index] = challenge;
});
}
function toggleChallenge(index) {
selectedChallenges[index].selected = document.getElementById(`challenge${index}`).checked;
}
function confirmChallengeSelection() {
let stationId = document.getElementById('challengeLocation').dataset.stationId
let station = stations[stationId]
let selectedChallengeData = selectedChallenges.filter(challenge => challenge.selected);
console.log("Selected Challenges: ",selectedChallengeData)
if (selectedChallengeData.length < 1){
alert ("Please select a challenge.")
return;
}
updateChallengeCompletionSection(selectedChallengeData[0],station)
google.script.run.challengeSelection(currentTeamId, selectedChallengeData[0], station)
}
function cancelChallengeSelection() {
document.getElementById('challengeOptionsSection').style.display = 'none';
document.getElementById('ticketSection').style.display = 'block';
document.getElementById('claimInitiation').style.display = 'block';
document.getElementById('challengeSection').style.display = 'block';
document.getElementById('challengeStationDropdown').style.display = 'none';
document.getElementById('selectLocationButton').style.display = 'none';
document.getElementById('challengeLocation').style.display = 'none';
document.getElementById('drawChallengeButton').style.display = 'none';
google.script.run
.withSuccessHandler(updateDisplay)
.cancelChallengeSelection(currentTeamId);
}
function updateChallengeCompletionSection(selectedChallenge,station){
document.getElementById('challengeCompletionSection').style.display = 'block';
document.getElementById('challengeOptionsSection').style.display = 'none';
document.getElementById('challengeSection').style.display = 'none';
challengeText = document.getElementById('challengeText');
challengeLocationDiv = document.getElementById('challengeLocationCompletion')
challengeText.innerHTML = `
<p><b>${selectedChallenge.title}</b> (${selectedChallenge.region}) for ${selectedChallenge.reward} Coins:<br> ${selectedChallenge.text}<br><br></p>
`;
if(selectedChallenge.region == "local"){
challengeLocationDiv.innerHTML =`
<p> You have to complete the challenge in the vicinity of the following station: ${station.name} (${station.canton}).</p>
`
challengeLocationDiv.style.display = 'block'
document.getElementById('claimInitiation').style.display = 'none';
document.getElementById('ticketSection').style.display = 'none';
}
else if(selectedChallenge.region == "cantonal"){
console.log("The ongoing challenge is cantonal")
if(gameStatus[currentTeamId].latestAction !== "Claim Initiated"){
console.log("There is no ongoing claim")
document.getElementById('claimInitiation').style.display = 'block';
document.getElementById('startStation').style.display = 'none';
document.getElementById('startClaimButton').style.display = 'none'
document.getElementById('claimSection').style.display = 'none';
document.getElementById('ticketSection').style.display = 'none';
}
else {
console.log("There is an ongoing claim")
document.getElementById('claimSection').style.display = 'block';
document.getElementById('claimInitiation').style.display = 'none';
setStartStationDisplay(stations[gameStatus[currentTeamId].location]);
}
}
}
function confirmChallengeCompletion(){
google.script.run
.withSuccessHandler(updateDisplay)
.confirmChallengeCompletion(currentTeamId);
document.getElementById('challengeOptionsSection').style.display = 'none';
document.getElementById('ticketSection').style.display = 'block';
document.getElementById('claimInitiation').style.display = 'block';
document.getElementById('challengeSection').style.display = 'block';
document.getElementById('challengeCompletionSection').style.display = 'none';
document.getElementById('challengeStationDropdown').style.display = 'none';
document.getElementById('selectLocationButton').style.display = 'none';
document.getElementById('challengeLocation').style.display = 'none';
document.getElementById('drawChallengeButton').style.display = 'none';
}
// Display related functions
function updateDisplay(data) {
console.log("Received data in updateDisplay:", data);
if (!data || typeof data !== 'object') {
console.error("Invalid data received in updateDisplay");
}
else{
stations = data.stations;
routes = data.routes;
gameStatus = data.gameStatus;
}
if(data.message){
message = data.message;
if (message === "Route claimed") {
alert("This route has been already claimed. Please enter a valid route or abort the claim.");
return;
}
else if (message === "Route too expensive") {
alert("You don't have enough coins to claim this route. Please enter a cheaper route or abort the claim.");
return;
}
}
// Update map
if (map) {
updateMap(routes);
updateTicketDisplay()
} else {
console.warn("Map not initialized, skipping route update");
}
if(gameStatus){
// Update game status display
updateGameStatusDisplay();
// Check if there's an ongoing claim
var currentTeamStatus = gameStatus[currentTeamId];
console.log("Current Team ID:", currentTeamId, "Current Team Status:", currentTeamStatus);
if(currentTeamStatus.ongoingChallenge === "Y"){
google.script.run
.withSuccessHandler(function(response){
let stationId = response[0];
let challengeData = response[1];
updateChallengeCompletionSection(challengeData[0],stations[stationId])
console.log("ChallengeCompletion Section loaded ")
})
.recreateChallengeOptions(currentTeamId)
document.getElementById('ticketOptionsSection').style.display = 'none';
document.getElementById('ticketSection').style.display = 'none';
}
else if (currentTeamStatus.latestAction === "Claim Initiated") {
document.getElementById('claimSection').style.display = 'block';
document.getElementById('claimInitiation').style.display = 'none';
document.getElementById('challengeSection').style.display = 'none';
document.getElementById('ticketOptionsSection').style.display = 'none';
document.getElementById('ticketSection').style.display = 'none';
setStartStationDisplay(stations[currentTeamStatus.location]);
}
else if (currentTeamStatus.latestAction === "Drew tickets"){
document.getElementById('ticketSection').style.display = 'none';
document.getElementById('claimInitiation').style.display = 'none';
document.getElementById('challengeSection').style.display = 'none';
document.getElementById('ticketOptionsSection').style.display = 'block';
google.script.run.withSuccessHandler(displayTicketOptions).recreateTicketOptions(currentTeamId);
}
else if (currentTeamStatus.latestAction === "Drew challenges"){
document.getElementById('ticketSection').style.display = 'none';
document.getElementById('claimInitiation').style.display = 'none';
document.getElementById('challengeSection').style.display = 'none';
document.getElementById('challengeOptionsSection').style.display = 'block';
google.script.run
.withSuccessHandler(function(response){
let stationId = response[0];
let challengeData = response[1];
setChallengeLocationDiv(stations[stationId])
displayChallengeOptions(challengeData)
})
.recreateChallengeOptions(currentTeamId);
}
else {
document.getElementById('claimSection').style.display = 'none';
document.getElementById('ticketOptionsSection').style.display = 'none';
document.getElementById('claimInitiation').style.display = 'block';
document.getElementById('claimStationDropdown').style.display = 'none';
document.getElementById('selectStationButton').style.display = 'none';
document.getElementById('startStation').style.display = 'none';
document.getElementById('startClaimButton').style.display = 'none'
document.getElementById('ticketSection').style.display = 'block';
document.getElementById('challengeSection').style.display = 'block';
document.getElementById('challengeLocation').style.display ='none';
document.getElementById('challengeOptionsSection').style.display = 'none';
document.getElementById('challengeCompletionSection').style.display = 'none';
if(currentTeamStatus.location.length === 3){
setStartStationDiv(currentTeamStatus.location)
}
else if(currentTeamStatus.location.length > 0){
google.script.run
.withSuccessHandler(function(response){
let stationId = response[0];
setStartStationDiv(stationId)
})
.recreateChallengeOptions(currentTeamId)
}
}
}
}
function updateGameStatusDisplay() {
var statusDiv = document.getElementById('gameStatus');
var ticketDiv = document.getElementById('availableTickets');
ticketDiv.innerHTML = `
<p>Tickets left in the stack: ${data.numberAvailableTickets}</p>
`
statusDiv.innerHTML = '';
for (var teamId in gameStatus) {
var team = gameStatus[teamId];
if(team.challenge !== ''){
if(globalChallenges.length>0){
var challengeTitle = globalChallenges[team.challenge].title
}
else {var challengeTitle = ''}
}
else{
var challengeTitle = "No challenge completed yet"
}
if(team.location !== ''){
if(team.location.length === 2){
let index = team.location+"1"
var location = stations[index].canton
}
else if (team.location.length === 1){
if(team.location==="D"){
var location = "Deutschland"
}
else if(team.location==="I"){
var location = "Italia"
}
else if(team.location==="F"){
var location = "France"
}
else if(team.location==="L"){
var location = "Liechtenstein"
}
}
else{
var location = stations[team.location].name +" ("+stations[team.location].canton+")";
}
}
else{
var location ='unknown';
}
statusDiv.innerHTML += `
<h3>${teamId}</h3>
<p>Coins: ${team.coins}</p>
<p>Points: ${team.points}</p>
<p>Tickets: ${team.numberOfTickets}</p>
<p>Latest Action: ${team.latestAction}</p>
<p>Latest known location: ${location}</p>
`;
}
}
function updateTicketDisplay() {
google.script.run
.withSuccessHandler(displayTeamTickets)
.getTeamTickets(currentTeamId);
}
function displayTeamTickets(tickets) {
teamTickets = tickets;
let teamTicketsDiv = document.getElementById('teamTickets');
teamTicketsDiv.innerHTML = '<h3>Your Tickets:</h3>';
tickets.forEach((ticket, index) => {
let color = ticketColors[index % ticketColors.length]; // Cycle through colors if more than 5 tickets
teamTicketsDiv.innerHTML += `<p>${index + 1}.: ${ticket.from.name} (${ticket.from.canton}) to ${ticket.to.name} (${ticket.to.canton}) (${ticket.points} points)</p>`;
highlightTicketOnMap(ticket, color,index);
});
}
function highlightTicketOnMap(ticket,color,index) {
let startMarker = L.marker([ticket.from.lat, ticket.from.lon], {
//color: color,
fillColor: color,
//fillOpacity: 0.8,
//radius: 8
}).addTo(map);
startMarker.bindPopup(`Ticket ${index + 1} Start: ${ticket.from.name}`);
let endMarker = L.marker([ticket.to.lat, ticket.to.lon], {
//color: color,
fillColor: color,
//fillOpacity: 0.8,
//radius: 8
}).addTo(map);
endMarker.bindPopup(`Ticket ${index + 1} End: ${ticket.to.name}`);
ticketMarkers.push(startMarker, endMarker);
}
function startPolling() {