-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutotheod.js
369 lines (274 loc) · 12.9 KB
/
tutotheod.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
/**
*------
* BGA framework: Gregory Isabelli & Emmanuel Colin & BoardGameArena
* TutoTheod implementation : © <Théo de la Hogue> <[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.
* -----
*
* tutotheod.js
*
* TutoTheod user interface script
*
* In this file, you are describing the logic of your user interface, in Javascript language.
*
*/
define([
"dojo",
"dojo/_base/declare",
"dojo/fx",
"ebg/core/gamegui",
"ebg/counter"
],
function (dojo, declare, fx) {
return declare("bgagame.tutotheod", ebg.core.gamegui, {
/*
constructor:
Init global variables of user interface
*/
constructor: function()
{
console.log('tutotheod constructor');
this.animationChain = [];
},
/*
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 )
{
console.log( "Starting game setup" );
game_play_area = document.getElementById('game_play_area')
// Create board
game_play_area.insertAdjacentHTML('beforeend', `
<div id="board"></div>
`);
// Create 36 squares with a 9 slots grid inside
const board = document.getElementById('board');
const size = 64;
const ids = [
[ 5, 6, 7, 8, 9, 10], // y = 0
[ 4, 23, 24, 25, 26, 11], // y = 1
[ 3, 22, 33, 34, 27, 12], // y = 2
[ 2, 21, 32, 35, 28, 13], // y = 3
[ 1, 20, 31, 30, 29, 14], // y = 4
[ 0, 19, 18, 17, 16, 15], // y = 5
]; // x = 0, 1, 2, 3, 4, 5
for (let x=0; x<6; x++) {
for (let y=0; y<6; y++) {
const left = x * size;
const top = y * size;
board.insertAdjacentHTML(`beforeend`, `
<div id="square_${ids[x][y]}" class="square"></div>
`);
const square = document.getElementById(`square_${ids[x][y]}`);
/* Slots are ordered like this:
1 2 3
4 5 6
7 8 9
*/
for (let s=1; s<=9; s++) {
square.insertAdjacentHTML(`beforeend`, `
<div id="square_${ids[x][y]}_slot_${s}" class="slot"></div>
`);
}
}
}
// Create die
//var die_x = Math.floor(Math.random() * 6) + 1;
var die_value = Math.floor(Math.random() * 6) + 1;
// DEBUG
console.log( "random die value", die_value );
board.insertAdjacentHTML('beforeend', `
<div class="die" id="die" data-value="${die_value}" style="top: -50px; left: -50px;"></div>
`);
document.getElementById('die').addEventListener('click', event => this.onClickDie(event));
// Setting up player boards
/*
Object.values(gamedatas.players).forEach(player => {
// Example of setting up players boards
this.getPlayerPanelElement(player.id).insertAdjacentHTML('beforeend', `
<div id="player-counter-${player.id}">A player counter</div>
`);
// Example of adding a div for each player
board.insertAdjacentHTML('beforeend', `
<div id="board-${player.id}">
<strong>${player.name}</strong>
<div>Player zone content goes here</div>
</div>
`);
});
*/
// Set up your game interface here, according to "gamedatas"
// DEBUG
console.log( "gamedatas.tokens", Object.values(gamedatas.tokens) );
// Create tokens
Object.values(gamedatas.tokens).forEach(token => {
// Create token
board.insertAdjacentHTML('beforeend', `
<div class="token_wrapper" id="token_${token.color}">
<div class="token" data-color="${token.color}""></div>
</div>
`);
// Setup token position
this.slideTokenToSquareSlot( token.color, token.square, token.slot ).play();
});
// Setup game notifications to handle (see "setupNotifications" method below)
this.setupNotifications();
console.log( "Ending game setup" );
},
///////////////////////////////////////////////////
//// 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 )
{
console.log( 'Entering state: '+stateName, args );
switch( stateName )
{
case 'playerTurn':
// Update moved tokens (mainly useful when the game start)
for (const [key, token] of Object.entries(args.args.tokens)) {
this.slideTokenToSquareSlot( token.color, token.square, token.slot ).play();
}
//this.highligthActivePlayerToken();
break;
case 'moveToken':
// Append token movements to a animation chain
for (const [key, token] of Object.entries(args.args.tokens)) {
this.animationChain.push( this.slideTokenToSquareSlot( token.color, token.square, token.slot ) );
}
console.log( 'Animation chain ('+this.animationChain.length+')', this.animationChain );
break;
case 'endOfMove':
console.log( 'Animation chain ('+this.animationChain.length+')', this.animationChain );
// Play all token movements one by one
fx.chain( this.animationChain ).play();
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 )
{
console.log( 'Leaving state: '+stateName );
switch( stateName )
{
case 'playerTurn':
// Clear animation chain
while (this.animationChain.length > 0) {
this.animationChain.pop();
}
console.log( 'Animation chain ('+this.animationChain.length+')', this.animationChain );
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 )
{
console.log( 'onUpdateActionButtons: '+stateName, args );
if( this.isCurrentPlayerActive() )
{
switch( stateName )
{
case 'playerTurn':
this.addActionButton('actThrowDie-btn', _('Throw die'), () => this.bgaPerformAction("actThrowDie"), null, null, 'gray');
this.addActionButton('actReturnDie-btn', _('Return die'), () => this.bgaPerformAction("actReturnDie"), null, null, 'gray');
break;
case 'endRound':
this.addActionButton('actContinue-btn', _('Continue'), () => this.bgaPerformAction("actContinue"), null, null, 'gray');
this.addActionButton('actEndGame-btn', _('End game'), () => this.bgaPerformAction("actEndGame"), null, null, 'gray');
break;
}
}
},
///////////////////////////////////////////////////
//// Utility methods
getActivePlayerColor: function()
{
return this.gamedatas.players[this.getActivePlayerId()].color;
},
slideTokenToSquareSlot: function( color, square, slot )
{
/* Tokens are placed over square's slots like this:
1 2 3 2 0 3
4 5 6 -> 0 1 0
7 8 9 4 0 5
slots tokens
The first arriving token is always stored on central slot (5)
while others are stored around starting from the upper left slot (1).
*/
console.log( 'slideTokenToSquareSlot:', color, square, slot);
return this.slideToObject( `token_${color}`, `square_${square}_slot_${slot}`, 1000 );
},
highligthActivePlayerToken: function()
{
var activePlayerColor = this.getActivePlayerColor();
console.log( 'highligthActivePlayerToken:', activePlayerColor );
// Clear former active token
document.querySelectorAll('.active').forEach(token => token.classList.remove('active'));
// Add new active token
document.getElementById('token_'+activePlayerColor).firstElementChild.classList.add('active');
this.addTooltipToClass( 'activePlayerToken', '', _('This is the active player token.') );
},
///////////////////////////////////////////////////
//// 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
*/
onClickDie: function( event )
{
console.log( 'onClickDie', event );
// Stop this event propagation
event.preventDefault();
event.stopPropagation();
this.bgaPerformAction("actThrowDie");
},
///////////////////////////////////////////////////
//// 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 tutotheod.game.php file.
*/
setupNotifications: function()
{
console.log( 'notifications subscriptions setup' );
// TODO: here, associate your game notifications with local methods
// Example 1: standard notification handling
// dojo.subscribe( 'cardPlayed', this, "notif_cardPlayed" );
// Example 2: standard notification handling + tell the user interface to wait
// during 3 seconds after calling the method in order to let the players
// see what is happening in the game.
// dojo.subscribe( 'cardPlayed', this, "notif_cardPlayed" );
// this.notifqueue.setSynchronous( 'cardPlayed', 3000 );
//
},
// TODO: from this point and below, you can write your game notifications handling methods
/*
Example:
notif_cardPlayed: function( notif )
{
console.log( 'notif_cardPlayed' );
console.log( notif );
// Note: notif.args contains the arguments specified during you "notifyAllPlayers" / "notifyPlayer" PHP call
// TODO: play the card in the user interface.
},
*/
});
});