-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegyptianratscrew.js
344 lines (287 loc) · 13.8 KB
/
egyptianratscrew.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
/**
*------
* BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
* Hearts implementation : © Gregory Isabelli <gisabelli@boardgamearena.com>
*
* This code has been produced on the BGA studio platform for use on http://boardgamearena.com.
* See http://en.boardgamearena.com/#!doc/Studio for more information.
* -----
*
* egyptianratscrew.js
*
* Hearts user interface script
*
* In this file, you are describing the logic of your user interface, in Javascript language.
*
*/
////////////////////////////////////////////////////////////////////////////////
/*
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"
],
function (dojo, declare) {
return declare("bgagame.egyptianratscrew", ebg.core.gamegui, {
constructor: function () {
console.log('Egyptian Ratscrew constructor');
this.cardwidth = 72;
this.cardheight = 96;
// Here, you can init the global variables of your user interface
this.players = [];
this.tableStock = null;
this.playerStocks = [];
this.cardsOrder = { '-1': 0 };
this.slapCounter = 0;
},
/*
setup:
This method must set up the game user interface according to current game situation specified
in parameter.
The method is called each time the game interface is displayed to a player, ie:
_ when the game starts
_ when a player refresh the game page (F5)
"gamedatas" argument contains all datas retrieved by your "getAllDatas" PHP method.
*/
setup: function (gamedatas) {
console.log("starting game setup");
// Create cards stocks:
this.createTableStock();
this.initTableCards();
this.players = gamedatas.players;
for (let player_id in gamedatas.players) {
let nbr_cards = gamedatas.players[player_id].cards;
this.createPlayerStock(player_id);
this.addPlayerCards(player_id, nbr_cards);
}
// Setup game actions trigger
// dojo.connect($("pile"), "onclick", this, "onSlapPile");
// dojo.connect(this.tableStock, 'onChangeSelection', this, 'onSlapPile');
dojo.connect($("player_hand_" + this.player_id), 'onclick', this, 'onSlapPile');
dojo.connect(this.playerStocks[this.player_id], 'onChangeSelection', this, 'onPlayCard');
// Setup game notifications to handle (see "setupNotifications" method below)
this.setupNotifications();
this.ensureSpecificImageLoading(['../common/point.png']);
},
///////////////////////////////////////////////////
//// 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) {
switch (stateName) {
case 'endTurn':
// this.showHands(false);
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 'playerTurn':
// this.showHands(true);
break;
case 'validateTurn':
// reset hands position
this.resetHands();
this.slapCounter = 0;
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 'validateTurn':
this.addActionButton('validateTurn_button', _('Validate turn'), 'onValidateTurn');
break;
}
}
},
///////////////////////////////////////////////////
//// Utility methods
/*
Here, you can defines some utility methods that you can use everywhere in your javascript
script.
*/
sleep: function(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
createTableStock: function() {
this.tableStock = new ebg.stock();
this.tableStock.image_items_per_row = 13;
this.tableStock.setOverlap(10,5);
this.tableStock.setSelectionMode(1);
this.tableStock.setSelectionAppearance(null);
// this.tableStock.centerItems = true;
this.tableStock.create(this, $('pile'), this.cardwidth, this.cardheight);
// Add back card type
this.tableStock.addItemType(-1, 0, g_gamethemeurl + 'img/card_back.png');
// Add visible card types
for (var color = 1; color <= 4; color++) {
for (var value = 2; value <= 14; value++) {
// Build card type id
var card_type_id = this.getCardUniqueId(color, value);
this.tableStock.addItemType(card_type_id, 0, g_gamethemeurl + 'img/cards.jpg', card_type_id);
}
}
},
initTableCards: function() {
// Initialize table cards
let cardsOnTable = this.gamedatas.cardsontable;
for (let i in cardsOnTable) {
let card = cardsOnTable[i];
let card_id = this.getCardUniqueId(card.type, card.type_arg);
this.cardsOrder[card_id] = card.play_time;
this.tableStock.addToStockWithId(card_id, card_id);
}
// Sort cards to keep their play order
this.tableStock.changeItemsWeight(this.cardsOrder);
// Hack: -10 to keep stock order (there are maximum 4 hidden cards on table)
for (let j = 0; j < this.gamedatas.hiddenCards; j++) {
this.tableStock.addToStockWithId(-1, j);
}
},
createPlayerStock: function(player_id) {
let target = new ebg.stock();
target.image_items_per_row = 13;
target.setOverlap(2,0);
target.item_margin = 0;
target.setSelectionMode(1);
target.setSelectionAppearance(null);
target.create(this, $('player_cards_' + player_id), this.cardwidth, this.cardheight);
target.addItemType(-1, 0, g_gamethemeurl + 'img/card_back.png');
this.playerStocks[player_id] = target;
},
addPlayerCards: function(player_id, nbr_cards) {
for (let i = 0; i < nbr_cards; i++) {
this.playerStocks[player_id].addToStockWithId(-1, i);
}
},
removePlayerCards: async function(player_id, nbr_cards) {
for (let i = 0; i < nbr_cards; i++) {
this.playerStocks[player_id].removeFromStock(-1, 'pile');
await this.sleep(150);
}
},
/**
* Get card unique identifier based on its color and value
*/
getCardUniqueId: function (color, value) {
return (color - 1) * 13 + (value - 2);
},
moveCardToPile: function (player_id, card_id) {
// Refresh cards order
this.tableStock.changeItemsWeight(this.cardsOrder);
// Slide the card from player to pile and delete it after animation
let player_stock = this.playerStocks[player_id];
// TODO top card not bottom
player_stock.removeFromStock(-1, 'pile');
// Add visible card to the pile
setTimeout(() => {
this.tableStock.addToStock(card_id);
}, 500);
},
moveCardsToBottomPile: function(player_id, nbr_cards) {
this.removePlayerCards(player_id, nbr_cards);
let player_count = this.playerStocks[player_id].count();
if (player_count < nbr_cards) {
nbr_cards = player_count;
}
// TODO make penalty cards visible and bottom of the stack (check front-end and back-end)
for (let i = 0; i < nbr_cards; i++) {
this.tableStock.addToStock(-1);
}
},
moveAllCardsFromPileToPlayer: function(player_id, nbr_cards) {
this.tableStock.removeAllTo("player_cards_" + player_id);
setTimeout(() => {
this.addPlayerCards(player_id, nbr_cards);
this.cardsOrder = { '-1': 0 };
}, 500);
},
showHands: function (visible) {
for (let player_id in this.players) {
if (visible) {
dojo.style('player_hand_' + player_id, 'visibility', 'visible');
} else {
dojo.style('player_hand_' + player_id, 'visibility', 'hidden');
}
}
},
resetHands: function() {
for (let player_id in this.players) {
// position must match 'top' and 'left' values in css
this.slideToObjectPos("player_hand_" + player_id, "player_seat_" + player_id, 85, 35).play();
}
},
///////////////////////////////////////////////////
//// 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
*/
onSlapPile: function (event) {
this.ajaxcall("/egyptianratscrew/egyptianratscrew/slapPile.html", {}, this, function (result) {}, function (is_error) {});
},
onPlayCard: function (event) {
this.ajaxcall("/egyptianratscrew/egyptianratscrew/playCard.html", {}, this, function (result) {}, function (is_error) {});
},
onValidateTurn: function (event) {
this.ajaxcall("/egyptianratscrew/egyptianratscrew/validateTurn.html", {}, 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 your "notifyAllPlayers" and "notifyPlayer" calls in
your emptygame.game.php file.
*/
setupNotifications: function () {
console.log('notifications subscriptions setup');
dojo.subscribe('slapPile', this, "notif_slapPile");
dojo.subscribe('playCard', this, "notif_playCard");
dojo.subscribe('slapFailed', this, "notif_slapFailed");
dojo.subscribe('slapWon', this, "notif_slapWon");
},
notif_playCard: function (notif) {
let player_id = notif.args.player_id;
let card_id = this.getCardUniqueId(notif.args.color, notif.args.value);
this.cardsOrder[card_id] = notif.args.timestamp;
this.moveCardToPile(player_id, card_id);
},
notif_slapPile: function (notif) {
let player_id = notif.args.player_id;
let hand_id = "player_hand_" + player_id;
// adjust z-index to keep hand order
dojo.style(hand_id, 'z-index', this.slapCounter + 100);
dojo.style('player_seat_' + player_id, 'z-index', this.slapCounter + 100);
this.slapCounter++;
// animation
this.slideToObject(hand_id, "pile", 100).play();
},
notif_slapFailed: function (notif) {
let players_id = notif.args.players_id;
let penalty = notif.args.penalty;
players_id.forEach((player_id) => {
this.moveCardsToBottomPile(player_id, penalty);
});
},
notif_slapWon: function (notif) {
let player_id = notif.args.player_id;
let nbr_cards = notif.args.nbr_cards;
this.moveAllCardsFromPileToPlayer(player_id, nbr_cards);
},
});
});