-
Notifications
You must be signed in to change notification settings - Fork 0
/
marcopoloexpansions.js
2963 lines (2669 loc) · 162 KB
/
marcopoloexpansions.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 <[email protected]> & Emmanuel Colin <[email protected]>
* MarcoPoloExpansions implementation : © Hershey Sakhrani <[email protected]> & Vinicius Rezende <[email protected]>
*
* 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.
* -----
*
* marcopoloexpansions.js
*
* MarcoPoloExpansions user interface script
*
* In this file, you are describing the logic of your user interface, in Javascript language.
*
*/
define([
"dojo", "dojo/_base/declare",
"ebg/core/gamegui",
"ebg/counter"
],
function (dojo, declare) {
return declare("bgagame.marcopoloexpansions", ebg.core.gamegui, {
constructor: function () {
this.uiItems = [];
this.myCharacterType = 0;
this.currentMove;
this.currentMoveArgs;
this.canUndo = false;
this.mainActionAvailable = false;
this.canBuyBlackDie = false;
this.canUsePersonalCityCard = false;
this.playerResources = {};
this.playerMatContainerUiTypes = { "small": ["die", "gift"], "large": ["goal_card", "contract", "1x_gift", "city_card"] };
this.runningPlayerMatAnimations = {};
this.blinkPassHandle = null;
},
//#region uiItems
attachFunctionsToUiItems: function () {
var _self = this;
this.uiItems._lastUid = 0;
this.uiItems.getByUid = function (uid) {
return this.find(function (u) { return u.uid == uid });
}
this.uiItems.getByUiType = function (uiType) {
return this.filter(function (u) { return u.uiType == uiType });
}
this.uiItems.getByUiTypeAndId = function (uiType, id) {
return this.find(function (u) { return u.uiType == uiType && parseInt(u.data.id) == parseInt(id) });
}
this.uiItems.getByUiTypeAndTypeArg = function (uiType, typeArg) {
return this.find(function (u) { return u.uiType == uiType && parseInt(u.data.type_arg) == parseInt(typeArg) });
}
this.uiItems.getByUiTypes = function (uiTypes) {
return this.filter(function (u) { return uiTypes.includes(u.uiType) });
}
this.uiItems.getPlayerUiItems = function (uiType, location, playerId) {
return this.filter(function (u) { return u.uiType == uiType && (u.data.player_id == playerId || u.data.location_arg == playerId) && u.data.location == location });
}
this.uiItems.getPlayerId = function (uiItem) {
var playerId = ["contract", "goal_card", "gift"].includes(uiItem.uiType) ? uiItem.data.location_arg : uiItem.data.player_id;
if (uiItem.uiType == "contract" && uiItem.data.location == "board") {
playerId = null;
}
return playerId;
}
this.uiItems.isPlayerItem = function (uiItem) {
var isPlayerItem = false;
if (uiItem.data.location) {
isPlayerItem = uiItem.data.location.endsWith("hand") || uiItem.data.location == "player_mat" || uiItem.data.location == "zombie_mat";
}
return isPlayerItem;
}
this.uiItems.resetSelectableAnimation = function () //need code to restart it - https://css-tricks.com/restart-css-animation/
{
var items = this.getSelectableItems(false);
for (var i = 0; i < items.length; i++) {
items[i].htmlNode.classList.remove("selectable");
void items[i].htmlNode.offsetWidth;
items[i].htmlNode.classList.add("selectable");
}
}
this.uiItems.getSelectedItems = function () {
return this.filter(function (u) { return u.isSelected; });
}
this.uiItems.getSelectedItemsByUiType = function (uiType) {
return this.filter(function (u) { return u.isSelected && u.uiType == uiType; });
}
this.uiItems.getFirstSelectedItemByUiType = function (uiType) {
var items = this.getSelectedItemsByUiType(uiType);
return items.length > 0 ? items[0] : null;
}
this.uiItems.getFirstSelectedItemByUiTypes = function (uiTypes) {
var item = null;
for (var i = 0; i < uiTypes.length; i++) {
item = this.getFirstSelectedItemByUiType(uiTypes[i]);
if (item != null) { break; }
}
return item;
}
this.uiItems.getSelectableItems = function (includeSelected) {
if (includeSelected)
return this.filter(function (u) { return u.isSelectable; });
return this.filter(function (u) { return u.isSelectable && !u.isSelected; });
}
this.uiItems.makeSelectable = function (items) {
for (var i = 0; i < items.length; i++) {
items[i].isSelectable = true;
dojo.addClass(items[i].htmlNode, "selectable");
}
this.resetSelectableAnimation();
}
this.uiItems.toggleSelection = function (uiItem) {
if (uiItem.isSelectable) {
if (uiItem.isSelected) {
dojo.addClass(uiItem.htmlNode, "selectable");
dojo.removeClass(uiItem.htmlNode, "selected");
}
else {
dojo.removeClass(uiItem.htmlNode, "selectable")
dojo.addClass(uiItem.htmlNode, "selected");
}
uiItem.isSelected = !uiItem.isSelected;
}
}
this.uiItems.resetSelectable = function (items) {
for (var i = 0; i < items.length; i++) {
var item = items[i];
dojo.removeClass(item.htmlNode, "selectable");
dojo.removeClass(item.htmlNode, "selected");
item.isSelected = false;
item.isSelectable = false;
}
}
this.uiItems.resetAllSelectable = function () {
this.resetSelectable(this);
}
this.uiItems.resetAllNotSelected = function () {
this.resetSelectable(this.getSelectableItems(false));
}
this.uiItems.resetAllSelectableByType = function (uiType, exceptUiItem) {
var items = this.getByUiType(uiType);
items = items.filter(function (u) { return u != exceptUiItem });
this.resetSelectable(items);
}
this.uiItems.resetAllMapNodeBorders = function () {
var mapNodes = this.getByUiType("map_node");
for (var i = 0; i < mapNodes.length; i++) {
dojo.setStyle(mapNodes[i].htmlNode, "border", "");
}
}
this.uiItems.createItems = function (uiType, dataArray) {
this.createItemsViaCallback(function (d) { return uiType; }, dataArray);
}
this.uiItems.createItemsViaCallback = function (dataCallback, dataArray) {
for (var i = 0; i < dataArray.length; i++) {
var data = dataArray[i];
this.createAndAddItem(dataCallback(data), data);
}
}
//based on the image provided
this.uiItems.itemBackgroundConfig = {
"city_card": { items_per_row: 10, width: 197, height: 126.5, type_property: "type_arg" },
"character": { items_per_row: 10, width: 199, height: 301, type_property: "character_type" },
"contract": { items_per_row: 10, width: 150, height: 150, type_property: "type_arg" },
"outpost": { items_per_row: 5, width: 168, height: 101, type_property: "type_arg" },
"city_bonus": { items_per_row: 10, width: 136, height: 0, type_property: "type_arg" },
"gift": { items_per_row: 16, width: 107, height: 96, type_property: "type_arg" },
};
this.uiItems.adjustBackgroundPositionForFullGoalCard = function (background) {
background.x += 4;
background.y += 110;
return background;
}
this.uiItems.getBackgroundPosition = function (uiType, typeArg) {
var background = { x: 0, y: 0 };
background.x = (typeArg % this.itemBackgroundConfig[uiType].items_per_row) * -1 * this.itemBackgroundConfig[uiType]["width"];
background.y = Math.floor(typeArg / this.itemBackgroundConfig[uiType].items_per_row) * -1 * this.itemBackgroundConfig[uiType]["height"];
return background;
}
this.uiItems.getBackgroundPositionForUiItem = function (uiItem) {
var background = { x: 0, y: 0 };
if (this.itemBackgroundConfig[uiItem.uiType] != undefined) {
var propertyName = this.itemBackgroundConfig[uiItem.uiType]["type_property"];
var typeArg = parseInt(uiItem.data[propertyName]);
background = this.getBackgroundPosition(uiItem.uiType, typeArg);
}
else if (uiItem.uiType == "goal_card") {
var type_arg = parseInt(uiItem.data.type_arg);
background.x = (type_arg % 10) * -180 - 9;
background.y = Math.floor(type_arg / 10) * -281 - 110;
if (uiItem.data.location == "goalSelection") { background = this.adjustBackgroundPositionForFullGoalCard(background); }
}
else if (uiItem.uiType == "die") {
background.x = dojo.hasClass(uiItem.htmlNode, "black") ? -805 : -905;
background.y = (parseInt(uiItem.data.value) - 1) * -100;
}
else {
background = null;
}
if (uiItem.uiType == "character" && uiItem.data.character_type == "0") //mercator has different image depending on player count
{
var numPlayers = Object.keys(_self.gamedatas.players).length;
if (numPlayers == 4) {
background.x = -1791;
background.y = -602;
}
else if (numPlayers == 3) {
background.x = -1592;
background.y = -602;
}
}
return background;
}
this.uiItems.setBackgroundUiItem = function (uiItem) {
var background = this.getBackgroundPositionForUiItem(uiItem);
var htmlNode = uiItem.htmlNode;
if (background != null && uiItem.uiType == "die") {
htmlNode = dojo.query(".die_pip:first-child", uiItem.htmlNode)[0];
}
if (background != null) {
var backgroundPosition = background.x + "px" + " " + background.y + "px";
dojo.setStyle(htmlNode, "background-position", backgroundPosition);
}
}
this.uiItems.weightConfig = {
"die": 100, "die_white": 200, "die_black": 300, "gift": 400, "agent": 500, "goal_card": 600, "contract": 800, "city_card": 900, "default": 10000
}
this.uiItems.getWeight = function (uiItem) {
var weight = this.weightConfig["default"];
if (this.weightConfig[uiItem.uiType]) {
weight = this.weightConfig[uiItem.uiType];
if (uiItem.uiType == "die" && uiItem.data.type != "regular") {
weight = this.weightConfig["die_" + uiItem.data.type];
}
if (uiItem.uiType == "die") {
weight += parseInt(uiItem.data.value) * 10;
}
}
return weight;
}
this.uiItems.getColorName = function (uiType, params) {
var colourName = "";
if (uiType == "die" && params.type == "white" || params.type == "black") {
colourName = params.type;
}
else if (uiType == "die" && params.type == "fixed") {
colourName = _self.getUnusedColorName();
}
else {
colourName = _self.getColourNameForPlayerId(params.player_id);
}
return colourName;
}
this.uiItems.extractDescriptionFromResourceArray = function (resources) {
var description = "";
var resourceNameMap = {
"camel": _("camel(s)"), "pepper": _("pepper"), "silk": _("silk"), "gold": _("gold"), "black_die": _("black die"), "vp": _("victory point"),
"contract": _("new contract"), "travel": _("travel movement"), "choice_of_good": _("choice of pepper/silk/gold"), "2_diff_goods": _("choice of two different resources"),
"coin": _("coin"), "trigger_other_city_bonus": _("trigger another city bonus (you do not have to have a trading post there)"),
"placed_trading_post": _("placed trading post"), "gift": _("gift")
};
for (var resourceId in resources) {
description += resources[resourceId] + " ";
description += resourceNameMap[resourceId] + ", ";
}
if (description.length > 0) { description = description.substr(0, description.length - 2); }
return description;
}
this.uiItems.getTooltipDescription = function (uiItem) {
var description = "";
if (uiItem.uiType == "character") {
description = _(_self.gamedatas.material.character_types[uiItem.data.character_type].description);
}
else if (uiItem.uiType == "contract") {
var contractData = _self.gamedatas.material.contracts[uiItem.data.type];
var give = this.extractDescriptionFromResourceArray(contractData.cost);
var get = this.extractDescriptionFromResourceArray(contractData.award);;
description = dojo.string.substitute(_("Fulfill this contract by returning ${give_resources} in return you will receive ${get_resources}"), { contractNumber: uiItem.data.type, give_resources: give, get_resources: get });
}
else if (uiItem.uiType == "goal_card") {
description = _("<p>Get VP shown on card for having trading posts in both cities indicated.</p><p>Get 1/3/6/10 VP for having trading posts in multiple unique goal cities.</p><p>Points awarded at end of game.</p>")
}
else if (uiItem.uiType == "outpost") {
var outpostData = _self.gamedatas.material.outpost_bonuses[uiItem.data.type_arg];
description = dojo.string.substitute(_("<p>If you are the first to place a trading post here get outpost bonus of ${award}.</p><p>Only awarded after all your travel movements have been completed.</p>"), { award: this.extractDescriptionFromResourceArray(outpostData.award) });
}
else if (uiItem.uiType == "city_bonus") {
var cityBonusData = _self.gamedatas.material.city_bonuses[uiItem.data.type_arg];
description = dojo.string.substitute(_("<p>Immediately get ${award} once you place a trading post here, and finish all your travel movements.</p><p>You will receive this award in all future rounds as well.</p>"), { award: this.extractDescriptionFromResourceArray(cityBonusData.award) });
}
else if (uiItem.uiType == "city_card") {
var cityCardData = _self.gamedatas.material.city_cards[uiItem.data.type_arg];
if (cityCardData.description) {
description = _(cityCardData.description);
}
else if (cityCardData.kind == "multiple") {
description = dojo.string.substitute(_("Trade in sets of ${cost} for ${award} up to die value placed"), { cost: this.extractDescriptionFromResourceArray(cityCardData.cost), award: this.extractDescriptionFromResourceArray(cityCardData.award) });
}
description = dojo.string.substitute("${fullDescription}", { cardNumber: uiItem.data.type_arg, fullDescription: description });
}
else if (uiItem.uiType == "board_spot") {
var spotData = _self.gamedatas.material.spots.find(function (s) { return s.index == uiItem.data.index && s.is_award_spot == false && uiItem.data.place == s.place; });
description = _(spotData.description);
}
else if (uiItem.uiType == "award_spot") {
var awardData = _self.gamedatas.material.spots.find(function (s) { return s.tied_to_index == uiItem.data.tied_to_index && s.index == uiItem.data.index && s.is_award_spot && uiItem.data.place == s.place; });
var award = this.extractDescriptionFromResourceArray(awardData.award);
if (awardData.cost) {
var cost = this.extractDescriptionFromResourceArray(awardData.cost);
description = dojo.string.substitute(_("Pay ${cost} for ${award}"), { "cost": cost, "award": award });
}
else {
description = award;
}
}
else if (uiItem.uiType == "map_node" && uiItem.data.id == 8) {
description = _("<p><b>Beijing</b></p>");
description += _("<p>If you end your movement in Beijing, you place a trading post in the city as usual. Place it on the empty space showing the largest number of victory points.</p>");
description += _("<p>At the end of the game if you have a trading post in Beijing you score the victory points indicated by the space your trading post is on.</p>")
description += _("<p>If you have a trading post in Beijing, you score 1 victory point for every 2 goods you have left. Players without a trading post in Beijing score nothing for their remaining goods. Important: Camels are not goods!</p>")
}
return description;
}
this.uiItems.addTooltip = function (uiItem) {
var divId = dojo.getAttr(uiItem.htmlNode, 'id');
if (uiItem.uiType == "board_spot" || uiItem.uiType == "award_spot") {
_self.addTooltip(divId, this.getTooltipDescription(uiItem), "");
}
else if (uiItem.uiType == "map_node" && uiItem.data.id == 8) {
_self.addTooltip(divId, this.getTooltipDescription(uiItem), "");
}
else if (this.itemConfig[uiItem.uiType].tooltip) {
var cssClass = this.itemConfig[uiItem.uiType].cssClass;
var backgroundPosition = this.getBackgroundPositionForUiItem(uiItem);
var description = this.getTooltipDescription(uiItem);
var descriptionWidth = "0px";
if (uiItem.uiType == "goal_card") { backgroundPosition = this.adjustBackgroundPositionForFullGoalCard(backgroundPosition); }
if (description != "") {
descriptionWidth = dojo.getComputedStyle(uiItem.htmlNode).width;
}
var html = _self.format_block('jstpl_larger_uiItem_tooltip', { className: cssClass, x: backgroundPosition.x, y: backgroundPosition.y, description: description, descriptionWidth: descriptionWidth });
if (uiItem.uiType == "city_bonus" || uiItem.uiType == "outpost") {
divId = dojo.getAttr(this.getByUiTypeAndId("map_node", uiItem.data.location_arg).htmlNode, 'id');
}
_self.addTooltipHtml(divId, html, 500);
}
}
this.uiItems._extendUiItem_die = function (uiItem) {
var pip = dojo.create("div", { "class": "piece die_pip" });
dojo.place(pip, uiItem.htmlNode);
}
this.uiItems._extendUiItem_character = function (uiItem) {
if (uiItem.data.character_type == 7) //Matteo Polo (optional contract)
{
this.createAndAddItem("character_spot", { "player_id": uiItem.data.player_id, "character_type": 7, "index": 0 });
}
else if (uiItem.data.character_type == 8) //Fratre Nicoalo
{
this.createAndAddItem("character_spot", { "player_id": uiItem.data.player_id, "character_type": 8, "index": 0 });
}
else if (uiItem.data.character_type == 11) //Gunj Kököchin
{
this.createAndAddItem("character_spot", { "player_id": uiItem.data.player_id, "character_type": 11, "place": "gunj", "index": 0 });
this.createAndAddItem("character_spot", { "player_id": uiItem.data.player_id, "character_type": 11, "place": "gunj", "index": 1 });
}
}
this.uiItems.itemConfig = {
"character": { cssClass: "character", "zIndex": 30, tooltip: true },
"character_spot": { cssClass: "character_spot", "zIndex": 50 },
"die": { cssClass: "piece die", color: true, "width": 32, "height": 32, "zIndex": 100 },
"trading_post": { cssClass: "piece trading_post", color: true, "zIndex": 200 },
"figure": { cssClass: "piece figure", color: true, "zIndex": 300 },
"city_card": { cssClass: "city_card", tooltip: true, width: 106 },
"city_bonus": { cssClass: "city_bonus", tooltip: true },
"outpost": { cssClass: "outpost", tooltip: true },
"contract": { cssClass: "contract", "width": 80, tooltip: true },
"goal_card": { cssClass: "goal_card", "width": 114, "height": 90, tooltip: true },
"map_node": { htmlNode: "map_node_${id}", "zIndex": 500 },
"board_spot": { htmlNode: "board_spot_${place}_${index}", "zIndex": 400, tooltip: false },
"award_spot": { htmlNode: "award_spot_${place}_${tied_to_index}_${index}" },
"gift": { cssClass: "gift", "width": 54, tooltip: true },
"1x_gift": { cssClass: "piece keep_gift_1x", "width": 80, tooltip: false }
}
this.uiItems.createAndAddItem = function (uiType, params) {
this._lastUid++;
var htmlNode = null;
var clickHandler = null;
if (this.itemConfig[uiType].cssClass) {
htmlNode = dojo.create("div", { "class": this.itemConfig[uiType].cssClass });
dojo.setAttr(htmlNode, "id", "uid-" + this._lastUid);
}
else {
htmlNode = $(dojo.string.substitute(this.itemConfig[uiType].htmlNode, params));
}
if (this.itemConfig[uiType].color) {
dojo.addClass(htmlNode, this.getColorName(uiType, params));
}
dojo.setAttr(htmlNode, "data-uid", "uid-" + this._lastUid);
clickHandler = dojo.connect(htmlNode, "onclick", _self, "onClickUiItem");
var item = { "uid": this._lastUid, "uiType": uiType, "data": params, "htmlNode": htmlNode, "clickHandler": clickHandler, isSelected: false, isSelectable: false, uiPosition: 0 };
if (this["_extendUiItem_" + uiType] != undefined) { this["_extendUiItem_" + uiType](item); }
this.setBackgroundUiItem(item);
this.push(item);
return item;
}
},
//#endregion
setupGoalCards: function (goalCardData) {
this.uiItems.createItems("goal_card", goalCardData); //create fake ones
for (var playerId in this.gamedatas.players) {
var playerGoalCard = this.uiItems.getByUiType("goal_card").filter(function (g) { return g.data.location_arg == playerId; }.bind(this));
if (playerGoalCard.length == 4) //set goal cards in hand or selection mode
{
dojo.forEach(playerGoalCard, function (g) {
g.data.location = "goalSelection";
});
}
if (playerGoalCard.length == 0 || playerGoalCard.length == 4) {
var fakeGoalCardData = [{ type: 29, type_arg: 29, location: "goal_hand", location_arg: playerId }, { type: 29, type_arg: 29, location: "goal_hand", location_arg: playerId }];
this.uiItems.createItems("goal_card", fakeGoalCardData);
}
}
var backOnlyCards = this.uiItems.getByUiType("goal_card").filter(function (g) { return g.data.type_arg == 29; }); //add goal card 29 class
dojo.forEach(backOnlyCards, function (c) {
dojo.addClass(c.htmlNode, "goal_card_back");
});
},
resetSetup: function () {
for (var i = 0; i < this.uiItems.length; i++) {
if (this.uiItems[i].onClickHandle) {
dojo.disconnect(this.uiItems[i].onClickHandle);
}
if (this.uiItems[i].htmlNode) {
dojo.destroy(this.uiItems[i].htmlNode);
}
}
this.uiItems = [];
},
/*
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.
*/
getValuesFromObject: function (data) {
var values = [];
for (var key in data) {
values.push(data[key]);
}
return values;
},
setup: function (gamedatas) {
if (gamedatas.expansions.length == 0 || !gamedatas.expansions.includes("0")) {
this.dontPreloadImage('gifts.png');
dojo.setStyle("gift_pile", "display", "none");
}
this.resetSetup();
this.attachFunctionsToUiItems();
this.uiItems.createItems("contract", this.getValuesFromObject(gamedatas.contracts));
this.uiItems.createItems("contract", this.getValuesFromObject(gamedatas.player_contracts));
this.uiItems.createItems("gift", this.getValuesFromObject(gamedatas.player_gifts));
this.uiItems.createItems("map_node", gamedatas.material.board);
this.uiItems.createItemsViaCallback(function (s) { return s.is_award_spot ? "award_spot" : "board_spot" }, gamedatas.material.spots);
this.uiItems.createItemsViaCallback(function (d) { return d.type; }, this.getValuesFromObject(gamedatas.pieces));
this.uiItems.createItems("die", this.getValuesFromObject(gamedatas.dice));
for (var playerId in gamedatas.players) // Setting up player boards
{
var player = gamedatas.players[playerId];
this.playerResources[playerId] = {};
dojo.forEach(['coin', 'camel', 'pepper', 'silk', 'gold', 'vp'], function (r) {
this.playerResources[playerId][r] = parseInt(player[r]);
}.bind(this));
var completedContracts = gamedatas.player_contracts_completed[playerId] == undefined ? 0 : parseInt(gamedatas.player_contracts_completed[playerId].num_of);
gamedatas.player_contracts_completed[playerId] = completedContracts;
dojo.setAttr($('contracts-complete-' + playerId), "innerHTML", completedContracts);
if (player.character_type != "99") {
this.uiItems.createItems("character", [{ player_id: playerId, character_type: player.character_type }]);
if (this.player_id == playerId) {
this.myCharacterType = player.character_type;
}
}
if (this.player_id == playerId) { dojo.setStyle($('myCharacterAndGoalArea-' + playerId), "display", "block"); }
dojo.place(this.format_block('jstpl_player_panel', { player_id: playerId }), $('player_board_' + playerId));
this.updateResourceInfo(playerId);
if (player.hourglass == "1") { this.updateHourGlass(playerId); }
}
this.setupGoalCards(this.getValuesFromObject(gamedatas.goal_cards));
this.updateCurrentRound(gamedatas.current_round);
this.setupNotifications(); // Setup game notifications to handle (see "setupNotifications" method below)
this.drawUi();
this.addTooltipToClass('.panel.trading_post', _("Number of trading posts placed + remaining"), "");
this.addTooltipToClass('.panel.completed_contracts', _("Number of completed contracts. At end of game 7VP to player(s) who complete the most"), "");
this.addTooltipToClass('.panel.vp', _("Number of victory points"), "");
this.addTooltipToClass('.panel.gold', _("Number of gold"), "");
this.addTooltipToClass('.panel.silk', _("Number of silk"), "");
this.addTooltipToClass('.panel.pepper', _("Number of pepper"), "");
this.addTooltipToClass('.panel.camel', _("Number of camels"), "");
this.addTooltipToClass('.panel.coin', _("Number of coins"), "");
this.addTooltipToClass('.contract_back', _("Contract draw pile"), "");
this.addTooltipToClass('.panel_hourglass', _("Hourglass, player who will start first next round. Changes when a player travels"), "");
this.addTooltip('transparent_figure', _("Toggle player figure transparency on board"), "");
dojo.connect($("transparent_figure"), "onclick", this, "onClickTransparentFigure");
dojo.connect($("preference_control_100"), "onchange", this, "onChangeGoalAnchorPreference");
},
///////////////////////////////////////////////////
//// Game & client states
pickCharacter: function (uiItem) {
var selectedItems = this.uiItems.getSelectedItemsByUiType("character");
if (this.isCurrentPlayerActive()) {
dojo.setStyle("btnConfirm", "display", "inline-block");
}
for (var i = 0; i < selectedItems.length; i++) {
if (selectedItems[i] != uiItem) {
this.uiItems.toggleSelection(selectedItems[i]);
}
}
var selectedCharacter = this.uiItems.getFirstSelectedItemByUiType("character");
if (selectedCharacter != null) {
var description = _(this.gamedatas.material.character_types[selectedCharacter.data.character_type].description);
dojo.setStyle("characterSelectionDescription", "display", "block");
dojo.setAttr("characterSelectionDescription", "innerHTML", description);
dojo.place(this.getCharacterItemsDescription(selectedCharacter.data.character_type), "characterSelectionDescription");
}
else {
dojo.setStyle("characterSelectionDescription", "display", "none");
}
this.uiItems.resetSelectableAnimation();
},
pickGoals: function (uiItem) {
this.showGoalAnchors(this.uiItems.getSelectedItemsByUiType("goal_card"), uiItem);
this.uiItems.resetSelectableAnimation();
},
playerBonus: function (uiItem) {
if (uiItem.uiType == "character_spot" || uiItem.uiType == "map_node") {
this.sendTriggerBonus(uiItem.data.bonusId);
}
},
playerTurnSendAction: function (selectedDice, selectedPlace, selectedGifts, uiItem) {
var actionSent = true;
if (uiItem.uiType == "contract") {
this.sendFulfillContract(uiItem);
}
else if (uiItem.uiType == "award_spot") {
this.sendPlaceDie(selectedDice, selectedPlace, uiItem, selectedGifts);
}
else if (uiItem.uiType == "city_card" && uiItem.data.location === 'player_mat') {
this.sendFulfillArghun(uiItem);
}
else if (uiItem.uiType == "city_card") {
this.sendPlaceDie(selectedDice, uiItem, null, selectedGifts);
}
else if (uiItem.uiType == "board_spot" && this.getAwardSpots(uiItem, selectedDice).length == 0) {
this.sendPlaceDie(selectedDice, uiItem, null, selectedGifts);
}
else if (uiItem.uiType == "board_spot" && this.getAwardSpots(selectedPlace, selectedDice).length == 1) {
var awardSpot = this.getAwardSpots(selectedPlace, selectedDice);
this.sendPlaceDie(selectedDice, uiItem, awardSpot[0], selectedGifts);
}
else if (uiItem.uiType == "character_spot") {
this.sendPlaceDie(selectedDice, uiItem, null, selectedGifts);
}
else if (uiItem.uiType == "gift" && uiItem.data.type_arg == 4) {
this.switchToClientChangeDie(uiItem.data.id);
}
else if (uiItem.uiType == "gift" && uiItem.data.type_arg == 10) {
var boardIds = this.getValidBoardIdsFiguresAreOn(this.player_id);
if (boardIds.length == 0)
this.showMessage(_("No place to put a trading posts"), "error");
else if (boardIds.length == 1)
this.sendFulfillGift(uiItem, boardIds[0]);
else if (boardIds.length > 1)
this.switchToClientGiftPickTradingPost(uiItem.data.id, boardIds);
}
else if (uiItem.uiType == "gift" && uiItem.data.type_arg != 7) // do not call for free gift
{
this.sendFulfillGift(uiItem, null);
}
else {
actionSent = false;
}
return actionSent;
},
playerTurn: function (uiItem) {
var _self = this;
var playerId = this.player_id;
var selectableItems = [];
var selectedDice = this.uiItems.getSelectedItemsByUiType("die");
var selectedPlace = this.uiItems.getFirstSelectedItemByUiTypes(["board_spot", "city_card"]);
var selectedGifts = this.uiItems.getSelectedItemsByUiType("gift");
var actionSent = false;
this.updatePlayerTurnButtons(selectedDice);
this.uiItems.resetAllNotSelected();
if (uiItem && uiItem.uiType == "gift" && uiItem.isSelected) {
this.uiItems.resetAllSelectableByType("gift", uiItem);
}
if (uiItem && uiItem.isSelected) {
actionSent = this.playerTurnSendAction(selectedDice, selectedPlace, selectedGifts, uiItem);
}
if (uiItem && uiItem.uiType == "die" && !actionSent) //reset selected places if any die changed
{
selectedPlace = null;
var allPlaces = this.uiItems.getByUiTypes(["board_spot", "city_card"]);
this.uiItems.resetSelectable(allPlaces);
}
if (!actionSent) {
selectableItems = selectableItems.concat(this.uiItems.getPlayerUiItems("die", "player_mat", playerId));
selectableItems = selectableItems.concat(this.getSelectableGiftUIItemsForPlayerTurnState(selectedDice, playerId));
if (selectedDice.length == 0) {
selectableItems = selectableItems.concat(this.getFulfillableContracts(playerId));
const city_cards = this.getSelectableCityCardsUIItemsForPlayerTurnState(playerId);
selectableItems = selectableItems.concat(city_cards);
}
if (this.mainActionAvailable && selectedPlace && this.getAwardSpots(selectedPlace, selectedDice).length > 1) //place selected, show award spots
{
selectableItems.push(selectedPlace);
selectableItems = selectableItems.concat(this.getAwardSpots(selectedPlace, selectedDice));
}
else if (this.mainActionAvailable && selectedDice.length > 0 && selectedPlace == null) {
var allPlaces = this.uiItems.getByUiTypes(["board_spot", "city_card"]);
var filteredPlaces = allPlaces.filter(function (u) { return _self.isPlaceSpotAvailable(u, selectedDice, playerId) });
selectableItems = selectableItems.concat(this.getSelectableCharacterSpotUIItemsForPlayerTurnState(selectedDice, playerId));
this.uiItems.resetSelectable(allPlaces);
selectableItems = selectableItems.concat(filteredPlaces);
}
else if (!this.mainActionAvailable && selectedDice.length == 1) //always make coin3 available
{
var coin3 = this.uiItems.getByUiType("board_spot").find(function (s) { return s.data.place == "coin3"; });
selectableItems.push(coin3);
}
}
this.uiItems.makeSelectable(selectableItems);
},
playerTravel: function (uiItem) {
var _self = this;
var playerId = this.player_id;
var figures = this.uiItems.getPlayerUiItems("figure", "board", playerId);
var selectedFigure = this.uiItems.getFirstSelectedItemByUiType("figure");
var selectedMapNode = this.uiItems.getFirstSelectedItemByUiType("map_node");
var gift = this.uiItems.getByUiType("gift").find(function (g) { return g.data.type_arg == 10 && g.data.location_arg == playerId; }); //only gift 10 is allowed in travel
if (selectedFigure == null) {
selectedFigure = this.currentMoveArgs.selectedFigure;
}
this.uiItems.makeSelectable(figures);
if (uiItem && uiItem.uiType == "gift") {
this.playerTurnSendAction([], [], [], uiItem);
}
else if (uiItem && uiItem.uiType == "figure" && figures.length > 1) {
selectedFigure = uiItem;
this.uiItems.resetAllSelectable();
this.uiItems.makeSelectable(figures);
}
else if (selectedFigure == null) {
selectedFigure = figures[0];
}
if (!selectedFigure.isSelected) {
this.uiItems.toggleSelection(selectedFigure);
}
if (figures.length > 1) //lift over map_node if more than one choice
{
if (selectedFigure && selectedMapNode) {
this.resetUiItemsZIndex(figures);
}
else {
this.forceChangeUiItemsZIndex(figures, 600);
}
}
if (gift != null) { this.uiItems.makeSelectable([gift]); }
if (selectedFigure && selectedMapNode == null) {
var currentMapNode = this.uiItems.getByUiType("map_node").find(function (m) { return m.data.id == selectedFigure.data.location_arg });
var nodes = this.uiItems.getByUiType("map_node").filter(function (m) { return _self.isMapNodeAvailable(currentMapNode, m); });
this.uiItems.makeSelectable(nodes);
}
else if (selectedFigure && selectedMapNode) {
this.sendTravel(selectedFigure, selectedMapNode);
}
},
playerPickContract: function () {
var playerId = this.player_id;
var dieValue = parseInt(this.last_server_state.args.die_value) - 1;
var validContractIds = this.last_server_state.args.valid_contract_ids.split("_");
var selectedContract = this.uiItems.getSelectedItems();
var myContracts = this.uiItems.getByUiType("contract").filter(function (c) { return c.data.location == "hand" && c.data.location_arg == playerId; });
if (selectedContract.length == 0) {
var contracts = this.uiItems.getByUiType("contract").filter(function (c) { return c.data.location == "board" && parseInt(c.data.location_arg) <= dieValue; });
this.uiItems.makeSelectable(contracts);
}
else if (selectedContract.length > 0 && myContracts.length > 1) {
myContracts = myContracts.filter(function (c) { return validContractIds.includes(c.data.id); });
this.uiItems.resetAllNotSelected();
this.uiItems.makeSelectable(myContracts);
this.setClientState("client_playerDiscardContract", { descriptionmyturn: _("${you} must select a contract to discard") });
}
else if (selectedContract.length == 1 && myContracts.length <= 1) {
this.sendPickContract(selectedContract[0], null);
}
},
playerTriggerOtherCityBonus: function (uiItem) {
var cities = [];
var cityBonus = this.uiItems.getByUiType("city_bonus");
var tradingPostOnly = this.currentMoveArgs.trading_post_only;
var offlimitCities = this.currentMoveArgs.offlimit_city_bonuses.split(',');
for (var i = 0; i < cityBonus.length; i++) {
var c = cityBonus[i];
var hasTradingPost = this.uiItems.getPlayerUiItems("trading_post", "board", this.player_id).find(function (t) { return t.data.location_arg == c.data.location_arg });
var canSelect = (tradingPostOnly && hasTradingPost) || !tradingPostOnly;
canSelect = canSelect && offlimitCities.includes(c.data.type_arg) == false;
if (canSelect) {
var city = this.uiItems.getByUiType("map_node").find(function (m) { return parseInt(m.data.id) == parseInt(c.data.location_arg) });
cities.push(city);
}
}
this.uiItems.makeSelectable(cities);
if (uiItem) {
var bonusUiItem = this.uiItems.getByUiType("city_bonus").find(function (b) { return parseInt(b.data.location_arg) == parseInt(uiItem.data.id) });
this.sendTriggerOtherCityBonus(bonusUiItem);
}
},
playerMoveTradingPost: function (uiItem) {
var items = this.uiItems.getPlayerUiItems("trading_post", "board", this.player_id);
this.uiItems.resetAllSelectable();
var mapNodeId = this.currentMoveArgs.map_node_id;
var mapNode = this.uiItems.getByUiTypeAndId("map_node", mapNodeId).htmlNode;
dojo.setStyle(mapNode, "border", "4px solid yellow");
this.forceChangeUiItemsZIndex(items, 600);
this.uiItems.makeSelectable(items);
if (uiItem) { this.uiItems.toggleSelection(uiItem); } //re-select
},
client_gift10PickBoardId: function (uiItem) {
var boardIds = this.currentMoveArgs.boardIds;
var mapNodes = this.uiItems.getByUiType("map_node").filter(function (m) { return boardIds.includes(m.data.id) });
if (uiItem) {
this.sendFulfillGift(this.currentMoveArgs.giftId, uiItem.data.id);
}
this.makeSelectable(mapNodes);
},
client_playerDiscardContract: function (uiItem) {
var selectedContract = this.uiItems.getSelectedItems();
if (selectedContract.length > 1) {
var pickedContract = selectedContract.find(function (c) { return c.data.location == "board" });
var replacedContract = selectedContract.find(function (c) { return c.data.location == "hand" });
this.sendPickContract(pickedContract, replacedContract);
}
else if (uiItem.isSelected == false && uiItem.data.location == "board") {
this.onClickCancel();
}
},
client_playerForceDiscardContract: function () {
var playerId = this.player_id;
var myContracts = this.uiItems.getByUiType("contract").filter(function (c) { return c.data.location == "hand" && c.data.location_arg == playerId; });
var selectedContract = this.uiItems.getFirstSelectedItemByUiType("contract");
this.uiItems.makeSelectable(myContracts);
if (selectedContract) {
this.onClickChooseResource(selectedContract.data.id);
}
},
client_playerForceDiscardGift: function (uiItem) {
var giftIds = this.currentMoveArgs.giftIds
var gifts = this.uiItems.getByUiType("gift").filter(
g => giftIds.includes(g.data.id)
)
var keepGiftItem = this.uiItems.getByUiType("1x_gift").find(
i => i.data.location == "player_mat"
)
if (keepGiftItem)
gifts.push(keepGiftItem)
this.uiItems.makeSelectable(gifts)
if (uiItem && uiItem.uiType == "gift") {
if (this.prefs[101].value == 0)
this.onClickChooseResource(uiItem.data.id)
else
this.confirmationDialog(
_('Are you sure you want to discard this gift?'),
dojo.hitch(this, () => { this.onClickChooseResource(uiItem.data.id) }),
dojo.hitch(this, () => {
this.uiItems.toggleSelection(uiItem)
this.client_playerForceDiscardGift(null)
})
)
}
else if (uiItem && uiItem.uiType == "1x_gift") {
if (this.prefs[101].value == 0)
this.onClickUsePlayerPiece(uiItem.data.id)
else
this.confirmationDialog(
_('Are you sure you want to use this piece? This move cannot be undone'),
dojo.hitch(this, () => this.onClickUsePlayerPiece(uiItem.data.id)),
dojo.hitch(this, () => {
this.uiItems.toggleSelection(uiItem)
this.client_playerForceDiscardGift(null)
})
)
}
},
transitionPlayerBonusAction: function (action) {
if (this.isCurrentPlayerActive() && action) {
var actionType = action.type;
switch (actionType) {
case "trigger_other_city_bonus":
this.setClientState("playerTriggerOtherCityBonus", { descriptionmyturn: _("${you} must select another city bonus"), args: { offlimit_city_bonuses: action.type_arg, trading_post_only: false } });
break;
default:
this.setClientState("playerChooseResource", { descriptionmyturn: _("${you} must choose resources"), args: { action: action } });
break;
}
}
},
transitionPlayerChooseResource: function (action) {
if (this.isCurrentPlayerActive() && action) {
var actionType = action.type;
switch (actionType) {
case "discard_contract":
this.setClientState("client_playerForceDiscardContract", { descriptionmyturn: _("${you} must select a contract to discard") });
this.client_playerForceDiscardContract(); //todo odd? why not rely on statemachine?
break;
case "discard_gift":
var selectableGiftIds = action.type_arg.split('_');
var discardGiftArgs = { "num_remaining": action.remaining_count, "giftIds": selectableGiftIds };
this.setClientState("client_playerForceDiscardGift", { descriptionmyturn: _("${you} must discard gifts (remaining: ${num_remaining})"), args: discardGiftArgs });
this.client_playerForceDiscardGift(null); //todo odd? why not rely on statemachine?
break;
}
}
},
/* Highlight Player bonuses like Matteo Polo's extra contract */
highlightBonuses: function (bonuses) {
var items = [];
var playerId = this.player_id;
bonuses = bonuses.filter(function (b) { return b.player_id == playerId; });
for (var i = 0; i < bonuses.length; i++) {
var bonus = bonuses[i];
if (bonus.bonus_type == "character" && bonus.bonus_type_arg == "contract") //mateo polo
{
var spot = this.uiItems.getByUiType("character_spot").find(function (c) { return c.data.character_type == 7 });
spot.data.bonusId = bonus.id;
items.push(spot);
}
else if (bonus.bonus_type == "character" && bonus.bonus_type_arg == "pick_gift") //Fratre Nicoalo
{
var spot = this.uiItems.getByUiType("character_spot").find(function (c) { return c.data.character_type == 8 });
spot.data.bonusId = bonus.id;
items.push(spot);
}
else if (bonus.bonus_type == "city_bonus") {
var spot = this.uiItems.getByUiType("city_bonus").find(function (c) { return c.data.type_arg == bonus.bonus_location });
spot = this.uiItems.getByUiTypeAndId("map_node", spot.data.location_arg);
spot.data.bonusId = bonus.id;
items.push(spot);
}
}
this.uiItems.makeSelectable(items);
},
showCharacterSelection: function (characterTypes) {
var items = [];
for (var i = 0; i < characterTypes.length; i++) {
var charType = characterTypes[i].character_type;
var charUiItem = this.uiItems.getByUiType("character").find(function (c) { return c.data.character_type == charType; });
if (charUiItem == null) {
charUiItem = this.uiItems.createAndAddItem("character", { player_id: "99", character_type: charType });
dojo.place(charUiItem.htmlNode, "characterSelection", "first");
}
items.push(charUiItem);
}
if (this.gamedatas.players[this.player_id]) {
this.uiItems.makeSelectable(items);
}
if (this.isCurrentPlayerActive()) {
dojo.setStyle("btnConfirm", "display", "none");
}
},
transitionPlayerBonusState: function (pendingActions, pendingBonuses) {
if (!this.isCurrentPlayerActive()) //not active player, nothing to do
return;
var playerId = this.player_id;
var myPendingAction = pendingActions.find(function (a) { return a.pending_player_id == playerId; });
var myPendingBonuses = pendingBonuses.filter(function (b) { return b.player_id == playerId; });
if (myPendingAction != null) {
this.uiItems.resetAllNotSelected();
this.transitionPlayerBonusAction(myPendingAction);
}
else if (myPendingBonuses) {
this.uiItems.resetAllSelectable();
this.highlightBonuses(myPendingBonuses); //highlight bonuses available
}
},
// 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) {
this.currentMove = stateName;
this.currentMoveArgs = args.args;
switch (stateName) {
case 'pickCharacter':
dojo.setStyle("characterSelection", "display", "block");