/
sevenwondersduel.js
8036 lines (6770 loc) · 419 KB
/
sevenwondersduel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
/**
*------
* BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
* SevenWondersDuel implementation : © Koen Heltzel <koenheltzel@gmail.com>
*
* This code has been produced on the BGA studio platform for use on http://boardgamearena.com.
* See http://en.boardgamearena.com/#!doc/Studio for more information.
* -----
*
* sevenwondersduel.js
*
* SevenWondersDuel user interface script
*
* In this file, you are describing the logic of your user interface, in Javascript language.
*
*/
define([
"dojo",
"dojo/_base/declare",
"dojo/query",
"dojo/NodeList-traverse",
"dojo/on",
"dojo/dom",
"dojo/cookie",
"ebg/core/gamegui",
"ebg/counter",
g_gamethemeurl + "modules/js/CoinAnimator.js",
g_gamethemeurl + "modules/js/CubeAnimator.js",
g_gamethemeurl + "modules/js/MilitaryTrackAnimator.js",
],
function (dojo, declare, on, dom, cookie) {
return declare("bgagame.sevenwondersduel", ebg.core.gamegui, {
instance: null,
LAYOUT_AUTO: 'auto',
LAYOUT_LANDSCAPE: 'landscape',
LAYOUT_SQUARE: 'square',
LAYOUT_PORTRAIT: 'portrait',
// Show console.log messages
debug: 0,
pantheon: 0,
agora: 0,
expansion: 0,
// Settings
autoScale: 1,
scale: 1,
autoLayout: 1,
layout: "",
quality: "1x",
showOpponentCost: 1,
cookieExpireDays: 60,
// Freezes layout during disabled card selection (remembering old scroll position)
freezeLayout: 0,
rememberScrollX: 0,
rememberScrollY: 0,
// Tooltip settings
toolTipDelay: 200,
// Game logic properties
playerTurnBuildingId: null,
playerTurnNode: null,
currentAge: 0,
myConspiracies: [],
// Pantheon
activateDivinityNode: null,
activateDivinityId: 0,
activateDivinitySpace: 0,
activateDivinityOfferingTokenIds: [],
chooseDivinityFromDeckAmount: 0,
chooseDivinityFromDeckId: 0,
// Agora
senateActionsSection: 0,
moveInfluenceFrom: 0,
moveDecreeFrom: 0,
swapMeBuildingId: 0,
swapOpponentBuildingId: 0,
// General properties
customTooltips: [],
windowResizeTimeoutId: null,
autoUpdateScaleTimeoutId: null,
previousAvailableDimensions: null,
playerTurnDescription: null,
playerTurnDescriptionMyTurn: null,
// Animation durations
constructBuildingAnimationDuration: 1000,
discardBuildingAnimationDuration: 400,
constructWonderAnimationDuration: 1600,
prepareConspiracyAnimationDuration: 1600,
selectWonderAnimationDuration: 300,
progressTokenDuration: 1000,
twistCoinDuration: 250,
turnAroundCardDuration: 500,
putDraftpoolCard: 250,
coin_slide_duration: 500,
coin_slide_delay: 100,
notification_safe_margin: 100,
victorypoints_slide_duration: 1000,
conspire_duration: 1000,
construct_conspiracy_duration: 1600,
choose_conspire_remnant_position_duration: 1600,
place_divinity_duration: 1200,
activate_divinity_duration: 1000,
constructor: function () {
bgagame.sevenwondersduel.instance = this;
// Tooltip settings
// dijit.Tooltip.defaultPosition = ["above-centered", "below-centered"];
},
/*
setup:
This method must set up the game user interface according to current game situation specified
in parameters.
The method is called each time the game interface is displayed to a player, ie:
_ when the game starts
_ when a player refreshes the game page (F5)
"gamedatas" argument contains all datas retrieved by your "getAllDatas" PHP method.
*/
setup: function (gamedatas) {
if (this.debug) console.log("setup(gamedatas)", gamedatas);
if (this.debug) dojo.destroy('debug_output'); // See http://en.doc.boardgamearena.com/Tools_and_tips_of_BGA_Studio#Speed_up_game_re-loading_by_disabling_Input.2FOutput_debug_section
this.gamedatas = gamedatas;
this.agora = this.gamedatas.agora;
this.pantheon = this.gamedatas.pantheon;
this.expansion = this.agora || this.pantheon;
// Because of spectators we can't assume everywhere that this.player_id is one of the two players.
this.me_id = parseInt(this.gamedatas.me_id); // me = alias for the player on the bottom
this.opponent_id = parseInt(this.gamedatas.opponent_id); // opponent = alias for the player on the top
if (this.isCookieSetAndValid('swd_layout_v2')) {
this.layout = dojo.cookie('swd_layout_v2');
this.setLayout(this.layout);
}
else {
this.layout = this.LAYOUT_AUTO;
}
$('setting_layout').value = this.layout;
if (this.isCookieSetAndValid('swd_autoScale_v2', true)) {
this.autoScale = parseInt(dojo.cookie('swd_autoScale_v2'));
}
if (!this.autoScale) {
if (this.isCookieSetAndValid('swd_scale', true)) {
this.scale = parseFloat(dojo.cookie('swd_scale'));
this.setScale(this.scale);
}
else {
this.autoScale = 1;
}
}
if (this.isCookieSetAndValid('swd_quality_v2')) {
this.quality = dojo.cookie('swd_quality_v2');
}
if (this.isCookieSetAndValid('swd_opponent_cost', true)) {
this.showOpponentCost = parseInt(dojo.cookie('swd_opponent_cost'));
$('setting_opponent_cost').value = this.showOpponentCost;
}
this.updateSettings();
this.updateQuality(); // To update the data-quality attribute (it's important to have called updateSettings() first, so $('setting_quality').value is set correctly).
if (this.quality == '2x') {
this.dontPreloadImage('board.png');
this.dontPreloadImage('buildings_v3.jpg');
this.dontPreloadImage('linked-building-icons.png');
this.dontPreloadImage('progress_tokens_v3.jpg');
this.dontPreloadImage('sprites.png');
this.dontPreloadImage('wonders_v3.jpg');
}
if (!this.agora) {
this.dontPreloadImage('agora_senate_tooltips.png');
}
if (this.quality == '2x' || !this.agora) {
this.dontPreloadImage('agora_conspiracies.jpg');
this.dontPreloadImage('agora_decrees.png');
this.dontPreloadImage('agora_senate.png');
this.dontPreloadImage('agora_sprites.png');
}
if (this.quality == '2x' || !this.pantheon) {
this.dontPreloadImage('pantheon_sprites.png');
this.dontPreloadImage('pantheon_board.png');
this.dontPreloadImage('pantheon_divinities.png');
}
if (this.quality == '1x') {
this.dontPreloadImage('board@2X.png');
this.dontPreloadImage('buildings_v3@2X.jpg');
this.dontPreloadImage('linked-building-icons@2X.png');
this.dontPreloadImage('progress_tokens_v3@2X.jpg');
this.dontPreloadImage('sprites@2X.png');
this.dontPreloadImage('wonders_v3@2X.jpg');
}
if (this.quality == '1x' || !this.agora) {
this.dontPreloadImage('agora_conspiracies@2X.jpg');
this.dontPreloadImage('agora_decrees@2X.png');
this.dontPreloadImage('agora_senate@2X.png');
this.dontPreloadImage('agora_sprites@2X.png');
}
if (this.quality == '1x' || !this.pantheon) {
this.dontPreloadImage('pantheon_sprites@2X.png');
this.dontPreloadImage('pantheon_board@2X.png');
this.dontPreloadImage('pantheon_divinities@2X.png');
}
// Setup game situation.
this.updateWondersSituation(this.gamedatas.wondersSituation);
this.updateDraftpool(this.gamedatas.draftpool, true);
this.updateProgressTokensSituation(this.gamedatas.progressTokensSituation);
this.updateMilitaryTrack(this.gamedatas.militaryTrack);
this.updateDiscardedBuildings(this.gamedatas.discardedBuildings);
if (this.pantheon) {
this.updateMythologyTokensSituation(this.gamedatas.mythologyTokensSituation);
this.updateOfferingTokensSituation(this.gamedatas.offeringTokensSituation);
this.updateDivinitiesSituation(this.gamedatas.divinitiesSituation);
}
if (this.agora) {
this.updateDecreesSituation(this.gamedatas.decreesSituation);
this.updateConspiracyDeckCount(this.gamedatas.conspiraciesSituation['deckCount']);
this.myConspiracies = this.gamedatas.myConspiracies;
this.updateConspiraciesSituation(this.gamedatas.conspiraciesSituation);
this.updateSenateSituation(this.gamedatas.senateSituation);
}
// Setting up player boards
for (var player_id in gamedatas.players) {
var player = gamedatas.players[player_id];
var buttonNode;
if (player_id == this.me_id) {
buttonNode = $('buttonPlayerLeft');
} else {
buttonNode = $('buttonPlayerRight');
}
dojo.attr(buttonNode, 'data-player-id', player_id);
dojo.query('span', buttonNode)[0].innerHTML = player.name;
// Try (!) to get the player avatar. If we can't find the avater due to future BGA changes, remove it from the button.
var buttonImg = dojo.query('img', buttonNode)[0];
var avatar = dojo.query('#avatar_' + player_id)[0];
if (avatar) {
var avatarSrc = dojo.attr(avatar, 'src');
if (avatarSrc) {
dojo.attr(buttonImg, 'src', avatarSrc);
}
} else {
dojo.destroy(buttonImg);
}
this.updatePlayerWonders(player_id, this.gamedatas.wondersSituation[player_id]);
this.updatePlayerBuildings(player_id, this.gamedatas.playerBuildings[player_id]);
if (this.pantheon) {
// For Pantheon, only show 1 instead of 3 Progress Token outline placeholders (because we have placeholders for Mythology & Offering tokens too), so delete the last 2.
let outlineNodes = dojo.query('.player_info.' + this.getPlayerAlias(player_id) + ' .player_area_progress_tokens .progress_token_outline:empty');
dojo.destroy(outlineNodes[2]);
dojo.destroy(outlineNodes[1]);
}
this.updatePlayerProgressTokens(player_id, this.gamedatas.progressTokensSituation[player_id]);
}
// After updatePlayerProgressTokens so we can highlight the Law progress token for the endgameanimation.
this.updatePlayersSituation(this.gamedatas.playersSituation);
// Set setting dropdown values (translations don't work yet in the constructor, so we do it here).
dojo.query('#setting_layout option[value="' + this.LAYOUT_AUTO + '"]')[0].innerText = _('Auto');
dojo.query('#setting_layout option[value="' + this.LAYOUT_PORTRAIT + '"]')[0].innerText = _('Portrait');
dojo.query('#setting_layout option[value="' + this.LAYOUT_SQUARE + '"]')[0].innerText = _('Square');
dojo.query('#setting_layout option[value="' + this.LAYOUT_LANDSCAPE + '"]')[0].innerText = _('Landscape');
dojo.query('#setting_auto_scale option[value="1"]')[0].innerText = _('Fit to screen (no scrolling)');
dojo.query('#setting_auto_scale option[value="2"]')[0].innerText = _('Use full width (scroll vertically)');
dojo.query('#setting_auto_scale option[value="0"]')[0].innerText = _('Manual');
dojo.query('#setting_quality option[value="1x"]')[0].innerText = _('Normal');
dojo.query('#setting_quality option[value="2x"]')[0].innerText = _('High definition');
dojo.query('#setting_opponent_cost option[value="1"]')[0].innerText = _('Yes');
dojo.query('#setting_opponent_cost option[value="0"]')[0].innerText = _('No');
// Click handlers using event delegation:
dojo.query('body')
.on("#swd *:click",
dojo.hitch(this, "closeTooltips")
);
dojo.query('body')
.on("#swd[data-state=selectWonder] #wonder_selection_container .wonder:click",
dojo.hitch(this, "onWonderSelectionClick")
);
dojo.query('body')
.on("#swd[data-state=playerTurn] #draftpool .building.available:click",
dojo.hitch(this, "onPlayerTurnDraftpoolClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_useAgeCard] #player_wonders_" + this.me_id + " .wonder_small:click",
dojo.hitch(this, "onPlayerTurnConstructWonderSelectedClick")
);
dojo.query('body')
.on("#swd[data-state=chooseProgressToken] #board_progress_tokens .progress_token_small:click",
dojo.hitch(this, "onProgressTokenClick")
);
dojo.query('body')
.on("#swd[data-state=selectStartPlayer] #select_start_player .action_button:click",
dojo.hitch(this, "onStartPlayerClick")
);
dojo.query('body')
.on("#swd[data-state=chooseOpponentBuilding] .player_building_column.red_border .building_header_small:click",
dojo.hitch(this, "onOpponentBuildingClick")
);
dojo.query('body')
.on("#swd[data-state=chooseDiscardedBuilding] #discarded_cards_container .building_small:click",
dojo.hitch(this, "onDiscardedBuildingClick")
);
dojo.query('body')
.on("#swd[data-state=chooseProgressTokenFromBox] #progress_token_from_box_container .progress_token :click",
dojo.hitch(this, "onProgressTokenFromBoxClick")
);
dojo.query('body')
.on("#adminControls a.admin_function :click",
dojo.hitch(this, "onAdminFunctionClick")
);
// Click handlers without event delegation:
dojo.query("#buttonConstructBuilding").on("click", dojo.hitch(this, "onPlayerTurnConstructBuildingClick"));
dojo.query("#buttonDiscardBuilding").on("click", dojo.hitch(this, "onPlayerTurnDiscardBuildingClick"));
dojo.query("#buttonConstructWonder").on("click", dojo.hitch(this, "onPlayerTurnConstructWonderClick"));
dojo.query("#setting_auto_scale").on("change", dojo.hitch(this, "onSettingAutoScaleChange"));
dojo.query("#setting_scale").on("change", dojo.hitch(this, "onSettingScaleChange"));
dojo.query("#setting_layout").on("change", dojo.hitch(this, "onSettingLayoutChange"));
dojo.query("#setting_quality").on("change", dojo.hitch(this, "onSettingQualityChange"));
dojo.query("#setting_opponent_cost").on("change", dojo.hitch(this, "onSettingOpponentCostChange"));
if (this.pantheon) {
// Pantheon click handlers using event delegation:
dojo.query('body')
.on("#swd[data-state=chooseAndPlaceDivinity] #choose_and_place_divinity .divinity_small :click",
dojo.hitch(this, "onChooseDivinityClick")
);
dojo.query('body')
.on("#swd[data-state=chooseAndPlaceDivinity] .pantheon_space_containers .red_border :click",
dojo.hitch(this, "onPlaceDivinityClick")
);
dojo.query('body')
.on("#swd[data-state=playerTurn] .pantheon_space_containers .divinity.divinity_border :click",
dojo.hitch(this, "onActivateDivinityClick")
);
dojo.query('body')
.on("#swd[data-state=discardMilitaryToken] .military_token.red_border :click",
dojo.hitch(this, "onDiscardMilitaryTokenClick")
);
dojo.query('body')
.on("#swd[data-state=applyMilitaryToken] .military_token.red_border :click",
dojo.hitch(this, "onApplyMilitaryTokenClick")
);
dojo.query('body')
.on("#swd[data-state=constructWonderWithDiscardedBuilding] #discarded_cards_container .building_small:click",
dojo.hitch(this, "onConstructWonderWithDiscardedBuildingClick")
);
dojo.query('body')
.on("#swd[data-state=constructWonderWithDiscardedBuilding] #player_wonders_" + this.me_id + " .wonder_small:click",
dojo.hitch(this, "onConstructWonderWithDiscardedBuildingWonderClick")
);
dojo.query('body')
.on("#swd[data-state=deconstructWonder] .player_wonders .red_border:click",
dojo.hitch(this, "onDeconstructWonderClick")
);
dojo.query('body')
.on("#swd[data-state=placeMinervaToken] #board_container .military_position.red_stroke:click",
dojo.hitch(this, "onPlaceMinervaTokenClick")
);
dojo.query('body')
.on("#swd[data-state=discardAgeCard] #draftpool .building_small:click",
dojo.hitch(this, "onDiscardAgeCardClick")
);
dojo.query('body')
.on("#swd[data-state=placeSnakeToken] .player_building_column.red_border .building_header_small:click",
dojo.hitch(this, "onPlaceSnakeTokenClick")
);
dojo.query('body')
.on("#swd[data-state=chooseDivinityFromTopCards] #choose_divinity_from_top_cards .divinity :click",
dojo.hitch(this, "onChooseDivinityFromTopCardsClick")
);
dojo.query('body')
.on("#swd[data-state=chooseDivinityDeck] #mythology_decks_container .divinity.red_border :click",
dojo.hitch(this, "onChooseDivinityDeckClick")
);
dojo.query('body')
.on("#swd[data-state=chooseDivinityFromDeck][data-client-state=] #choose_divinity_from_deck .divinity :click",
dojo.hitch(this, "onChooseDivinityFromDeckClick")
);
dojo.query('body')
.on("#swd[data-state=chooseDivinityFromDeck][data-client-state=client_chooseDivinityForTopOfDeck] #choose_divinity_from_deck .top_of_deck_button :click",
dojo.hitch(this, "onChooseDivinityFromDeckTopClick")
);
dojo.query('body')
.on("#swd[data-state=chooseEnkiProgressToken] .player_conspiracies .divinity_compact[data-divinity-id=1] .progress_token.red_border :click",
dojo.hitch(this, "onChooseEnkiProgressTokenClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_activateDivinity] #activate_divinity_tokens .offering_token:click",
dojo.hitch(this, "onActivityDivinityOfferingTokenAddClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_activateDivinity] #activate_divinity_payment_steps .offering_token:click",
dojo.hitch(this, "onActivityDivinityOfferingTokenRemoveClick")
);
// Pantheon click handlers without event delegation:
dojo.query("#activate_divinity_confirm").on("click", dojo.hitch(this, "onActivateDivinityConfirmClick"));
}
// Click handlers shared between Agora and Pantheon:
if (this.agora || this.pantheon) {
dojo.query('body')
.on("#swd[data-state=takeBuilding] .player_building_column.red_border .building_header_small:click",
dojo.hitch(this, "onTakeBuildingClick")
);
dojo.query('body')
.on("#swd[data-state=takeUnconstructedWonder] .wonder.red_border:click",
dojo.hitch(this, "onTakeUnconstructedWonderClick")
);
}
if (this.agora) {
// Agora click handlers using event delegation:
dojo.query('body')
.on("#swd[data-state=chooseConspiratorAction] #choose_conspirator_action .bgabutton_blue :click",
dojo.hitch(this, "onChooseConspiratorActionClick")
);
dojo.query('body')
.on("#swd[data-state=conspire] .conspiracy_small :click",
dojo.hitch(this, "onChooseConspiracyClick")
);
dojo.query('body')
.on("#swd[data-state=chooseConspireRemnantPosition] #choose_conspire_remnant_position .action_button :click",
dojo.hitch(this, "onChooseConspireRemnantPositionClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_useAgeCard] #player_conspiracies_" + this.me_id + " .conspiracy_small[data-conspiracy-prepared=\"0\"][data-conspiracy-triggered=\"0\"]:click",
dojo.hitch(this, "onPlayerTurnPrepareConspiracySelectedClick")
);
dojo.query('body')
.on("#swd[data-state=playerTurn] #player_conspiracies_" + this.me_id + " .conspiracy_small[data-conspiracy-prepared=\"1\"][data-conspiracy-triggered=\"0\"].green_border:click," +
"#swd[data-state=triggerUnpreparedConspiracy] #player_conspiracies_" + this.me_id + " .conspiracy_small[data-conspiracy-prepared=\"0\"][data-conspiracy-triggered=\"0\"].green_border:click",
dojo.hitch(this, "onPlayerTurnTriggerConspiracyClick")
);
dojo.query('body')
.on("#swd[data-state=triggerUnpreparedConspiracy] #buttonSimpleSkip:click",
dojo.hitch(this, "onPlayerTurnSkipTriggerConspiracyClick")
);
dojo.query('body')
.on("#swd[data-state=moveInfluence] #buttonSimpleSkip:click",
dojo.hitch(this, "onSenateActionsSkipButtonClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_placeInfluence] #senate_chambers .red_stroke:click," +
"#swd[data-state=placeInfluence] #senate_chambers .red_stroke:click",
dojo.hitch(this, "onPlaceInfluenceClick")
);
dojo.query('body')
.on("#swd[data-state=removeInfluence] #senate_chambers .red_stroke:click",
dojo.hitch(this, "onRemoveInfluenceClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_moveInfluenceFrom] #senate_chambers .red_stroke:click",
dojo.hitch(this, "onMoveInfluenceFromClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_moveInfluenceTo] #senate_chambers .red_stroke:click",
dojo.hitch(this, "onMoveInfluenceToClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_moveInfluenceTo] #senate_chambers .gray_stroke:click",
dojo.hitch(this, "onMoveInfluenceCancelClick")
);
dojo.query('body')
.on("#swd[data-state=constructLastRowBuilding] #draftpool .row1.red_border:click",
dojo.hitch(this, "onConstructLastRowBuildingClick")
);
dojo.query('body')
.on("#swd[data-state=destroyConstructedWonder] .wonder.red_border:click",
dojo.hitch(this, "onDestroyConstructedWonderClick")
);
dojo.query('body')
.on("#swd[data-state=discardAvailableCard] #draftpool .available:click",
dojo.hitch(this, "onDiscardAvailableCardClick")
);
dojo.query('body')
.on("#swd[data-state=discardAvailableCard] #buttonSimpleSkip:click",
dojo.hitch(this, "onDiscardAvailableCardSkipButtonClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_moveDecreeFrom] .decree_containers .red_border:click",
dojo.hitch(this, "onMoveDecreeFromClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_moveDecreeTo] #senate_chambers .red_stroke:click",
dojo.hitch(this, "onMoveDecreeToClick")
);
dojo.query('body')
.on("#swd[data-client-state=client_moveDecreeTo] .decree_containers .gray_border:click",
dojo.hitch(this, "onMoveDecreeCancelClick")
);
dojo.query('body')
.on("#swd[data-state=swapBuilding] .player_building_column.red_border .building_header_small:click",
dojo.hitch(this, "onSwapBuildingClick")
);
dojo.query('body')
.on("#swd[data-state=constructBuildingFromBox] #construct_building_from_box .building_small:click",
dojo.hitch(this, "onConstructBuildingFromBoxClick")
);
dojo.query('body')
.on("#swd[data-state=lockProgressToken] .progress_token_small :click",
dojo.hitch(this, "onLockProgressTokenClick")
);
// Agora click handlers without event delegation:
dojo.query("#buttonPrepareConspiracy").on("click", dojo.hitch(this, "onPlayerTurnPrepareConspiracyClick"));
dojo.query('body')
.on("#swd #senate_actions #buttonSenateActionsPlaceInfluence.bgabutton_blue :click",
dojo.hitch(this, "onSenateActionsPlaceInfluenceButtonClick")
);
dojo.query('body')
.on("#swd #senate_actions #buttonSenateActionsMoveInfluence.bgabutton_blue :click",
dojo.hitch(this, "onSenateActionsMoveInfluenceButtonClick")
);
dojo.query("#buttonSenateActionsSkip").on("click", dojo.hitch(this, "onSenateActionsSkipButtonClick"));
}
// Resize/scroll handler to determine layout and scale factor
window.addEventListener('resize', dojo.hitch(this, "onWindowUpdate"));
window.addEventListener('scroll', dojo.hitch(this, "onWindowUpdate"));
// Tool tips using event delegation:
this.setupTooltips();
// Setup game notifications to handle (see "setupNotifications" method below)
this.setupNotifications();
// Debug tooltip content by placing a tooltip at the top of the screen.
// dojo.place( this.getWonderTooltip(11, this.opponent_id, '<div class="coin"><span style="color: red !important">9</span></div>'), 'swd_wrap', 'first' );
// dojo.place( this.getBuildingTooltip(21, false, '', '', '', ''), 'swd_wrap', 'first');
// At the beginning swdPosition's y position is 265 (when it's not visible), so retry after loading to update the layout.
this.callFunctionAfterLoading(dojo.hitch(this, "updateLayout"));
},
///////////////////////////////////////////////////
//// Reaction to cometD notifications
/*
setupNotifications:
In this method, you associate each of your game notifications with your local method to handle it.
Note: game notification names correspond to "notifyAllPlayers" and "notifyPlayer" calls in
your sevenwondersduel.game.php file.
*/
setupNotifications: function () {
if (this.debug) console.log('notifications subscriptions setup');
// Example 1: standard notification handling
// dojo.subscribe( 'cardPlayed', this, "notif_cardPlayed" );
// Example 2: standard notification handling + tell the user interface to wait
// during 3 seconds after calling the method in order to var the players
// see what is happening in the game.
// dojo.subscribe( 'cardPlayed', this, "notif_cardPlayed" );
// this.notifqueue.setSynchronous( 'cardPlayed', 3000 );
//
dojo.subscribe('wonderSelected', this, "notif_wonderSelected");
this.notifqueue.setSynchronous('wonderSelected');
dojo.subscribe('nextAge', this, "notif_nextAge");
dojo.subscribe('constructBuilding', this, "notif_constructBuilding");
this.notifqueue.setSynchronous('constructBuilding');
dojo.subscribe('discardBuilding', this, "notif_discardBuilding");
this.notifqueue.setSynchronous('discardBuilding');
dojo.subscribe('constructWonder', this, "notif_constructWonder");
this.notifqueue.setSynchronous('constructWonder');
dojo.subscribe('progressTokenChosen', this, "notif_progressTokenChosen");
this.notifqueue.setSynchronous('progressTokenChosen');
dojo.subscribe('opponentDiscardBuilding', this, "notif_opponentDiscardBuilding");
this.notifqueue.setSynchronous('opponentDiscardBuilding');
dojo.subscribe('nextAgeDraftpoolReveal', this, "notif_nextAgeDraftpoolReveal");
this.notifqueue.setSynchronous('nextAgeDraftpoolReveal');
dojo.subscribe('nextPlayerTurnScientificSupremacy', this, "notif_nextPlayerTurnScientificSupremacy");
this.notifqueue.setSynchronous('nextPlayerTurnScientificSupremacy');
dojo.subscribe('nextPlayerTurnMilitarySupremacy', this, "notif_nextPlayerTurnMilitarySupremacy");
this.notifqueue.setSynchronous('nextPlayerTurnMilitarySupremacy');
dojo.subscribe('nextPlayerTurnPoliticalSupremacy', this, "notif_nextPlayerTurnPoliticalSupremacy");
this.notifqueue.setSynchronous('nextPlayerTurnPoliticalSupremacy');
dojo.subscribe('nextPlayerTurnEndGameScoring', this, "notif_nextPlayerTurnEndGameScoring");
this.notifqueue.setSynchronous('nextPlayerTurnEndGameScoring');
dojo.subscribe('endGameCategoryUpdate', this, "notif_endGameCategoryUpdate");
this.notifqueue.setSynchronous('endGameCategoryUpdate');
// Pantheon
dojo.subscribe('takeToken', this, "notif_takeToken");
this.notifqueue.setSynchronous('takeToken');
dojo.subscribe('placeDivinity', this, "notif_placeDivinity");
this.notifqueue.setSynchronous('placeDivinity');
dojo.subscribe('activateDivinity', this, "notif_activateDivinity");
this.notifqueue.setSynchronous('activateDivinity');
dojo.subscribe('discardMilitaryToken', this, "notif_discardMilitaryToken");
this.notifqueue.setSynchronous('discardMilitaryToken');
dojo.subscribe('applyMilitaryToken', this, "notif_applyMilitaryToken");
this.notifqueue.setSynchronous('applyMilitaryToken');
dojo.subscribe('deconstructWonder', this, "notif_deconstructWonder");
this.notifqueue.setSynchronous('deconstructWonder');
dojo.subscribe('placeMinervaToken', this, "notif_placeMinervaToken");
this.notifqueue.setSynchronous('placeMinervaToken');
dojo.subscribe('placeSnakeToken', this, "notif_placeSnakeToken");
this.notifqueue.setSynchronous('placeSnakeToken');
dojo.subscribe('returnDivinities', this, "notif_returnDivinities");
this.notifqueue.setSynchronous('returnDivinities');
// Agora
dojo.subscribe('constructConspiracy', this, "notif_constructConspiracy");
this.notifqueue.setSynchronous('constructConspiracy');
dojo.subscribe('chooseConspireRemnantPosition', this, "notif_chooseConspireRemnantPosition");
this.notifqueue.setSynchronous('chooseConspireRemnantPosition');
dojo.subscribe('conspireKeepBoth', this, "notif_conspireKeepBoth");
this.notifqueue.setSynchronous('conspireKeepBoth');
dojo.subscribe('prepareConspiracy', this, "notif_prepareConspiracy");
this.notifqueue.setSynchronous('prepareConspiracy');
dojo.subscribe('triggerConspiracy', this, "notif_triggerConspiracy");
this.notifqueue.setSynchronous('triggerConspiracy');
dojo.subscribe('placeInfluence', this, "notif_placeInfluence");
this.notifqueue.setSynchronous('placeInfluence');
dojo.subscribe('moveInfluence', this, "notif_moveInfluence");
this.notifqueue.setSynchronous('moveInfluence');
dojo.subscribe('removeInfluence', this, "notif_removeInfluence");
this.notifqueue.setSynchronous('removeInfluence');
dojo.subscribe('decreeControlChanged', this, "notif_decreeControlChanged");
this.notifqueue.setSynchronous('decreeControlChanged');
dojo.subscribe('destroyConstructedWonder', this, "notif_destroyConstructedWonder");
this.notifqueue.setSynchronous('destroyConstructedWonder');
dojo.subscribe('lockProgressToken', this, "notif_lockProgressToken");
this.notifqueue.setSynchronous('lockProgressToken');
dojo.subscribe('moveDecree', this, "notif_moveDecree");
this.notifqueue.setSynchronous('moveDecree');
dojo.subscribe('swapBuilding', this, "notif_swapBuilding");
this.notifqueue.setSynchronous('swapBuilding');
dojo.subscribe('takeBuilding', this, "notif_takeBuilding");
this.notifqueue.setSynchronous('takeBuilding');
dojo.subscribe('takeUnconstructedWonder', this, "notif_takeUnconstructedWonder");
this.notifqueue.setSynchronous('takeUnconstructedWonder');
},
///////////////////////////////////////////////////
//// Game & client states
// onEnteringState: this method is called each time we are entering into a new game state.
// You can use this method to perform some user interface changes at this moment.
//
onEnteringState: function (stateName, args) {
if (this.debug) console.log('Entering state: ' + stateName, args);
// Reset the client state
dojo.attr($('swd'), 'data-client-state', '');
if (args.args && stateName.substring(0, 7) != "client_") {
dojo.attr($('swd'), 'data-state', stateName);
// Update player coins / scores
if (args.args.playersSituation) {
this.updatePlayersSituation(args.args.playersSituation);
}
if (args.args.divinitiesSituation) {
let enkiProgressTokenIds = (args.args._private && args.args._private.enkiProgressTokenIds) ? args.args._private.enkiProgressTokenIds : null;
this.updateDivinitiesSituation(args.args.divinitiesSituation, enkiProgressTokenIds);
}
if (args.args.wondersSituation) this.updateWondersSituation(args.args.wondersSituation);
if (args.args._private && args.args._private.myConspiracies) this.myConspiracies = args.args._private.myConspiracies;
if (args.args.conspiraciesSituation) this.updateConspiraciesSituation(args.args.conspiraciesSituation);
if (args.args.mythologyTokensSituation) this.updateMythologyTokensSituation(args.args.mythologyTokensSituation);
if (args.args.offeringTokensSituation) this.updateOfferingTokensSituation(args.args.offeringTokensSituation);
if (args.args.senateSituation) this.updateSenateSituation(args.args.senateSituation);
if (args.args.wonderSelectionRound) {
$('wonder_selection_block_title').innerText = dojo.string.substitute(_("Wonders selection - round ${round} of 2"), {
round: args.args.wonderSelectionRound
});
}
if (args.args.draftpool) {
// Wait for loading screen (for Agora, when Age I draftpool gets revealed at the start of the game).
this.callFunctionAfterLoading(dojo.hitch(this, "updateDraftpool"), [args.args.draftpool]);
}
this.updateLayout(); // Because of added height of action button divs being auto shown/hidden because of the state change, it's a good idea to update the layout here.
// We chose to group all of the states' functions together, so we create a seperate "onEnter{StateName}" function and call it here if it exists.
var functionName = 'onEnter' + stateName.charAt(0).toUpperCase() + stateName.slice(1);
if (typeof this[functionName] === 'function') {
this[functionName](args.args);
}
}
else {
dojo.attr($('swd'), 'data-client-state', stateName);
}
},
// onLeavingState: this method is called each time we are leaving a game state.
// You can use this method to perform some user interface changes at this moment.
//
onLeavingState: function (stateName) {
if (this.debug) console.log('Leaving state: ' + stateName);
switch (stateName) {
/* Example:
case 'myGameState':
// Hide the HTML block we are displaying only during this game state
dojo.style( 'my_html_block_id', 'display', 'none' );
break;
*/
case 'discardAvailableCard':
this.clearRedBorder(); // Clear Insider Influence borders if the second step is skipped.
break;
}
},
// onUpdateActionButtons: in this method you can manage "action buttons" that are displayed in the
// action status bar (ie: the HTML links in the status bar).
//
onUpdateActionButtons: function (stateName, args) {
if (this.debug) console.log('onUpdateActionButtons: ' + stateName);
if (this.isCurrentPlayerActive()) {
switch (stateName) {
/*
Example:
case 'myGameState':
// Add 3 action buttons in the action status bar:
this.addActionButton( 'button_1_id', _('Button 1 label'), 'onMyMethodToCall1' );
this.addActionButton( 'button_2_id', _('Button 2 label'), 'onMyMethodToCall2' );
this.addActionButton( 'button_3_id', _('Button 3 label'), 'onMyMethodToCall3' );
break;
*/
}
}
},
// __ __ _
// \ \ / /__ _ __ __| | ___ _ __ ___
// \ \ /\ / / _ \| '_ \ / _` |/ _ \ '__/ __|
// \ V V / (_) | | | | (_| | __/ | \__ \
// \_/\_/ \___/|_| |_|\__,_|\___|_| |___/
getWonderCardData: function (playerId, wonderId) {
for (var i = 0; i < this.gamedatas.wondersSituation[playerId].length; i++) {
var cardData = this.gamedatas.wondersSituation[playerId][i];
if (cardData.wonder == wonderId) {
return cardData;
}
}
return null;
},
getWonderDivHtml: function (wonderId, displayCost, cost, playerCoins) {
var wonder = this.gamedatas.wonders[wonderId];
var data = {
jsId: wonderId,
jsName: _(wonder.name),
jsConstructed: displayCost ? 0 : 1,
jsDisplayCost: displayCost ? 'inline-block' : 'none',
jsCost: this.getCostValue(cost),
jsCostColor: this.getCostColor(cost, playerCoins),
};
data.jsCostClass = isNaN(data.jsCost) ? 'cost_free' : '';
var spritesheetColumns = 5;
data.jsX = (wonderId - 1) % spritesheetColumns;
data.jsY = Math.floor((wonderId - 1) / spritesheetColumns);
return this.format_block('jstpl_wonder', data);
},
updateWondersSituation: function (situation) {
this.gamedatas.wondersSituation = situation;
if (situation.selection.length < 4) { // else
this.updateWonderSelection(situation.selection);
}
for (var player_id in this.gamedatas.players) {
this.updatePlayerWonders(player_id, situation[player_id]);
}
},
/*
Transition (3D flip) from old node to new node. Destroy old node afterwards.
*/
twistAnimation: function (oldNode, newNode, autoPlay=true, delay=0, duration=-1) {
if (duration == -1) duration = this.twistCoinDuration;
if (!oldNode || dojo.style(oldNode, 'display') == 'none' || oldNode.outerHTML != newNode.outerHTML) {
var displayValue = dojo.style(newNode, 'display'); // probably inline-block or block.
dojo.style(newNode, 'display', 'none');
var anims = [];
if (oldNode) {
dojo.place(oldNode, newNode.parentElement);
var oldNodeAnim = dojo.animateProperty({
node: oldNode,
delay: delay,
duration: duration / 2,
easing: dojo.fx.easing.linear,
properties: {
propertyTransform: {start: 0, end: 90}
},
onAnimate: function (values) {
dojo.style(oldNode, 'transform', 'perspective(40em) rotateY(' + parseFloat(values.propertyTransform.replace("px", "")) + 'deg)');
},
onEnd: dojo.hitch(this, function (node) {
dojo.destroy(oldNode);
})
});
anims.push(oldNodeAnim);
}
var newNodeAnim = dojo.animateProperty({
node: newNode,
duration: duration / 2,
easing: dojo.fx.easing.linear,
properties: {
propertyTransform: {start: -90, end: 0}
},
onPlay: function (values) {
dojo.destroy(oldNode);
dojo.style(newNode, 'display', displayValue);
dojo.style(newNode, 'transform', 'perspective(40em) rotateY(-90deg)'); // When delay > 0 this is necesarry to hide the new node.
},
onAnimate: function (values) {
dojo.style(newNode, 'transform', 'perspective(40em) rotateY(' + parseFloat(values.propertyTransform.replace("px", "")) + 'deg)');
}
});
anims.push(newNodeAnim);
var anim = dojo.fx.chain(anims);
if (autoPlay) anim.play();
return anim;
} else {
dojo.destroy(oldNode);
}
},
updatePlayerNumberOfWondersStyling: function (playerId, count) {
let playerWonderContainer = dojo.query('.player_wonders.player' + playerId)[0];
let wondersCount = Math.max(4, count);
this.setCssVariable('--number-of-wonders', wondersCount , playerWonderContainer);
this.setCssVariable('--number-of-wonder-rows-landscape', Math.ceil(wondersCount / 2) , playerWonderContainer);
let portraitWondersCount = this.agora ? 5 : 4;
this.setCssVariable('--number-of-wonder-rows-portrait', Math.ceil(wondersCount / portraitWondersCount) , playerWonderContainer);
if (wondersCount > 4) {
dojo.style(dojo.query('.player_wonders.player' + playerId + ' > div:nth-of-type(5)')[0], 'display', wondersCount > 4 ? 'inline-block' : 'none');
}
if (wondersCount > 5) {
dojo.style(dojo.query('.player_wonders.player' + playerId + ' > div:nth-of-type(6)')[0], 'display', wondersCount > 5 ? 'inline-block' : 'none');
}
this.autoUpdateScale();
},
updatePlayerWonders: function (playerId, rows) {
if (this.debug) console.log('updatePlayerWonders', playerId, rows);
this.updatePlayerNumberOfWondersStyling(playerId, Math.max(4, rows.length > 0 ? parseInt(rows[rows.length - 1].position) : 0));
var i = 1;
Object.keys(rows).forEach(dojo.hitch(this, function (index) {
var row = rows[index];
var container = dojo.query('.player_wonders.player' + playerId + '>div:nth-of-type(' + row.position + ')')[0];
var oldNode = dojo.query('.wonder_container', container)[0];
var wonderDivHtml = this.getWonderDivHtml(row.wonder, row.constructed == 0, row.cost, this.getPlayerCoins(playerId, true));
var newNode = dojo.place(wonderDivHtml, container);
if (row.constructed > 0) {
var data = {
jsX: row.ageCardSpriteXY[0],
jsY: row.ageCardSpriteXY[1]
};
dojo.place(this.format_block('jstpl_wonder_age_card', data), dojo.query('.age_card_container', newNode)[0]);
}
this.twistAnimation(
dojo.query('.player_wonder_cost', oldNode)[0],
dojo.query('.player_wonder_cost', newNode)[0],
);
if (oldNode) dojo.destroy(oldNode);
i++;
}));
},
// ____ _ _ _ _ ____ __ _ _
// | __ ) _ _(_) | __| (_)_ __ __ _ ___ | _ \ _ __ __ _ / _| |_ _ __ ___ ___ | |
// | _ \| | | | | |/ _` | | '_ \ / _` / __| _____ | | | | '__/ _` | |_| __| '_ \ / _ \ / _ \| |
// | |_) | |_| | | | (_| | | | | | (_| \__ \ |_____| | |_| | | | (_| | _| |_| |_) | (_) | (_) | |
// |____/ \__,_|_|_|\__,_|_|_| |_|\__, |___/ |____/|_| \__,_|_| \__| .__/ \___/ \___/|_|
// |___/ |_|
getBuildingDivHtml: function (id, building) {
var data = {
jsId: id,
jsX: building.spriteXY[0],
jsY: building.spriteXY[1],
jsOrder: 0
};
if (building.type == "Green") {
data.jsOrder = building.scientificSymbol.toString() + building.age.toString();
}
return this.format_block('jstpl_player_building', data);
},
updatePlayerBuildings: function (playerId, cards) {
if (this.debug) console.log('updatePlayerBuildings: ', playerId, cards);
var i = 1;
Object.keys(cards).forEach(dojo.hitch(this, function (index) {
var building = this.gamedatas.buildings[cards[index].id];
var container = dojo.query('.player_buildings.player' + playerId + ' .' + building.type)[0];
dojo.place(this.getBuildingDivHtml(building.id, building), container);
i++;
}));
},
getDraftpoolCardData: function (buildingId) {
for (var i = 0; i < this.gamedatas.draftpool.cards.length; i++) {
var position = this.gamedatas.draftpool.cards[i];
if (typeof position.building != 'undefined' && position.building == buildingId) {
return position;
}
}
return null;
},
/**
* @param draftpool
* @param setupGame
* @returns {number} Duration of the full animation.
*/
updateDraftpool: function (draftpool, setupGame = false, forceAnimation = false) {