-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1097 lines (902 loc) · 35.4 KB
/
server.js
File metadata and controls
1097 lines (902 loc) · 35.4 KB
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
let directory = __dirname
let assets = {
blue: "../Assets/CrescendoBlueField.png",
red: "../Assets/CrescendoRedField.png",
/*blue: "../Assets/blueField.png",
red: "../Assets/redField.png",*/
blueAlt: "../Assets/blueField_alt.png",
redAlt: "../Assets/redField_alt.png"
}
let field = {}
let grid = {}
let timesheet = {}
let competition = {}
let superCharged = {
"blue": 0,
"red": 0,
}
const express = require('express')
const bodyParser = require("body-parser")
const cookieParser = require("cookie-parser")
const session = require("express-session")
const http = require("http")
const socketio = require("socket.io")
const path = require("path")
const gp = require('./Server/gamePieces')
const ut = require('./Server/utility.js')
const fw = require('./Server/fileWriter')
const ref = require('./Server/referee')
const app = express()
const httpserver = http.Server(app)
const io = socketio(httpserver)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
const sessionMiddleware = session({
secret: "54119105",
saveUninitialized: false,
//cookie: { maxAge: 3600000 },
resave: false
})
app.use(sessionMiddleware)
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cookieParser())
express.static(path.join(__dirname, "node_modules/bootstrap/dist/js"));
app.use(
"/css",
express.static(path.join(__dirname, "node_modules/bootstrap/dist/css"))
)
app.use(express.static(__dirname + "/Rooms"))
app.post('/scoutdata', (request, response) =>
{
let scoutData = fw.getScoutData()
response.json(scoutData)
})
app.post("/signin", (request, response) =>
{
let message, notification
let body = request.body
let user = new gp.User(body.username, body.password)
if (user.isBlank())
{
message = "Please choose a scouter."
notification = ut.notification(message)
response.send(notification)
}
else if (user.hasName("admin") && !user.hasPassword("password"))
{
message = "That password is incorrect"
notification = ut.notification(message)
response.send(notification)
}
else if (match.hasConnectedScouter(user.name))
{
message = "Sorry, but somebody already joined under that name."
notification = ut.notification(message)
response.send(notification)
}
else if (user.hasName("admin"))
{
request.session.authenticated = true
request.session.scout = "admin"
match.connectAdmin()
response.redirect('/admin')
}
else if (match.getGamePlay(fw.getAllianceColor(user.name)).isFull())
{
message = "Sorry, but the session you are trying to join is full."
notification = ut.notification(message)
response.send(notification)
}
else if (match.inSession() && !(timesheet.hasScouter(user.name)))
{
message = "Sorry, but you are not scheduled for this match."
notification = ut.notification(message)
response.send(notification)
}
else if (match.inSession() && fw.getAllianceColor(user.name) && match.hasAdmin())
{
request.session.authenticated = true
request.session.scout = user.name
request.session.allianceColor = fw.getAllianceColor(user.name)
response.redirect('/' + request.session.allianceColor)
}
else if (!match.hasAdmin())
{
message = "The admin has not joined yet, please be patient"
notification = ut.notification(message)
response.send(notification)
}
else if (!match.inSession())
{
message = "The admin has not started the match yet, please be patient."
notification = ut.notification(message)
response.send(notification)
}
else
{
message = "Sorry, but that name was not found on the scouter list."
notification = ut.notification(message)
response.send(notification)
}
})
app.post('/schedule/blue', (request, response) =>
{
let schedule = timesheet.getSchedule("blue")
let table = new ut.JsonTable(schedule)
response.json(table.json())
})
app.post('/schedule/red', (request, response) =>
{
let schedule = timesheet.getSchedule("red")
let table = new ut.JsonTable(schedule)
response.json(table.json())
})
app.post('/ondeck/blue', (request, response) =>
{
let lineup = timesheet.getTimeTable("blue").getCurrentMatchLineUp()
let table = new ut.JsonTable(lineup)
response.json(table.json())
})
app.post('/ondeck/red', (request, response) =>
{
let lineup = timesheet.getTimeTable("red").getCurrentMatchLineUp()
let table = new ut.JsonTable(lineup)
response.json(table.json())
})
app.post('/logout', (request, response) =>
{
request.session.destroy()
response.redirect('/lobby')
})
app.get('/game', (request, response) =>
{
let file = path.join(directory, 'Rooms/index.html')
response.sendFile(file)
})
app.get('/blue', (request, response) =>
{
let file = path.join(directory, 'Rooms/blue/index.html')
response.sendFile(file)
})
app.get('/red', (request, response) =>
{
let file = path.join(directory, 'Rooms/red/red.html')
response.sendFile(file)
})
app.get('/admin', (request, response) =>
{
let file = path.join(directory, 'Rooms/admin/admin.html')
response.sendFile(file)
})
app.get('/lobby', (request, response) =>
{
let file = path.join(directory, 'Rooms/lobby/lobby.html')
response.sendFile(file)
})
app.get('*', (request, response) =>
{
response.redirect('/lobby')
})
let match = {}
let score = {}
const gameStates = ["pregame", "auton", "teleop"]
const wrap = middleware =>
(socket, next) =>
middleware(socket.request, {}, next)
io.use(wrap(sessionMiddleware))
io.use((socket, next) =>
{
const session = socket.request.session;
if (session && session.authenticated)
{
next()
}
else
{
console.log("unauthorized user joined")
}
})
initGame()
io.on('connection', connected);
function connected(socket) {
const session = socket.request.session
let allianceGamePlay
let team
if (session.allianceColor)
{
allianceGamePlay = match.getGamePlay(session.allianceColor)
team = allianceGamePlay.getTeamByScout(session.scout)
}
//console.log(session)
//console.log("session id: " + socket.request.session.id + "\n")
//console.log("scout name: " + socket.request.session.scout + "\n")
socket.on('newScouter', data => {
socket.leaveAll()
socket.join(session.allianceColor)
console.log("New client connected, with id (yeah): " + socket.id)
if ((team) && !team.hasTeamNumber())
{
let gameplay = match.getGamePlay(team.allianceColor)
let position = gameplay.getIdx()
let schedule = competition.getTimeTable(team.allianceColor)
let teamNumber = schedule.getCurrentLineUpPosition(position)
team.setTeamNumber(teamNumber)
match.getGamePlay(team.allianceColor).increment()
if(team.allianceColor == 'red') //
{
let lineup = timesheet.getTimeTable("red").getCurrentLineUp()
let index = lineup.indexOf(team.scout) + 4
team.setIdx(index)
}
else
{
let lineup = timesheet.getTimeTable("blue").getCurrentLineUp()
let index = lineup.indexOf(team.scout) + 1
team.setIdx(index)
}
//team.setIdx(timesheet.getTimeTable(team.allianceColor).getCurrentLineUp().indexOf(team.scout) + num)
}
team.connect()
//team.gameState[allianceGamePlay.gameState] = new gp.GameState()
team.setGameState(allianceGamePlay.gameState, new gp.GameState())
//arena object
socket.emit('rotate', field[team.allianceColor].getRotation())
socket.emit('drawfield', field[team.allianceColor].getDimensions(), grid[team.allianceColor].getDimensions())
socket.emit('AssignRobot', team)
io.to('admin').emit('AssignRobot', team)
//to do: fix this so that it only renders game markers on the newly joined client instead of re-drawing everyone's fields
io.to(team.allianceColor).emit('clear')
io.to(team.allianceColor).emit('draw', allianceGamePlay.getPreGameMarkers())
io.to(team.allianceColor).emit('draw', allianceGamePlay.getAutonMarkers())
io.to(team.allianceColor).emit('draw', allianceGamePlay.getTeleOpMarkers())
})
socket.on('setMatch', matchNumber =>
{
match.getGamePlay("blue").clearIdx()
match.getGamePlay("red").clearIdx()
match.setMatchNumber(matchNumber)
timesheet.setMatchNumber(matchNumber)
competition.setMatchNumber(matchNumber)
match.open()
if (fw.fileExists(("match" + matchNumber)))
{
io.to('admin').emit('confirm')
}
else
{
fw.addNewGame("match" + match.matchNumber)
}
let lineup = {
blue: timesheet.getTimeTable("blue").getCurrentLineUp(),
red: timesheet.getTimeTable("red").getCurrentLineUp()
} //
socket.emit('setScouters', lineup.blue, lineup.red) //
})
socket.on('start', () =>
{
console.log("match " + match.matchNumber + " is starting")
match.start()
})
socket.on('newAdmin', data =>
{
let table = {
"blue": {},
"red": {}
}
socket.leaveAll()
socket.join("admin")
//console.log(fw.getMatchData())
let compLength = (Object.keys(fw.getMatchData())).length//.at(1)
//console.log("complength: " + compLength)
io.to('admin').emit('compLength', compLength)
for (team of match.gamePlay.blue.teams)
{
if (team.isConnected())
{
io.to('admin').emit('AssignRobot', team)
}
}
for (team of match.gamePlay.red.teams)
{
if (team.isConnected())
{
io.to('admin').emit('AssignRobot', team)
}
}
io.to('admin').emit('rotate', field.blue.getRotation())
io.to('admin').emit('drawfield', 'blue', field.blue.getDimensions(), grid.blue.getDimensions())
io.to('admin').emit('drawfield', 'red', field.red.getDimensions(), grid.red.getDimensions())
io.to('admin').emit('draw', 'blue', match.gamePlay.blue.getPreGameMarkers())
io.to('admin').emit('draw', 'blue', match.gamePlay.blue.getAutonMarkers())
io.to('admin').emit('draw', 'blue', match.gamePlay.blue.getTeleOpMarkers())
io.to('admin').emit('draw', 'red', match.gamePlay.red.getPreGameMarkers())
io.to('admin').emit('draw', 'red', match.gamePlay.red.getAutonMarkers())
io.to('admin').emit('draw', 'red', match.gamePlay.red.getTeleOpMarkers())
table.blue = new ut.DynamicJsonTable(timesheet.getSchedule("blue"), "blue-table")
table.red = new ut.DynamicJsonTable(timesheet.getSchedule("red"), "red-table")
io.to('admin').emit('schedule', table.blue.json(), table.red.json())
table.blue = new ut.DynamicJsonTable(competition.getSchedule("blue"), "blue-match-table")
table.red = new ut.DynamicJsonTable(competition.getSchedule("red"), "red-match-table")
io.to('admin').emit('teams', table.blue.json(), table.red.json())
})
socket.on('flip', () =>
{
let style = {}
if (field.blue.isFlipped())
{
field.blue.flip()
field.red.flip()
field.blue.rotate("0deg")
field.red.rotate("0deg")
field.blue.bg = assets.blue
field.red.bg = assets.red
style.direction = "row"
style.alignment = "flex-start"
style.order = "1"
}
else
{
field.blue.flip()
field.red.flip()
field.blue.rotate("180deg")
field.red.rotate("180deg")
field.blue.bg = assets.blueAlt
field.red.bg = assets.redAlt
style.direction = "row-reverse"
style.alignment = "flex-end"
style.order = "-1"
}
io.to('admin').emit('restyle', style)
io.to('admin').emit('rotate', field.blue.getRotation())
io.to('blue').emit('rotate', field.blue.getRotation())
io.to('red').emit('rotate', field.red.getRotation())
io.to('admin').emit('drawfield', 'blue', field.blue.getDimensions(), grid.blue.getDimensions())
io.to('admin').emit('drawfield', 'red', field.red.getDimensions(), grid.red.getDimensions())
io.to('blue').emit('drawfield', field.blue.getDimensions(), grid.blue.getDimensions())
io.to('red').emit('drawfield', field.red.getDimensions(), grid.red.getDimensions())
})
socket.on('saveSchedule', (color, schedule) =>
{
timesheet.getTimeTable(color).setSchedule(schedule)
})
socket.on('saveMatch', (blueMatches, redMatches) =>
{
competition.getTimeTable("blue").setSchedule(blueMatches)
competition.getTimeTable("red").setSchedule(redMatches)
})
//super charged node increasing/decreasing
socket.on('inc', (color) =>
{
let autonScore
let teleopScore
console.log(superCharged[color])
if (superCharged[color] >= 0)
{
superCharged[color]++
}
if (team.gameState['auton'])
{
autonScore = team.gameState['auton']
team.autonScore = autonScore;
}
if (team.gameState['teleop'])
{
teleopScore = team.gameState['teleop']
team.teleopScore = teleopScore;
}
score.UpdateMarkers(
match.gamePlay["blue"].ReturnTeleOpMarkers(),
match.gamePlay["red"].ReturnTeleOpMarkers(),
match.gamePlay["blue"].ReturnAutonMarkers(),
match.gamePlay["red"].ReturnAutonMarkers(),
team.teamNumber,
team,
superCharged["red"],
superCharged["blue"]
)
let ScoreBoard = {
alliance: team.allianceColor,
totalScore: score.GetBoard(),
team: team,
autonScore: autonScore,
teleopScore: teleopScore,
startTime: match.startTime
}
io.to(team.allianceColor).emit('scoreboard', ScoreBoard)
io.to('admin').emit('scoreboard', ScoreBoard, team.scout)
})
socket.on('dec', (color) =>
{
let autonScore
let teleopScore
console.log(superCharged[color])
if (superCharged[color] > 0)
{
superCharged[color]--
}
if (team.gameState['auton'])
{
autonScore = team.gameState['auton']
team.autonScore = autonScore;
}
if (team.gameState['teleop'])
{
teleopScore = team.gameState['teleop']
team.teleopScore = teleopScore;
}
score.UpdateMarkers(
match.gamePlay["blue"].ReturnTeleOpMarkers(),
match.gamePlay["red"].ReturnTeleOpMarkers(),
match.gamePlay["blue"].ReturnAutonMarkers(),
match.gamePlay["red"].ReturnAutonMarkers(),
team.teamNumber,
team,
superCharged["red"],
superCharged["blue"]
)
let ScoreBoard = {
totalScore: score.GetBoard(),
team: team,
autonScore: autonScore,
teleopScore: teleopScore,
startTime: match.startTime
}
io.to(team.allianceColor).emit('scoreboard', ScoreBoard)
io.to('admin').emit('scoreboard', ScoreBoard, team.scout)
})
/** DRAW MARKER HELPERS */
function isValidLocation(markerType)
{
if(markerType == '')
return false;
return true;
}
socket.on('drawMarker', (allianceColor, data) => {
if (session.scout == "admin")
{
allianceGamePlay = match.getGamePlay(allianceColor)
team = allianceGamePlay.getTeamByScout(session.scout)
}
if (!team.gameState[allianceGamePlay.gameState])
{
team.gameState[allianceGamePlay.gameState] = new gp.GameState()
//team.setGameState(allianceGamePlay.gameState, new gp.GameState())
}
// ignore markers before game starts
if (allianceGamePlay.isPreGame() && session.scout != "admin")
return;
team.markerColor.alpha = allianceGamePlay.gameStateIndicator()
let drawMarker = new gp.Markers(data.x, data.y)
let markerType = allianceGamePlay.setMarkerType(
drawMarker,
team.getGameState(allianceGamePlay.gameState).parkingState,
allianceGamePlay.gameState
) // this returns the marker type but also set details about the marker
let markerId = drawMarker.getCoordinates()
if (markerType == '')
return; // marker is out of bounds
// check the location of the marker to see if it is a valid placement for the gamestate
//let markerPlacement = allianceGamePlay.playingField.getFieldLocation(drawMarker)
// the team is already mobile so skip the new marker
if (team.isMobile() && markerType == 'Mobility')
{
return;
}
// check to see if the marker does not already exist
// if isMarkedOnce is false then we want to allow it to click multiple times
let isMarkerPresent = false;
if (drawMarker.isMarkedOnce == "true" && (allianceGamePlay.getMarker(markerId)))
isMarkerPresent = true;
if (isMarkerPresent == false)
{
drawMarker.setMarkerColor(
team.markerColor.red,
team.markerColor.green,
team.markerColor.blue,
allianceGamePlay.gameStateIndicator()
)
drawMarker.setGameState(allianceGamePlay.gameState)
drawMarker.setTeamNumber(team.teamNumber)
//drawMarker.setType(allianceGamePlay.GetMarkerType(markerId, team.gameState[allianceGamePlay.gameState].parkingState, allianceGamePlay.gameState)) //
/*drawMarker.setType(
markerType
)*/
// don't draw markers during pregame
if(allianceGamePlay.isPreGame() && session.scout == "admin")
{
allianceGamePlay.addPreGameMarker(drawMarker, markerId)
io.to(team.allianceColor).emit('placeMarker', drawMarker)
io.to('admin').emit('placeMarker', team.allianceColor, drawMarker)
}
else
{
switch(drawMarker.markerType){
case 'Amplifier':
if (drawMarker.markerLocationType == 'AmplifierUndo' && allianceGamePlay.amplifierCounter > 0) // undo the last marker placed
{
allianceGamePlay.deleteMarker(drawMarker.markerType + allianceGamePlay.amplifierCounter)
allianceGamePlay.amplifierCounter--;
}
else if (drawMarker.markerLocationType == 'AmplifierUndo' && allianceGamePlay.amplifierCounter == 0)
return; // there is no marker to remove
else
{
allianceGamePlay.amplifierCounter++;
allianceGamePlay.addMarker(drawMarker, drawMarker.markerType + allianceGamePlay.amplifierCounter)
}
break;
case 'Speaker':
if (drawMarker.markerLocationType == 'SpeakerUndo' && allianceGamePlay.speakerCounter > 0) // undo the last marker placed
{
allianceGamePlay.deleteMarker(drawMarker.markerType + allianceGamePlay.speakerCounter)
allianceGamePlay.speakerCounter--;
}
else if (drawMarker.markerLocationType == 'SpeakerUndo' && allianceGamePlay.speakerCounter == 0)
return; // there is no marker to remove
else
{
allianceGamePlay.speakerCounter++;
allianceGamePlay.addMarker(drawMarker, drawMarker.markerType + allianceGamePlay.speakerCounter)
}
break;
case 'Amplified':
if (drawMarker.markerLocationType == 'AmplifiedUndo' && allianceGamePlay.amplifiedCounter > 0) // undo the last marker placed
{
allianceGamePlay.deleteMarker(drawMarker.markerType + allianceGamePlay.amplifiedCounter)
allianceGamePlay.amplifiedCounter--;
}
else if (drawMarker.markerLocationType == 'AmplifiedUndo' && allianceGamePlay.amplifiedCounter == 0)
return; // there is no marker to remove
else
{
allianceGamePlay.amplifiedCounter++;
allianceGamePlay.addMarker(drawMarker, drawMarker.markerType + allianceGamePlay.amplifiedCounter)
}
break;
case 'Pass':
allianceGamePlay.passCounter++;
allianceGamePlay.addMarker(drawMarker, drawMarker.markerType + allianceGamePlay.passCounter)
break;
default:
allianceGamePlay.addMarker(drawMarker, markerId)
break;
}
/*if(drawMarker.markerType == 'Amplifier')
{
allianceGamePlay.amplifierCounter++;
allianceGamePlay.addMarker(drawMarker, markerType + allianceGamePlay.amplifierCounter)
}
else if (drawMarker.markerType == 'Speaker')
{
allianceGamePlay.speakerCounter++;
allianceGamePlay.addMarker(drawMarker, markerType + allianceGamePlay.speakerCounter)
}
else if (drawMarker.markerType == 'Amplified')
{
allianceGamePlay.amplifiedCounter++;
allianceGamePlay.addMarker(drawMarker, markerType + allianceGamePlay.amplifiedCounter)
}
else
allianceGamePlay.addMarker(drawMarker, markerId)
*/
drawMarker.createTimeStamp(match.startTime)
if (drawMarker.isMobile())
{
team.mobilize()
}
else if (drawMarker.isParked()) // set the marker as parked
{
team.getGameState(allianceGamePlay.gameState).park();
}
io.to(team.allianceColor).emit('placeMarker', drawMarker)
io.to('admin').emit('placeMarker', team.allianceColor, drawMarker)
}
}
/*else if (allianceGamePlay.clickedChargingStation(markerId) && !(team.isEngaged()) && (allianceGamePlay.getMarker(markerId).hasTeamNumber(team.teamNumber)))
{
allianceGamePlay.chargingStation.engage()
team.engage()
drawMarker = allianceGamePlay.getMarker(markerId)
drawMarker.setMarkerColor(
team.markerColor.red,
team.markerColor.green,
team.markerColor.blue,
allianceGamePlay.gameStateIndicator() * 2
)
drawMarker.setGameState(allianceGamePlay.gameState)
drawMarker.setTeamNumber(team.teamNumber)
//drawMarker.setType(allianceGamePlay.GetMarkerType(markerId, team.gameState[allianceGamePlay.gameState].parkingState)) //
drawMarker.setType(
allianceGamePlay.setMarkerType(
markerId,
team.getGameState(allianceGamePlay.gameState).
parkingState
)
)
drawMarker.createTimeStamp(match.startTime)
io.to(team.allianceColor).emit('placeMarker', drawMarker)
io.to('admin').emit('placeMarker', team.allianceColor, drawMarker)
} */
else if (allianceGamePlay.getMarker(markerId).hasTeamNumber(team.teamNumber))
{
if (allianceGamePlay.getMarker(markerId).isMobile())
{
team.immobilize()
}
else if (!allianceGamePlay.getMarker(markerId).isItem())
{
team.getGameState(allianceGamePlay.gameState).resetParking()
}
// unpark the robot if the marker is deleted
allianceGamePlay.deleteMarker(markerId)
redrawGamePieces(team.allianceColor)
}
// scoring compoentents here
try
{
allianceGamePlay.score.UpdateMarkers(
allianceGamePlay.getAutonMarkers(),
allianceGamePlay.getTeleOpMarkers(),
team
) //
}
catch (err)
{
console.log(err);
}
/* let autonScore = {}
let teleopScore = {}
if (team.gameState['auton'])
{
autonScore = team.gameState['auton'] //
team.autonScore = autonScore;
}
if (team.gameState['teleop'])
{
teleopScore = team.gameState['teleop'] //
team.teleopScore = teleopScore;
}*/
allianceGamePlay.getTeamByNumber(team.teamNumber).autonScore = team.autonScore
allianceGamePlay.getTeamByNumber(team.teamNumber).teleopScore = team.teleopScore
let ScoreBoard = {
alliance: team.allianceColor,
totalScoreBlue: match.gamePlay.blue.score.GetBoard(),
totalScoreRed: match.gamePlay.red.score.GetBoard(),
team: team,
// autonScore: autonScore,
//teleopScore: teleopScore,
startTime: match.startTime
}
io.to(team.allianceColor).emit('scoreboard', ScoreBoard)
io.to('admin').emit('scoreboard', ScoreBoard, team.scout)
// team.allianceColor.setScoreBoard(ScoreBoard)
team.gameStateScore = JSON.stringify(team.gameState);
let scoreData =
{
matchNumber: match.matchNumber,
matchTime: match.startTime,
blue: {
totalScore: match.gamePlay.blue.score.GetBoard(),
preGameMarkers: match.gamePlay.blue.getPreGameMarkers(),
autonGameMarkers: match.gamePlay.blue.getAutonMarkers(),
teleopGameMarkers: match.gamePlay.blue.getTeleOpMarkers(),
teams: match.gamePlay.blue.getActiveTeams()
},
red: {
totalScore: match.gamePlay.red.score.GetBoard(),
preGameMarkers: match.gamePlay.red.getPreGameMarkers(),
autonGameMarkers: match.gamePlay.red.getAutonMarkers(),
teleopGameMarkers: match.gamePlay.red.getTeleOpMarkers(),
teams: match.gamePlay.red.getActiveTeams()
}
}
fw.saveScoreData(scoreData)
//fw.saveScoreData(match)
})
// this tells all pieces to redraw
function redrawGamePieces(allianceColor){
io.to(allianceColor).emit('clear')
io.to('admin').emit('clear', allianceColor)
io.to(allianceColor).emit('draw', allianceGamePlay.getPreGameMarkers())
io.to(allianceColor).emit('draw', allianceGamePlay.getAutonMarkers())
io.to(allianceColor).emit('draw', allianceGamePlay.getTeleOpMarkers())
io.to('admin').emit('draw', allianceColor, allianceGamePlay.getPreGameMarkers())
io.to('admin').emit('draw', allianceColor, allianceGamePlay.getAutonMarkers())
io.to('admin').emit('draw', allianceColor, allianceGamePlay.getTeleOpMarkers())
}
//Customer function to handle amplified for the Crescendo game
function endAmplify(allianceColor){
let allianceGamePlay = match.getGamePlay(allianceColor)
allianceGamePlay.setAmplified(false); // switch back to teleop
let amplify = {
"allianceColor": allianceColor,
"amplify": "off"
};
io.to(allianceColor).emit('amplify', amplify)
io.to('admin').emit('amplify', amplify)
redrawGamePieces(allianceColor);
}
socket.on('startAmplify', (allianceColor) =>
{
let allianceGamePlay = match.getGamePlay(allianceColor)
if (allianceGamePlay.gameState == 'teleop')
{
allianceGamePlay.setAmplified(true);
let amplify = {
"allianceColor": allianceColor,
"amplify": "on"
};
io.to(allianceColor).emit('amplify', amplify)
io.to('admin').emit('amplify', amplify)
setTimeout(endAmplify, 10000, allianceColor);
}
})
socket.on('gameChange', (allianceColor, value) =>
{
allianceGamePlay = match.getGamePlay(allianceColor)
allianceGamePlay.switchGameState(gameStates, value)
//allianceGamePlay.undockAll()
//allianceGamePlay.disengageAll()
// allianceGamePlay.chargingStation.reset()
if(value === 'auton')
match.autonStart()
console.log("the game mode for " + allianceColor + " is now set to " + allianceGamePlay.gameState)
socket.emit('toggleGameMode', allianceColor)
socket.emit('returnGameState', allianceGamePlay.gameState)
})
socket.on('scoutChange', scout => //
{
let admin, scouter
if (match.gamePlay.blue.hasScouter(scout))
{
admin = match.gamePlay.blue.getTeamByScout(session.scout)
scouter = match.gamePlay.blue.getTeamByScout(scout)
}
else if (match.gamePlay.red.hasScouter(scout))
{
admin = match.gamePlay.red.getTeamByScout(session.scout)
scouter = match.gamePlay.red.getTeamByScout(scout)
}
admin.setIdx(scouter.idx)
admin.setTeamNumber(scouter.teamNumber)
admin.setMarkerColor(scouter.markerColor)
})
socket.on('adminChange', () => //
{
match.gamePlay.blue.getTeamByScout(session.scout).reset()
match.gamePlay.red.getTeamByScout(session.scout).reset()
match.gamePlay.blue.getTeamByScout(session.scout).setMarkerColors(25, 25, 25, 0.5)
match.gamePlay.red.getTeamByScout(session.scout).setMarkerColors(25, 25, 25, 0.5)
})
socket.on('endMatch', () => //
{
match.gamePlay.blue.clearGameStates()
match.gamePlay.red.clearGameStates()
match.gamePlay.blue.deleteMarkers()
match.gamePlay.red.deleteMarkers()
/*match.gamePlay.blue.undockAll()
match.gamePlay.red.undockAll()
match.gamePlay.blue.disengageAll()
match.gamePlay.red.disengageAll()
match.gamePlay.blue.chargingStation.reset()
match.gamePlay.red.chargingStation.reset()*/
match.gamePlay.blue.resetTeams()
match.gamePlay.red.resetTeams()
match.reset()
io.to('blue').emit('gameOver')
io.to('red').emit('gameOver')
io.to('blue').emit('clear')
io.to('red').emit('clear')
io.to('admin').emit('clear', 'blue')
io.to('admin').emit('clear', 'red')
})
socket.on('disconnect', () =>
{
console.log("Goodbye client with id " + socket.id);
if (session.scout == "admin")
{
match.disconnectAdmin()
//match.reset()
// ^this completely halts the match if the admin disconnects. haven't seen a need to use it yet though, but uncomment it if necessary
// note that resetting the match upon admin disconnection messes with data collection if a match is still in session
}
else
{
team.disconnect()
}
io.to('admin').emit('disconnected', team)
})
socket.on('gameState', allianceColor =>
{
allianceGamePlay = match.getGamePlay(allianceColor)
socket.emit('returnGameState', allianceGamePlay.gameState)
})
}
function initGame() //
{
timesheet = new gp.TimeSheet(fw.parseBreaks())
const data = fw.getScoutData()
score = new ref.ScoreLive()
match = new gp.Match()
match.gamePlay.blue = new gp.GamePlay()
match.gamePlay.red = new gp.GamePlay()
match.gamePlay.blue.score = new ref.ScoreLive();
match.gamePlay.red.score = new ref.ScoreLive();