/
dipDappDoe.js
2491 lines (1919 loc) · 112 KB
/
dipDappDoe.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
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
// test/dipDappDoe.js
const DipDappDoe = artifacts.require("./DipDappDoe.sol");
const LibString = artifacts.require("./LibString.sol");
let gamesInstance, libStringInstance;
contract('DipDappDoe', function (accounts) {
const player1 = accounts[0];
const player2 = accounts[1];
const randomUser = accounts[5];
const testingGasPrice = 100000000000;
it("should be deployed", async function () {
gamesInstance = await DipDappDoe.deployed();
assert.isOk(gamesInstance, "instance should not be null");
assert.equal(typeof gamesInstance, "object", "Instance should be an object");
const timeout = await gamesInstance.timeout.call();
assert.equal(timeout.toNumber(), 2, "The base timeout to test should be set to 2");
libStringInstance = await LibString.deployed();
assert.isOk(libStringInstance, "instance should not be null");
assert.equal(typeof libStringInstance, "object", "Instance should be an object");
});
it("should start with no games at the begining", async function () {
let gamesIdx = await gamesInstance.getOpenGames.call();
assert.deepEqual(gamesIdx, [], "Should have zero games at the begining");
});
it("should use the saltedHash function from the library", async function () {
let hash1 = await libStringInstance.saltedHash.call(123, "my salt 1");
let hashA = await gamesInstance.saltedHash.call(123, "my salt 1");
let hash2 = await libStringInstance.saltedHash.call(123, "my salt 2");
let hashB = await gamesInstance.saltedHash.call(123, "my salt 2");
let hash3 = await libStringInstance.saltedHash.call(234, "my salt 1");
let hashC = await gamesInstance.saltedHash.call(234, "my salt 1");
assert.equal(hash1, hashA, "Contract hashes should match the library output");
assert.equal(hash2, hashB, "Contract hashes should match the library output");
assert.equal(hash3, hashC, "Contract hashes should match the library output");
assert.notEqual(hash1, hash2, "Different salt should produce different hashes");
assert.notEqual(hash1, hash3, "Different numbers should produce different hashes");
assert.notEqual(hash2, hash3, "Different numbers and salt should produce different hashes");
});
// DipDappDoe.createGame
it("should create a game with no money", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "my salt 1");
await gamesInstance.createGame(hash, "John");
assert.equal(await web3.eth.getBalance(gamesInstance.address).toNumber(), 0, "The contract should have registered a zero amount of ether owed to the players");
const emittedEvents = await eventWatcher.get();
assert.isOk(emittedEvents, "Events should be an array");
assert.equal(emittedEvents.length, 1, "There should be one event");
assert.isOk(emittedEvents[0], "There should be one event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), 0, "The game should have index zero");
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
let gamesIdx = await gamesInstance.getOpenGames.call();
gamesIdx = gamesIdx.map(n => n.toNumber());
assert.include(gamesIdx, gameIdx, "Should include the new game");
let [cells, status, amount, nick1, nick2, ...rest] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 0, "The game should not be started");
assert.equal(amount.toNumber(), 0, "The game should have no money");
assert.equal(nick1, "John", "The player 1 should be John");
assert.equal(nick2, "", "The player 2 should be empty");
assert.deepEqual(rest, [], "The response should have 5 elements");
let lastTransaction = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransaction.toNumber(), 0, "The last timestamp should be set");
let [p1, p2, ...rest3] = await gamesInstance.getGamePlayers(gameIdx);
assert.equal(p1, player1, "The address of player 1 should be set");
assert.equal(p2, "0x0000000000000000000000000000000000000000", "The address of player 2 should be empty");
assert.deepEqual(rest3, [], "The response should have 2 elements");
});
it("should create a game with money", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "my salt 1");
await gamesInstance.createGame(hash, "Jane", { value: web3.toWei(0.01, 'ether') });
let balance = await web3.eth.getBalance(gamesInstance.address);
assert.equal(balance.comparedTo(web3.toWei(0.01, 'ether')), 0, "The contract should have registered 0.01 ether owed to the players");
const emittedEvents = await eventWatcher.get();
assert.isOk(emittedEvents, "Events should be an array");
assert.equal(emittedEvents.length, 1, "There should be one event");
assert.isOk(emittedEvents[0], "There should be one event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), 1, "The game should have index one");
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
let gamesIdx = await gamesInstance.getOpenGames.call();
gamesIdx = gamesIdx.map(n => n.toNumber());
assert.include(gamesIdx, gameIdx, "Should include the new game");
let [cells, status, amount, nick1, nick2, ...rest] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 0, "The game should not be started");
assert.equal(amount.comparedTo(web3.toWei(0.01, 'ether')), 0, "The game should have 0.01 ether");
assert.equal(nick1, "Jane", "The player 1 should be Jane");
assert.equal(nick2, "", "The player 2 should be empty");
assert.deepEqual(rest, [], "The response should have 5 elements");
let lastTransaction = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransaction.toNumber(), 0, "The last timestamp should be set");
let [p1, p2, ...rest3] = await gamesInstance.getGamePlayers(gameIdx);
assert.equal(p1, player1, "The address of player 1 should be set");
assert.equal(p2, "0x0000000000000000000000000000000000000000", "The address of player 2 should be empty");
assert.deepEqual(rest3, [], "The response should have 2 elements");
});
// DipDappDoe.acceptGame
it("should reject accepting a non-existing game", async function () {
try {
await gamesInstance.acceptGame(1234, 0, "Mary");
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
it("should reject accepting games with a different amount of money than expected", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "my salt 1");
await gamesInstance.createGame(hash, "Johny", { value: web3.toWei(0.02, 'ether') });
let balance = await web3.eth.getBalance(gamesInstance.address);
assert.equal(balance.comparedTo(web3.toWei(0.03, 'ether')), 0, "The contract should have registered 0.02 ether owed to the players");
const emittedEvents = await eventWatcher.get();
assert.isOk(emittedEvents, "Events should be an array");
assert.equal(emittedEvents.length, 1, "There should be one event");
assert.isOk(emittedEvents[0], "There should be one event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), 2, "The game should have index two");
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
let gamesIdx = await gamesInstance.getOpenGames.call();
gamesIdx = gamesIdx.map(n => n.toNumber());
assert.include(gamesIdx, gameIdx, "Should include the new game");
let [cells, status, amount, nick1, nick2, ...rest] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 0, "The game should not be started");
assert.equal(amount.comparedTo(web3.toWei(0.02, 'ether')), 0, "The game should have 0.02 ether");
assert.equal(nick1, "Johny", "The player 1 should be Johny");
assert.equal(nick2, "", "The player 2 should be empty");
assert.deepEqual(rest, [], "The response should have 5 elements");
try {
await gamesInstance.acceptGame(gameIdx, 0, "Kathy");
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
it("should accept an available game", async function () {
const creationEventWatcher = gamesInstance.GameCreated();
const acceptanceEventWatcher = gamesInstance.GameAccepted();
let hash = await libStringInstance.saltedHash.call(123, "my salt 1");
// create game
await gamesInstance.createGame(hash, "James", { value: web3.toWei(0.005, 'ether') });
let balance = await web3.eth.getBalance(gamesInstance.address);
assert.equal(balance.comparedTo(web3.toWei(0.035, 'ether')), 0, "The contract should have registered 0.005 ether owed to the players");
let emittedEvents = await creationEventWatcher.get();
assert.isOk(emittedEvents, "Events should be an array");
assert.equal(emittedEvents.length, 1, "There should be one event");
assert.isOk(emittedEvents[0], "There should be one event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), 3, "The game should have index three");
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
let gamesIdx = await gamesInstance.getOpenGames.call();
gamesIdx = gamesIdx.map(n => n.toNumber());
assert.include(gamesIdx, gameIdx, "Should include the new game");
let [cells, status, amount, nick1, nick2, ...rest] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 0, "The game should not be started");
assert.equal(amount.comparedTo(web3.toWei(0.005, 'ether')), 0, "The game should have 0.005 ether");
assert.equal(nick1, "James", "The player 1 should be James");
assert.equal(nick2, "", "The player 2 should be still empty");
assert.deepEqual(rest, [], "The response should have 5 elements");
// accept game
await gamesInstance.acceptGame(gameIdx, 0, "Kathy", { value: web3.toWei(0.005, 'ether'), from: player2 });
balance = await web3.eth.getBalance(gamesInstance.address);
assert.equal(balance.comparedTo(web3.toWei(0.04, 'ether')), 0, "The contract should have registered 0.005 more ether owed to the players");
emittedEvents = await acceptanceEventWatcher.get();
assert.isOk(emittedEvents, "Events should be an array");
assert.equal(emittedEvents.length, 1, "There should be one accepted game event");
assert.isOk(emittedEvents[0], "There should be one accepted game event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The game should have the last gameIdx");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
[cells, status, amount, nick1, nick2, ...rest] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 0, "The game should not be started yet");
assert.equal(amount.comparedTo(web3.toWei(0.005, 'ether')), 0, "The game should have 0.005 ether");
assert.equal(nick1, "James", "The player 1 should be James");
assert.equal(nick2, "Kathy", "The player 2 should be Kathy");
assert.deepEqual(rest, [], "The response should have 5 elements");
lastTransaction = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransaction.toNumber(), 0, "The last timestamp should be set");
[p1, p2, ...rest3] = await gamesInstance.getGamePlayers(gameIdx);
assert.equal(p1, player1, "The address of player 1 should be set");
assert.equal(p2, player2, "The address of player 2 should be set");
assert.deepEqual(rest3, [], "The response should have 2 elements");
});
it("should reject accepting an already accepted game", async function () {
const creationEventWatcher = gamesInstance.GameCreated();
const acceptanceEventWatcher = gamesInstance.GameAccepted();
let hash = await libStringInstance.saltedHash.call(123, "my salt 1");
await gamesInstance.createGame(hash, "Jim");
let emittedEvents = await creationEventWatcher.get();
assert.isOk(emittedEvents, "Events should be an array");
assert.equal(emittedEvents.length, 1, "There should be one event");
assert.isOk(emittedEvents[0], "There should be one event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), 4, "The game should have index four");
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
let gamesIdx = await gamesInstance.getOpenGames.call();
gamesIdx = gamesIdx.map(n => n.toNumber());
assert.include(gamesIdx, gameIdx, "Should include the new game");
await gamesInstance.acceptGame(gameIdx, 0, "Dana", { from: player2 });
emittedEvents = await acceptanceEventWatcher.get();
assert.isOk(emittedEvents, "Events should be an array");
assert.equal(emittedEvents.length, 1, "There should be one accepted game event");
assert.isOk(emittedEvents[0], "There should be one accepted game event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The game should have the last gameIdx");
try {
await gamesInstance.acceptGame(gameIdx, 0, "Dana", { from: randomUser });
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
try {
await gamesInstance.acceptGame(gameIdx, 1, "Donna", { from: player2 });
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
try {
await gamesInstance.acceptGame(gameIdx, 1, "Dolly");
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
it("should reject accepting a game if it is already started", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 200, "Dana", {from: player2});
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: player1});
try {
await gamesInstance.acceptGame(gameIdx, 150, "Jack", {from: randomUser});
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should have been reverted. Instead, the error was: " + err.message);
}
});
it("should reject accepting a game if it has already ended", async function () {
const eventWatcher = gamesInstance.GameCreated();
const endingWatcher = gamesInstance.GameEnded();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
let emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Dana", {from: player2});
// now the player 2 will win, and the game will end
await gamesInstance.confirmGame(gameIdx, 124, "initial salt", {from: player1});
emittedEvents = await endingWatcher.get();
assert.equal(emittedEvents.length, 2, "GameEnded should have 2 events");
assert.isOk(emittedEvents[0].args.opponent, "Opponent should be an address");
assert(emittedEvents[0].args.opponent == player1 || emittedEvents[0].args.opponent == player2, "The opponent should be among the players");
assert(emittedEvents[1].args.opponent == player1 || emittedEvents[1].args.opponent == player2, "The opponent should be among the players");
try {
await gamesInstance.acceptGame(gameIdx, 150, "Jack", {from: randomUser});
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
it("should remove the game from the list of available games when accepted", async function(){
const eventWatcher = gamesInstance.GameCreated();
let gamesIdx1 = await gamesInstance.getOpenGames.call();
gamesIdx1 = gamesIdx1.map(n => n.toNumber());
let hash = await libStringInstance.saltedHash.call(123, "my salt 1");
await gamesInstance.createGame(hash, "Jim");
let emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
let gamesIdx2 = await gamesInstance.getOpenGames.call();
gamesIdx2 = gamesIdx2.map(n => n.toNumber());
await gamesInstance.acceptGame(gameIdx, 0, "Dana", { from: player2 });
let gamesIdx3 = await gamesInstance.getOpenGames.call();
gamesIdx3 = gamesIdx3.map(n => n.toNumber());
assert.notInclude(gamesIdx1, gameIdx, "Should not include the new game yet");
assert.include(gamesIdx2, gameIdx, "Should include the new game");
assert.notInclude(gamesIdx3, gameIdx, "Should not include the new game anymore");
});
// DipDappDoe.confirmGame
it("should reject confirming a non existing game", async function () {
try {
await gamesInstance.confirmGame(12345687, 100, "some salt");
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
try {
await gamesInstance.confirmGame(23456789, 200, "some more salt");
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
it("should reject confirming a game that has not been accepted yet", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
try {
await gamesInstance.confirmGame(gameIdx, 123, "some salt");
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
try {
await gamesInstance.confirmGame(gameIdx, 200, "some more salt");
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
it("should give the game for the second user if the hash does not match with the revealed values", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Dana", {from: player2});
await gamesInstance.confirmGame(gameIdx, 124, "initial salt", {from: player1});
// 123 != 124 => player 2 should be the winner
let [cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 12, "Player 2 should be the winner");
});
it("should confirm a valid game for player 1", async function () {
const creationWatcher = gamesInstance.GameCreated();
const startingWatcher = gamesInstance.GameStarted();
let hash = await libStringInstance.saltedHash.call(100, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const creationEvents = await creationWatcher.get();
let gameIdx = creationEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 200, "Dana", {from: player2});
let lastTransactionpre = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransactionpre.toNumber(), 0, "The last timestamp should be set");
await new Promise(resolve => setTimeout(resolve, 1000));
await gamesInstance.confirmGame(gameIdx, 100, "initial salt", {from: player1});
const startingEvents = await startingWatcher.get();
gameIdx = startingEvents[0].args.gameIdx.toNumber();
assert.isOk(startingEvents, "Events should be an array");
assert.equal(startingEvents.length, 1, "There should be one started game event");
assert.isOk(startingEvents[0], "There should be one started game event");
assert.equal(startingEvents[0].args.gameIdx.toNumber(), gameIdx, "The game should have the last gameIdx");
assert.equal(startingEvents[0].args.opponent, player2, "The opponent should be player 2");
// 100 ^ 200 is even => player 1 should start
let [cells, status, amount, nick1, nick2, ...rest] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 1, "Player 1 should be able to start");
assert.equal(amount.comparedTo(0), 0, "The game should have 0 ether");
assert.equal(nick1, "Jim", "The player 1 should be Jim");
assert.equal(nick2, "Dana", "The player 2 should be Dana");
assert.deepEqual(rest, [], "The response should have 5 elements");
let lastTransactionpost;
lastTransactionpost = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransactionpost.toNumber(), lastTransactionpre.toNumber(), "The last timestamp should be newer");
const [p1, p2, ...rest3] = await gamesInstance.getGamePlayers(gameIdx);
assert.equal(p1, player1, "The address of player 1 should still be set");
assert.equal(p2, player2, "The address of player 2 should still be set");
assert.deepEqual(rest3, [], "The response should have 2 elements");
});
it("should confirm a valid game for player 2", async function () {
const creationWatcher = gamesInstance.GameCreated();
const startingWatcher = gamesInstance.GameStarted();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const creationEvents = await creationWatcher.get();
let gameIdx = creationEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 200, "Dana", {from: player2});
let lastTransactionpre = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransactionpre.toNumber(), 0, "The last timestamp should be set");
await new Promise(resolve => setTimeout(resolve, 1000));
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: player1});
const startingEvents = await startingWatcher.get();
gameIdx = startingEvents[0].args.gameIdx.toNumber();
assert.isOk(startingEvents, "Events should be an array");
assert.equal(startingEvents.length, 1, "There should be one started game event");
assert.isOk(startingEvents[0], "There should be one started game event");
assert.equal(startingEvents[0].args.gameIdx.toNumber(), gameIdx, "The game should have the last gameIdx");
assert.equal(startingEvents[0].args.opponent, player2, "The opponent should be player 2");
// 123 ^ 200 is odd => player 2 should start
let [cells, status, amount, nick1, nick2, ...rest] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board should be empty");
assert.equal(status.toNumber(), 2, "Player 2 should be able to start");
assert.equal(amount.comparedTo(0), 0, "The game should have 0 ether");
assert.equal(nick1, "Jim", "The player 1 should be Jim");
assert.equal(nick2, "Dana", "The player 2 should be Dana");
assert.deepEqual(rest, [], "The response should have 5 elements");
let lastTransactionpost = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransactionpost.toNumber(), lastTransactionpre.toNumber(), "The last timestamp should be newer");
const [p1, p2, ...rest3] = await gamesInstance.getGamePlayers(gameIdx);
assert.equal(p1, player1, "The address of player 1 should still be set");
assert.equal(p2, player2, "The address of player 2 should still be set");
assert.deepEqual(rest3, [], "The response should have 2 elements");
});
it("should reject confirming a game if it is already started", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 200, "Dana", {from: player2});
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: player1});
try {
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: player1});
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should have been reverted. Instead, the error was: " + err.message);
}
});
it("should reject confirming a game if it has already ended", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Dana", {from: player2});
// now the player 2 will win, and the game will end
await gamesInstance.confirmGame(gameIdx, 124, "initial salt", {from: player1});
try {
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: player1});
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
it("should reject game confirmations from users other than the creator", async function () {
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
const emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 200, "Dana", {from: player2});
try {
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: randomUser});
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
});
// DipDappDoe.markPosition
it("should register a user's valid move, emit an event and change the turn", async function(){
const createEventWatcher = gamesInstance.GameCreated();
const markEventWatcher = gamesInstance.PositionMarked();
let hash = await libStringInstance.saltedHash.call(100, "initial salt");
await gamesInstance.createGame(hash, "James", {from: player1});
let emittedEvents = await createEventWatcher.get();
let gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Jane", {from: player2});
await gamesInstance.confirmGame(gameIdx, 100, "initial salt", {from: player1});
// ------ GAME 1 ------
let lastTransactionpre = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransactionpre.toNumber(), 0, "The last timestamp should be set");
await new Promise(resolve => setTimeout(resolve, 1500));
// 1 0 0
// 0 0 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 0, {from: player1});
let [cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [1, 0, 0, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 2, "The game should be for player 2");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
let lastTransactionpost = await gamesInstance.getGameTimestamp(gameIdx);
assert.isAbove(lastTransactionpost.toNumber(), lastTransactionpre.toNumber(), "The last timestamp should be newer");
// 1 0 2
// 0 0 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 2, {from: player2});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [1, 0, 2, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
// 1 0 2
// 1 0 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 3, {from: player1});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [1, 0, 2, 1, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 2, "The game should be for player 2");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
// 1 0 2
// 1 0 2
// 0 0 0
await gamesInstance.markPosition(gameIdx, 5, {from: player2});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [1, 0, 2, 1, 0, 2, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
// 1 0 2
// 1 0 2
// 1 0 0
await gamesInstance.markPosition(gameIdx, 6, {from: player1});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [1, 0, 2, 1, 0, 2, 1, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 11, "The game should be won by player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
// ------------------------------
hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "James", {from: player1});
emittedEvents = await createEventWatcher.get();
gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Jane", {from: player2});
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: player1});
// ------ GAME 2 ------
// 2 0 0
// 0 0 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 0, {from: player2});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [2, 0, 0, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
// 2 0 1
// 0 0 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 2, {from: player1});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [2, 0, 1, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 2, "The game should be for player 2");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
// 2 0 1
// 0 2 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 4, {from: player2});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [2, 0, 1, 0, 2, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
// 2 0 1
// 0 2 1
// 0 0 0
await gamesInstance.markPosition(gameIdx, 5, {from: player1});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [2, 0, 1, 0, 2, 1, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 2, "The game should be for player 2");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
// 2 0 1
// 0 2 1
// 0 0 2
await gamesInstance.markPosition(gameIdx, 8, {from: player2});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [2, 0, 1, 0, 2, 1, 0, 0, 2], "The board does not match");
assert.equal(status.toNumber(), 12, "The game should be won by player 2");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
// ------------------------------
hash = await libStringInstance.saltedHash.call(100, "initial salt");
await gamesInstance.createGame(hash, "James", {from: player1});
emittedEvents = await createEventWatcher.get();
gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Jane", {from: player2});
await gamesInstance.confirmGame(gameIdx, 100, "initial salt", {from: player1});
// ------ GAME 3 ------
// 0 1 0
// 0 0 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 1, {from: player1});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 1, 0, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 2, "The game should be for player 2");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
// 0 1 2
// 0 0 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 2, {from: player2});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 1, 2, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
// 0 1 2
// 0 1 0
// 0 0 0
await gamesInstance.markPosition(gameIdx, 4, {from: player1});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 1, 2, 0, 1, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 2, "The game should be for player 2");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
// 0 1 2
// 0 1 2
// 0 0 0
await gamesInstance.markPosition(gameIdx, 5, {from: player2});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 1, 2, 0, 1, 2, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player1, "The opponent should be player 1");
// 0 1 2
// 0 1 2
// 0 1 0
await gamesInstance.markPosition(gameIdx, 7, {from: player1});
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 1, 2, 0, 1, 2, 0, 1, 0], "The board does not match");
assert.equal(status.toNumber(), 11, "The game should be won by player 1");
emittedEvents = await markEventWatcher.get();
assert.equal(emittedEvents.length, 1, "There should be a new marked event");
assert.equal(emittedEvents[0].args.gameIdx.toNumber(), gameIdx, "The marked game should match");
assert.equal(emittedEvents[0].args.opponent, player2, "The opponent should be player 2");
});
it("should reject marks beyond the board's range", async function(){
const eventWatcher = gamesInstance.GameCreated();
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "James", {from: player1});
const emittedEvents = await eventWatcher.get();
const gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Jane", {from: player2});
await gamesInstance.confirmGame(gameIdx, 123, "initial salt", {from: player1});
try {
await gamesInstance.markPosition(gameIdx, 9, {from: player2}); // invalid move
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
let [cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 2, "The game should be for player 2");
await gamesInstance.markPosition(gameIdx, 8, {from: player2}); // valid move
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 2], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
try {
await gamesInstance.markPosition(gameIdx, 100, {from: player1}); // invalid move
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 2], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
try {
await gamesInstance.markPosition(gameIdx, 500, {from: player1}); // invalid move
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 2], "The board does not match");
assert.equal(status.toNumber(), 1, "The game should be for player 1");
});
it("should reject marks on non existing, not started or already ended games", async function(){
const eventWatcher = gamesInstance.GameCreated();
// Non existing
try {
await gamesInstance.markPosition(55555, 9, {from: player1}); // invalid move
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
let [cells, status] = await gamesInstance.getGameInfo(55555);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 0, "The game should not be started");
// NOT ACCEPTED
let hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
let emittedEvents = await eventWatcher.get();
let gameIdx = emittedEvents[0].args.gameIdx.toNumber();
try {
await gamesInstance.markPosition(gameIdx, 5, {from: player1}); // invalid move
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 0, "The game should not be started");
// NOT CONFIRMED
hash = await libStringInstance.saltedHash.call(123, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
emittedEvents = await eventWatcher.get();
gameIdx = emittedEvents[0].args.gameIdx.toNumber();
await gamesInstance.acceptGame(gameIdx, 234, "Dana", {from: player2});
try {
await gamesInstance.markPosition(gameIdx, 5, {from: player1}); // invalid move
assert.fail("The transaction should have thrown an error");
}
catch (err) {
assert.include(err.message, "revert", "The transaction should be reverted");
}
[cells, status] = await gamesInstance.getGameInfo(gameIdx);
cells = cells.map(n => n.toNumber());
assert.deepEqual(cells, [0, 0, 0, 0, 0, 0, 0, 0, 0], "The board does not match");
assert.equal(status.toNumber(), 0, "The game should not be started");
// ENDED
hash = await libStringInstance.saltedHash.call(100, "initial salt");
await gamesInstance.createGame(hash, "Jim", {from: player1});
emittedEvents = await eventWatcher.get();
gameIdx = emittedEvents[0].args.gameIdx.toNumber();