-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigmonster.js
2820 lines (2646 loc) · 144 KB
/
bigmonster.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>
* BigMonster implementation : © Nicolas Matton (nicolas@locla.be)
*
* 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.
* -----
*
* bigmonster.js
*
* BigMonster user interface script
*
* In this file, you are describing the logic of your user interface, in Javascript language.
*
*/
var isDebug = window.location.host == 'studio.boardgamearena.com' || window.location.hash.indexOf('debug') > -1; // add '#debug' to end of url on prod to enable debug logging
var debug = isDebug ? console.info.bind(window.console) : function () {};
// Load production bug report handler
define([
"dojo","dojo/_base/declare",
"ebg/core/gamegui",
"ebg/counter",
"ebg/scrollmap",
"ebg/stock",
g_gamethemeurl + "modules/scroller.js",
g_gamethemeurl + 'modules/modal.js',
],
function (dojo, declare) {
return declare("bgagame.bigmonster", ebg.core.gamegui, {
constructor: function(){
this.SCALE = 100;
this.tiledwidth = 50;
this.tileheight = 100;
this.medalwidth = 100;
this.medalheight = 100;
this.tilePerRow = 9; // number of tile per row on sprited tile image
this.tiles_img_path = 'img/monster_tiles_stock_v2.jpg'; // tile image path for stock component
this.tile_selected = false; // check if user has selected a tile in his hand
this.explo_selected = false; // check if user has selected a starting tile
this.busyShips = [] // list of ships selected in the current turn. Re-initialized at each turn. in INT type !!
this.buttonAdded = false; // button on the last tile selection
this.game_mode = 1; // 1 for indiv play; 2 for team play
this.explorers = {}; // list of explorers
this.explorer_id; // current player's explorer id
this.current_move = '0'; // current move of the player
this.possible_explorers = []; // list of possible explorers
this.selected_row = 0; // currently row in use (in 2-3 players)
this.selected_tile_id = 0; // id of the selected tile (in 2-3 players)
this.active_row = 0; // row that can be selected (0 = both; 1 = upper; 2 = lower) (in 2-3 players)
this.selected_tile_type = 0; // type of monster selected
this.hide_tiles = false; // hide tiles when countdown is running
this.coutdownpassed = false // coutdown expired
this.concurentselect = true // concurent selection in 2-3 p. mode
this.SelectedPlayTile = undefined // 2-3p variant - concurent mode, card selected to be played
this.SelectedDiscardTile = undefined // 2-3p variant - concurent mode, card selected to be discarded
},
/*
setup:
*/
setLoader(value, max) {
this.inherited(arguments);
if (!this.isLoadingComplete && value >= 100) {
this.isLoadingComplete = true;
this.onLoadingComplete();
}
},
onLoadingComplete() {
if (this.hidescore) {
debug(this.gamedatas.players)
for (var o = Object.keys(this.gamedatas.players).length - 1; o >= 0; o--) {
pid = Object.keys(this.gamedatas.players)[o];
debug(pid)
$('player_score_' + pid).innerHTML='?';
this.addTooltip( 'player_score_' + pid, _('live score is hidden by table option'), '', 10 )
}
}
},
setup: function( gamedatas )
{
// Create a new div for buttons to avoid BGA auto clearing it
dojo.place("<div id='customActions' style='display:inline-block'></div>", $('generalactions'), 'after');
this.isTeamPlay = gamedatas.isTeamPlay;
if (this.isTeamPlay) this.game_mode = 2;
this.teams = gamedatas.teams;
this.teams_values = Object.values(this.teams).filter(this.onlyUnique)
this.team_defined = toint(gamedatas.teamdefined);
this.team_ui_setup = false;
// **SCROLL AREAS** //
this.currentPlayer = this.player_id;
this.nums_of_players = Object.keys(gamedatas.players).length;
this.boards = [];
this.hidescore = gamedatas.hidescore;
this.is3pdraft = gamedatas.is3pdraft;
centerscroll = false;
if (!this.isTeamPlay) {
// individual game setup
for (var t of Object.keys(gamedatas.players)) {
this.boards[t] = new Scroller(ebg.scrollmap(),t, 0);
}
if (this.isSpectator) {
for (var o = Object.keys(gamedatas.players).length - 1; o >= 0; o--)
dojo.place( Object.keys(gamedatas.players)[o] + "_scrollmap_wrapper", "Boards", "after");
for (o = Object.keys(gamedatas.players).length - 1; o >= 0; o--) {
dojo.place( Object.keys(gamedatas.players)[o] + "_scrollmap_wrapper", "Boards", "after");
if ( Object.keys(gamedatas.players)[o] == this.currentPlayer)
break
}
} else {
for (var o = gamedatas.playerorder.length - 1; o >= 0; o--)
dojo.place(gamedatas.playerorder[o] + "_scrollmap_wrapper", "Boards", "after");
for (o = gamedatas.playerorder.length - 1; o >= 0; o--) {
dojo.place(gamedatas.playerorder[o] + "_scrollmap_wrapper", "Boards", "after");
if (gamedatas.playerorder[o] == this.currentPlayer)
break
}
}
centerscroll = true;
} else if (this.isTeamPlay && this.team_defined) {
this.team_ui_setup = true;
// team setup -- only create scroll area when teams are defined - nothing to show otherwise
this.player_team = this.teams[this.currentPlayer];
for (var t of Object.keys(gamedatas.players)) {
this.boards[t] = new Scroller(ebg.scrollmap(),t, 0);
}
this.teams_ordered = [];
this.teams_values.forEach(element => {
this.teams_ordered[element] = Object.keys(this.teams).filter(key => this.teams[key] == element);
});
debug('adding team setup')
dojo.query('.player_info_team').style('display','block')
// start by placing other teams
for (let team = this.teams_ordered.length-1; team >= 0; team--) {
if (Object.hasOwnProperty.call(this.teams_ordered, team)) {
const team_members = this.teams_ordered[team];
team_members.forEach(e => {
if (team != this.player_team) {
let team_color = this.gamedatas["players"][this.teams_ordered[team][0]]['color']
dojo.place(e + "_scrollmap_wrapper", "Boards", "after"); // place the scroll area on right place
dojo.style(e + '_team_info','background-color','#'+team_color); // set the team color
$(e + '_team_info').innerHTML='TEAM ' + (toint(team) + 1); // set the team name
// add banner on player miniboard
let tbDiv = this.format_block('jstpl_team_banner', {
color : '#'+team_color,
team_nr: toint(team) + 1
});
player_board = $('player_board_'+e)
dojo.place( tbDiv , player_board);
}
});
}
}
if (!this.isSpectator) {
// place current player scroll area
let team_color = this.gamedatas["players"][this.teams_ordered[this.player_team][0]]['color']
let teammate = this.getOtherTeamMember(this.teams, this.player_team, this.currentPlayer)
dojo.place(this.player_id + "_scrollmap_wrapper", "Boards", "after"); // place current player's scroll area just after Boards
// styling current player scroll area
dojo.style(this.player_id+'_team_info','background-color','#'+team_color);
$(this.player_id + '_team_info').innerHTML='TEAM ' + (toint(this.player_team) + 1); // set the team name
// add banner on player miniboard
let tbDiv = this.format_block('jstpl_team_banner', {
color : '#'+team_color,
team_nr: toint(this.player_team) + 1
});
player_board = $('player_board_'+this.player_id)
dojo.place( tbDiv , player_board);
// place other teams player's scroll areas just after current player's scroll area
dojo.place(teammate + "_scrollmap_wrapper", this.player_id + "_scrollmap_wrapper", "after");
dojo.style(teammate+'_team_info','background-color','#'+team_color);
$(teammate + '_team_info').innerHTML='TEAM ' + (toint(this.player_team) + 1); // set the team name
// add banner on player miniboard
player_board = $('player_board_'+teammate)
dojo.place( tbDiv , player_board);
}
centerscroll = true;
}
if (centerscroll) {
for (var t of Object.keys(gamedatas.players)) {
if (Object.keys(this.boards).includes(t)) {
this.boards[t].scrollTo(-this.SCALE / 2, -this.SCALE / 2)
}
}
}
// ** EXPLORER TILES ** //
this.explorers = this.gamedatas.explorers;
for (var i in this.gamedatas.explorers) {
var explorer_id = this.explorers[i]['explorer_id'];
if (i == this.player_id) {
this.explorer_id = explorer_id;
}
this.placeTile(i, explorer_id,explorer_id, 0,0,0,1,0);
let explo_info = gamedatas.help_explorers[explorer_id]['descr'];
if (this.nums_of_players >= 4 || this.is3pdraft) {
this.addTooltip( 'tile_e_'+explorer_id, _(explo_info) , _('draft cards to this player'), 10 )
} else {
this.addTooltip( 'tile_e_'+explorer_id, _(explo_info) ,'', 10 )
}
}
if (this.nums_of_players >= 4 || this.is3pdraft) {
dojo.query('.bm_exploTileClass').connect('onclick', this, 'onClickExplo');
}
// ** CARDS ON PLAYER BOARDS ** //
for (var card_id in this.gamedatas.cardsonboard) {
let player_id = this.gamedatas.cardsonboard[card_id]['card_location_arg'];
let monster_type = toint(this.gamedatas.cardsonboard[card_id]['card_type']);
let monster_kind = toint(this.gamedatas.cardsonboard[card_id]['card_type_arg']);
let mutation = toint(this.gamedatas.cardsonboard[card_id]['mutation']);
let tileNum = (monster_type - 1 ) * 10 + monster_kind - 1;
const [x, y, rot] = this.convert_coord(this.gamedatas.cardsonboard[card_id]['board_x'] , this.gamedatas.cardsonboard[card_id]['board_y'], monster_type);
this.placeTile(player_id, tileNum, card_id, x, y, rot, 0, mutation);
}
// ** SCORING BOARD SETUP ** //
if (this.isTeamPlay && this.team_defined) {
this.setTeamsScoringBoard();
} else if (!this.isTeamPlay) {
// setup score board for indivudual play
for (var player_id of Object.keys(gamedatas.players)) {
var player = gamedatas.players[player_id];
// Set up scoring table in advance (helpful for testing!)
let splitPlayerName = '';
let chars = player.name.split("");
for (let i in chars) {
splitPlayerName += `<span>${chars[i]}</span>`;
}
$('scoring-row-players').innerHTML += `<td><span id="scoring-row-name-p${player_id}" style="color:#${player.color};"><span>${splitPlayerName}</span></span></td>`;
$('scoring-row-ice').innerHTML += `<td id="scoring-row-ice-p${player_id}"></td>`;
$('scoring-row-bigmonster').innerHTML += `<td id="scoring-row-bigmonster-p${player_id}"></td>`;
$('scoring-row-lava').innerHTML += `<td id="scoring-row-lava-p${player_id}"></td>`;
$('scoring-row-grassland').innerHTML += `<td id="scoring-row-grassland-p${player_id}"></td>`;
$('scoring-row-swamp').innerHTML += `<td id="scoring-row-swamp-p${player_id}"></td>`;
$('scoring-row-diamonds').innerHTML += `<td id="scoring-row-diamonds-p${player_id}"></td>`;
$('scoring-row-explorer').innerHTML += `<td id="scoring-row-explorer-p${player_id}"></td>`;
$('scoring-row-medal').innerHTML += `<td id="scoring-row-medal-p${player_id}"></td>`;
$('scoring-row-total').innerHTML += `<td id="scoring-row-total-p${player_id}"></td>`;
}
// Add an extra column at the end, just for padding reasons
$('scoring-row-players').innerHTML += `<td></td>`;
$('scoring-row-ice').innerHTML += `<td></td>`;
$('scoring-row-bigmonster').innerHTML += `<td></td>`;
$('scoring-row-lava').innerHTML += `<td></td>`;
$('scoring-row-grassland').innerHTML += `<td></td>`;
$('scoring-row-swamp').innerHTML += `<td></td>`;
$('scoring-row-diamonds').innerHTML += `<td></td>`;
$('scoring-row-explorer').innerHTML += `<td></td>`;
$('scoring-row-medal').innerHTML += `<td></td>`;
$('scoring-row-total').innerHTML += `<td></td>`;
// remove the "team score total" row as it is individual play
dojo.destroy('scoring-row-teamtotal');
}
// **** TILES AND HAND MANAGEMENT **** //
// ** Create hands of tiles ** //
if (this.nums_of_players >= 4 || this.is3pdraft) {
if (!this.isSpectator) {
// 4 or more players, or 3 player draft
// remove board title
dojo.destroy('bm_title_board');
// remove pile card count (the rem cards is visible in hand)
dojo.destroy('card_left_count');
// remove "gridded" class
dojo.query('#myhand_area').removeClass('bm_gridded')
this.playerHand = new ebg.stock(); // new stock object for hand
this.playerHand.create( this, $('myhand'), this.tiledwidth, this.tileheight );
this.playerHand.setSelectionMode( 1 ) // only one card at a time can be selected
this.playerHand.extraClasses='bm_margin_stock';
this.playerHand.image_items_per_row = this.tilePerRow;
var pos = 0;
var kind_per_type = [4,2,2,5,1,9,1,6,1];
for (var type = 1; type <= 8; type++) {
for (var kind_monster = 1; kind_monster <= kind_per_type[type-1]; kind_monster++) {
// Build card type id
var card_type_id = this.getCardUniqueId(type, kind_monster);
pos = (type - 1) * 9 + kind_monster - 1
this.playerHand.addItemType(card_type_id, card_type_id, g_gamethemeurl + this.tiles_img_path, pos);
}
}
// ** SET CARDS ON HANDS ** //
for ( var i in this.gamedatas.hand) {
var card = this.gamedatas.hand[i];
var type = toint(card.type);
var kind_monster = toint(card.type_arg);
this.playerHand.addToStockWithId(this.getCardUniqueId(type, kind_monster), card.id);
this.setTileToolTip(card.id, type, kind_monster);
}
// add listeners on cards on hand
dojo.connect( this.playerHand, 'onChangeSelection', this, 'onPlayerHandSelectionChanged' );
} else {
dojo.destroy('myhand_wrap');
dojo.query('.hand_wrapper').style('display','none')
}
} else {
// 2 or 3 players, variant mode
// remove the ship area
dojo.destroy('shipwrap');
// remove my hand title
dojo.destroy('bm_title_myhand');
// update the number in card remaining count
dojo.query('#card_counter').innerHTML(this.gamedatas.remaining_piles)
// rename my_hand div and add extra row for tiles selection
$('myhand').id='upper_row';
dojo.place( "<div id='lower_row' class='whiteblock bm_tileArea'></div>" , $('myhand_area'));
this.upper_row = new ebg.stock();
this.lower_row = new ebg.stock();
this.upper_row.create( this, $('upper_row'), this.tiledwidth, this.tileheight );
this.lower_row.create( this, $('lower_row'), this.tiledwidth, this.tileheight );
if (!this.isSpectator && !this.concurentselect) {
console.log('succesive tile selection mode')
this.upper_row.setSelectionMode( 1 ) // only one card at a time can be selected
this.lower_row.setSelectionMode( 1 ) // only one card at a time can be selected
dojo.connect( this.upper_row, 'onChangeSelection', this, 'onTileInRowSelection' );
dojo.connect( this.lower_row, 'onChangeSelection', this, 'onTileInRowSelection' );
} else {
console.log('concurent tile selection mode')
this.lower_row.setSelectionMode( 0 )
this.upper_row.setSelectionMode( 0 )
}
this.upper_row.extraClasses='bm_margin_stock';
this.lower_row.extraClasses='bm_margin_stock';
this.upper_row.image_items_per_row = this.tilePerRow;
this.lower_row.image_items_per_row = this.tilePerRow;
var pos = 0;
var kind_per_type = [4,2,2,5,1,9,1,6,1];
for (var type = 1; type <= 8; type++) {
for (var kind_monster = 1; kind_monster <= kind_per_type[type-1]; kind_monster++) {
// Build card type id
var card_type_id = this.getCardUniqueId(type, kind_monster);
pos = (type - 1) * 9 + kind_monster - 1
this.upper_row.addItemType(card_type_id, card_type_id, g_gamethemeurl + this.tiles_img_path, pos);
this.lower_row.addItemType(card_type_id, card_type_id, g_gamethemeurl + this.tiles_img_path, pos);
}
}
// SET CARDS ON ROWS
this.active_row = this.gamedatas.active_row;
for ( var i in this.gamedatas.tilesonrows) {
var card = this.gamedatas.tilesonrows[i];
var row = card.location_arg;
var type = card.type;
var kind_monster = card.type_arg;
if (toint(row) == 1) {
let rowname = 'upper_row';
this.upper_row.addToStockWithId(this.getCardUniqueId(type, kind_monster), card.id);
this.setTileToolTip(card.id, type, kind_monster, rowname); // add tooltip
let tileoptionsDiv = this.format_block('jstpl_card_menu', {tile_id : card.id, row : rowname});
this.concurentselect && dojo.place(tileoptionsDiv, "upper_row_item_"+card.id, "first") // add menu options
} else if (toint(row) == 2) {
let rowname = 'lower_row';
this.lower_row.addToStockWithId(this.getCardUniqueId(type, kind_monster), card.id);
this.setTileToolTip(card.id, type, kind_monster, rowname);
let tileoptionsDiv = this.format_block('jstpl_card_menu', {tile_id : card.id, row : rowname});
this.concurentselect && dojo.place(tileoptionsDiv, "lower_row_item_"+card.id, "first") // add menu options
} else {
// row is equal to player ID => selected card
if (toint(this.active_row) == 1) {
var tilerow = this.upper_row;
var rowname = 'upper_row';
} else {
var tilerow = this.lower_row;
var rowname = 'lower_row';
}
tilerow.addToStockWithId(this.getCardUniqueId(type, kind_monster), card.id);
dojo.addClass(rowname + '_item_'+card.id , 'selected');
dojo.addClass(rowname + '_item_'+card.id , 'disabled');
this.setTileToolTip(card.id, type, kind_monster, rowname);
let tileoptionsDiv = this.format_block('jstpl_card_menu', {tile_id : card.id, row : rowname});
this.concurentselect && dojo.place(tileoptionsDiv, rowname+"_item_"+card.id, "first") // add menu options
this.selected_tile_id = toint(card.id);
this.changePageTitle('discard');
this.selected_tile_type = card.type;
}
this.concurentselect && this.connectbuttons(card.id)
}
// if a row is active (meaning that at least one card is selected/played on that row), make the other row disabled
if (toint(this.active_row) == 1) {
var id_list = this.lower_row.getAllItems();
var rowname = "lower_row";
this.lower_row.setSelectionMode( 0 )
} else if (toint(this.active_row) == 2) {
var id_list = this.upper_row.getAllItems();
var rowname = "upper_row";
this.upper_row.setSelectionMode( 0 )
}
if (toint(this.active_row) > 0) {
id_list.forEach(element => {
dojo.addClass(rowname+'_item_'+element['id'], 'disabled');
});
}
}
// **** MEDAL MANAGEMENT ***** //
// prepare area for medals in players boards
for (var t of Object.keys(gamedatas.players)) {
let maDiv = this.format_block('jstpl_player_board_medal_zone', {
player_id : t
});
player_board = $('player_board_'+t)
dojo.place( maDiv , player_board);
}
this.medals_status = [];
// insert medals
for (const i in this.gamedatas.medals) {
if (Object.hasOwnProperty.call(this.gamedatas.medals, i)) {
const medal = this.gamedatas.medals[i];
let medal_id = toint(medal['medal_id']);
let info_id = toint((medal_id>10) ? Math.floor(medal_id/10):medal_id);
let medal_type = (medal_id>10)?2:1;
let location_id = medal['player_id'];
// !! DIRTY HOTFIX !!
if (medal['back_id'] === null) {
var back_id = 2;
} else {
var back_id = medal['back_id'];
}
if (location_id == 0) {
// add the medal on stock of medals
let cardDiv = this.format_block('jstpl_medal_player_stock', {
medal_id : medal_id,
data_id: info_id,
type : medal_type,
back_id : back_id
});
medal_area = $('medals')
dojo.place( cardDiv , medal_area);
this.medals_status[medal_id] = false;
} else {
// add the medal to the player area
location_id_list = location_id.split(',');
location_id_list.forEach(element => {
let player_id = toint(element);
var player_medal_zone_div = $('ma_'+player_id);
dojo.place( this.format_block('jstpl_medal_player_area',{
medal_id : medal_id,
data_id: info_id,
player_id : player_id,
type : medal_type,
back_id : back_id} ), player_medal_zone_div );
});
this.medals_status[medal_id] = true;
}
let medal_info = (medal_type == 1 ) ? gamedatas.help_medals[medal_id]['name'] : gamedatas.help_medals[info_id]['name_team'];
this.addTooltip( 'medal_'+medal_id+'_'+medal_type, medal_info, '', 10 )
this.addTooltip( 'back_medal_'+medal_id, _(medal_info), '', 0 )
}
}
if (this.isTeamPlay) {
let placed_groups = [];
for (let i in this.medals_status){
if (!this.medals_status[i]) {
if (i>10) {
if (!placed_groups.includes(toint(Math.floor(i/10)))) {
let groupDiv = this.format_block('jstpl_medal_group', {
medal_group : Math.floor(i/10)
});
dojo.place(groupDiv, 'medals');
placed_groups.push(toint(Math.floor(i/10)));
}
dojo.place('stock_'+i, 'bottom_'+Math.floor(i/10));
if (i % 10 == 2) {
dojo.query('#stock_'+i).addClass('second');
}
} else {
if (!placed_groups.includes(toint(i))) {
let groupDiv = this.format_block('jstpl_medal_group', {
medal_group : i
});
dojo.place(groupDiv, 'medals');
placed_groups.push(toint(i));
}
dojo.place('stock_'+i, 'top_'+i);
}
}
}
this.rearrange_medals();
}
if (this.nums_of_players < 4 && $('ma_'+this.gamedatas.first_player) && this.gamedatas.first_player != 0 && !this.is3pdraft) {
// insert first player medal
var player_medal_zone_div = $('ma_'+this.gamedatas.first_player);
dojo.place( this.format_block('jstpl_first_player_medal',{}), player_medal_zone_div, 'first');
this.addTooltip( 'firstplayermedal', _('first player'), '', 10 )
}
// ** HELP AND OPTION ** //
if (this.isSpectator) {
dojo.place(jstpl_settings, document.querySelector('.player-board.spectator-mode'))
dojo.query('.player-board.spectator-mode .roundedbox_main').style('display', 'none');
} else {
dojo.place(jstpl_settings, 'ma_' + this.player_id, 'after');
}
dojo.connect($('help-icon'), 'click', () => this.displayPlayersHelp(this.nums_of_players));
let chk = $('face_select');
dojo.connect(chk, 'onchange', () => this.toggleMedalFace());
let center_btn = $('general_center_btn');
dojo.connect(center_btn, 'click', () => this.onScreenWidthChange());
// **** SHIP TILES MANAGEMENT **** //
// ** add listeners on ship tiles ** //
for (o = Object.keys(gamedatas.players).length - 1; o >= 0; o--) {
dojo.query("div#ship_" + Object.keys(gamedatas.players)[o]).connect("onclick", this, "onClickShipTile")
}
// ** add listeners on expand/reduce ship area ** //
dojo.connect( $('reduce_ships'), 'onclick', this, 'onClickCloseShipArea' );
dojo.connect( $('expand_ships'), 'onclick', this, 'onClickOpenShipArea' );
// ** set the selectable status of ships ** //
var turn_n = Math.ceil(toint(gamedatas.gamestate.updateGameProgression) * 17/100) - 1;
for (var pid in gamedatas.cardsOnShips) {
if (gamedatas.gamestate.name == "tileSelection") {
this.busyShips.push(toint(pid));
}
if (Object.keys(gamedatas.cardsonshiporigin).length > 0) {
// this check is usefull for to avoid breaking ongoing game without all the tables
let color = this.gamedatas.players[gamedatas.cardsonshiporigin[pid]]['color']
dojo.place( "<div id='tileOnShip_"+ pid +"_"+turn_n+"' class='bm_tileClass backtile' style='outline: solid #" + color + " 2px'></div>", "ship_" + pid, "last" );
} else {
dojo.place( "<div id='tileOnShip_"+ pid +"_"+turn_n+"' class='bm_tileClass backtile'></div>", "ship_" + pid, "last" );
}
}
/* Tooltips */
if ($('card_left_count')) {
this.addTooltip('card_left_count',_('remaing pile of cards (i.e. the number of remaining turns)'),'',50)
}
this.addTooltip('face_select',_('toggle side of medals'),'',50)
this.addTooltip('general_center_btn',_('center all play zones'),'',50)
this.addTooltip('help-icon', '', _('Display game help'));
// Setup game notifications to handle (see "setupNotifications" method below)
this.setupNotifications();
},
///////////////////////////////////////////////////
//// 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 )
{
debug('onEnteringState: '+stateName);
debug(args);
switch( stateName )
{
case 'explorerSelection':
if (!this.isReadOnly() || g_archive_mode) {
explorers = args.args['_private']['explorers'];
// show a popup for selecting explorer
// active only for players (not for spectators)
if (explorers.length == 2 || g_archive_mode) {
// only process when choice is still possible (if length < 2, it means that the player already made its choice)
popupcontent = '<div id="bm_popup" class ="bm_popin"><h2 id="popin_playersHelp_title" class="bm_popin_title">'+_('Select a starting explorer')+'</h2><div id="selectingExploDiv">'
for (var i = 0; i < explorers.length; i++) {
popupcontent += '<div id="explo' + i + '">'
var explo_id = explorers[i]['explorer_id'];
var explo_info = explorers[i]['explorer_info'];
popupcontent += this.tileHtml(explo_id, explo_id, 0, 0, 1, 1, 0);
popupcontent += '<p id="exploInfo"> ' + _(explo_info) + '</p></div>'
}
popupcontent += '</div><button id="conf_expl_btn" class="bm_accept-pending">Confirm selection</button></div>'
dojo.place(popupcontent, "handmedal_area", "before");
for (var i = 0; i < explorers.length; i++) {
var explo_id = explorers[i]['explorer_id'];
dojo.query("#tile_" + explo_id).connect("onclick", this, "onClickStartTile")
this.possible_explorers.push(explo_id);
}
}
}
// if team mode, add the team specific assets
if (this.isTeamPlay && !this.team_ui_setup) {
// add the scroll areas (where tiles are displayed) + banner on player miniboard
this.teams = args.args['team'];
this.teams_values = Object.values(this.teams).filter(this.onlyUnique)
this.team_ui_setup = true;
player_team = this.teams[this.currentPlayer];
for (var t of Object.keys(this.gamedatas.players)) {
this.boards[t] = new Scroller(ebg.scrollmap(),t, 0);
}
this.teams_ordered = [];
this.teams_values.forEach(element => {
this.teams_ordered[element] = Object.keys(this.teams).filter(key => this.teams[key] == element);
});
debug('adding team display');
dojo.query('.player_info_team').style('display','block')
// start by placing other teams
for (let team = this.teams_ordered.length-1; team >= 0; team--) {
if (Object.hasOwnProperty.call(this.teams_ordered, team)) {
debug('processing team '+ team);
const team_members = this.teams_ordered[team];
debug('team_members '+ team_members);
team_members.forEach(e => {
if (team != player_team) {
debug('placing team '+ team + ' for player '+ e);
let team_color = this.gamedatas["players"][this.teams_ordered[team][0]]['color']
dojo.place(e + "_scrollmap_wrapper", "Boards", "after"); // place the scroll area on right place
dojo.style(e + '_team_info','background-color','#'+team_color); // set the team color
$(e + '_team_info').innerHTML='TEAM ' + (toint(team) + 1); // set the team name
// add banner on player miniboard
let tbDiv = this.format_block('jstpl_team_banner', {
color : '#'+team_color,
team_nr: toint(team) + 1
});
player_board = $('player_board_'+e)
dojo.place( tbDiv , player_board);
}
});
}
}
if (!this.isSpectator) {
// place current player scroll area
debug('placing current player '+ this.currentPlayer);
let team_color = this.gamedatas["players"][this.teams_ordered[player_team][0]]['color']
let teammate = this.getOtherTeamMember(this.teams, player_team, this.currentPlayer)
dojo.place(this.player_id + "_scrollmap_wrapper", "Boards", "after"); // place current player's scroll area just after Boards
// styling current player scroll area
dojo.style(this.player_id+'_team_info','background-color','#'+team_color);
$(this.player_id + '_team_info').innerHTML='TEAM ' + (toint(player_team) + 1); // set the team name
// add banner on player miniboard
let tbDiv = this.format_block('jstpl_team_banner', {
color : '#'+team_color,
team_nr: toint(player_team) + 1
});
player_board = $('player_board_'+this.player_id)
dojo.place( tbDiv , player_board);
// place other teams player's scroll areas just after current player's scroll area
debug('placing other teams player scroll areas');
dojo.place(teammate + "_scrollmap_wrapper", this.player_id + "_scrollmap_wrapper", "after");
dojo.style(teammate+'_team_info','background-color','#'+team_color);
$(teammate + '_team_info').innerHTML='TEAM ' + (toint(player_team) + 1); // set the team name
// add banner on player miniboard
player_board = $('player_board_'+teammate)
dojo.place( tbDiv , player_board);
}
for (var t of Object.keys(this.gamedatas.players)) {
if (this.boards[t]) {
this.boards[t].scrollTo(-this.SCALE / 2, -this.SCALE / 2)
}
}
// set the scoring table
this.setTeamsScoringBoard();
}
break;
case 'tileSelection':
debug(args)
debug(args.args['coutdowntime'])
if (args.args['countcards'][this.player_id] == 2) {
this.lastTurn = true;
this.changePageTitle('lasttile');
}
debug($('bm_popup'))
if ($('bm_popup')) {
// remove explo selection popup (that remains on replay mode)
debug('removing explo popup')
dojo.destroy('bm_popup')
}
if (args.args['coutdowntime'] > 0 && !this.isReadOnly() && this.bRealtime && !this.coutdownpassed) {
this.coutdowntime = args.args['coutdowntime'];
// set countdown
debug('coutdowntime '+ this.coutdowntime);
// hide the tiles if present
if (dojo.query('.stockitem').length > 0) {
dojo.query('.bm_margin_stock').addClass('bm_stock_invisible')
dojo.query('.bm_margin_stock').addClass('bm_stock_hide')
} else {
this.hide_tiles = true; // in case the tiles are not yet on the board (late notification)
}
dojo.removeClass("bm_countdown", "bm_invisible");
this.countdown = setInterval(() => {
dojo.query("#bm_countdown > i").addClass('bm_countdown_anim')
document.querySelector("#bm_countdown > i").innerText=this.coutdowntime;
this.coutdowntime--;
if (this.coutdowntime == -1) {
clearInterval(this.countdown);
this.countdown = null;
this.hide_tiles = false;
dojo.addClass("bm_countdown", "bm_invisible");
dojo.query('.bm_margin_stock').removeClass('bm_stock_hide');
document.body.offsetHeight;
dojo.query('.bm_margin_stock').removeClass('bm_stock_invisible');
}
}, 1000);
} else {
this.coutdownpassed = true
}
dojo.query(".possibleMoveV").forEach(function(node, index, nodelist){
dojo.destroy(node);
});
dojo.query(".possibleMoveH").forEach(function(node, index, nodelist){
dojo.destroy(node);
});
break;
case 'var_tileSelection':
this.active_row = args.args[0];
dojo.query('.stockitem').removeClass('bm_unselectable')
let rowname = this.active_row == 1 ? 'upper_row' : 'lower_row';
debug('this.active_row', this.active_row)
debug('active row '+ rowname);
debug(dojo.query('.cardmenu[data-row="'+rowname+'"]'))
debug(dojo.query('.cardmenu[data-row="'+rowname+'"]').length)
if (this.isCurrentPlayerActive()) {
this.addActionButton( 'ValidateSelectionbutton', _('Validate selection'), 'onValidateTileSelection','customActions' );
dojo.addClass( 'ValidateSelectionbutton', 'disabled');
}
if (this.active_row == 0) {
// reactivate all tiles
if (!this.isSpectator && !this.concurentselect) {
// keep selectionmode 0 if spectator or concurent select
this.upper_row.setSelectionMode( 1 );
this.lower_row.setSelectionMode( 1 );
}
dojo.query('.stockitem').removeClass('disabled')
} else if (this.concurentselect && (dojo.query('.cardmenu[data-row="'+rowname+'"]').length == 2 || dojo.query('.cardmenu[data-row="'+rowname+'"]').length == 3)) {
let rowname = this.active_row == 1 ? 'upper_row' : 'lower_row'
let otherrowname = this.active_row == 1 ? 'lower_row' : 'upper_row'
dojo.query('[id^="play"][data-row="'+rowname+'"]').addClass('bm_singleselect')
dojo.query('[id^="discard"][data-row="'+rowname+'"]').addClass('bm_stock_hide')
dojo.query('[id^="'+otherrowname+'_item_"]').addClass('bm_unselectable')
}
if (this.new_turn) {
// update the first player token position
let active_player = this.getActivePlayers()[0]
if (!$('firstplayermedal')) {
var player_medal_zone_div = $('ma_'+active_player);
dojo.place( this.format_block('jstpl_first_player_medal',{}), player_medal_zone_div, 'first');
} else {
this.slideToObjectRelative( "firstplayermedal", "ma_" +active_player, 1000, 0, 'first');
}
// update the number of cards remaing
if (toint($('card_counter').innerHTML) > 0) {
dojo.query('#card_counter').innerHTML(toint($('card_counter').innerHTML)-1)
}
this.new_turn=false;
}
if ($('bm_popup')) {
// remove explo selection popup (that remains on replay mode)
debug('removing explo popup')
dojo.destroy('bm_popup')
}
dojo.query(".possibleMoveV").forEach(function(node, index, nodelist){
dojo.destroy(node);
});
dojo.query(".possibleMoveH").forEach(function(node, index, nodelist){
dojo.destroy(node);
});
if (this.isSpectator || !this.isCurrentPlayerActive()) {
dojo.query('.stockitem').addClass('bm_unselectable')
}
break;
case 'placeTile':
if (!this.isSpectator){
if (this.playerHand.items.length > 0) {
// some card on hand to be placed (if length = 0 means that the tile has been placed previously)
var pos = args.args['_private']['possibleMoves'];
var hdir = args.args['_private']['placement_dirH'];
var vdir = args.args['_private']['placement_dirV'];
var dir;
var kind_monter = Math.floor(this.playerHand.items[0].type / 10);
this.lastPossibleMoveIdx = [0,0]; // initiate recorded move position
this.dbMovepos = [0,0]; // selected pos to send to DB
// show the available places to put tiles
for (var idx in pos) {
if (kind_monter == 1) {
if (hdir[idx] == "X") {
continue;
}
dir = hdir[idx]
} else {
if (vdir[idx] == "X") {
continue;
}
dir = vdir[idx]
}
this.addPossiblePlacement(pos[idx][0],pos[idx][1], dir);
}
// connect click event
dojo.query('.possibleMoveV').connect('onclick', this, 'onClickPossibleMove');
dojo.query('.possibleMoveH').connect('onclick', this, 'onClickPossibleMove');
} else {
// place tile on board that are played (but not displayed due to "last_play = 1")
for (const prop in args.args['_private']) {
let monster_type = toint(args.args['_private'][prop]['card_type']);
let monster_kind = toint(args.args['_private'][prop]['card_type_arg']);
let mutation = toint(args.args['_private'][prop]['mutation']);
let card_id = prop;
let tileNum = (monster_type - 1 ) * 10 + monster_kind - 1;
const [x, y, rot] = this.convert_coord(args.args['_private'][prop]['board_x'] , args.args['_private'][prop]['board_y'], monster_type);
this.placeTile(this.player_id, tileNum, card_id, x, y, rot, 0, mutation);
}
}
}
break;
case 'var_placeTile':
dojo.query('.stockitem').addClass('bm_unselectable') // disable selection for all cards
dojo.query('.selected').removeClass('disabled');
if (this.isCurrentPlayerActive()) {
var pos = args.args['_private']['possibleMoves'];
var hdir = args.args['_private']['placement_dirH'];
var vdir = args.args['_private']['placement_dirV'];
var dir;
var kind_monter = this.selected_tile_type;
this.lastPossibleMoveIdx = [0,0]; // initiate recorded move position
this.dbMovepos = [0,0]; // selected pos to send to DB
// show the available places to put tiles
for (var idx in pos) {
if (kind_monter == 2) {
if (hdir[idx] == "X") {
continue;
}
dir = hdir[idx]
} else {
if (vdir[idx] == "X") {
continue;
}
dir = vdir[idx]
}
this.addPossiblePlacement(pos[idx][0],pos[idx][1], dir);
}
// connect click event
dojo.query('.possibleMoveV').connect('onclick', this, 'onClickPossibleMove');
dojo.query('.possibleMoveH').connect('onclick', this, 'onClickPossibleMove');
let card_id = document.querySelector('.cardmenu.show')?.dataset?.id
if (this.concurentselect && card_id) {
// reorganise selection buttons
dojo.query('#play_'+card_id).addClass('bm_singleselect')
dojo.query('#discard_'+card_id).addClass('bm_stock_hide')
}
}
break;
case 'var_endTurn':
this.selected_tile_id = 0;
this.selected_row = 0;
this.selected_tile_type = 0;
break;
case 'bmExploTileSelection':
if (this.isCurrentPlayerActive()) {
// update hand with discard tiles
for (var i in args.args) {
var card = args.args[i];
var type = card.type;
var kind_monster = card.type_arg;
this.playerHand.addToStockWithId(this.getCardUniqueId(type, kind_monster), card.id);
this.lastTurn = true;
}
}
break;
case 'bmExploTilePlacement':
if (this.isCurrentPlayerActive()) {
// update hand with discard tiles
var pos = args.args['_private']['possibleMoves'];
var hdir = args.args['_private']['placement_dirH'];
var vdir = args.args['_private']['placement_dirV'];
var dir;
var kind_monter = Math.floor(this.playerHand.items[0].type / 10);
this.lastPossibleMoveIdx = [0,0]; // initiate recorded move position
this.dbMovepos = [0,0]; // selected pos to send to DB
// show the available places to put tiles
for (var idx in pos) {
if (kind_monter == 1) {
if (hdir[idx] == "X") {
continue;
}
dir = hdir[idx]
} else {
if (vdir[idx] == "X") {
continue;
}
dir = vdir[idx]
}
this.addPossiblePlacement(pos[idx][0],pos[idx][1], dir);
}
// connect click event
dojo.query('.possibleMoveV').connect('onclick', this, 'onClickPossibleMove');
dojo.query('.possibleMoveH').connect('onclick', this, 'onClickPossibleMove');
}
}
},
// 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 )
{
switch( stateName )
{
case 'tileSelection':
this.busyShips = [];
break;
case 'var_newTurn':
this.new_turn = true;
}
},
// 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.isCurrentPlayerActive())
{
switch( stateName )
{
case 'placeTile':
if (this.tile_selected) {
dojo.empty('customActions');
this.addActionButton( 'button_conf_move', _('Place Tile'), 'onClickConfirmTilePositionButton', 'customActions' );
}
break;
case 'teamSelection' :
if (this.isTeamPlay) {
debug(this.gamedatas)
dojo.empty('customActions');
Object.values(this.gamedatas.playerorder).forEach(t => {
if (t != this.player_id) {
this.addActionButton('buttonSelectPlayer' + t, this.coloredPlayerName(this.gamedatas.players[t].name), () => this.selectTeamPlayer(t), 'customActions');
}
})
}
break;
}
}
},
// ** Utility methods **
/*
Here, you can defines some utility methods that you can use everywhere in your javascript
script.
*/
/*
* Manage open and closing ship area
*/
onClickOpenShipArea() {
dojo.query('.ship_wrapper').toggleClass('bm_retracted');
setTimeout(()=>{dojo.query('#ships').toggleClass('bm_hidden_ships');dojo.query('#ships').toggleClass('bm_transparent_ships')},1000)
dojo.query('#reduce_ships').style('display', 'inline');
dojo.query('#expand_ships').style('display', 'none');
},
onClickCloseShipArea() {
dojo.query('#ships').toggleClass('bm_transparent_ships');
setTimeout(()=>{dojo.query('#ships').toggleClass('bm_hidden_ships');dojo.query('.ship_wrapper').toggleClass('bm_retracted')},1000)
dojo.query('#reduce_ships').style('display', 'none');
dojo.query('#expand_ships').style('display', 'inline');
},
/*
* Add a timer on an action button :
* params:
* - buttonId : id of the action button
* - time : time before auto click
* - pref : 0 is disabled (auto-click), 1 if normal timer, 2 if no timer and show normal button
*/
startActionTimer(buttonId, time, pref, autoclick = false) {
var button = $(buttonId);
var isReadOnly = this.isReadOnly();
debug($(buttonId));
if (button == null || isReadOnly || pref == 2) {