-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameScript.js
529 lines (478 loc) · 15.5 KB
/
gameScript.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
// Author Petter Andersson
'use strict'
var playingPlayers;
var players = [];
var turn = 0;
var gameCounter = 0;
var gameEnded = false;
/*
TODO
Move Deck card logic to Card.js
Pass functions for cards as a card field instead of having logic in deck.js looking at which card it is
So in deck: if function != null, run function
Top of Discard show 'Empty' image instead of not showing when discard empty
After this, move it before status messages
Make cards for other players show backside by default
Make the backside of the cards
When backside is added, add a Deck element, same as discard
Deck and Discard visual cards can have their amount of cards on their back (atleast deck)
If a nicer way of showing what is bought than discard pile is found, discard pile could be backside up aswell
Sound
Music on/off button
Sound on/off button
Sound
New Turn
Selects
Animations:
All UI updates
Shop availability changes
Buying a card
Firefox:
Council Room Firefox, sizes differ, text overlap
Gameplay:
Make it more obvious whne you have more buys (1 moire buy with 1 gold maybe)
Code
Change global vars to Caps / Const
Currently animation for both draw and dispaly card - Problem? Seems fine animation wise Check me.
Estetic
Better looking new name CSS input field
Start to Center stuff in css
Choose color and name
Make Player own areas more clearer
Better background color to buttons so they are more easily spottable
Make it more obvious in UI when having to choose a card from your hand (currently the skip button appears only)
Also should be available for shop for Cards where you receive a free card
Scalable card sizes
Use 3 existing card sizes, when card amount in hand goes over a certain limit
Is it required to remove money cards that are used? In buy phase, they are hardly used so its visual only
Could be good in netplay
More visual on what was bought
netplay
Restructure:
Board in middle
Later:
New Cards:
Listed in Deck.js
netplay - nodejs
Remove code about order of players divs
On restructure, this should be set to each user anyway
board cards sent
amount of cards sent to all, which cards only to the player
Deck updates (buys) are sent
Move Player and Deck to server side instead of client side
Private variables / exchange var to let / const
Gather variables in one place
*/
function startGame(){
// Init players
playingPlayers = sessionStorage.getItem('playersPlaying');;
// Init Cards
if(!Number.isInteger(parseInt(playingPlayers))){
playingPlayers = 2;
}
/*
// TODO: Add so cards can be loaded before hand, not important
cards_global = JSON.parse(sessionStorage.getItem('Cards'));
cards_global_id = JSON.parse(sessionStorage.getItem('Cards_id'));
cards_treasure = JSON.parse(sessionStorage.getItem('Cards_Treasure'));
cards_action = JSON.parse(sessionStorage.getItem('Cards_Action'));
cards_victory = JSON.parse(sessionStorage.getItem('Cards_Victory'));
*/
initCardsGlobal();
for(var i = 0; i < playingPlayers; i++){
var tempPlayer = new Player(i);
tempPlayer.initPlayer();
players.push(tempPlayer);
players[i].drawHand();
}
// Choosing turn and start
turn = Math.floor(Math.random() * playingPlayers);
initNewUIElement('div', new Map().set('id', 'turn'), 'info', ['inline']);
initNewUIElement('div', new Map().set('id', 'turn_box'), 'turn', ['inline', 'bold', 'size3_text_medium', 'text_shadow']);
initNewUIElement('div', new Map().set('id', 'helpDiv'), 'info', ['inline']);
initNewUIElement('audio', new Map().set('id', 'audioMain').set('src', 'res/villageMusicShort.mp3').set('loop', ''), //.set('controls', '')
'helpDiv', ['inline', 'margin_top_10']).innerHTML = 'Your browser does not support the audio element';
// TODO: Move the strings to vars
createButton(MUSIC_STRING_PLAY, 'audioButton', 'helpDiv', togglePlay, ['normalButton', 'margin_left_10', 'margin_top_2']);
createButton(HELP_MESSAGE_OPEN, 'helpButton', 'helpDiv', (function(){
var currentName = document.getElementById('helpButton').innerHTML;
if(currentName === HELP_MESSAGE_OPEN){
changeText('helpButton', HELP_MESSAGE_CLOSE);
} else{
changeText('helpButton', HELP_MESSAGE_OPEN)
}
modifyCSSID('toggle', 'helpMessage', 'invis');
}).bind(this), ['normalButton', 'margin_left_10', 'margin_top_2']);
initNewUIElement('div', new Map().set('id', 'helpMessage'), 'helpDiv', ['flex_container', 'invis']);
var stringActions = getHelpString();
var splitted = stringActions.split('\n');
for(var i = 0; i < splitted.length; i++){
var el = initNewUIElement('div', new Map().set('id', 'helpMessage_' + i), 'helpMessage', ['inline', 'bold', 'size2_text_medium', 'text_shadow']);
el.innerHTML = splitted[i];
}
changeText('turn_box', getPlayer(turn).name + ":s turn");
document.getElementById('turn_box').style.backgroundColor = getPlayerColor(turn);
for(var i = 0; i < playingPlayers; i++){
document.getElementById(id_player + i).style.order = 2;
}
document.getElementById(id_player + turn).style.order = 1;
players[turn].startTurn();
}
// Init cards
async function initCardsGlobal(){
initCards();
console.log('Cards should be done - Shop Init');
initShopHTML();
}
function togglePlay(){
var myAudio = document.getElementById('audioMain');
if(myAudio.paused){
changeText('audioButton', MUSIC_STRING_PAUSE); // ⏸
modifyCSSID('add', 'helpDiv', 'margin_top_05');
return myAudio.play();
} else {
changeText('audioButton', MUSIC_STRING_PLAY);
modifyCSSID('remove', 'helpDiv', 'margin_top_05');
return myAudio.pause();
}
}
function changeTurn(){
if(turn + 1 >= players.length){
turn = 0;
} else {
turn++;
}
var element = document.getElementById('turn');
element.addEventListener('animationstart', listener, false);
element.addEventListener('animationend', listener, false);
modifyCSSEl('add', element, 'animation_slideOut');
function listener(event) {
switch(event.type) {
case 'animationstart':
//console.log('animationstart: 1: ' + event.elapsedTime);
break;
case 'animationend':
//console.log('animationend: 1: ' + event.elapsedTime);
modifyCSSEl('remove', element, 'animation_slideOut');
// Outside of screen
changeText('turn_box', players[turn].name + ":s turn"); // Decide ' or :
document.getElementById('turn_box').style.backgroundColor = getPlayerColor(turn);
modifyCSSEl('add', element, 'invis_opacity');
element.removeEventListener('animationstart', listener);
element.removeEventListener('animationend', listener);
element.addEventListener('animationstart', listener2, false);
element.addEventListener('animationend', listener2, false);
modifyCSSEl('add', element, 'animation_slideIn');
function listener2(event) {
switch(event.type) {
case 'animationstart':
//console.log('animationstart: 2: ' + event.elapsedTime);
break;
case 'animationend':
//console.log('animationend: 2: ' + event.elapsedTime);
modifyCSSEl('remove', element, 'invis_opacity');
element.removeEventListener('animationstart', listener2);
element.removeEventListener('animationend', listener2);
modifyCSSEl('remove', element, 'animation_slideIn');
for(var i = 0; i < players.length; i++){
document.getElementById(id_player + i).style.order = 2;
}
document.getElementById(id_player + turn).style.order = 1;
players[turn].startTurn();
break;
}
}
break;
/*
case 'animationiteration':
console.log('animationiteration: ' + event.elapsedTime);
break;
}*/
}
}
}
function backMainMenu(){
location.replace('index.html');
// Reset variables
}
function updateTextPrint(playerIndex, message, printEverywhere = true){
console.log('P' + (playerIndex+1) + ': ' + message);
if(printEverywhere){
document.getElementById(id_text + playerIndex+id_3).innerHTML = document.getElementById(id_text + playerIndex+id_2).innerHTML;
document.getElementById(id_text + playerIndex+id_2).innerHTML = document.getElementById(id_text + playerIndex+id_1).innerHTML;
document.getElementById(id_text + playerIndex+id_1).innerHTML = '> ' + message;
}
}
function updateShopText(message){
//console.log('DEBUG END', message, gameEnded);
if(!gameEnded){
document.getElementById(id_shop + id_text + '1').innerHTML = message;
} else {
if(gameCounter === 0){
document.getElementById(id_shop + id_text + '1').innerHTML = message;
gameCounter++;
}
}
}
function shuffle(array) {
var counter = array.length;
while (counter > 0) {
// Pick a random index
var index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
var temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
function getIDFromCard(id){
var parsed = id.split('_')[1];
if(isNaN(parseInt(parsed))){
return parsed;
}
return parseInt(parsed);
}
function getIDImgFromDiv(id){
var splitted = id.split('_');
return splitted[0] + '_' + splitted[1];
}
function isTurn(playerIndex){
if(turn === playerIndex){
return true;
}
return false;
}
function getPlayer(pid){
return players[pid];
}
function getPlayerCard(pid, cardID){
return getPlayer(pid).cards.hand.getCard(cardID);
}
function getCorrectImage(card){
var sPre = 'res/';
var sPost = '.png';
switch(card.cardType){
case CardType.ACTION_CARD:
return sPre + 'Action' + sPost;
case CardType.VICTORY_CARD:
return sPre + 'Victory' + sPost;
case CardType.TREASURE_CARD:
default:
return sPre + 'Treasure' + sPost;
}
}
// Returns css class for CardType
function getCssClassCard(card){
switch(card.cardType){
case CardType.ACTION_CARD:
return 'card_action';
case CardType.VICTORY_CARD:
return 'card_victory';
case CardType.TREASURE_CARD:
default:
return 'card_treasure';
}
}
function getCssAlign(string, width){
switch(width){
case width_smallest:
return 'size1_' + string;
case width_biggest:
return 'size3_' + string;
case width_middle:
default:
return 'size2_' + string;
}
}
// Returns Font size for center text
function getCssFontSize(card, width, isCenter){
if(isCenter){
switch(card.cardType){
case CardType.ACTION_CARD:
return getCssAlign('text_small', width);
case CardType.VICTORY_CARD:
if(card.name === 'Garden'){
return getCssAlign('text_small', width);
}
case CardType.TREASURE_CARD:
default:
return getCssAlign('text_big', width);
}
} else{
return getCssAlign('text_medium', width);
}
}
// Returns orderNum for card, style.order
// phase = 3 : => score screen
function getCssOrderCard(card, phase){
if(phase === 3){ // Score screen sorting
switch(card.cardType){
case CardType.ACTION_CARD:
return 2;
case CardType.TREASURE_CARD:
return 3;
case CardType.VICTORY_CARD:
return 1;
default:
return 4;
}
} else {
switch(card.cardType){
case CardType.ACTION_CARD:
if(phase === 0 || phase === 2){
return 1;
} else {
return 4;
}
case CardType.TREASURE_CARD:
return 2;
case CardType.VICTORY_CARD:
return 3;
default:
return 4;
}
}
}
// Returns background player color for HTML
function getPlayerColor(index){
switch(index){
case 0:
return 'aquamarine'
case 1:
return 'greenyellow'
case 2:
return 'lightblue'
case 3:
return 'lightpink';
default:
return 'lightgray'
}
}
function getStringNotZero(money, buysLeft, actionsLeft){
var s = '(';
var added = false;
if(money != 0){
s += 'Money: ' + money;
added = true;
}
if(buysLeft != 0){
if(added){
s += ', ';
}
s += 'BuysLeft: ' + buysLeft;
added = true;
}
if(actionsLeft != 0){
if(added){
s += ', ';
}
s += 'ActionsLeft: ' + actionsLeft;
added = true;
}
s += ')';
if(s !== '()'){
return s;
} else {
return '';
}
}
// Called when points should be calculated to see who won
function endGame(){
console.log('Game ending!');
gameEnded = true;
modifyCSSID('add', 'shopCards', 'invis')
var pointsArray = [];
var highestPointPlayer = [];
var highestPoints = -100;
var allPlayerCards = [];
for(var i = 0; i < players.length; i++){
modifyCSSID('add', id_info_cards + i, 'invis');
var cards = players[i].cards.endGetAllCards();
allPlayerCards[i] = cards;
pointsArray[i] = 0;
for(var j = 0; j < cards.length; j++){
if(cards[j].cardType === CardType.VICTORY_CARD){
if(cards[j].name === 'Garden'){
var temp = cards.length;
temp -= (cards.length % 10);
pointsArray[i] += temp / 10;
} else{
pointsArray[i] += cards[j].getValue();
}
}
}
if(pointsArray[i] === highestPoints){
highestPointPlayer.push(i);
} else if(pointsArray[i] > highestPoints){
highestPoints = pointsArray[i];
highestPointPlayer = [];
highestPointPlayer.push(i);
}
}
// TODO: Can add functionality to check for total cost of hand, in treasure, victory & actions cards etc
var s = '';
if(highestPointPlayer.length > 1){
s += 'The winners are ';
} else{
s += 'The winner is ';
}
for(var i = 0; i < highestPointPlayer.length; i++){
if(i !== 0){
s += 'and ';
}
s += players[highestPointPlayer[i]].name + ' ';
}
s += 'with ' + highestPoints + ' points!';
console.log(s);
updateShopText(String(s));
console.log('All Results:');
// Current Data: info_cards, info_discard
for(var i = 0; i < players.length; i++){
removeChildren(id_info_stats + i);
var el = document.getElementById(id_info_stats + i);
modifyCSSEl('add', el, 'flex_container')
var cards = allPlayerCards[i];
var newEl = initNewUIElement('div', new Map(), id_info_stats + i, ['noclick', 'text_shadow', 'size3_text_medium']);
newEl.innerHTML = pointsArray[i] + ' points'; // players[i].name + ': ' +
var newEl2 = initNewUIElement('div', new Map(), id_info_stats + i, ['noclick', 'text_shadow', 'size3_text_medium']);
newEl2.innerHTML = cards.length + ' cards total';
console.log(players[i].name + ': ' + pointsArray[i] + ' points');
console.log(cards.length + ' cards total');
var map = new Map();
for(var j = 0; j < cards.length; j++){
if(typeof map.get(cards[j].name) === 'undefined'){
map.set(cards[j].name, 1);
} else {
map.set(cards[j].name, map.get(cards[j].name) + 1);
}
}
map.forEach(function(value, key){ // Unknown order of these cards
var card = cards_global.get(key);
var victoryCardString = '';
if(card.cardType === CardType.VICTORY_CARD){
var points = 0;
if(card.name === 'Garden'){
var temp = cards.length;
temp -= (cards.length % 10);
points = temp / 10;
} else{
points = card.getValue();
}
var totalPoints = points * value;
victoryCardString = ', Victory Points Worth: ' + totalPoints;
}
var div = initNewUIElement('div', new Map().set('id', id_scoreScreen + id_card + card.name + '_' + i), id_info_stats + i, ['card_container']);
div.style.order = getCssOrderCard(card, 3);
generateCardHTML(card, id_scoreScreen + id_card + card.id + i, id_scoreScreen + id_card + card.name + '_' + i, false, 'card_discard', [getCssClassCard(card)]);
initNewUIElement('div', new Map().set('id', id_scoreScreen + id_text + card.name + '_' + i + id_div), id_scoreScreen + id_card + card.name + '_' + i, 'endScreen_texts').style.order = 1;
initNewUIElement('div', new Map().set('id', id_scoreScreen + id_text + i), id_scoreScreen + id_text + card.name + '_' + i + id_div, ['noclick', 'text_shadow', 'text16'])
.innerHTML = 'Amount: ' + value + victoryCardString;
//tempEl.innerHTML = value + ' ' + key;
console.log(value + ' ' + key);
});
console.log('--------------------------');
}
}