-
Notifications
You must be signed in to change notification settings - Fork 0
/
calypso.js
1041 lines (944 loc) · 44.5 KB
/
calypso.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]>
* Calypso implementation : © Andy Bond <[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.
* -----
*
* calypso.js
*
* Calypso 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",
"ebg/stock" // for stock class
],
function (dojo, declare) {
return declare("bgagame.calypso", ebg.core.gamegui, {
constructor: function(){
this.cardwidth = 72;
this.cardheight = 96;
// see material.inc.php
this.spades = 1;
this.hearts = 2;
this.clubs = 3;
this.diamonds = 4;
},
/*
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 )
{
// constants
this.pack_files = {
1: "cards.webp",
2: "cards_four.webp",
};
this.suits_translate_lookup = {
1: _("Spades"),
2: _("Hearts"),
3: _("Clubs"),
4: _("Diamonds"),
}
this.suits_translate_lookup_sing = {
1: _("Spade"),
2: _("Heart"),
3: _("Club"),
4: _("Diamond"),
}
for( let player_id in gamedatas.players )
{
let player = gamedatas.players[player_id];
let player_trump = player["trump_suit"];
if(player_id == gamedatas.dealer){
let dealer_area_id = 'clp-dealer-' + player_id;
dojo.place(this.format_block('jstpl_dealerindicator', {
player_id : player_id
}), dealer_area_id);
}
this.setTrickPile(player_id, player["trick_pile"]);
this.setupCalypsoArea(player_id, player_trump);
}
this.playerHand = new ebg.stock();
this.playerHand.create( this, $('clp-myhand'), this.cardwidth, this.cardheight );
// suit info relative to current player suitable for hand ordering
// suit_ranking has player, partner, player matching colour, other suit
let suit_ranking = gamedatas["suits_by_status"];
// reverse order so that player suit is last (highest index)
suit_ranking.reverse();
const num_decks = 4;
// 101 is pack face option
const pack_file = 'img/' + this.pack_files[this.prefs[101].value];
for (let suit = 1; suit <= 4; suit++) {
for (let rank = 2; rank <= 14; rank++) {
for (let deck = 1; deck <= num_decks; deck++){
// Build card type id
let card_type_id = this.getCardUniqueId(suit, rank, deck);
let card_type = this.getCardUniqueType(suit, rank);
let card_weight = this.getCardWeight(suit, rank, suit_ranking);
// args are id, weight (for hand-sorting), img url,
// and img position (within the url sprite)
this.playerHand.addItemType(
card_type_id, card_weight, g_gamethemeurl + pack_file, card_type
);
}
}
}
this.playerHand.centerItems = true;
this.playerHand.image_items_per_row = 13;
this.playerHand.setOverlap( 70, 0 );
this.playerHand.extraClasses = "clp-hand-card";
dojo.connect( this.playerHand, 'onChangeSelection', this, 'onPlayerHandSelectionChanged' );
// Cards in player's hand
for ( let i in gamedatas.hand) {
let card = gamedatas.hand[i];
let suit = card.type;
let rank = card.type_arg;
this.addCardToPlayerHand(suit, rank, card.id);
}
this.setHandActiveness(this.isCurrentPlayerActive(), gamedatas.playable_cards);
// Cards played on table
for (i in gamedatas.cardsontable) {
let card = gamedatas.cardsontable[i];
let suit = card.type;
let rank = card.type_arg;
let player_id = card.location_arg;
this.playCardOnTable(player_id, suit, rank, card.id);
}
// Cards in calypsos
for (i in gamedatas.cardsincalypsos) {
let card = gamedatas.cardsincalypsos[i];
let suit = card.type;
let rank = card.type_arg;
let player_id = card.location_arg;
this.placeCardInCalypso(player_id, suit, rank);
}
const team_lookup = {
[this.spades]: "major",
[this.hearts]: "major",
[this.clubs]: "minor",
[this.diamonds]: "minor",
};
const team_lookup_display = {
[this.spades]: _("Major suits team"),
[this.hearts]: _("Major suits team"),
[this.clubs]: _("Minor suits team"),
[this.diamonds]: _("Minor suits team"),
};
// set player info on the object so we can use it for tooltips
this.player_infos = gamedatas.players;
for( player_id in gamedatas.players )
{
const player = gamedatas.players[player_id];
const player_board_div = $(`player_board_${player_id}`);
this.setCalypsoPile(player_id, player["completed_calypsos"]);
dojo.place(
this.format_block(
'jstpl_playerbox_additions',
{
team_name: team_lookup[player["trump_suit"]],
team_name_display: team_lookup_display[player["trump_suit"]],
...player,
}
),
player_board_div
);
}
// score table buttons
const totalrounds = gamedatas.totalrounds;
const currentround = gamedatas.roundnumber;
for(let round_number = 1; round_number < currentround; round_number++){
this.activateScoreButton(round_number, gamedatas.roundscoretable[round_number]);
}
const awaiting_new_round = (
["gameEnd", "awaitNewRound"].includes(gamedatas.gamestate["name"])
);
if(awaiting_new_round){
this.activateScoreButton(currentround, gamedatas.roundscoretable[currentround]);
}
if(currentround != 1 | awaiting_new_round){
this.activateOverallScoreButton(gamedatas.overallscoretable);
}
// button text!
for(let round_number = 1; round_number <= totalrounds; round_number++){
$(`clp-round-scores-button-${round_number}`).textContent = dojo.string.substitute(
_("Round ${round_number} scores"),
{round_number: round_number}
);
}
$("clp-round-scores-button-overall").textContent = _("Round-by-round scores");
// renounce flags, if the game option is active
if(gamedatas.renounce_flags_on == "on"){
for (i in gamedatas.renounce_flags) {
let info = gamedatas.renounce_flags[i];
this.setRenounceFlag(
info.player_id,
info.suit,
info.trump_suit,
info.player_name
);
}
}
this.updateGameStatus(gamedatas.handnumber, currentround, totalrounds);
this.setupNotifications();
// tooltips ahoy:
this.refreshTooltips();
// set the empty animation queue:
this.clearQueue();
},
///////////////////////////////////////////////////
//// 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 )
{
// only action is classing the hand so that active hands have cursor, card highlight on hover
switch( stateName )
{
case 'playerTurn':
break;
}
},
// 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 'dummmy':
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.isCurrentPlayerActive() )
{
switch( stateName )
{
case 'awaitNewRound':
this.addActionButton( 'clp-confirm-new-round', _('Ready for next round'), 'confirmNewRound');
break;
}
}
},
///////////////////////////////////////////////////
//// Utility methods
/*
Here, you can defines some utility methods that you can use everywhere in your javascript
script.
*/
// Get card unique identifier based on its suit and rank
getCardUniqueType : function(suit, rank) {
return (suit - 1) * 13 + (rank - 2);
},
// this is only when we distinguish between identical copies, by their deck
getCardUniqueId : function(suit, rank, deck) {
return (deck - 1) * 52 + (suit - 1) * 13 + (rank - 2);
},
// get 'weight' of card given ordering of suits
getCardWeight: function(suit, rank, suit_ranking) {
// last in array has highest weight
return (suit_ranking.indexOf(suit) - 1) * 13 + (rank - 2);
},
// calypso display area populated
setupCalypsoArea : function(player_id, suit) {
for (let rank = 2; rank <= 14; rank++) {
dojo.place(this.format_block('jstpl_calypsocard', {
rank : rank,
suit : suit,
player_id : player_id
}), 'clp-calypsoholder-' + player_id);
}
},
addCardToPlayerHand(suit, rank, card_id){
const unique_type = this.getCardUniqueType(suit, rank);
this.playerHand.addToStockWithId(unique_type, card_id);
},
setHandActiveness(active, playable_cards=[]){
const hand_div_id = "clp-myhand";
if(active){
dojo.addClass(hand_div_id, "clp-active-hand");
dojo.removeClass(hand_div_id, "clp-inactive-hand");
this.highlightPlayable(hand_div_id, true, playable_cards);
} else{
dojo.removeClass(hand_div_id, "clp-active-hand");
dojo.addClass(hand_div_id, "clp-inactive-hand");
this.highlightPlayable(hand_div_id, false);
}
},
highlightPlayable(hand_div_id, make_playable, playable_cards=[]){
// add css class to playable / not playable cards if make_playable is true
// else remove all classes
// actual behaviour covered by css, as it is a user pref
const card_els = $(hand_div_id).children;
const regex_card_id = /clp-myhand_item_(?<card_id>\d+)/m;
const playable_card_ids = Object.values(playable_cards).map(
(card) => card.id
);
[...card_els].forEach(
(card_el) => {
let card_el_id = card_el.id;
let r_match = card_el_id.match(regex_card_id)
let card_id = r_match.groups.card_id;
if (make_playable) {
if (playable_card_ids.includes(card_id)) {
dojo.addClass(card_el_id, "clp-hand-card-playable");
} else {
dojo.addClass(card_el_id, "clp-hand-card-unplayable");
}
} else {
dojo.removeClass(card_el_id, "clp-hand-card-playable");
dojo.removeClass(card_el_id, "clp-hand-card-unplayable");
}
}
);
},
playCardOnTable : function(player_id, suit, rank, card_id) {
dojo.place(this.format_block('jstpl_cardontable', {
x : this.cardwidth * (rank - 2),
y : this.cardheight * (suit - 1),
player_id : player_id
}), 'clp-player-card-play-area-card-' + player_id);
if (player_id != this.player_id) {
// Move card from their general area
this.placeOnObject('clp-card-on-table-' + player_id, 'clp-player-all-captured-cards-' + player_id);
} else {
// div id is generated automatically from stock when we create hand
// via this.addToStockById()
if ($('clp-myhand_item_' + card_id)) {
this.placeOnObject('clp-card-on-table-' + player_id, 'clp-myhand_item_' + card_id);
this.playerHand.removeFromStockById(card_id);
this.setHandActiveness(false);
}
}
// In any case: move it to its final destination
let anim = this.slideToObject('clp-card-on-table-' + player_id, 'clp-player-card-play-area-card-' + player_id);
anim.play();
},
placeCardInCalypso : function(player_id, suit, rank) {
const card_el_id = `clp-calypsocard-${player_id}-${rank}`;
dojo.addClass( card_el_id, `clp-calypsocard-face-${suit}-${rank}`);
dojo.addClass( card_el_id, 'clp-face-up-card' );
dojo.removeClass( card_el_id, 'clp-calypsocard-space' );
},
removeCardFromCalypso : function(player_id, suit, rank) {
const card_el_id = `clp-calypsocard-${player_id}-${rank}`;
dojo.removeClass( card_el_id, `clp-calypsocard-face-${suit}-${rank}`);
dojo.removeClass( card_el_id, 'clp-face-up-card' );
dojo.addClass( card_el_id, 'clp-calypsocard-space' );
},
setTrickPile : function(player_id, value) {
const cards_el_id = `clp-trickpile-${player_id}`;
if(value > 0){
dojo.addClass( cards_el_id, 'clp-trickpile-full' );
dojo.removeClass( cards_el_id, 'clp-trickpile-empty' );
} else {
dojo.removeClass( cards_el_id, 'clp-trickpile-full' );
dojo.addClass( cards_el_id, 'clp-trickpile-empty' );
}
this.refreshTooltips();
},
setCalypsoPile: function(player_id, value) {
const cards_el_id = `clp-calypsopile-${player_id}`;
if(value > 0){
dojo.addClass( cards_el_id, 'clp-calypsopile-full' );
dojo.removeClass( cards_el_id, 'clp-calypsopile-empty' );
} else {
dojo.removeClass( cards_el_id, 'clp-calypsopile-full' );
dojo.addClass( cards_el_id, 'clp-calypsopile-empty' );
}
this.refreshTooltips();
},
getRenounceFlagUniqueClass : function(suit, trump_suit, player_name) {
return `clp-active-renounce-${player_name}-${trump_suit}-${suit}`;
},
setRenounceFlag : function(player_id, suit, player_trump, player_name){
const renounce_el_id = `clp-renounce-${player_id}-${suit}`;
dojo.addClass( renounce_el_id, 'clp-active-renounce' );
// encode info for tooltip
dojo.addClass(
renounce_el_id,
this.getRenounceFlagUniqueClass(suit, player_trump, player_name)
);
dojo.removeClass( renounce_el_id, 'clp-inactive-renounce' );
this.refreshTooltips();
},
clearRenounceFlags: function(players, suits){
for (player_id in players) {
let player = players[player_id];
for(j in suits) {
let suit = suits[j];
let renounce_el_id = `clp-renounce-${player_id}-${suit}`;
dojo.removeClass( renounce_el_id, 'clp-active-renounce' );
// detailed class for toolTips
dojo.removeClass(
renounce_el_id,
this.getRenounceFlagUniqueClass(
suit,
player.trump_suit,
player.player_name
)
);
dojo.addClass( renounce_el_id, 'clp-inactive-renounce' );
}
}
this.refreshTooltips();
},
clearCalypsos: function(player_ids){
// runs on new round
let all_player_animations = [];
for (player of player_ids){
let player_id = player["id"];
let suit = player["suit"];
let animation = this.animateCalypso(player_id, suit, [], to_prefix="clp-trickpile", play=false, delay=100);
all_player_animations.push(animation);
}
return dojo.fx.combine(all_player_animations);
},
clearCalypsoPiles: function(player_ids){
for (player of player_ids){
let player_id = player["id"];
this.setCalypsoPile(player_id, 0);
}
},
clearTrickPiles: function(player_ids){
let animations = [];
for (player of player_ids){
let player_id = player["id"];
let anim = this.slideTemporaryObject(
'<div class="clp-trickpile-full clp-trickpile" style="z-index:30"></div>',
"clp-table-centre",
`clp-trickpile-${player_id}`, "clp-table-centre",
);
anim.duration = 400;
animations.push(anim);
}
return dojo.fx.combine(animations);
},
changeDealer : function(new_dealer_id) {
const new_dealer_area_id = 'clp-dealer-' + new_dealer_id;
this.attachToNewParent( 'clp-dealerbutton', new_dealer_area_id );
},
updateGameStatus: function(handnumber, roundnumber, totalrounds) {
// don't need to translate game title
$("clp-game-info").innerHTML = dojo.string.substitute(
'<span class="clp-gametitle">Calypso</span>' +
"<br>" + _("Round ${roundnumber} of ${totalrounds}") +
" - " + _("Hand ${handnumber} of 4"),
{
roundnumber: roundnumber,
handnumber: handnumber,
totalrounds: totalrounds,
}
);
},
refreshTooltips: function() {
this.addTooltipToClass( "clp-dealerbutton", _( "This player is the dealer for this hand" ), "" );
this.addTooltipToClass( "clp-trickpile-full", _( "This player has some cards in their trick-pile" ), "" );
this.addTooltipToClass( "clp-trickpile-empty", _( "This player has no cards in their trick-pile" ), "" );
this.addTooltipToClass( "clp-calypsopile-full", _( "This player has completed one or more calypsos this round" ), "" );
this.addTooltipToClass( "clp-active-renounce", _( "This player failed to follow this suit this hand" ), "" );
this.refreshRenounceTooltips();
const elements_without_tooltips = dojo.query(".clp-inactive-renounce, .clp-calypsopile-empty");
for(let element of elements_without_tooltips){
this.removeTooltip(element["id"]);
}
},
refreshRenounceTooltips: function() {
player_infos = this.player_infos;
for( player_id in player_infos) {
const player = player_infos[player_id];
for (let [suit_index, suit_name_trans] of Object.entries(this.suits_translate_lookup)) {
const class_name = this.getRenounceFlagUniqueClass(
suit_index,
player['trump_suit'],
player['player_name']
);
const tooltip = dojo.string.substitute(
_("The ${trump_suit_singular} player (${name}) has not followed suit to ${renounce_suit}"),
{
trump_suit_singular: this.suits_translate_lookup_sing[player['trump_suit']],
name: player['player_name'],
renounce_suit: this.suits_translate_lookup[suit_index]
}
);
this.addTooltipToClass( class_name, tooltip, "" );
}
};
},
// borrowed/modified from W. Michael Shirk's Grosstarock implementation
// saved a lot of pain in trying to hack something together!
showResultDialog: function (score_table, title=null) {
wrap_translation = (text_entry) => {
if(typeof text_entry === 'object' && text_entry.hasOwnProperty('for_round_number')){
return dojo.string.substitute(
_("Round ${round_number} score"),
{round_number: text_entry["for_round_number"]["round_number"]}
);
}
// keys from backend -> actually displayed (and translatable) text
const lookup = {
"calypso_count": _("Completed calypsos"),
"incomplete_calypso_count": _("Cards in incomplete calypsos"),
"trickpile_count": _("Cards in trickpile"),
"individual_score": _("Total individual score"),
"partnership_score": _("Total round score (partnership)"),
"score": _("score"),
"total_score": _("Total score"),
};
return lookup[text_entry] || text_entry;
}
// put entries inside containers with given class, so we can style freely
wrap_class = (table_entry) => {
if(typeof table_entry === 'object' && table_entry.hasOwnProperty('to_wrap')){
let items = table_entry["to_wrap"];
// maybe having it concatenated here is good so client knows to translate properly?
return '<div class=\"' + items.class_name + '\">' + wrap_translation(items.string_key) + '</div>';
}
return table_entry;
}
score_table = score_table.map((row) => row.map(wrap_class));
let scoring_dialog = this.displayTableWindow(
"roundScore",
title,
score_table,
"",
this.format_string_recursive(
'<div id="tableWindow_actions"><a id="close_btn" class="bgabutton bgabutton_blue">${close}</a></div>',
{ close: _("Close") }
)
)
scoring_dialog.show()
},
showResultDialogByRound: function(round_number, score_table){
const title = dojo.string.substitute(
_("Round ${round_number} score"),
{round_number: round_number}
);
this.showResultDialog(score_table, title);
},
activateScoreButton: function(round_number, score_table){
const round_button_id = `clp-round-scores-button-${round_number}`;
$(round_button_id).onclick = (
() => this.showResultDialogByRound(round_number, score_table)
);
dojo.addClass( round_button_id, 'clp-score-button-active' );
dojo.removeClass( round_button_id, 'clp-score-button-inactive' );
},
activateOverallScoreButton: function(overall_score){
const overall_scores_button_id = 'clp-round-scores-button-overall';
$(overall_scores_button_id).onclick = (
() => this.showResultDialog(
overall_score, _("Round-by-round score summary"))
);
dojo.addClass( overall_scores_button_id, 'clp-score-button-active' );
dojo.removeClass( overall_scores_button_id, 'clp-score-button-inactive' );
},
// animates clearing calypso both for completing calypso, and at end of round when we clear the table
// to_prefix flags for us which one
// play=true means we add it to the animation queue rather than just returning it
animateCalypso: function(player_id, player_suit, fresh_ranks, to_prefix="clp-calypsopile", play=true, delay=30){
// make some modifications that we will undo at the end of the method
// for correct animation calculation
dojo.addClass("clp-public-area", "clp-no-transform");
let animations = [];
let anim;
let current_delay = 0;
for (let rank = 2; rank <= 14; rank++) {
let card_el_id = `clp-calypsocard-${player_id}-${rank}`;
// create animation for cards in the calypso
if(!dojo.hasClass(card_el_id, "clp-calypsocard-space")){
anim = this.slideToObject(card_el_id, `${to_prefix}-${player_id}` );
dojo.connect(anim, 'onEnd', function(node) {
dojo.destroy(node);
});
} else{
// dummy animation for blank spaces (unfilled slots) so that timings
// stay synced for all calypsos
anim = dojo.animateProperty({
node: card_el_id,
properties: {}
});
}
anim.delay = current_delay;
current_delay += delay;
anim.duration = 400;
// place existing card or blank space as appropriate, for underneath
// these are the moving items that will get swept away
if(fresh_ranks.includes(rank)){
dojo.connect(
anim, "onBegin",
() => {
dojo.place(this.format_block('jstpl_calypsocard_existing', {
rank : rank,
suit : player_suit,
player_id : player_id
}), 'clp-calypsoholder-' + player_id);
}
);
} else{
dojo.connect(
anim, "onBegin",
() => this.removeCardFromCalypso(player_id, player_suit, rank)
);
dojo.place(this.format_block('jstpl_calypsocard', {
rank : rank,
suit : player_suit,
player_id : player_id
}), 'clp-calypsoholder-' + player_id);
}
animations.push(anim);
}
dojo.removeClass("clp-public-area", "clp-no-transform");
return dojo.fx.combine(animations);
},
// animation queue handling - in case we need to zhuzh it up
addToQueue : function(anim, description = "") {
this.animation_queue.push(anim);
// description queue is for aid in debugging
this.description_queue.push(description);
},
playQueue : function() {
dojo.fx.chain(this.animation_queue).play();
},
clearQueue : function() {
this.animation_queue = [];
this.description_queue = [];
},
///////////////////////////////////////////////////
//// Player's action
/*
Here, you are defining methods to handle player's action (ex: results of mouse click on
game objects).
Most of the time, these methods:
_ check the action is possible at this game state.
_ make a call to the game server
*/
onPlayerHandSelectionChanged : function() {
const items = this.playerHand.getSelectedItems();
const action = 'playCard';
if (items.length > 0) {
if (this.checkAction(action, true)) {
// Can play a card
let card_id = items[0].id;
this.ajaxcall(
"/" + this.game_name + "/" + this.game_name + "/" + action + ".html",
{
id: card_id,
lock: true
},
this,
function (result) {
},
function (is_error) {
}
);
this.playerHand.unselectAll();
} else {
this.showMessage(_("It is not your turn to play a card!"), "error");
this.playerHand.unselectAll();
}
}
},
confirmNewRound: function(){
if (this.checkAction("confirmNewRound", true)) {
this.ajaxcall(
"/" + this.game_name + "/" + this.game_name + "/confirmNewRound.html",
{
lock: true
},
this,
function (result) {
},
function (is_error) {
}
);
}
},
///////////////////////////////////////////////////
//// 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 calypso.game.php file.
*/
setupNotifications : function() {
dojo.subscribe('newGame', this, "notif_newGame");
dojo.subscribe('newHandBegin', this, "notif_newHandBegin");
dojo.subscribe('newRound', this, "notif_newRound");
// the actual cards that a player receives
dojo.subscribe('newCards', this, "notif_newCards");
// admin around hand/dealer changing
dojo.subscribe('dealHand', this, "notif_dealHand");
dojo.subscribe('playableCards', this, "notif_playableCards");
// playing a card - the main game action occurring
dojo.subscribe('playCard', this, "notif_playCard");
// handles setting renounce flags when a player renounces
dojo.subscribe('renounceFlag', this, "notif_renounceFlag");
// just for starting off a hand, v. thin
dojo.subscribe('actionRequired', this, "notif_actionRequired");
// the various sets of things that (can) happen upon a trick finishing
dojo.subscribe( 'trickWin', this, "notif_trickWin" );
this.notifqueue.setSynchronous( 'trickWin', 1000 );
dojo.subscribe( 'moveCardsToWinner', this, "notif_moveCardsToWinner" );
dojo.subscribe( 'moveCardsToCalypsos', this, "notif_moveCardsToCalypsos" );
// need to leave enough time for _all_ cards to get to calypsos, otherwise they stick around
dojo.subscribe( 'calypsoComplete', this, "notif_calypsoComplete" );
dojo.subscribe( 'playAllAnimations', this, "notif_playAllAnimations" );
// updating scores/display score tables
dojo.subscribe( 'scoreDisplay', this, "notif_scoreDisplay" );
dojo.subscribe( 'scoreUpdate', this, "notif_scoreUpdate" );
},
notif_newGame: function(notif) {
// currently just displaying log information, but could always add some animation here
},
notif_newRound: function(notif) {
const player_ids = notif.args.player_ids;
for(let player of player_ids){
let player_id = player["id"];
const player_count_element = `clp-info-count-${player_id}`;
$(player_count_element).textContent = 0;
}
if(notif.args.round_number != 1){
this.clearCalypsoPiles(player_ids);
let cleanup_animation = this.clearCalypsos(player_ids);
dojo.connect(
cleanup_animation, "onEnd",
dojo.hitch(this, () => {
this.clearTrickPiles(player_ids);
player_ids.forEach(player_id => this.setTrickPile(player_id["id"], 0));
this.refreshTooltips();
})
);
cleanup_animation.play();
}
},
notif_newCards : function(notif) {
this.playerHand.removeAll();
for ( let i in notif.args.cards) {
let card = notif.args.cards[i];
let suit = card.type;
let rank = card.type_arg;
this.addCardToPlayerHand(suit, rank, card.id);
}
this.playerHand.updateDisplay();
},
notif_dealHand : function(notif) {
this.changeDealer(notif.args.dealer_id);
this.updateGameStatus(notif.args.hand_number, notif.args.round_number, notif.args.total_rounds);
if(notif.args.renounce_flags_clear){
this.clearRenounceFlags(notif.args.players, notif.args.suits);
}
},
notif_playableCards: function(notif) {
this.setHandActiveness(true, notif.args.playable_cards);
},
notif_playCard : function(notif) {
this.playCardOnTable(notif.args.player_id, notif.args.suit, notif.args.rank, notif.args.card_id);
},
notif_renounceFlag: function(notif) {
this.setRenounceFlag(
notif.args.player_id, notif.args.suit,
notif.args.player_trump, notif.args.player_name
);
},
notif_trickWin : function(notif) {
// We do nothing here (just wait in order players can view the 4 cards played before they're gone.
},
notif_calypsoComplete : function(notif) {
// put any cards to the new calypso in their RIGHTFUL PLACE
let fresh_cards = Object.values(notif.args.cards_to_fresh_calypso);
let fresh_ranks = fresh_cards.map(card => +card["rank"]);
const player_id = notif.args.player_id;
// for each card in calypso, get rid of it, but not too much
let anim_calypso = this.animateCalypso(
player_id, notif.args.player_suit, fresh_ranks, to_prefix="clp-calypsopile"
);
const player_count_element = `clp-info-count-${player_id}`;
const new_num_calypsos = notif.args.num_calypsos;
const calypso_pile_func = () => this.setCalypsoPile(player_id, new_num_calypsos);
// have transform woes if we try to use calypsopile
const anim_origin = "clp-table-centre";
let anim = this.slideTemporaryObject(
this.format_block('jstpl_suiticon', {
trump_suit : notif.args.player_suit,
}),
anim_origin,
anim_origin,
player_count_element
);
dojo.connect(anim, 'onEnd', function(node) {
$(player_count_element).textContent = new_num_calypsos;
});
dojo.connect(anim_calypso, 'onEnd', function(node) {
calypso_pile_func();
})
anim.duration = 800;
this.addToQueue(anim_calypso, "calypsos -> calypso pile");
this.addToQueue(anim, "suit icon to scoreboard to clue players");
},
notif_actionRequired : function(notif) {
// waiting for first player to play a card
},
notif_scoreDisplay: function(notif) {
this.showResultDialogByRound(notif.args.round_number, notif.args.table);
this.activateScoreButton(notif.args.round_number, notif.args.table);
this.activateOverallScoreButton(notif.args.overall_score);
},
notif_scoreUpdate : function(notif) {
notif.args.scores.forEach(
score_info => (
this.scoreCtrl[score_info.player_id].incValue(score_info.total_score)
)
);
},
notif_moveCardsToWinner : function(notif) {
// Move all cards on table to winners' card space, ready to be sent on
// construct the animation, and add it to the queue
const winner_id = notif.args.winner_id;
let anims = [];
for ( let player_id in this.gamedatas.players) {
let anim = this.slideToObject(
'clp-card-on-table-' + player_id,
'clp-player-card-play-area-card-' + winner_id
);
anims.push(anim);
}
// queue should already be clear, but can't hurt to make sure
this.clearQueue();
// start a new animation queue with these animations combined as a single one (simultaneous)
this.addToQueue(dojo.fx.combine(anims), "move all cards to winner");
},
notif_moveCardsToCalypsos : function(notif) {
// cardinal positions - how rotated are they (in degrees, sunwise)?
const degrees_lookup = {
'S': 0,
'W': 90,
'N': 180,
'E': -90,
};
function getRotationDegrees(element) {
// given a destination, how much should we rotate to end up pointing the right way
// find the player area which contains the destination element, and get the id
let id = element.closest(".clp-player-personal-area").id;
// id is e.g. clp-player-personal-area-N, so extract cardinal point
let cardinal_dir = id.at(-1);
return degrees_lookup[cardinal_dir];
};
let anims = [];
// duration of rotation animation in ms - 350 default.
const rotate_dur = 100;
const winner_id = notif.args.player_id;
// this has the admin on where all the cards come from, but more importantly go to
const moved_to = notif.args.moved_to;
// count all cards to stagger them slightly - a bit nicer
// helps it not seem like cards are disappearing
let card_count = 0;
const card_anim_delay = 100;
const flip_half_dur = 100;
// loop through the cards of the trick:
for ( let player in moved_to) {
let anim;
let send_to_id = moved_to[player]["owner"];
let send_from_player_id = moved_to[player]["originating_player"];
let send_from_el_id = `clp-card-on-table-${send_from_player_id}`;
let send_from_el = $(send_from_el_id);
if(send_to_id === 0){
// card is just going to trick pile
let send_to_el = $('clp-trickpile-' + winner_id);
let rot_deg = getRotationDegrees(send_to_el);
let anim_rot = new dojo.Animation({
curve: [0, rot_deg],
onAnimate: (v) => {
send_from_el.style.transform = 'rotate(' + v + 'deg)';
},
duration: rotate_dur,
delay: card_count * card_anim_delay,
});
let anim_slide = this.slideToObject(send_from_el, send_to_el);
let anim_flip_1 = new dojo.Animation({
curve: [1, 0],
onAnimate: (v) => {
// needs to still be rotated!
send_from_el.style.transform = 'rotate( ' + rot_deg + 'deg) scaleX(' + v + ') ';
},
duration: flip_half_dur,
});
dojo.connect(anim_flip_1, 'onEnd', (node) => {
send_from_el.classList.add("clp-trickpile-nearly");
send_from_el.classList.remove("clp-face-up-card");
})
let anim_flip_2 = new dojo.Animation({
curve: [0, 1],
onAnimate: (v) => {
// needs to still be rotated!
send_from_el.style.transform = 'rotate( ' + rot_deg + 'deg) scaleX(' + v + ') ';
},
duration: flip_half_dur,
});
dojo.connect(anim_flip_2, 'onEnd', (node) => {
dojo.destroy(send_from_el);
this.setTrickPile(winner_id, 1);
});
anim = dojo.fx.chain([anim_rot, anim_slide, anim_flip_1, anim_flip_2]);
} else{
// card goes to the one of the winning partnerships' calypsos
let calypso_player_id = send_to_id;
let rank = moved_to[player]["rank"];
let suit = moved_to[player]["suit"];
let send_to_el = $(`clp-calypsocard-${calypso_player_id}-${rank}`);