-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.gs
675 lines (598 loc) · 24.4 KB
/
library.gs
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
function initPlayers(tileSet) {
let players = [];
let playerColors = ['red', 'yellow', 'green', 'blue'];
let latestHindrances = [];
for (let j = 0; j < numPlayers; j++) {
players[j] = {
index: j,
passed: false,
color: playerColors[j],
deck: [],
deckIds: [],
activated: [],
workers: tileSet.w[j],
tracks: {
heart: 0,
mind: 0,
},
stats: {
q: 9, // crystals
cq: 0, // pool to collect earned q's within round
c: 0, // calmness
s: 0, // stress
h: 0, // hindrances
sh: 0, // saved hindrance-deductions
p: 0, // poisons
sp: 0, // saved antidotes
px: [0,0,0] // decoded poisons, [a,b,c] a is (rightmost) 1.poison
}
}
};
for(let p=0; p<4; p++){
// OUTGOs are yellow shields! / INCOMEs are shields with MINUS
pNewStats = applyCard2Hindrances(players[p], tileSet.i[p], tileSet.o[p]);
players[ p%numPlayers ].stats.h = pNewStats.h;
players[ p%numPlayers ].stats.sh = pNewStats.sh;
latestHindrances[ p%numPlayers ] = pNewStats.h;
players[ p%numPlayers ].stats.sp = pNewStats.sp;
/* pNewStats = modifyPoisons(players[p], tileSet.i[p], tileSet.o[p]);
_.set(players, '['+(p%4)+'].stats.px[0]', pNewStats.px[0]);
_.set(players, '['+(p%4)+'].stats.px[1]', pNewStats.px[1]);
_.set(players, '['+(p%4)+'].stats.px[2]', pNewStats.px[2]);
*/
}
logPlayerStats(null, { infos: ['places workers'], stats: tileSet.l, noCR: true });
logPlayerStats(null, { infos: ['receives crystals'], crystals: [9] });
return players;
}
function initPlayerDecks(deckIndexes, workerSet) {
let copySheet = spreadSheet.getRange('Sheet1!A2:BZ' + spreadSheet.getLastRow()).getValues();
let copyCalc = spreadSheet.getRange('calc!A2:O' + spreadSheet.getLastRow()).getValues();
console.log('new deck has: '+deckIndexes.join('; '));
for (let j = 0; j < numPlayers; j++) {
for (let i = 0; i < 6; i++) {
let aCard = deckIndexes.shift();
if (!aCard)
aCard = deckIndexes.shift();
let theCard = decodeSheetRow(aCard, copySheet[aCard], copyCalc[aCard]);
// count the number of hindrances to sort descending
let numHindrances = 0;
for(let bit=0; bit<5; bit++)
if ((theCard.outgo & (1<<bit)) == (1<<bit))
numHindrances++;
theCard.nH = numHindrances;
// number of the negative hindrances (result of card)
numHindrances = 0;
for(let bit=0; bit<5; bit++)
if ((theCard.income & (1<<bit)) == (1<<bit))
numHindrances++;
theCard.nHn = numHindrances;
players[j].deck.push(theCard);
players[j].deckIds.push(theCard.id);
}
}
logPlayerStats(null, {
infos: ['picks cards'],
stats: [
players[0].deckIds.join(';'),
players[1].deckIds.join(';'),
players[2].deckIds.join(';'),
players[3].deckIds.join(';')
],
noCR: true
});
for (let x = 0; x < numPlayers; x++) {
players[x].deck = _.sortBy(players[x].deck, ['xp', 'xpp', 'q']);
for (let y = 0; y < 6; y++) {
// players[x].deck[y].act.q -= 100;
}
}
return players;
}
/**
* Initializes the hexagonal action-tiles objects and their attributes,
* by looking up rows 200+ on the values-sheet and its corresponding
* calculated values sheets. Two tiles will be set up for a location,
* thus selecting randomly one side.
* The created action-tile objects are similar with the card objects,
* both use the same decoding and bit-coding methods.
*
* @param {sheet object} - The sheet object
* @return {array} - Array of action-tile objects
*
*/
function initActionTiles() {
let actionTiles = [];
let sibA, sibB, decoded, decoFwd;
let coin = true;
let grid = new BHex.Grid(8);
grid.initMarkers();
let actionSheet = spreadSheet.getRange('Sheet1!A200:BZ227').getValues();
let actionCalcSheet = spreadSheet.getRange('calc!A200:BZ227').getValues();
let posBuffer = new BHex.Axial(0,0);
const colPlayersNum = fromA1Notation('BI1');
const colStage = fromA1Notation('BJ1');
for(let tileIndex=0; tileIndex<actionSheet.length; tileIndex++){
decoded = decodeSheetRow(tileIndex, actionSheet[tileIndex], actionCalcSheet[tileIndex]);
if(tileIndex<actionSheet.length-1){
decoFwd = decodeSheetRow(tileIndex+1, actionSheet[tileIndex+1], actionCalcSheet[tileIndex+1]);
}
decoded.pos = _.pick(posBuffer, ['x', 'y']);
decoded.type = 'tile';
decoded.players = actionSheet[tileIndex][colPlayersNum.column-1];
if(tileIndex % 2 == 0 && _.isObject(decoFwd)){
decoded.sibling = decoFwd.id;
sibA = decoded.id;
}
if(tileIndex % 2 == 1){
decoded.sibling = sibA;
}
if(decoded.players=='') decoded.players = 0;
decoded.stage = actionSheet[tileIndex][colStage.column-1];
if(decoded.stage=='') decoded.stage = '*';
decoded.side = (tileIndex % 2 == 0) ? coin : !coin;
actionTiles.push(
_.pick(decoded, ['pos', 'x', 'y', 'type', 'side', 'xp', 'no', 'id', 'income', 'outgo', 'poisons', 'antidotes', 'players', 'bonuscard', 'stage', 'title', 'sibling']));
// console.info('>>> placing the tile '+decoded.id+' onto grid '+grid.getHexAt(posBuffer).getKey()+' with side '+decoded.side);
// every half of a tile is @same location
if(tileIndex % 2 == 1 && tileIndex<actionSheet.length){
// pick a random neighbor for the new tile from neighbors only
coin = _.random(0, 9999) % 2 == 0 ? false : true;
// this patches up to ensure these tiles are selected initially, other tiles randomly...
if(decoded.players == 2 || decoded.stage == 'wood' || decoded.stage == '*')
coin = true;
posBuffer = grid.placeAtBorder(posBuffer);
if(!posBuffer){break;}
}
}
// console.log(actionTiles);
return actionTiles;
}
/**
* Converts a row from the sheet into a usable object
*
* @param {integer} - The card number from the shuffled deck
* @param {sheet object} - The sheet block containing the card attributes
* @param {sheet object} - The sheet block containing values based on the attributes
* @returns {object} A deck object with values coming from the sheets:
* no: number of the card
* id: id for the card
* type: card | tile
* xp: xp-value
* xpp: boosted xp-value (=when any worker is on a matching tile)
* cost: cost-value
* lvl: level of card for set-collection
* q: crystal cost
* income: bit-masked flags for the 5-hindrances and 3-antidotes
* outgo: bit-masked flags... higher 5 bits (xxxxx000) are hindrances 5-1,
* lower 3 bits (00000xxx) are poisons 3-1
*/
function chooseTiles(placedTiles) {
let tileSet = [{},{},{},{}];
let lines = ['','','',''];
let usedTiles = [];
let outgos = [];
let incomes = [];
let theTile, status, nama;
let filteredTiles = _.shuffle(_.concat(
_.filter(placedTiles, { players: 2, side: true, stage: '*' }),
_.filter(placedTiles, { players: 2, side: true, stage: 'wood' })
));
// first select tiles for masters w/o colliding & tile-doubling
// the slaves are placed onto any tiles w/o above priority-filters
// - a players slave cant be placed where he has a master
// -
while(filteredTiles.length > 1) {
for(let p=0; p<4*2; p++){
theTile = filteredTiles.pop();
usedTiles.push(theTile.id);
if(p<4) {
tileSet[ p%4 ].master = theTile;
// save the outgos & incomes of the masters tiles for all players
outgos[p] = theTile.outgo;
incomes[p] = theTile.income;
nama = 'master';
}else{
tileSet[ p%4 ].slaveOne = theTile;
nama = 'slave';
}
status = nama + ' on ' + theTile.id + ' ('+theTile.title+') @' +
theTile.pos.x + 'x' + theTile.pos.y + ((p<4)?'\n':'');
lines[ p%4 ] += status;
if(p==3){ // refresh the source for slaves
filteredTiles = _.shuffle(_.concat(
_.filter(placedTiles, { players: 2, side: true }),
_.filter(placedTiles, { players: 3, side: true })));
for(let k=0; k<usedTiles.length; k++){
// do we need to remove the siblings also?
filteredTiles = _.reject(filteredTiles, { id: usedTiles[k] });
}
}
}
// console.warn('selected tiles are:');
// console.log(tileSet);
return {w: tileSet, l: lines, o:outgos, i:incomes, t: usedTiles};
}
}
function decodeSheetRow(i, srcValues, calcValues) {
let xpp, xtal, io, obj;
if(_.isArray(srcValues) && _.isArray(calcValues)) {
io = _.slice(srcValues, fromA1Notation('T1').column); // i/o flags
const colPay = fromA1Notation('Q1').column;
const colXP = fromA1Notation('A1').column; // XP
const colXPP = fromA1Notation('B1').column; // XPP
if(!_.isUndefined(calcValues) && _.isArray(calcValues) && calcValues.length > colXPP)
xpp = calcValues[colXPP-1];
xtal = srcValues[fromA1Notation('R1').column-1];
/*
if(!_.isUndefined(xtal) && !_.isUndefined(srcValues[colPay]))
xtal *= (srcValues[colPay] == true) ? -1 : 1;
*/
// drop3 | drop2 | drop1
// AU AV AW AX AY AZ BA BB BC
// 3 | 2 | 1 | 3 | 2 | 1 | 3 | 2 | 1
// XX | XX | XX
let poisoned = ((srcValues[fromA1Notation('AV1').column-1] << 1 |
srcValues[fromA1Notation('AW1').column-1]) << 4) |
((srcValues[fromA1Notation('AY1').column-1] << 1 |
srcValues[fromA1Notation('AZ1').column-1]) << 2) |
((srcValues[fromA1Notation('BB1').column-1] << 1 |
srcValues[fromA1Notation('BC1').column-1]));
if (srcValues[fromA1Notation('AU1').column-1])
poisoned |= 48;
if (srcValues[fromA1Notation('AX1').column-1])
poisoned |= 12;
if (srcValues[fromA1Notation('BA1').column-1])
poisoned |= 3;
obj = {
no: i,
id: srcValues[0],
type: 'card',
title: srcValues[fromA1Notation('BP1').column-1],
act: {
q: Number(calcValues[fromA1Notation('C1').column-1]), // either +Q or Qin is added:
qq: Number(calcValues[fromA1Notation('D1').column-1]), // +Q on calc sheet
qr: Number(calcValues[fromA1Notation('E1').column-1]) // Qin on calc sheet
},
xp: Number(calcValues[fromA1Notation('A1').column-1]),
xpp: Number(xpp.substr(2)), // remove [/-slash] from cell content
cost: calcValues[fromA1Notation('G1').column-1],
str: calcValues[fromA1Notation('I1').column-1],
stage: srcValues[fromA1Notation('BJ1').column-1],
lvl: srcValues[fromA1Notation('F1').column-1],
calm: ((io[13] && io[12]) ? 2 : ((io[13] || io[12]) ? 1 : 0)),
stress: ((io[14] && io[15] && io[16]) ? 3 : ((io[14] && io[15]) ? 2 : (io[14] ? 1 : 0))),
income:
(srcValues[fromA1Notation('Z1').column-1] || srcValues[fromA1Notation('AE1').column-1]) << 4 |
(srcValues[fromA1Notation('Y1').column-1] || srcValues[fromA1Notation('AD1').column-1]) << 3 |
(srcValues[fromA1Notation('X1').column-1] || srcValues[fromA1Notation('AC1').column-1]) << 2 |
(srcValues[fromA1Notation('W1').column-1] || srcValues[fromA1Notation('AB1').column-1]) << 1 |
(srcValues[fromA1Notation('V1').column-1] || srcValues[fromA1Notation('AA1').column-1]) << 0,
antidotes:
srcValues[fromA1Notation('U1').column-1] << 1 | srcValues[fromA1Notation('T1').column-1],
outgo:
// higher 5 bits are hindrances, lower 3 bits are poisons
(srcValues[fromA1Notation('AK1').column-1] || srcValues[fromA1Notation('AP1').column-1]) << 4 |
(srcValues[fromA1Notation('AL1').column-1] || srcValues[fromA1Notation('AQ1').column-1]) << 3 |
(srcValues[fromA1Notation('AM1').column-1] || srcValues[fromA1Notation('AR1').column-1]) << 2 |
(srcValues[fromA1Notation('AN1').column-1] || srcValues[fromA1Notation('AS1').column-1]) << 1 |
(srcValues[fromA1Notation('AO1').column-1] || srcValues[fromA1Notation('AT1').column-1]) << 0,
poisons: poisoned
};
}
return (obj);
}
function modifyPoisons(logSheet, playerObj, incomeVal, outgoVal) {
let oldSP = playerObj.stats.sp;
let oldPX = playerObj.stats.px;
//==============================================
// 0x111 first 2 bits: value
// \---> if set: addressed poison nr (OUTGO)
//(3rd bit) clear: number of antidotes (INCOME)
//==============================================
outgoVal &= 7; // mask out upper bits
incomeVal &= 7;
if(isFlagSet(outgoVal, 1))
oldPX[0]++;
if(isFlagSet(outgoVal, 2))
oldPX[1]++;
if(isFlagSet(outgoVal, 4))
oldPX[2]++;
// 1. apply saved SPs to current poisons, higher-to-lower (e.g.ignorance(3) as first!)
// 2. deduct outgo's from the current poisons
// 3. add new antidotes number as saved SP's
// subtract, if income is set
if(oldSP>0 && isFlagSet(incomeVal, 2) && isFlagSet(incomeVal, 1)){
oldPX[2]--;
oldSP--;
if(oldSP>0 && isFlagSet(incomeVal, 2)){
oldPX[1]--;
oldSP--;
if(oldSP>0 && isFlagSet(incomeVal, 1)){
oldPX[0]--;
oldSP--;
}
}
}
// save remaining SPs back...
logPlayerStats(playerObj, {
playerboard: {
value: '',
note: 'poisons:\n\n'+
'poison-1: '+oldPX[0]+'\n'+
'poison-2: '+oldPX[1]+'\n'+
'poison-3: '+oldPX[2]+'\n'
},
noCR: true,
});
return {sp: oldSP, px: oldPX};
}
function applyCard2Hindrances(playerObj, incomeVal, outgoVal) {
// OUTGOs are yellow shields! / INCOMEs are shields with MINUS
let oldH = playerObj.stats.h;
let oldSH = playerObj.stats.sh;
// OR flags from outcome into hindrances
// if SH has flags, clear them on hindrances
//
// 01001 H
// 01010 SH
// AND
// 01000 >DIFF
// x01001 NOT H
// 00001 =RESULT = NEW H
//
// 01010 SH
// 01000 DIFF
// 00010 AND&NOT = REMAINING SH
//
// set the hindrance-flags according to outgo
oldH |= outgoVal;
oldSH |= incomeVal;
// create a diff of flags and clear them on the hindrance-flags
// diff is what is set on both sides, so only clear those flags afterwards
let diff = oldH & oldSH;
oldH |= diff; // set the flags
// now remove also the 'used' diff from SH, thus saving the remaining SH
oldSH &= ~diff; // kill the flags from sh
return {h: oldH, sh: oldSH};
}
/**
* Extracts fields with no master workers; if third parameter is passed,
* it also extracts fields with slave workers as well.
* It uses only the 'pos' attribute of the tiles.
*
* @param {array, tiles} - Array of location objects where tiles are distributed
* @param {array, masters} - Array of location objects for master workers, extracted from players
* @param {array, slaves} - Array of location objects for slave workers, extracted from players
*
* @return {array} - Array of location objects without masters and/or slaves
*/
function movableFields(tiles, masters, slaves) {
let fF = [];
for(let i=0; i<tiles.length; i++) {
tiles[i] = _.pick(tiles[i], ['pos']).pos;
}
tiles = _.uniqWith(tiles, function(a,b){return a.x==b.x && a.y==b.y});
// collect masters and subtract it from placed tile positions
for(let i=0; i<masters.length; i++) {
if(_.isObject(masters[i].workers.master))
if(_.isObject(masters[i].workers.master.pos))
fF.push(masters[i].workers.master.pos);
}
fF = _.differenceWith(tiles, fF, function(a,b){return a.x==b.x && a.y==b.y});
if(slaves.length>0)
fF = _.differenceWith(fF, slaves, function(a,b){return a.x==b.x && a.y==b.y});
// console.warn('tiles where slaves can move to:');
// console.log(fF);
return(fF);
}
function shuffleCardIndexes(n) {
var arr = [];
for (let i = 0; i < n-1; i++)
arr[i] = i + 1;
return _.shuffle(arr);
}
function changePlayerStats(p, v, s) { // playerNo, whichStat, statNo or value (c/s/q)
if(v == 'h') players[p].stats.h ^= (1 << (s+3)); // toggles the bit on/off
// else if(v == 'p') players[p].stats.p ^= (1 << s);
// else players[p].stats[v] += s;
}
function trashCards(playerNo) {
// TO-DO: give priority to higher valued cards
playerNo %= numPlayers;
const pObj = players[playerNo];
const tempDeck = _.orderBy(pObj.deck, 'act.q', 'desc');
// trash cards for their crystal-values
if( (pObj.stats.q < 2) &&
(pObj.passed == false) &&
(pObj.deck.length > 2) &&
(pObj.activated.length > 2)){
const trashedCard = _.pullAt(pObj.deck, [_.findIndex(pObj.deck, {id: tempDeck[0].id})])[0];
console.log('trashed card is '+trashedCard.id+' with a q value of '+trashedCard.act.qr);
players[ playerNo ].deck = pObj.deck;
players[ playerNo ].stats.q += trashedCard.act.qr;
logPlayerStats(pObj, {
info: 'trashes a card',
crystal: players[ playerNo ].stats.q,
stat: '"' + trashedCard.title + '" (' + trashedCard.id + ')',
note: trashedCard.title + '\n' + JSON.stringify(trashedCard, null, 4),
noCR: false
});
}
}
function playACard(playerNo) {
playerNo %= numPlayers;
let pNewStats, tempQ;
const pObj = players[ playerNo ];
// if crystals still exist & personal deck's not finished
// if available crystals are more than the last cards' cost
if( (pObj.stats.q > 0 && pObj.deck.length > 0) &&
// if upcoming card cost is payable by the player
(pObj.stats.q >= pObj.deck[ _.size(pObj.deck)-1 ].act.q)){
// sort upon:
// - bigger crystal income
// - smaller crystal outgo
// - bigger xp
// - level of players pyramid; next level is always prior; passed levels are unimportant
// - lesser cost
// - matching activity-icons with available prospects
// - more activity-icons
// - axes/required active emotiles corrolation
// create a sorting based on the priority-score for cards
// _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
_.set(pObj.deck, _.orderBy(pObj.deck,
['xp', 'nH', 'nHn', 'str'],
['asc', 'asc', 'desc', 'asc']));
const activatedCard = pObj.deck.pop();
players[ playerNo ].deckIds = _.pull(pObj.deckIds, activatedCard.id);
// pay the crystals
// if their workers are on a matching tile, they receive crystal & rushed xp
console.info('activating card "'+activatedCard.title+'" ('+activatedCard.id+') for player '+playerNo+' in stage '+activatedCard.stage);
// matching workers check
const workerNama = ['master', 'slaveOne', 'slaveTwo', 'slaveThree'];
tempQ = activatedCard.act.q;
for(j=0; j<workerNama.length; j++){
if(_.has(pObj.workers, workerNama[j])){
// being on a matching tile lowers the cost to 1.
if(activatedCard.stage != '*')
if(pObj.workers[ workerNama[j] ].stage == activatedCard.stage) {
tempQ = 1;
break;
}
}
}
if(_.isInteger(tempQ))
players[ playerNo ].stats.q -= tempQ;
// Qin + (Q) -> crystals received within turn
// +Q + (Q) -> crystals received at the end of round
// Qin -> if card is trashed, receive only this amount of crystals
players[ playerNo ].stats.cq +=
activatedCard.act.q + activatedCard.act.qq;
applyHindrances2Gameboard(pObj, activatedCard, null);
pNewStats = applyCard2Hindrances(pObj, activatedCard.income, activatedCard.outgo);
players[ playerNo ].stats.h = pNewStats.h;
players[ playerNo ].stats.sh = pNewStats.sh;
// pNewStats = modifyPoisons(logSheet, pObj, activatedCard.income, activatedCard.outgo);
applyPoisons2Playerboard(pObj, activatedCard);
players[ playerNo ].stats.sp = pNewStats.sp;
const tempArr = pObj.activated;
tempArr.push(activatedCard.id);
players[ playerNo ].activated = tempArr;
logPlayerStats(pObj, {
info: 'plays a card',
crystal: players[ playerNo ].stats.q,
stat: '"' + activatedCard.title + '" (' + activatedCard.id + ')',
note: activatedCard.title + '\n' + JSON.stringify(activatedCard, null, 4),
noCR: true
});
return activatedCard;
}else{
players[ playerNo ].passed = true;
}
}
function applyPoisons2Playerboard(playerObj, card, extender) {
card = _.pick(card, ['poisons', 'antidotes']);
}
function applyHindrances2Gameboard(playerObj, card, extender) {
// decode hindrances
card = _.pick(card, ['income', 'outgo']);
let obstacles = [];
let intacles = [];
// add them to the global tracks
for(let bit=0; bit<5; bit++){
// console.log('outgo:' + card.outgo + '...bit:' + bit + '::' + (1<<bit));
if ((card.outgo & (1<<bit)) == (1<<bit)) {
obstacles.push(bit+1);
gameBoard.tracks['hindrance'+(bit+1)].push(playerObj.color);
}
}
for(let bit=0; bit<5; bit++){
if ((card.income & (1<<bit)) == (1<<bit)) {
intacles.push(bit+1);
gameBoard.tracks['hindrance'+(bit+1)] = _.pullAt(gameBoard.tracks['hindrance'+(bit+1)], 0);
}
}
intacles=_.pull(intacles, 0);
obstacles=_.pull(obstacles, 0);
let baseObj = {
gameboard: {
value:
(obstacles.length > 0 ? 'puts cubes onto hindrance-tracks: '+_.sortBy(obstacles).join(' & ') : '') +
( intacles.length > 0 ? '\nremoves cubes from tracks: '+_.sortBy(intacles).join(' & ') : ''),
note: 'hindrances:\n\n'+
'track-1: '+gameBoard.tracks.hindrance1.join(', ')+'\n'+
'track-2: '+gameBoard.tracks.hindrance2.join(', ')+'\n'+
'track-3: '+gameBoard.tracks.hindrance3.join(', ')+'\n'+
'track-4: '+gameBoard.tracks.hindrance4.join(', ')+'\n'+
'track-5: '+gameBoard.tracks.hindrance5.join(', ')
}
};
logPlayerStats(playerObj, _.isPlainObject(extender) ?
_.merge(baseObj, extender, {noCR: true}) : _.merge(baseObj, {noCR: true}));
}
// fetches top contributers on each track in an ugly way...
function topContributers(theArr) {
if(theArr.length>0){
const arr = _.invertBy(_.countBy(theArr));
return arr[ _.max(_.keys(arr)) ];
}
return [];
}
function roundEnding() {
const playerColors = ['red', 'yellow', 'green', 'blue'];
const allRemaining = _.concat(topContributers(gameBoard.tracks.hindrance1), topContributers(gameBoard.tracks.hindrance2), topContributers(gameBoard.tracks.hindrance3), topContributers(gameBoard.tracks.hindrance4), topContributers(gameBoard.tracks.hindrance5));
if(roundNo>0){
for(let i=0;i<4;i++){
players[i].stats.q = players[i].stats.cq;
players[i].stats.cq = 0;
players[i].passed = false;
players[i].deck = [];
players[i].deckIds = [];
logPlayerStats(players[i], {
noCR: true,
info: 'round clean-up',
crystal: players[i].stats.q,
gameboard: {
value: _.indexOf(allRemaining, playerColors[i]) == -1 ? 'no cubes on any track' :
'leaves ' + _.countBy(allRemaining)[playerColors[i]] + ' cubes on tracks.',
note: 'hindrance-tracks left-overs:\n\n'+
'top1: '+topContributers(gameBoard.tracks.hindrance1)+'\n'+
'top2: '+topContributers(gameBoard.tracks.hindrance2)+'\n'+
'top3: '+topContributers(gameBoard.tracks.hindrance3)+'\n'+
'top4: '+topContributers(gameBoard.tracks.hindrance4)+'\n'+
'top5: '+topContributers(gameBoard.tracks.hindrance5)
}
});
}
}
}
function gameCleanup() {
roundEnding();
logSheet.insertRowsAfter(logSheet.getMaxRows(), 1);
}
function addSlave(p) {
// rule: every slave addition costs a less income object for the master
const slaveNames = ['slaveOne', 'slaveTwo', 'slaveThree'];
}
function applyActiontiles() {
let pObj;
if(roundNo==0){
roundEnding();
logSheet.insertRowsAfter(logSheet.getMaxRows(), 1);
}
for(let i=0;i<4;i++){
pObj = players[i];
// applyPoisons2Playerboard(pObj, pObj.workers.master, {});
applyHindrances2Gameboard(pObj, pObj.workers.master, {
info: 'receives tile benefits',
crystal: pObj.stats.q,
stat: '"' + pObj.workers.master.title + '" (' + pObj.workers.master.id + ')',
note: pObj.workers.master.title + '\n' + JSON.stringify(pObj.workers.master, null, 4),
});
}
}
function flipWorker(p) {
let buff = players[p].workers.slaveOne;
players[p].workers.slaveOne = players[p].workers.master;
players[p].workers.master = buff;
}