-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.coffee
executable file
·1594 lines (1541 loc) · 60.5 KB
/
client.coffee
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
Db = require 'db'
Time = require 'time'
Dom = require 'dom'
Modal = require 'modal'
Obs = require 'obs'
Plugin = require 'plugin'
Page = require 'page'
Server = require 'server'
Ui = require 'ui'
CSS = require 'css'
Geoloc = require 'geoloc'
Form = require 'form'
Icon = require 'icon'
Toast = require 'toast'
Event = require 'event'
# Get config values, access with 'Config.<property>' (check 'config.common.coffee')
CommonConfig = require 'config'
Config = CommonConfig.getConfig()
window.redraw = Obs.create(0) # For full redraw
window.indicationArrowRedraw = Obs.create(0) # For indication arrow redraw
window.checkinLocationFunctionId = undefined
# ========== Events ==========
# Main function, called when plugin is started
exports.render = ->
log 'FULL RENDER 3'
Obs.onClean ->
log 'FULL CLEAN'
window.inRangeCheckinCount[Plugin.groupCode()] = 0
#Server.send 'log', Plugin.userId(), Plugin.agent().android
version = Plugin.agent().android
if version?
if version <2.4
Dom.text 'Sorry this game is unavailable for android version 2.3 or lower'
return 0
if not (window.inRangeCheckinRunning?)
window.inRangeCheckinRunning = {}
if not (inRangeCheckinRunning[Plugin.groupCode()]?)
window.inRangeCheckinRunning[Plugin.groupCode()] = false
if not (window.inRangeCheckinCount?)
window.inRangeCheckinCount = {}
if not (window.inRangeCheckinCount[Plugin.groupCode()]?)
window.inRangeCheckinCount[Plugin.groupCode()] = 0
window.restoreMapLocation = true
log 'restoreMapLocation='+restoreMapLocation
#Server.call 'log', Plugin.userId(), "FULL RENDER"
loadOpenStreetMap()
Obs.observe ->
# Check if cleanup from last game is required
local = Db.local.peek 'gameNumber'
remote = Db.shared.get 'gameNumber'
log 'Game cleanup checked, local=', local, ', remote=', remote
if !(local?) || local != remote
log ' Cleanup performed'
Db.local.set 'gameNumber', remote
# Do cleanup stuff
Db.local.remove 'currentSetupPage'
stopLocationSending()
# Ask for location
if !Geoloc.isSubscribed()
Geoloc.subscribe()
# gamestate check
Obs.observe ->
mainElement = document.getElementsByTagName("main")[0]
mainElement.setAttribute 'id', 'main'
window.inRangeCheckinCount[Plugin.groupCode()] = 0 # Player is active, reset count
redraw.get();
gameState = Db.shared.get('gameState')
if gameState is 0 # Setting up game by user that added plugin
setupContent()
Page.setTitle !->
Dom.text 'Conquest setup'
stopLocationSending()
else if gameState is 1 # Game is running
# Set page title
page = Page.state.get(0)
page = "main" if (not page? or page == '')
# Display the correct page
if page == 'main'
mainContent()
Obs.observe ->
nEnd = Db.shared.get('game', 'newEndTime')
end = Db.shared.get('game', 'endTime')
if nEnd? and nEnd isnt 0
end = nEnd
# Make the subtitle black if the game is within 1 hour of ending
#log 'title end='+end+', nEdn='+nEnd
Page.setTitle !->
if (end - Plugin.time()) < 3600
Dom.text "Conquest ends "
Time.deltaText end
Dom.text "!"
else
Dom.text "Conquest ends "
Time.deltaText end
else if page == 'scores'
scoresContent()
Page.setTitle !->
Dom.text 'Conquest ranking'
else if page == 'log'
logContent()
Page.setTitle !->
Dom.text 'Conquest game events'
else if gameState is 2 # Game ended
page = Page.state.get(0)
page = "main" if (not page? or page == '')
if page == 'main'
endGameContent()
Page.setTitle !->
Dom.div !->
if Db.shared.peek( 'game', 'firstTeam') is getTeamOfUser(Plugin.userId())
Dom.text "Your team won!"
else
Dom.text "Your team lost!"
else if page == 'scores'
scoresContent()
Page.setTitle !->
Dom.text 'Conquest ranking'
else if page == 'log'
logContent()
Page.setTitle !->
Dom.text 'Conquest game events'
stopLocationSending()
Obs.observe ->
deviceId = Db.local.peek 'deviceId'
if not deviceId?
result = Obs.create(null)
Server.send 'getNewDeviceId', Plugin.userId(), result.func()
Obs.observe ->
if result.get()?
log 'Got deviceId ' + result.get()
Db.local.set 'deviceId', result.get()
else
log 'Did not get deviceId from server'
# Render info page (gear/info icon in top bar)
exports.renderInfo = !->
window.inRangeCheckinCount[Plugin.groupCode()] = 0 # Player is active, reset count
Dom.text "On the main map there are several beacons. You need to venture to the real location of a beacon to conquer it. "
Dom.text "When you get in range of the beacon, you'll automatically start to conquer it. "
Dom.text "When the bar at the top of your screen has been filled with your team color, you've conquered the beacon. "
Dom.text "A neutral beacon will take 30 seconds to conquer, but an occupied beacon will take one minute. You first need to drain the opponents' color, before you can fill it with yours! "
Dom.br()
Dom.br()
Dom.h2 "Rewards and winning"
Dom.text "You gain "+Config.beaconValueInitial+" "
if Config.beaconValueInitial==1 then Dom.text "point" else Dom.text "points"
Dom.text " for being the first team to conquer a certain beacon. "
Dom.text "Beacons that are in possession of your team, will have a circle around it in your team color. "
Dom.text "Every hour the beacon is in your posession, it will generate "+Config.beaconHoldScore+" "
if Config.beaconHoldScore==1 then Dom.text "point" else Dom.text "points"
Dom.text ". "
Dom.text "Unfortunately for you, your beacons can be conquered by other teams. "
Dom.text "Every time a beacon is conquered the value of the beacon will drop. Scores for conquering a beacon will decrease with "+Config.beaconValueDecrease+" until a minimum of "+Config.beaconValueMinimum+". "
Dom.text "The team with the highest score at the end of the game wins. "
Dom.text "If a team captures all beacons, the game will end quickly if the other teams stay inactive. "
Dom.h2 "Bugs and help"
Dom.text "Did you find a bug in the plugin? Do you have a question about the plugin that you cannot find the answer for? Contact 'thijs17' and we will try to answer you. If you are familiar with GitHub then you can also report the bug on our GitHub repository: "
Dom.a "https://github.com/VincentSmit/cappening/issues" # TODO: check how to get this link clickable
Dom.text "."
# Method that is called when admin changes settings (only restart game for now)
exports.renderSettings = !->
if Db.shared
Form.check
name: 'restart'
text: tr 'Restart'
sub: tr 'Check this to destroy the current game and start a new one.'
# ========== Content fuctions ==========
#Method that renders bar on top of page.
addBar = ->
Dom.div !->
Dom.style
height: "50px"
width: '100%'
top: '0'
left: '0'
position: 'absolute'
boxShadow: "0 3px 10px 0 rgba(0, 0, 0, 0.3)"
backgroundColor: hexToRGBA(Db.shared.peek('colors', getTeamOfUser(Plugin.userId()), 'hex'), 0.9)
_textShadow: '0 0 5px #000000, 0 0 5px #000000'
# Button to event log
Dom.div !->
Icon.render data: 'clipboard', color: '#fff', size: 30, style: {verticalAlign: 'middle'}
Dom.div !->
Dom.text 'Events'
Dom.style verticalAlign: 'middle', display: 'inline-block', marginLeft: '5px', fontSize: '13px'
Event.renderBubble ['log']
Dom.cls 'bar-button'
Dom.onTap !->
Page.nav 'log'
# Button to scores page
Dom.div !->
Dom.div !->
Dom.style
width: '29px'
height: '30px'
verticalAlign: 'middle'
display: 'inline-block'
background: "url(#{Plugin.resourceUri('ranking.png')})"
backgroundRepeat: "no-repeat"
backgroundPosition: "0 0"
backgroundSize: "26px 26px"
Dom.div !->
Dom.text 'Ranking'
Dom.style verticalAlign: 'middle', display: 'inline-block', marginLeft: '5px', fontSize: '13px'
Dom.cls 'bar-button'
Dom.onTap !->
Page.nav 'scores'
#Renders the progress bar when a capture is happening
addProgressBar = ->
Obs.observe ->
Db.shared.iterate 'game', 'beacons', (beacon) !->
action = beacon.get('action') # Subscribe to changes in action, only thing that matters
inRangeValue = beacon.peek('inRange', Plugin.userId(), 'device')
if inRangeValue? and (inRangeValue == 'true' || inRangeValue == Db.local.get('deviceId'))
log 'Rendering progress bar'
Obs.onClean ->
log 'Cleaned progress bar...'
dbPercentage = beacon.peek("percentage")
nextPercentage = -1
ownTeam = getTeamOfUser(Plugin.userId())
nextColor = ""
owner = beacon.peek('owner')
nextOwner = beacon.peek('nextOwner')
actionStarted = beacon.peek("actionStarted")
barText = ""
if action == "capture"
nextPercentage=100
dbPercentage += (new Date() /1000 -actionStarted)/30 * 100
if dbPercentage > 100
dbPercentage = 100
if dbPercentage < 0
dbPercentage = 0
nextColor = Db.shared.peek('colors', nextOwner, 'hex')
barText = "Capturing..."
else if action == "recapture"
nextPercentage=100
dbPercentage += (new Date() /1000 -actionStarted)/30 * 100
if dbPercentage > 100
dbPercentage = 100
if dbPercentage < 0
dbPercentage = 0
nextColor = Db.shared.peek('colors', owner, 'hex')
if parseInt(ownTeam) is parseInt(owner)
barText = "Recapturing..."
else
barText = "Enemy is recapturing..."
else if action == "neutralize"
nextPercentage=0
dbPercentage -= (new Date() /1000 -actionStarted)/30 * 100
if dbPercentage < 0
dbPercentage = 0
if dbPercentage > 100
dbPercentage = 100
nextColor = Db.shared.peek('colors', owner, 'hex')
barText = "Neutralizing..."
else if action == "competing"
if owner == -1
nextColor = Db.shared.peek('colors', nextOwner, 'hex')
else
nextColor = Db.shared.peek('colors', owner, 'hex')
fromOtherTeams = 0
log 'fromOtherTeams='+fromOtherTeams+', nextPercentage='+nextPercentage+', dbPercentage='+dbPercentage
nextPercentage = dbPercentage
beacon.iterate 'inRange', (player) ->
if getTeamOfUser(player.key()) isnt ownTeam
fromOtherTeams++
if dbPercentage == 100 and owner == nextOwner
if parseInt(ownTeam) is parseInt(owner)
if fromOtherTeams > 1
barText = "Captured, but an enemy in range!"
else
barText = "Captured, but enemies in range!"
else
if fromOtherTeams > 1
barText = "Enemies are blocking the neutralize!"
else
barText = "An enemy is blocking the neutralize!"
else
if parseInt(owner) isnt -1
if parseInt(ownTeam) is parseInt(owner)
if fromOtherTeams > 1
barText = "Preventing neutralize by the enemies!"
else
barText = "Preventing neutralize by an enemy!"
else
if fromOtherTeams > 1
barText = "Enemies are preventing the neutralize!"
else
barText = "An enemy is preventing the neutralize!"
else
if parseInt(ownTeam) is parseInt(nextOwner)
if fromOtherTeams > 1
barText = "Enemies are preventing the capture!"
else
barText = "An enemy is preventing the capture!"
else
if fromOtherTeams > 1
barText = "Preventing capture by the enemies!"
else
barText = "Preventing capture by an enemy!"
else
nextPercentage = dbPercentage
if owner == -1
nextColor = Db.shared.peek('colors', nextOwner, 'hex')
else
nextColor = Db.shared.peek('colors', owner, 'hex')
barText = "Captured"
time = 0
if nextPercentage != dbPercentage
time = Math.abs(dbPercentage-nextPercentage) * 300
log "nextPercentage = ", nextPercentage, ", dbPercentage = ", dbPercentage, ", time = ", time, ", action = ", action, "actionStarted=", actionStarted
Dom.div !->
Dom.style
height: "25px"
width: "100%"
position: 'absolute'
left: '0'
top: '50px'
boxShadow: 'rgba(0, 0, 0, 0.6) 0px 2px 6px 0px'
backgroundColor: 'rgba(0, 0, 0, 0.3)'
border: '0'
Dom.div !->
Dom.style
height: "25px"
#background: '-moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.3) 100%)'
#background: '-webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,0.3)))'
#background: '-webkit-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.3) 100%)'
#background: '-o-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.3) 100%)'
#background: '-ms-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.3) 100%)'
background_: 'linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.3) 100%)'
filter: "progid:DXImageTransform.Microsoft.gradient( startColorstr='#00000000', endColorstr='#4d000000',GradientType=0 )"
backgroundColor: nextColor
zIndex: "10"
Dom._get().style.width = dbPercentage + "%"
log "dbPercentage after balancing = ", dbPercentage
Dom._get().style.transition = "width " + time + "ms linear"
window.progressElement = Dom._get()
timer = () -> window.progressElement.style.width = nextPercentage + "%"
window.setTimeout(timer, 100)
Dom.div !->
Dom.text barText
Dom.style
width: '100%'
color: 'white'
marginTop: '-22px'
textAlign: 'center'
fontSize: '15px'
_textShadow: '0 0 5px #000000, 0 0 5px #000000' # Double for extra visibility
#Renders bar when game is ended. Displays the winner and admin can restart the game
addEndGameBar = ->
Dom.div !->
Dom.cls 'endGameBar'
Dom.style
backgroundColor: hexToRGBA(Db.shared.peek('colors', Db.shared.peek('game', 'firstTeam'), 'hex'), 0.9)
if parseInt(Db.shared.peek('game', 'firstTeam')) is parseInt(getTeamOfUser(Plugin.userId()))
Dom.text "Your team won the game!"
else
Dom.text "Team " + Db.shared.peek('colors', Db.shared.peek('game', 'firstTeam'), 'name') + " won, good luck next round!"
if Plugin.userIsAdmin() or Plugin.ownerId() is Plugin.userId()
Dom.style paddingRight: '145px'
Dom.div !->
Dom.cls 'restartButton'
Dom.text "RESTART GAME"
Dom.onTap !->
Server.call 'restartGame'
# ========== Page Contents ==========
# Setup pages
setupContent = ->
if Plugin.userIsAdmin() or Plugin.ownerId() is Plugin.userId()
currentPage = Db.local.get('currentSetupPage')
currentPage = 'setup0' if not currentPage?
log ' currentPage =', currentPage
if currentPage is 'setup0' # Setup team and round time
# Variables
numberOfTeams = Obs.create Db.shared.peek('game', 'numberOfTeams')
roundTimeNumber = Obs.create Db.shared.peek('game', 'roundTimeNumber')
roundTimeUnit = Obs.create Db.shared.peek('game', 'roundTimeUnit')
# Bar to indicate the setup progress
Dom.div !->
Dom.cls 'stepbar'
# Left button
Dom.div !->
Dom.text tr("Prev")
Dom.cls 'stepbar-button'
Dom.cls 'stepbar-left'
Dom.cls 'stepbar-disable'
# Middle block
Dom.div !->
Dom.text tr("Basic settings")
Dom.cls 'stepbar-middle'
# Right button
Dom.div !->
Dom.text tr("Next")
Dom.cls 'stepbar-button'
Dom.cls 'stepbar-right'
Dom.onTap !->
Server.send 'setTeams', numberOfTeams.get()
Server.send 'setRoundTime', roundTimeNumber.get(), roundTimeUnit.get()
Db.local.set('currentSetupPage', 'setup1')
Dom.div !->
Dom.style paddingTop: '50px'
# Not enough players warning
if Plugin.users.count().get() <= 1
Dom.h2 "Warning"
Dom.text "Be sure to invite some friends if you actually want to play the game. One cannot play alone! (You can test it out though)."
# Teams input
Dom.h2 tr("Select the number of teams")
Dom.div !->
Dom.cls "team-text"
Dom.div !->
log 'numberOfTeams.get(): ', numberOfTeams.get()
Dom.style margin: '0 0 20px 0', height: '50px'
renderTeamButton = (number) ->
Dom.div !->
Dom.text number
Dom.cls "team-button"
if numberOfTeams.peek() is number
Dom.cls "team-button-current"
else
Dom.onTap !->
numberOfTeams.set number
userCount = Math.min(Plugin.users.count().get(),6)
for number in [2..Math.max(userCount,2)]
renderTeamButton number
# Duration input
Dom.h2 tr("Round time")
Dom.text tr "Select the round time, recommended: 7 days."
Dom.div !->
Dom.style margin: '10px 0 10px 0', height: '81px'
sanitize = (value) ->
if value < 1
return 1
else if value > 999
return 999
else
return value
renderArrow = (direction) !->
Dom.div !->
Dom.style
width: 0
height: 0
margin: '0 auto'
borderStyle: "solid"
borderWidth: "#{if direction>0 then 0 else 20}px 20px #{if direction>0 then 20 else 0}px 20px"
borderColor: "#{if roundTimeNumber.get()<=1 then 'transparent' else '#0077cf'} transparent #{if roundTimeNumber.get()>=999 then 'transparent' else '#0077cf'} transparent"
if (direction>0 and roundTimeNumber.get()<999) or (direction<0 and roundTimeNumber.get()>1)
Dom.onTap !->
roundTimeNumber.set sanitize(roundTimeNumber.peek()+direction)
# Number input
Dom.div !->
Dom.style margin: '0 10px 0 0', width: '51px', float: 'left'
renderArrow 1
Dom.input !->
inputElement = Dom.get()
Dom.prop
size: 2
value: roundTimeNumber.get()
Dom.style
fontFamily: 'monospace'
fontSize: '30px'
fontWeight: 'bold'
textAlign: 'center'
border: 'inherit'
backgroundColor: 'inherit'
color: 'inherit'
Dom.on 'input change', !-> roundTimeNumber.set sanitize(inputElement.value())
Dom.on 'click', !-> inputElement.select()
renderArrow -1
# Unit inputs
Dom.div !->
Dom.style float: 'left', paddingTop: '13px'
renderTimeButton = (unit) ->
Dom.div !->
Dom.text unit
Dom.cls "time-button"
if roundTimeUnit.get() is unit
Dom.cls "time-button-current"
else
Dom.onTap !->
roundTimeUnit.set unit
renderTimeButton 'Hours'
renderTimeButton 'Days'
renderTimeButton 'Months'
# Gameinfo setup page:
Dom.div !->
Dom.h2 "Game information"
Dom.text "For game information, click the gear or information icon on the top of the page"
else if currentPage is 'setup1' # Setup map boundaries
# Bar to indicate the setup progress
Dom.div !->
Dom.cls 'stepbar'
# Left button
Dom.div !->
Dom.text tr("Prev")
Dom.cls 'stepbar-button'
Dom.cls 'stepbar-left'
Dom.onTap !->
Db.local.set('currentSetupPage', 'setup0')
# Middle block
Dom.div !->
Dom.text tr("Select gamearea")
Dom.cls 'stepbar-middle'
# Right button
Dom.div !->
Dom.text tr("Next")
Dom.cls 'stepbar-button'
Dom.cls 'stepbar-right'
Dom.onTap !->
Db.local.set('currentSetupPage', 'setup2')
renderMap()
renderBeacons()
Obs.observe ->
if mapReady()
# Setup map corners
# Update the play area square thing
markerDragged = ->
if mapReady()
Server.sync 'setBounds', window.locationOne.getLatLng(), window.locationTwo.getLatLng(), !->
log 'predicting bounds change'
Db.shared.set 'game', 'bounds', {one: {lat: window.locationOne.getLatLng().lat, lng: window.locationOne.getLatLng().lng}, two: {lat: window.locationTwo.getLatLng().lat, lng: window.locationTwo.getLatLng().lng}}
log 'predicted bounds: ', {one: {lat: window.locationOne.getLatLng().lat, lng: window.locationOne.getLatLng().lng}, two: {lat: window.locationTwo.getLatLng().lat, lng: window.locationTwo.getLatLng().lng}}
checkAllBeacons()
# Corner 1
lat1 = Db.shared.get('game', 'bounds', 'one', 'lat')
lng1 = Db.shared.get('game', 'bounds', 'one', 'lng')
if not lat1? or not lng1?
lat1 = 52.249822176849
lng1 = 6.8396973609924
loc1 = L.latLng(lat1, lng1)
window.locationOne = L.marker(loc1, {draggable: true})
locationOne.on 'dragend', ->
log 'marker drag 1'
markerDragged()
locationOne.addTo(map)
# Corner 2
lat2 = Db.shared.get('game', 'bounds', 'two', 'lat')
lng2 = Db.shared.get('game', 'bounds', 'two', 'lng')
if not lat2? or not lng2?
lat2 = 52.236578295702
lng2 = 6.8598246574402
loc2 = L.latLng(lat2, lng2)
window.locationTwo = L.marker(loc2, {draggable: true})
locationTwo.on 'dragend', ->
log 'marker drag 2'
markerDragged()
locationTwo.addTo(map)
window.boundaryRectangle = L.rectangle([loc1, loc2], {color: "#ff7800", weight: 5, clickable: false})
boundaryRectangle.addTo(map)
Obs.onClean ->
log 'onClean() rectangle + corners'
if mapReady()
map.removeLayer locationOne if locationOne?
map.removeLayer locationTwo if locationTwo?
map.removeLayer boundaryRectangle if boundaryRectangle?
# Info bar
Dom.div !->
Dom.cls 'infobar'
Dom.div !->
Dom.style
float: 'left'
marginRight: '10px'
width: '30px'
_flexGrow: '0'
_flexShrink: '0'
Icon.render data: 'info', color: '#fff', style: { paddingRight: '10px'}, size: 30
Dom.div !->
Dom.style
_flexGrow: '1'
_flexShrink: '1'
Dom.text "Drag the corners of the orange rectangle to define the gamearea. Click here for more information."
Dom.onTap !->
Modal.show tr("Gamearea setup information"), !->
Dom.div !->
Dom.text "Drag the corners of the orange rectangle to define the gamearea."
#Dom.text " Choose this area wisely, this is where the map will be limited to during the game."
Dom.br()
Dom.br()
Dom.text "Your own location is drawn on the map as a pushpin."
Dom.style maxWidth: '300px', textAlign: 'left'
, (ok)->
ok= undefined;
,['ok', tr("Ok")]
else if currentPage is 'setup2' # Setup beacons
# Bar to indicate the setup progress
Dom.div !->
Dom.cls 'stepbar'
# Left button
Dom.div !->
Dom.text tr("Prev")
Dom.cls 'stepbar-button'
Dom.cls 'stepbar-left'
Dom.onTap !->
Db.local.set('currentSetupPage', 'setup1')
# Middle block
Dom.div !->
Dom.text tr("Place beacons")
Dom.cls 'stepbar-middle'
# Right button
Dom.div !->
Dom.text tr("Start")
Dom.cls 'stepbar-button'
Dom.cls 'stepbar-right'
log 'setup2 new'
if Db.shared.count('game', 'beacons').get() >=1
Dom.onTap !->
Server.send 'startGame'
else
Dom.cls 'stepbar-disable'
renderMap()
renderBeacons()
Obs.observe ->
if mapReady()
loc1 = L.latLng(Db.shared.get('game', 'bounds', 'one', 'lat'), Db.shared.get('game', 'bounds', 'one', 'lng'))
loc2 = L.latLng(Db.shared.get('game', 'bounds', 'two', 'lat'), Db.shared.get('game', 'bounds', 'two', 'lng'))
if loc1? and loc2
log loc1 + " " + loc2
window.boundaryRectangle = L.rectangle([loc1, loc2], {color: "#ff7800", weight: 5, fillOpacity: 0.05, clickable: false})
boundaryRectangle.addTo(map)
map.on('contextmenu', addMarkerListener)
Obs.onClean ->
log 'onClean() rectangle'
if mapReady()
map.removeLayer boundaryRectangle if boundaryRectangle?
map.off('contextmenu', addMarkerListener)
# Info bar
Dom.div !->
Dom.cls 'infobar'
Dom.div !->
Dom.style
float: 'left'
marginRight: '10px'
width: '30px'
_flexGrow: '0'
_flexShrink: '0'
Icon.render data: 'info', color: '#fff', size: 30
Dom.div !->
Dom.style
_flexGrow: '1'
_flexShrink: '1'
if Plugin.agent().android? or Plugin.agent().ios?
Dom.text "Tap-and-hold"
else
Dom.text "Right-click"
Dom.text " to place beacon on the map, "
if Plugin.agent().android? or Plugin.agent().ios?
Dom.text "tap-and-hold"
else
Dom.text "right-click"
Dom.text " a beacon to delete it. Click here for more information."
Dom.onTap !->
Modal.show tr("Beacon setup information"), !->
Dom.div !->
if Plugin.agent().android? or Plugin.agent().ios?
Dom.text "Tap-and-hold"
else
Dom.text "Right-click"
Dom.text " to place beacon on the map. The circle indicates the capture area for a beacon, players will have to walk to that area to capture the beacon. "
if Plugin.agent().android? or Plugin.agent().ios?
Dom.text "Tap-and-hold"
else
Dom.text "Right-click"
Dom.text " a beacon to delete it. Be sure to place beacons in places you and other members of this Happening visit often."
Dom.br()
Dom.br()
Dom.text "It is recommended to place at least 10 beacons, depending on how many members this Happening has."
Dom.style maxWidth: '300px', textAlign: 'left'
, (ok)->
ok= undefined;
,['ok', tr("Ok")]
else
renderMap()
renderBeacons()
Dom.div !->
Dom.cls 'infobar'
Dom.style top: '0', bottom: 'auto'
Dom.div !->
Dom.style
float: 'left'
marginRight: '10px'
width: '30px'
_flexGrow: '0'
_flexShrink: '0'
Icon.render data: 'info', color: '#fff', style: { paddingRight: '10px'}, size: 30
Dom.div !->
Dom.style
_flexGrow: '1'
_flexShrink: '1'
Dom.text "The admin/plugin owner is setting up a new game."
# Home page with map
mainContent = ->
log "mainContent()"
addBar()
addProgressBar()
renderMap()
renderBeacons()
#Tutorial for playing this game the first time
userId = Plugin.userId()
tutorial = Db.personal.peek('tutorialState', 'mainContent')
if undefined is tutorial
Server.send 'updateTutorialState', userId, 'mainContent'
Modal.show tr("Are you ready to capture your first beacon?"), !->
Dom.div tr("Walk towards the indicated area's on the map to capture a beacon.")
Dom.div tr("You will be awarded points for capturing a beacon, and for holding it.")
Dom.div tr("Tip: Capture all beacons with your team and win in one hour!")
, (ok)->
ok= undefined;
,['ok', tr("Got it")]
# Scores page
scoresContent = ->
Ui.list !->
Dom.style
padding: '0'
Db.shared.iterate 'game', 'teams', (team) !->
teamColor = Db.shared.peek('colors', team.key(), 'hex')
teamName = Db.shared.peek('colors', team.key(), 'name')
teamScore = Db.shared.get('game', 'teams', team.key(), 'teamScore')
# list of teams and their scores
expanded = Obs.create(false)
Ui.item !->
Dom.style
padding: '14px'
minHeight: '71px'
alignItems: 'stretch'
Dom.div !->
Dom.style
width: '70px'
height: '70px'
background: teamColor
backgroundSize: 'cover'
position: 'absolute'
_textShadow: '0 0 3px rgba(0,0,0,0.8)'
Dom.div !->
rank = team.get('ranking')
Dom.style
fontSize: "40px"
paddingTop: "12px"
textAlign: "center"
color: "white"
paddingRight: if rank==1 then '10px' else '15px'
Dom.text rank
rankingSuffix = {1: "st", 2: "nd", 3: "rd", 4: "th", 5: "th", 6: "th"}
Dom.div !->
Dom.text rankingSuffix[rank]
Dom.style
position: 'absolute'
fontSize: '17px'
left: if rank==1 then '38px' else '40px'
top: '15px'
Dom.div !->
Dom.style Flex: 1, fontSize: '100%', paddingLeft: '84px'
Dom.text "Team " + teamName + " scored " + teamScore + " points"
if parseInt(team.key()) is parseInt(getTeamOfUser(Plugin.userId()))
Dom.style fontWeight: 'bold'
#log 'users: ', Plugin.users, ', length: ', Plugin.users.count().peek()
# To Do expand voor scores
if expanded.get() || Plugin.users.count().peek() <= 10
team.iterate 'users', (user) !->
Dom.div !->
if parseInt(user.key()) isnt Plugin.userId()
Dom.style fontWeight: 'normal'
Dom.style clear: 'both'
Ui.avatar Plugin.userAvatar(user.key()),
style: margin: '6px 10px 0 0', float: 'left'
size: 40
onTap: (!-> Plugin.userInfo(user.key()))
Dom.div !->
Dom.br()
Dom.style fontSize: '75%', marginTop: '6px', marginRight: '6px', display: 'block', float: 'left', minWidth: '75px'
Dom.text Plugin.userName(user.key()) + " has: "
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px', display: 'block', float: 'left'
Dom.text user.get('userScore') + " points"
Dom.br()
Dom.text user.get('captured') + " captured"
Dom.br()
Dom.text user.get('neutralized') + " neutralized"
, (user) -> (-user.get('userScore'))
else
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px'
Dom.text "Tap for details"
if Plugin.users.count().peek() > 10
Dom.onTap !->
expanded.set(!expanded.get())
, (team) -> [team.get('ranking')]
# Eventlog page
logContent = ->
Event.showStar tr('Game events')
Ui.list !->
Dom.style
padding: '0'
Db.shared.iterate 'game', 'eventlist', (capture) !->
if capture.key() != "maxId"
#log 'capture'
Ui.item !->
Dom.style
padding: '14px'
if capture.peek('type') is "capture" and mapReady()
beaconId = capture.peek('beacon')
teamId = capture.peek('conqueror')
teamColor = Db.shared.peek('colors', teamId, 'hex')
teamName = Db.shared.peek('colors', teamId, 'name')
Dom.onTap !->
viewSet = ->
map.setView(L.latLng(Db.shared.peek('game', 'beacons' ,beaconId, 'location', 'lat'), Db.shared.peek('game', 'beacons' ,beaconId, 'location', 'lng')), 16)
setTimeout(viewSet, 50)
Toast.show !->
Dom.text 'Captured '
Time.deltaText(capture.peek('timestamp'))
if capture.peek('members')?
Dom.text " by " + userStringToFriendly(capture.peek('members')) + ' of team ' + teamName
else
Dom.text ' by team '+teamName
Page.back()
Dom.div !->
Dom.style
width: '70px'
height: '70px'
marginRight: '10px'
background: teamColor+" url(#{Plugin.resourceUri('marker-plain.png')}) no-repeat 10px 10px"
backgroundSize: '50px 50px'
Dom.div !->
Dom.style Flex: 1, fontSize: '100%'
if Event.isNew(capture.peek('timestamp'))
Dom.style color: '#5b0'
if capture.peek('members')?
Dom.text userStringToFriendly(capture.peek('members')) + ' of team ' + teamName + ' captured a beacon'
else
Dom.text "Team " + teamName + " captured a beacon"
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px'
Dom.text "Captured "
Time.deltaText capture.peek('timestamp')
else if capture.peek('type') is "captureAll"
beaconId = capture.peek('beacon')
teamId = capture.peek('conqueror')
teamColor = Db.shared.peek('colors', teamId, 'hex')
teamName = Db.shared.peek('colors', teamId, 'name')
log "print capture: teamId; " + teamId
Dom.onTap !->
viewSet = ->
map.setView(L.latLng(Db.shared.peek('game', 'beacons', beaconId, 'location', 'lat'), Db.shared.peek('game', 'beacons', beaconId, 'location', 'lng')), 16)
setTimeout(viewSet, 50)
Toast.show !->
Dom.text 'Captured '
Time.deltaText(capture.peek('timestamp'))
if capture.peek('members')?
Dom.text " by " + userStringToFriendly(capture.peek('members')) + ' of team ' + teamName
else
Dom.text ' by team '+teamName
Page.back()
Dom.div !->
Dom.style
width: '70px'
height: '70px'
marginRight: '10px'
background: teamColor+" url(#{Plugin.resourceUri('markers-plain.png')}) no-repeat 10px 10px"
backgroundSize: '50px 50px'
Dom.div !->
Dom.style Flex: 1, fontSize: '100%'
if Event.isNew(capture.peek('timestamp'))
Dom.style color: '#5b0'
Dom.text "Team " + teamName + " team captured all beacons"
if capture.peek('members')?
Dom.text " thanks to " + userStringToFriendly(capture.peek('members'))
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px'
Dom.text "Captured "
Time.deltaText capture.peek('timestamp')
else if capture.peek('type') is "score"
teamId = capture.peek('leading')
teamColor = Db.shared.peek('colors', teamId, 'hex')
teamName = Db.shared.peek('colors', teamId, 'name')
#log "print score: teamId; " + teamId
Dom.onTap !->
Page.nav 'scores'
Dom.div !->
Dom.style
width: '70px'
height: '70px'
marginRight: '10px'
background: teamColor+" url(#{Plugin.resourceUri('rank-switch.png')}) no-repeat 10px 10px"
backgroundSize: '50px 50px'
Dom.div !->
Dom.style Flex: 1, fontSize: '100%'
if Event.isNew(capture.peek('timestamp'))
Dom.style color: '#5b0'
Dom.text "Team " + teamName + " took the lead"
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px'
Dom.text "Captured "
Time.deltaText capture.peek('timestamp')
else if capture.peek('type') is "cancel"
Dom.style
padding: '14px'
Dom.div !->
Dom.style
width: '70px'
height: '70px'
marginRight: '10px'
background: "#DDDDDD url(#{Plugin.resourceUri('markers-cancel-plain.png')}) no-repeat 10px 10px"
backgroundSize: '50px 50px'
Dom.div !->
Dom.style Flex: 1, fontSize: '16px'
if Event.isNew(capture.peek('timestamp'))
Dom.style color: '#5b0'
Dom.text "No longer are all beacons owned by one team"
started = Db.shared.peek 'game', 'startTime'
if started?
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px'
Dom.text 'Happened '
Time.deltaText started
else if capture.peek('type') is "start"
Dom.style
padding: '14px'
Dom.div !->
Dom.style
width: '70px'
height: '55px'
marginRight: '10px'
background: '#DDDDDD'
backgroundSize: 'cover'
paddingTop: '15px'
Dom.div !->
Dom.style
margin: '0 0 0 20px'
borderLeft: '34px solid #FFFFFF'
borderTop: '20px solid transparent'
borderBottom: '20px solid transparent'
Dom.div !->
Dom.style Flex: 1, fontSize: '16px'
if Event.isNew(capture.peek('timestamp'))
Dom.style color: '#5b0'
Dom.text "Start of the game!"
started = Db.shared.peek 'game', 'startTime'
if started?
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px'
Dom.text 'Started '
Time.deltaText started
else if capture.peek('type') is "end"
teamId = Db.shared.peek('game', 'firstTeam')
teamColor = Db.shared.peek('colors', teamId, 'hex')
teamName = Db.shared.peek('colors', teamId, 'name')
Dom.style
padding: '14px'
Dom.div !->
Dom.style
width: '70px'
height: '70px'
marginRight: '10px'
background: teamColor + " url(#{Plugin.resourceUri('ranking-plain.png')}) no-repeat 10px 10px"
backgroundSize: '50px 50px'
Dom.div !->
Dom.style Flex: 1, fontSize: '16px'
if Event.isNew(capture.peek('timestamp'))
Dom.style color: '#5b0'
Dom.text "Team " + teamName + " won the game"
started = Db.shared.peek 'game', 'startTime'
if started?
Dom.div !->
Dom.style fontSize: '75%', marginTop: '6px'
Dom.text 'Started '
Time.deltaText started
, (capture) -> (-capture.key())
# End game main content page
endGameContent = ->
log "endGameContent()"
addBar()
renderMap()
renderBeacons()
#mModal.show("Team " + Db.shared.peek('colors', Db.shared.peek('game', 'winningTeam'), 'name') + " won the game!")
addEndGameBar()
# ========== Map functions ==========
# Render a map
renderMap = ->
log " renderMap()"
# Insert map element
Obs.observe ->
if mapElement?
# use it again