-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm_crypto.html
1753 lines (1627 loc) · 72.7 KB
/
tm_crypto.html
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
<!doctype html>
<html>
<head>
<title>Crypto-forming Mars</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon"
href="http://sliceofbread.neocities.org/tm/mars.ico">
<!--
Copyright 2019 SliceOfBread
-->
<style>
* {box-sizing: border-box}
body {
font-family: "Arial", sans-serif;font-size:28px;
}
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 5px;
}
select {
padding: 5px;
}
#firstelement {
width:900px;
}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current "tab button" class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
float: left;
display: none;
padding: 2px 2px;
border: 1px solid #ccc;
border-left: none;
}
input {
padding: 0px;
font-family:Arial, sans-serif;font-size:24px;
margin: 0;}
select {
font-family:Arial, sans-serif;font-size:28px;
}
.c_heat{background-color:#cb0000;color:#fcff2f;padding:5px;}
img{vertical-align: text-bottom;}
.bigbutt {padding:10px 20px;font-size:30px;border-width:2px}
.bigfont {font-size:30px}
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;padding:0px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-weight:bold;padding:5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;text-align:center;}
.tg .tg-cly1{text-align:center;vertical-align:middle}
.tg .tg-ft9w{background-color:#80ff80;vertical-align:middle}
.tg .tg-k12w{background-color:#ffd480;vertical-align:middle}
.tg .tg-baly{background-color:#fd6864;vertical-align:middle}
.tg-play{background-color:#66ffff;vertical-align:middle}
.tg-corp{background-color:#ff9933;padding:5px;vertical-align:middle}
.tg-prel{background-color:#ff9999;padding:5px;vertical-align:middle}
.tg .tg-col{background-color:#ffe6cc;padding:5px;vertical-align:middle}
</style>
</head>
<body>
<select id="gamelist" onchange="gameListChange()">
<option value="select">Select...</option>
<option value="new">New game</option>
<option value="restart">Restart on new device</option>
</select>
<input id="threadnum" type="number" placeholder="BGG thread number" min="1000000" max="9999999">
<input id="secretseed" type="text" placeholder="Secret seed" size="8" maxlength="8" style="display:none">
<button class="bigbutt" onclick="goClick()">Go!</button>
<div id="setuplink" style="display:none"><a href="tm_crypto_setup.html" target="_blank">Game setup link</a></div>
<h3 id="threadname"></h3>
<button id="rebuildbutt" class="bigbutt" onclick="clickRebuild()" style="color:white;background-color:indigo;display:none">Rebuild game</button>
<button id="deletebutt" class="bigbutt" onclick="clickDelete()" style="color:white;background-color:red;display:none">Delete game</button><br>
<textarea id="boxLog" rows="10" cols="80"></textarea><br>
<div id="myname" class="bigfont" style="display:none">My name is:<select id="selectMe" style="display:inline" onchange="nameChange()">
</select><button class="bigbutt" onclick="clickNameSelected()">Name selected!</button></div>
<div id="requests"><button class="bigbutt" onclick="clickRequestCards()">Request</button>
<select id="reqnum">
<option value="4">4 project cards</option>
<option value="3">3 project cards</option>
<option value="2">2 project cards</option>
<option value="1">1 project card</option>
<option value="64">Research, 4 proj/player</option>
<option class="glblblock" value="16">1 Global Event card</option>
<option class="prellblock" value="8">1 PRELUDE card</option>
<option value="32">REVEAL 1 project card</option>
</select></div>
<textarea id="copyPasteBox" rows="10" cols="80" style="display:block"></textarea>
<button class="bigbutt" onclick="copyToClipboard()">Copy to Clipboard</button>
<input type="checkbox" id="openbgg" checked><span style="font-size:14px">Also open new BGG tab</span><br>
<table class="tg">
<tr>
<th class="tg-corp"><button class="bigbutt tg-corp" onclick="clickCardLink('mycorp')">Corporation</button></th>
<th class="tg-prel dispprel"><button class="bigbutt tg-prel" onclick="clickCardLink('mypreludes')">Prelude</button></th>
<th class="tg-col dispcol"><button class="bigbutt tg-col" onclick="clickCardLink('colonies')">Colonies</button></th>
</tr>
<tr>
<td class="tg-corp"><select id="mycorp" class="tg-corp"></select></td>
<td class="tg-prel dispprel"><select id="mypreludes" class="tg-prel" size="1" multiple></select></td>
<td class="tg-col dispcol"><select id="colonies" class="tg-col" size="1" multiple></select></td>
</tr>
</table>
<table class="tg dispglbl">
<tr style="color:white;background-color:#993300;">
<th colspan="2">Global Events</th>
</tr>
<tr style="background-color:#ff6600;">
<td onclick="clickEvent('current')">Current</td>
<td onclick="clickEvent('current')" id="currentGE"></td>
</tr>
<tr style="background-color:#e6e6e6;">
<td onclick="clickEvent('coming')">Coming</td>
<td onclick="clickEvent('coming')" id="comingGE"></td>
</tr>
<tr style="background-color:#99ccff;">
<td onclick="clickEvent('distant')">Distant</td>
<td onclick="clickEvent('distant')" id="distantGE"></td>
</tr>
</table>
<table class="tg">
<tr>
<th class="tg-ft9w"><button class="bigbutt tg-ft9w" onclick="clickCardLink('hand')">In Hand</button></th>
<td class="tg-cly1" rowspan="3">
<button class="bigbutt" onclick="clickDealt2Hand()" style="display:inline;background-color:#80ff80;">←</button></td>
<th class="tg-k12w"><button class="bigbutt tg-k12w" onclick="clickCardLink('dealt')">Dealt</button></th>
</tr>
<tr>
<td class="tg-ft9w" rowspan="3"><select id="hand" style="display:block;background-color:#80ff80;" size="8" multiple></select></td>
<td class="tg-k12w"><select id="dealt" style="display:block;background-color:#ffd480;"multiple></select></td>
</tr>
<tr>
<td class="tg-cly1"><button class="bigbutt" onclick="clickDealt2Discard()" style="display:inline;background-color:#fd6864;">⇓</button></td>
</tr>
<tr>
<td class="tg-cly1">
<button class="bigbutt" onclick="clickHand2Discard()" style="display:block;background-color:#fd6864;">⇒</button></td>
<th class="tg-baly"><button class="bigbutt tg-baly" onclick="clickCardLink('discard')">Discarded</th>
</tr>
<tr>
<td class="tg-cly1" colspan="2"><button class="bigbutt" onclick="clickHand2Played()" style="display:inline;background-color:#66ffff;">↓</button></td>
<td class="tg-baly" rowspan="3"><select id="discard" style="display:block;background-color:#fd6864;" size="7" multiple></select></td>
</tr>
<tr>
<th class="tg-play" colspan="2"><button class="bigbutt tg-play" onclick="clickCardLink('played')">Played</th>
</tr>
<tr>
<td class="tg-play" colspan="2"><select id="played" style="display:block;background-color:#66ffff;" multiple></select></td>
</tr>
</table>
<table class="tg">
<tr>
<td id="otherplayers"></td>
</tr>
</table>
<div></div>
<button class="bigbutt" onclick="test()" style="display:none">test</button><br>
<textarea id="testLog" rows="10" cols="80" style="display:none"></textarea><br>
<script src="https://cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js">
</script>
<script src="https://peterolson.github.io/BigInteger.js/BigInteger.min.js"></script>
<script src="cards.js"></script>
<script src="tm_crypto.js"></script>
<script>
//startGame();
var game = {};
var pairNum;
var primeBitLength = 128;
var bigIntPrime;
var enc = [];
var dec = [];
var threadNum = "select";
var appMode = "new"; // or "update" or "restart"
var currPos = {"proj":0, "prel":0, "glbl":0};
var lastArticle = "";
const mask = {
"proj" : 1 << 12,
"corp" : 1 << 13,
"prel" : 1 << 14,
"col" : 1 << 15,
"glbl" : 1 << 16};
var masterList = {};
var reshuffleDeck = [];
var playedCards = {};
var shuffleStr = "";
var reshuffleStr = "";
var needReshuffle = false;
var rebuild = false;
var lastNotFinalized = {};
var decObjTemplate = '{"decrypt":{"name":"","corp":{},"prel":{},"proj":{},"glbl":{}}}';
var updateObjTemplate = '{"update":{"name":"","hand":{"corp":[],"prel":[],"proj":[]},"discard":{"corp":[],"prel":[],"proj":[]},"played":{"corp":[],"prel":[],"proj":[]}}}';
var reqProjObjTemplate = '{"request":{"name":""}}';
//var newGenObjTemplate = '{"newGen":{"name":"","proj":0}}';
const locs = ["dealt", "hand", "discard", "played"];
const allLocs = ["dealt", "hand", "discard", "played", "mycorp", "mypreludes", "colonies"];
const deckType = ["corp", "prel", "proj", "glbl"];
function initAll() {
masterList = {
"corp": [],
"prel":[],
"proj":[],
"col":[],
"glbl":[]};
reshuffleDeck = [];
game = {
state:"none",
myName:"",
numPlayers:1,
playerNames:[],
playerOrder:[],
colonies:[],
options:{},
mySeed:"",
threadName:"",
prime:5,
deck:{corp:[],
proj:[],
glbl:[],
prel:[]},
cryptState:{corp:[],
proj:[],
glbl:[],
prel:[]},
postState:{corp:[],
proj:[],
glbl:[],
prel:[]},
baseEnc: [],
baseDec: [],
projEnc: [],
projDec: [],
corpEnc: [],
corpDec: [],
glblEnc: [],
glblDec: [],
prelEnc: [],
prelDec: []};
game.options.corpsPerPlayer = 2;
game.options.prelsPerPlayer = 0;
game.options.projsPerPlayer = 10;
}
function gameListChange() {
var tList = document.getElementsByTagName("table");
for (var t=0; t < tList.length; t++) {
tList[t].style.display = "none";
}
var tn = document.getElementById("gamelist").value;
// if regenerating, show box for mySeed
if (document.getElementById("gamelist").value == "restart") {
document.getElementById("secretseed").style.display = "inline-block";
} else {
document.getElementById("secretseed").style.display = "none";
}
if (document.getElementById("gamelist").value == "new") {
document.getElementById("setuplink").style.display = "block";
} else {
document.getElementById("setuplink").style.display = "none";
}
if (!isNaN(tn)) {
// threadnum
document.getElementById("deletebutt").style.display = "inline-block";
document.getElementById("rebuildbutt").style.display = "inline-block";
game = JSON.parse(localStorage.getItem("thread" + tn));
document.getElementById("threadname").innerHTML = game.threadName;
//document.getElementById("threadnum").value = tn;
textBoxClear("boxLog");
textBoxAppend("boxLog", `Secret seed for thread #${tn} is ${game.mySeed}`, true);
if (game.playerOrder.length) {
textBoxAppend("boxLog", `player order = ${playersOrderString()}`);
}
document.getElementById("threadnum").style.display = "none";
} else {
document.getElementById("deletebutt").style.display = "none";
document.getElementById("rebuildbutt").style.display = "none";
document.getElementById("threadnum").style.display = "inline-block";
document.getElementById("threadname").innerHTML = "";
}
}
function nameChange() {
game.myName = document.getElementById("selectMe").value;
if (!isNaN(threadNum)) {
localStorage.setItem("thread" + threadNum, JSON.stringify(game));
}
}
function goClick() {
const abc = "qwertyuiopasdfghjklzxcvbnm";
var gameSelect = document.getElementById("gamelist").value;
var tn = document.getElementById("threadnum").value;
if (gameSelect === "select") {
// do nothing
} else if (gameSelect === "new") {
initAll();
if (lclThreads.includes(tn)) {
window.alert("Thread " + tn + " already saved locally.");
return;
}
threadNum = Number(tn);
game.state = "init";
appMode = "new";
textBoxClear("boxLog");
// gen seed
var newseed = "";
for (var i=0; i<8; i++) {
newseed += abc.charAt(Math.floor(myRand() * abc.length));
}
textBoxAppend("boxLog", "You will need following seed if you switch computers or browsers");
textBoxAppend("boxLog", "You may want to send to yourself in an email");
textBoxAppend("boxLog", "Keep it secret!");
textBoxAppend("boxLog", `new seed:${newseed}`);
game.mySeed = newseed;
// add thread to saved lists
//lclThreads.push(tn);
//localStorage.cryptoThreads = JSON.stringify(lclThreads);
//addGameToList(tn);
//localStorage.setItem("thread" + tn, JSON.stringify(game));
textBoxAppend("boxLog", "Fetching data from BGG...");
// load data from BGG to get: primeRolls, playerNames
loadDoc(tn, 1, 0);
// Note: processing continues in loadDoc
} else if (gameSelect === "restart") {
// this is for games started on one browser/computer and continuing on another
// user needs to enter both thread # and secret seed
if (document.getElementById("secretseed").value.length < 8) {
window.alert("You must enter 8 character secret seed");
return;
}
if (lclThreads.includes(tn)) {
window.alert("Thread " + tn + " already saved locally.");
return;
}
appMode = "restart";
threadNum = Number(tn);
game.mySeed = document.getElementById("secretseed").value;
game.state = "init";
// add thread to saved lists
//lclThreads.push(tn);
//localStorage.cryptoThreads = JSON.stringify(lclThreads);
//addGameToList(tn);
//localStorage.setItem("thread" + tn, JSON.stringify(game));
// load data from BGG to get: primeRolls, playerNames
loadDoc(tn, 1, 0);
} else {
threadNum = Number(document.getElementById("gamelist").value);
game = JSON.parse(localStorage.getItem("thread" + gameSelect));
appMode = "update";
for (var l of allLocs) {
document.getElementById(l).innerHTML = "";
}
textBoxClear("copyPasteBox");
// loadDoc and process based on game.state
// Note: it is possible game was started on one device, continued on another
// and then continued again on first device which must update itself
loadDoc(gameSelect, 0, 0);
}
}
function processBggInit(d) {
if (d.indexOf("primerolls")==1) {
var r = [];
r = d.match(/>\d+</g);
var pr = "";
for (var i=0; i<r.length; i++) {
r[i] = r[i].slice(1, r[i].length - 1);
while (r[i].length < 6) {
r[i] = "0" + r[i];
}
pr = pr + r[i];
}
game.primeRolls = pr;
textBoxAppend("boxLog", `primeRolls=${game.primeRolls}`);
} else if (d.indexOf("playerNames")==2) {
var names = JSON.parse(d);
game.playerNames = names.playerNames;
game.numPlayers = game.playerNames.length;
textBoxAppend("boxLog", `playerNames=${game.playerNames}`);
document.getElementById("selectMe").innerHTML = "";
for (var n of game.playerNames) {
var toAdd = document.createElement("option");
toAdd.value = n;
toAdd.innerHTML = n;
document.getElementById("selectMe").appendChild(toAdd);
}
} else if (d.indexOf("options")==2) {
var o = JSON.parse(d);
game.options = o.options;
}
textBoxAppend("boxLog", "");
}
function processBggDecks(d) {
// look for shuffled decks
if ((d.indexOf("decks") > 0) && (d.indexOf("decks") < 4)) {
shuffleStr = d;
}
}
function processBggPlay(d) {
if ((d.indexOf("decrypt") > 0) && (d.indexOf("decrypt") < 4)) {
processDecrypt(d);
} else if ((d.indexOf("update") > 0) && (d.indexOf("update") < 4)) {
// check if our update and if so update cryptState
if (game.state != "play") return(0);
processUpdate(d);
} else if ((d.indexOf("request") > 0) && (d.indexOf("request") < 4)) {
if (game.state != "play") return(0);
processRequest(d, "request");
} else if ((d.indexOf("reveal") > 0) && (d.indexOf("reveal") < 4)) {
if (game.state != "play") return(0);
processRequest(d, "reveal");
}
return(1);
}
function processDecrypt(d) {
var o = JSON.parse(d);
// got some decrypt codes
if (o.decrypt.name == game.myName) {
// if ours, mark that we know they were posted
for (var dt of deckType) {
if (o.decrypt.hasOwnProperty(dt)) {
for (var k in o.decrypt[dt]) {
var knum = Number(k);
if (game.postState[dt][knum] & 1) {
game.postState[dt][knum] &= 0xfc;
game.postState[dt][knum] |= 2;
}
}
}
}
return;
}
// codes from other players
var thisPl = game.playerNames.indexOf(o.decrypt.name);
if (thisPl < 0) {
window.alert(`Unknown player name in decrypt: ${o.decrypt.name}`);
return;
}
// check if we need them
for (var dt of deckType) {
if (o.decrypt.hasOwnProperty(dt)) {
for (var k in o.decrypt[dt]) {
// is corp/prel/proj[k] already decoded by this player?
// k / knum is the position in corresponding deck being decoded
var knum = Number(k);
if ((game.cryptState[dt][knum] & (1 << thisPl)) != 0) {
// already decoded, skip it
continue;
}
// Use string to decode
game.deck[dt][knum] = decrypt(game.deck[dt][knum], o.decrypt[dt][k]);
// mark that it was decoded with this player's string
game.cryptState[dt][knum] |= (1 << thisPl);
if ((game.cryptState[dt][knum] & 0x1f) == ((1 << game.numPlayers) - 1)) {
// finished decoding
logDecrypt(game.deck[dt][knum],knum, o.decrypt.name);
}
}
}
}
}
function processRequest(d, rtype) {
// mark any non-posted request as "to be posted"
// update deck atrray pointers
var o = JSON.parse(d);
for (var j=1; j<deckType.length; j++) {// only process prel and proj and glbl
var thisType = deckType[j];
// process prel/proj/glbl
if (o[rtype].hasOwnProperty(thisType)) {
if (thisType == "glbl") {
// global events get treated differently, mark this glbl needs posting
game.postState[thisType][o[rtype][thisType]] = 1;
} else {
// check for overflow, need reshuffle
if (currPos[thisType] + o[rtype][thisType] > game.postState[thisType].length) {
// this request is for more than we can handle
if (thisType != "proj") {
window.alert(`Error! Request for ${thisType} exceeds limits!`);
continue;
}
// proj request past end of deck means we need to reshuffle
textBoxAppend("boxLog", "Project discards need to be reshuffled");
needReshuffle = true;
} else {
if ((o[rtype].name != game.myName) || (rtype == "reveal")) {
// if request was not from us, mark cards needing decrypt
for (var x=0; x < o[rtype][thisType]; x++) {
if (game.postState[thisType][currPos[thisType] + x] == 0) {
// mark for posting (1)
game.postState[thisType][currPos[thisType] + x] = 1;
if (rtype == "reveal") {
game.postState[thisType][currPos[thisType] + x] = 9;
}
}
}
}
// whether request was for us or not, move array pointer
currPos[thisType] += o[rtype][thisType];
}
}
}
}
}
function playerPosNum(name) {
return(game.playerNames.indexOf(name));
}
function processUpdate(d) {
var o = JSON.parse(d);
// got some updates
// process the ones that were ours
if (o.update.name == game.myName) {
// update status of cards that have been posted
// process hand/discard/played
for (var i=1; i<locs.length; i++) {
var thisloc = locs[i];
if (o.update.hasOwnProperty(thisloc)) {
// process corp/prel/proj
for (var j=0; j<deckType.length; j++) {
var thisType = deckType[j];
if (o.update[thisloc].hasOwnProperty(thisType)) {
for (var x of o.update[thisloc][thisType]) {
game.cryptState[thisType][x] &= 0xff;
game.cryptState[thisType][x] |= (0x100 << i);
}
}
}
}
}
} else {
// keep list of all played cards by other players
if (o.update.hasOwnProperty("played")) {
// process corp/prel/proj
for (var j=0; j<deckType.length; j++) {
var thisType = deckType[j];
if (o.update.played.hasOwnProperty(thisType)) {
for (var x of o.update.played[thisType]) {
playedCards[o.update.name][thisType].push(x);
}
}
}
}
}
}
function processCardStates() {
// update corporation selected, if any
if (!game.cryptState.hasOwnProperty("corp")) return;
for (var c=0; c < game.cryptState["corp"].length; c++) {
for (var i=0; i<locs.length; i++) {
thisloc = locs[i];
if ((0x100 << i) & game.cryptState["corp"][c]) {
var tmpIndex = parseInt(game.deck["corp"][c], 16) & 0xfff;
var toAdd = document.createElement("option");
toAdd.value = game.deck["corp"][c];
toAdd.classList.add("tg-corp");
// convert number to card name
toAdd.innerHTML = cards["corp"][tmpIndex].cardNum + ":" + cards["corp"][tmpIndex].cardName;
if (thisloc == "played") {
document.getElementById("mycorp").appendChild(toAdd);
} else {
document.getElementById(thisloc).appendChild(toAdd);
}
}
}
}
// update preludes selected, if any
var tmpPrel = "";
for (var p=0; p < game.cryptState["prel"].length; p++) {
for (var i=0; i<locs.length; i++) {
thisloc = locs[i];
if ((0x100 << i) & game.cryptState["prel"][p]) {
var tmpIndex = parseInt(game.deck["prel"][p], 16) & 0xfff;
var toAdd = document.createElement("option");
toAdd.value = game.deck["prel"][p];
toAdd.classList.add("tg-prel");
// convert number to card name
toAdd.innerHTML = cards["prel"][tmpIndex].cardNum + ":" + cards["prel"][tmpIndex].cardName;
if (thisloc == "played") {
document.getElementById("mypreludes").appendChild(toAdd);
document.getElementById("mypreludes").size = document.getElementById("mypreludes").childNodes.length;
} else {
document.getElementById(thisloc).appendChild(toAdd);
}
}
}
}
// update projects
for (var p=0; p < game.cryptState["proj"].length; p++) {
for (var i=0; i<locs.length; i++) {
thisloc = locs[i];
if ((0x100 << i) & game.cryptState["proj"][p]) {
var tmpIndex = parseInt(game.deck["proj"][p], 16) & 0xfff;
var toAdd = document.createElement("option");
toAdd.value = game.deck["proj"][p];
// convert number to card name
toAdd.innerHTML = cards["proj"][tmpIndex].cardNum + ":" + cards["proj"][tmpIndex].cardName;
document.getElementById(thisloc).appendChild(toAdd);
}
}
}
// set colonies, if used
if (game.colonies.length) {
document.getElementById("colonies").innerHTML = "";
document.getElementById("colonies").size = game.colonies.length;
for (var i=0; i < cards["col"].length; i++) {
if (game.colonies.indexOf(cards["col"][i].cardName) == -1) continue;
var toAdd = document.createElement("option");
var tmp = mask["col"] | i;
toAdd.value = tmp.toString(16);
toAdd.innerHTML = cards["col"][i].cardName + ":";
document.getElementById("colonies").insertBefore(toAdd, document.getElementById("colonies").childNodes[0]);
}
}
// update global events, if turmoil used
if (game.options.expansions.indexOf("turmoil") != -1) {
document.getElementById("currentGE").innerHTML = "";
document.getElementById("comingGE").innerHTML = "";
document.getElementById("distantGE").innerHTML = "";
for (var e=0; e < game.cryptState["glbl"].length; e++) {
if ((game.cryptState["glbl"][e] & 0x1f) == ((1 << game.numPlayers) - 1)) {
// move Coming to Current
document.getElementById("currentGE").innerHTML = document.getElementById("comingGE").innerHTML;
// move Distant to Coming
document.getElementById("comingGE").innerHTML = document.getElementById("distantGE").innerHTML;
// move new glbl to Distant
var num = parseInt(game.deck.glbl[e], 16) & 0xfff;
document.getElementById("distantGE").innerHTML = `${cards.glbl[num].cardNum}:${cards.glbl[num].cardName}`;
}
}
}
}
function logDecrypt(c,csNum, plName) {
// csNum is position in deck
if (c.length > 5) {
textBoxAppend("boxLog", "Bad decryption");
} else {
var cnum = parseInt(c, 16);
var num = cnum & 0xfff;
// cnum is numeric card position in master deck, along with decktype
// reminder 0x1nnn are projects
// 0x2nnn are corporations
// 0x4nnn are preludes
// num is numeric card position in appropriate master deck
for (var dt of deckType) {
if (cnum & mask[dt]) {
//textBoxAppend("boxLog", `Decoded ${dt} ${num}`);
if (dt == "glbl") {
// move global cards to correct spots
// move Coming to Current
document.getElementById("currentGE").innerHTML = document.getElementById("comingGE").innerHTML;
// move Distant to Coming
document.getElementById("comingGE").innerHTML = document.getElementById("distantGE").innerHTML;
// move new glbl to Distant
document.getElementById("distantGE").innerHTML = `${cards[dt][num].cardNum}:${cards[dt][num].cardName}`;
textBoxAppend("boxLog", `New Global Event ${dt}:${csNum} which is ${cards[dt][num].cardNum}:${cards[dt][num].cardName}`,true);
} else if (game.postState[dt][csNum] == 0) {
// only mark 'dealt' if card was not to us (postState == 0)
if ((0xf00 & game.cryptState[dt][csNum]) == 0) {
game.cryptState[dt][csNum] |= 0x100; // mark dealt
}
textBoxAppend("boxLog", `We were dealt ${dt}:${csNum} which is ${cards[dt][num].cardNum}:${cards[dt][num].cardName}`);
} else if (game.postState[dt][csNum] & 8) {
// this was a reveal
textBoxAppend("boxLog", `${plName} revealed ${dt}:${csNum} which is ${cards[dt][num].cardNum}:${cards[dt][num].cardName}`);
} else {
// post card info to log
textBoxAppend("boxLog", `${plName} played ${dt}:${csNum} which is ${cards[dt][num].cardNum}:${cards[dt][num].cardName}`,true);
}
}
}
}
}
function postDecrypts() {
// assemble object with decrypt codes for other players
var decObj = JSON.parse(decObjTemplate);
decObj.decrypt.name = game.myName;
var postFlag = false;
// decrypt codes for other players corps
for (var co=0; co < game.postState["corp"].length; co++) {
if ((Number(game.postState["corp"][co]) & 0x1)) {
decObj.decrypt.corp[co.toString()] = game.corpDec[co];
postFlag = true;
}
}
for (var prel=0; prel < game.postState["prel"].length; prel++) {
if ((Number(game.postState["prel"][prel]) & 0x1)) {
decObj.decrypt.prel[prel.toString()] = game.prelDec[prel];
postFlag = true;
}
}
for (var proj=0; proj < game.postState["proj"].length; proj++) {
if ((Number(game.postState["proj"][proj]) & 0x1)) {
decObj.decrypt.proj[proj.toString()] = game.projDec[proj];
postFlag = true;
}
}
for (var glbl=0; glbl < game.postState["glbl"].length; glbl++) {
if ((Number(game.postState["glbl"][glbl]) & 0x1)) {
decObj.decrypt.glbl[glbl.toString()] = game.glblDec[glbl];
postFlag = true;
}
}
if (postFlag) {
textBoxAppend("copyPasteBox", `[size=5]:::${JSON.stringify(decObj)}:::[/size]` );
}
return(postFlag);
}
function postUpdate() {
// assemble object with
//updateObjTemplate = '{"update":{"name":"","hand":{"corp":[],"prel":[],"proj":[]},"discard":{"corp":[],"prel":[],"proj":[]},"played":{"corp":[],"prel":[],"proj":[]}}}';
var updateObj = JSON.parse('{"update":{"name":""}}');
updateObj.update.name = game.myName;
var postFlag = false;
for (var i = 1; i<locs.length; i++) {
var thisloc = document.getElementById(locs[i]);
for (var e=0; e < thisloc.childNodes.length; e++) {
var thisChildValue = thisloc.childNodes[e].value;
var thisCardNum = parseInt(thisChildValue, 16);
var thisCardDeckPos;
if (thisloc.childNodes[e].innerHTML.substr(0,1) == '*') {
// only update if card is marked as moved
for (var dt of deckType) {
if (thisCardNum & mask[dt]) {
thisCardDeckPos = game.deck[dt].indexOf(thisloc.childNodes[e].value);
if (!updateObj.update.hasOwnProperty(locs[i])) {
updateObj.update[locs[i]] = {};
}
if (!updateObj.update[locs[i]].hasOwnProperty(dt)) {
updateObj.update[locs[i]][dt] = [];
}
updateObj.update[locs[i]][dt].push(thisCardDeckPos);
postFlag = true;
if (locs[i] == "played") {
// card moved to played so also post decrypt
game.postState[dt][thisCardDeckPos] = 5;
}
}
}
}
}
}
if (postFlag) {
textBoxAppend("copyPasteBox", `:::${JSON.stringify(updateObj)}:::` );
}
return(postFlag);
}
function textBoxClear(boxname) {
document.getElementById(boxname).value = "";
}
function textBoxAppend(boxname, i, force) {
if (force || (appMode != "restart")) {
document.getElementById(boxname).value += i + "\n";
document.getElementById(boxname).scrollTop = document.getElementById(boxname).scrollHeight;
}
}
function saveGame() {
if (typeof(Storage) !== "undefined") {
var tn = threadNum.toString();
lclThreads.push(tn);
localStorage.setItem("thread" + tn, JSON.stringify(game));
}
}
function loadDoc(thread, count, minId) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
var tmp = this.responseText;
var tmpdoc = this.responseXML;
lastArticle = "";
//var bodies = tmpdoc.getElementsByTagName("body");
tmp = tmp.replace(/"/g, '\"');
tmp = tmp.replace(/>/g, '>');
tmp = tmp.replace(/</g, '<');
var bggStuff = tmp.split(":::");
// bggStuff is array of [garbage, data, garbage, data, garbage]
// now we remove the garbage
var g=0;
while (g < bggStuff.length) {
bggStuff.splice(g, 1); // throw away garbage
g++;
}
// process bgg data
if (game.state == "init") {
textBoxAppend("boxLog", "Initializing...", true);
for (var xx=0; xx < bggStuff.length; xx++) {
if (bggStuff[xx].indexOf("shuffledBy") != -1) break;
processBggInit(bggStuff[xx]);
}
if (!game.numPlayers) {
window.alert("I didn't find any player names. Double check thread #.");
return;
}
// extract thread name (subject)
game.threadName = tmpdoc.getElementsByTagName("subject")[0].innerHTML;
document.getElementById("threadname").innerHTML = game.threadName;
if (typeof(Storage) !== "undefined") {
var tn = threadNum.toString();
if (!lclThreads.includes(tn)) {
lclThreads.push(tn);
localStorage.cryptoThreads = JSON.stringify(lclThreads);
}
localStorage.setItem("thread" + tn, JSON.stringify(game));
} else {
window.alert("Your browser does not support local storage. Nothing will be saved!")
}
if (!rebuild) {
// get player name
document.getElementById("myname").style.display = "block";
// note: Code continues in clickNameSelected
} else {
document.getElementById("myname").style.display = "block";
rebuild = false;
setTimeout(afterNameSelected, 200);
// note: Code continues in afterNameSelected
}
}
// deal with game options
if (Number(game.options.numColonies)) {
//setClassDisplay("dispcol", "table-cell");
} else {
setClassDisplay("dispcol", "none");
}
if (game.options.expansions.indexOf("prelude") != -1) {
//setClassDisplay("dispprel", "table-cell");
//setClassDisplay("prelblock", "block");
} else {
setClassDisplay("dispprel", "none");
setClassDisplay("prelblock", "none");
}
if (game.options.expansions.indexOf("turmoil") != -1) {
//setClassDisplay("dispglbl", "table-cell");
//setClassDisplay("glblblock", "block");
} else {
setClassDisplay("dispglbl", "none");
setClassDisplay("glblblock", "none");
}
// create master lists of cards
if (masterList.corp.length == 0) {
// we haven't created masterLists yet so do that now
for (var dt of deckType) {
// if no Prelude/Turmoil, do not make lists
if ((dt=="prel") && (game.options.expansions.indexOf("prelude") == -1)) continue;
if ((dt=="glbl") && (game.options.expansions.indexOf("turmoil") == -1)) continue;
for (var c=0; c < cards[dt].length; c++) {
var firstChar = cards[dt][c].cardNum.substr(0,1);
if (isNaN(firstChar)) {
// card starts with a letter
if ((firstChar =='P') && (game.options.expansions.indexOf("prelude") == -1)) continue;
if ((firstChar =='C') && (game.options.expansions.indexOf("colonies") == -1)) continue;
if ((firstChar =='X') && (game.options.expansions.indexOf("promo") == -1)) continue;
if ((firstChar =='T') && (game.options.expansions.indexOf("turmoil") == -1)) continue;
if (firstChar =='Z') {
var pnum = Number(cards[dt][c].cardNum.substr(1));
if ((pnum > 12) && (pnum < 18) && (game.options.expansions.indexOf("venus") == -1)) continue;
if ((pnum > 17) && (pnum < 23) && (game.options.expansions.indexOf("prelude") == -1)) continue;
if ((pnum > 22) && (pnum < 28) && (game.options.expansions.indexOf("colonies") == -1)) continue;
if ((pnum > 27) && (pnum < 33) && (game.options.expansions.indexOf("turmoil") == -1)) continue;
if ((pnum > 32) && (pnum < 39) && (game.options.expansions.indexOf("promo") == -1)) continue;
}
} else {
// must be a project check if in venus or promo range
var pnum = Number(cards[dt][c].cardNum);
if ((pnum > 212) && (pnum < 262) && (game.options.expansions.indexOf("venus") == -1)) continue;
if ((pnum > 208) && (pnum < 213) && (game.options.expansions.indexOf("promo") == -1)) continue;
}
// if this cardNum is on exclude list then skip it
if (game.options.hasOwnProperty('exclude') && (game.options.exclude[dt].indexOf(cards[dt][c].cardNum) != -1)) continue;
var tmp = mask[dt] | c;
masterList[dt].push(tmp.toString(16));
}
}
if (game.options.expansions.indexOf("colonies") != -1) {
for (var c=0; c < cards["col"].length; c++) {
if (game.options.hasOwnProperty('exclude') && (game.options.exclude["col"].indexOf(cards["col"][c].cardNum) != -1)) continue;
masterList["col"].push(cards["col"][c].cardName);
}
}
}
if (game.state == "shuffle") {
textBoxAppend("boxLog", "Processing shuffle...", true);
game.deck["corp"]=[];
game.deck["proj"]=[];
game.deck["prel"]=[];
game.deck["glbl"]=[];
shuffleStr = "";
bggStuff.forEach(processBggDecks);
processShuffleStr();
localStorage.setItem("thread" + threadNum, JSON.stringify(game));
}
if (game.state == "initialdeal") {
textBoxAppend("boxLog", "Processing initial deal...", true);
// process any decrypt stuff posted
needReshuffle = false;
for (var x=0; x < bggStuff.length; x++) {
// run decryption on any cards needing it
if (!processBggPlay(bggStuff[x])) break;
if (needReshuffle) break;
}
// if we haven't yet then post codes for initial deal
// do this by checking if any corps await posting
var weAreWaiting = false;
for (var c=0; c < game.postState["corp"].length; c++) {
if ((game.postState["corp"][c] & 0xf) == 1) weAreWaiting = true;
}
if (weAreWaiting) {
// still need to post initial deal
postDecrypts();
} else {
// we have posted initial deal
game.state = "play";
localStorage.setItem("thread" + threadNum, JSON.stringify(game));
};
}
currPos.proj = game.numPlayers * game.options.projsPerPlayer;
currPos.prel = game.numPlayers * game.options.prelsPerPlayer;
needReshuffle = false;