-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rsh
530 lines (457 loc) · 18.1 KB
/
index.rsh
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
'reach 0.1';
//#region Enums
const RSS_ENUM_SIZE = 4;
const PLAYER_COUNT = 3;
const MAXIMUM_BUILDINGS_ON_TILE = 3;
const [isResource, WHEAT, ORE, WOOD, BRICK] = makeEnum(RSS_ENUM_SIZE);
const [isPlayer, pNONE, pALICE, pBOB, pCARL] = makeEnum(4);
const [isGamePhase, RESOURCE_GEN, BUILDING, TRADE] = makeEnum(3);
//#endregion
//#region Player Definitions
const MAP_SIZE = 7;
const Player =
{
...hasRandom,
log: Fun(true, Null),
informTimeout: Fun([], Null),
getSeed: Fun([], UInt),
seeMap: Fun([Array(Object({
rss: UInt,
roll: UInt,
}), MAP_SIZE)], Null),
seeGameState: Fun([Object({
winner: UInt,
roll: UInt,
round: UInt,
turn: UInt,
phase: UInt,
resources: Array(Array(UInt, RSS_ENUM_SIZE), PLAYER_COUNT),
buildings: Array(Array(UInt, MAXIMUM_BUILDINGS_ON_TILE), MAP_SIZE)
})], Null),
placeBuilding: Fun([], Object({ tile: UInt, skip: Bool })),
offerTrade: Fun([], Object({
recievePlayer: UInt,
offer: Array(UInt, RSS_ENUM_SIZE),
payment: Array(UInt, RSS_ENUM_SIZE),
skip: Bool
})),
recieveTradeOffer: Fun([Object({
offerPlayer: UInt,
offer: Array(UInt, RSS_ENUM_SIZE),
payment: Array(UInt, RSS_ENUM_SIZE)
})], Bool),
offerTradeCallback: Fun([Bool], Null)
};
const Alice = {
...Player,
wager: UInt,
testaroonie: UInt,
};
const Bob = {
...Player,
acceptWager: Fun([UInt], Null)
};
//#endregion
export const main = Reach.App(
{}, [Participant('Alice', Alice), Participant('Bob', Bob), Participant('Carl', Bob)], (A, B, C) => {
//#region Alice Presents Wager
// shows alice's wager
A.only(() => {
const wager = declassify(interact.wager);
});
// pays + publish wager
A.publish(wager).pay(wager);
commit();
//#endregion
//#region Bob + Carl Accept Wager
B.only(() => {
interact.acceptWager(wager);
});
B.pay(wager);
//.timeout(DEADLINE, () => closeTo(A, informTimeout));
commit();
C.only(() => {
interact.acceptWager(wager);
});
C.pay(wager);
//.timeout(DEADLINE, () => closeTo(A, informTimeout));
commit();
//#endregion
//#region World Generation
//@TODO: Try putting first world generation step in with the wager step
//@TODO: Delete the two statements below and uncomment the true logic
A.only(() => {
const deleteThisCode = declassify(interact.testaroonie);
});
A.publish(deleteThisCode);
/*
A.only(() => {
const _seedA = interact.getSeed();
const [_commitA, _saltA] = makeCommitment(interact, _seedA);
const commitA = declassify(_commitA);
});
A.publish(commitA);
// .timeout(DEADLINE, () => closeTo(B, informTimeout));
commit();
unknowable(B, A(_seedA, _saltA));
unknowable(C, A(_seedA, _saltA));
B.only(() => {
const _seedB = interact.getSeed();
const [_commitB, _saltB] = makeCommitment(interact, _seedB);
const commitB = declassify(_commitB);
});
B.publish(commitB);
// .timeout(DEADLINE, () => closeTo(B, informTimeout));
commit();
unknowable(A, B(_seedB, _saltB));
unknowable(C, B(_seedB, _saltB));
C.only(() => {
const seedC = declassify(interact.getSeed());
});
C.publish(seedC);
// .timeout(DEADLINE, () => closeTo(A, informTimeout));
commit();
A.only(() => {
const [saltA, seedA] = declassify([_saltA, _seedA]);
});
A.publish(saltA, seedA);
checkCommitment(commitA, saltA, seedA);
commit();
B.only(() => {
const [saltB, seedB] = declassify([_saltB, _seedB]);
});
B.publish(saltB, seedB);
checkCommitment(commitB, saltB, seedB);
*/
//@TODO: Remove these seeds because they conflict with the generated ones
const seedA = 324445;
const seedB = 164775;
const seedC = 824649;
// seed is calculated from the (hopefully random) input of each player
const seed = seedA + seedB + seedC;
// decides the world
// didnt want to make algorithm for good random world bc im lazy so instead you get this
const map = array(Object({ rss: UInt, roll: UInt }), [
{ rss: (seed + 6) % RSS_ENUM_SIZE, roll: (seed + 9) % 8 },
{ rss: seed % RSS_ENUM_SIZE, roll: seed % 8 },
{ rss: (seed + 18) % RSS_ENUM_SIZE, roll: (seed + 21) % 8 },
{ rss: (seed + 3) % RSS_ENUM_SIZE, roll: (seed + 6) % 8 },
{ rss: (seed + 15) % RSS_ENUM_SIZE, roll: (seed + 18) % 8 },
{ rss: (seed + 12) % RSS_ENUM_SIZE, roll: (seed + 15) % 8 },
{ rss: (seed + 9) % RSS_ENUM_SIZE, roll: (seed + 12) % 8 },
]);
//#endregion
// ------ GAMEPLAY BEGINS HERE ------
//#region Resources + Actions
// show everyone the map before any interactions start
each([A, B, C], () => {
interact.seeMap(map);
});
function createStarterResourceArray() {
return array(UInt, [2, 2, 2, 2]);
}
function createStarterBuildingArray() {
return array(UInt, [pNONE, pNONE, pNONE]);
}
var gameState = {
winner: pNONE,
// stores the resources of each player.
// 0 - Alice, 1 - Bob, 2 - Carl
resources: array(Array(UInt, RSS_ENUM_SIZE), [
createStarterResourceArray(),
createStarterResourceArray(),
createStarterResourceArray(),
]),
// stores which round that the gameState is in
round: 0,
// stores the roll so that the frontend can be responsive
roll: 4,
// stores which player is supposed to be playing
turn: pALICE,
// stores the player's phase of the game
phase: RESOURCE_GEN,
//buildings
buildings: array(Array(UInt, MAXIMUM_BUILDINGS_ON_TILE), [
createStarterBuildingArray(),
createStarterBuildingArray(),
createStarterBuildingArray(),
createStarterBuildingArray(),
createStarterBuildingArray(),
createStarterBuildingArray(),
createStarterBuildingArray(),
])
};
invariant(
isPlayer(gameState.winner) && //fails
gameState.resources.length == PLAYER_COUNT &&
gameState.roll >= 2 && gameState.roll <= 12 && //fails
gameState.round >= 0 &&
isPlayer(gameState.turn) && // fails
isGamePhase(gameState.phase) && //fails
balance() == wager * PLAYER_COUNT
);
while (gameState.winner == pNONE) {
commit();
// sends the resource data to the frontend
function letPlayersSeeGameState(localGameState) {
each([A, B, C], () => {
interact.seeGameState(localGameState);
});
}
// returns true if the specified player has enough resources in the localGameState
function ensurePlayerHasResources(localGameState, player, resources) {
//resources.length != localGameState.resources[player].length ? false :
return ( //TODO!!: first 0 should be player
localGameState.resources[0][0] >= resources[0] &&
localGameState.resources[0][1] >= resources[1] &&
localGameState.resources[0][2] >= resources[2] &&
localGameState.resources[0][3] >= resources[3]
);
}
// rolls the dice and gives players the correct amount of resources
function diceRollPhase(localGameState, player) {
/**
* We're being lazy here so this is all deterministic based off of the PUBLIC seed
* In other words, user front ends can predict future rolls.
* SOLUTIONS:
* 1. Make a new seed at the beginning but obfuscate it. Probably won't be safe after first roll.
* 2. Ask for a new random from each player each round (annoying and cost ineffective).
* 3. Base rolls calculation off of player interactions to make it different each time (players affect the roll by design).
*/
const localRoll =
((seedA + seedB) * gameState.round) % 6 + ((seedB + seedC) * gameState.round % 6) + 2;
assert(localRoll < 12);
return {
winner: localGameState.winner,
roll: localRoll,
round: localGameState.round + 1,
turn: player,
phase: BUILDING, // transitions to the next phase, which is building
buildings: localGameState.buildings,
// yeah... right now it's just giving resources to player 1
resources: localGameState.resources.set(0, array(UInt, [
localGameState.resources[0][0] + 1,
localGameState.resources[0][1] + 1,
localGameState.resources[0][2] + 1,
localGameState.resources[0][3] + 1
]))
};
}
// returns the game state when attempting to build a building
function attemptBuildingPhase(localGameState, player, buildCmd) {
// returns 3 if there is no building space, otherwise it returns the empty space
function tileHasBuildingSpace(tile) {
const result = 3
- (tile[0] == pNONE ? 1 : 0)
- (tile[1] == pNONE ? 1 : 0)
- (tile[2] == pNONE ? 1 : 0);
return result;
}
/*@BUG-186
* After updating reach on 6/14/2021 at 6:00 PST, the unhandled rejection disappeared,
* but strange behaviour associated with it continued.
*
* To demonstrate, run the program as it is now. Click on a tile as player 1, and
* click build. After a few smart contract interactions, it should appear as built.
* Then, redo the process but this time remove all of the "A.interact" calls within
* this function. Doing the same as before will not result in the same output.
*
* I have noticed that this functionality relies on the "A.interact" being within the
* if/else statements.
*
* I still recieve an unhandled rejection further down the line, after the build phase.
* To see it, first compile with the "A.interact" lines uncommented. During the build
* phase, build a building. Then, during the trade phase, cancel the trade.
*
* Note that for some reason this will not occur when then user opts to "cancel" the
* building phase (skipping the conditional "A.interact" lines).
*/
// skips if that's what the player wants to do
if (buildCmd.skip) {
A.interact.log("It got within the skip, so it should be returning winner as 2.");
return {
winner: localGameState.winner,
roll: localGameState.roll,
round: localGameState.round,
turn: player,
phase: TRADE, // transitions to the next phase, which is trade
resources: localGameState.resources,
buildings: localGameState.buildings,
};
}
// issues the command if that's what the player wants to do
else if (buildCmd.tile >= 0 && buildCmd.tile < MAP_SIZE) {
A.interact.log("It got within the second if condition.");
const buildingSpace = tileHasBuildingSpace(localGameState.buildings[buildCmd.tile]);
A.interact.log(buildingSpace);
A.interact.log(MAXIMUM_BUILDINGS_ON_TILE);
// if the tile to build on has space
if (buildingSpace < MAXIMUM_BUILDINGS_ON_TILE) {
A.interact.log("It got within the third if condition.");
return {
winner: localGameState.winner,
roll: localGameState.roll,
round: localGameState.round,
turn: player,
phase: TRADE, // transitions to the next phase, which is trade
resources: localGameState.resources,
buildings: localGameState.buildings.set(buildCmd.tile, array(UInt, [
buildingSpace == 0 ? player : localGameState.buildings[buildCmd.tile][0],
buildingSpace == 1 ? player : localGameState.buildings[buildCmd.tile][1],
buildingSpace == 2 ? player : localGameState.buildings[buildCmd.tile][2],
]))
};
}
}
// if it hasn't returned at this point, then a faulty command was given
return {
winner: localGameState.winner,
roll: localGameState.roll,
round: localGameState.round,
turn: player,
phase: TRADE, // transitions to the next phase, which is trade
resources: localGameState.resources,
buildings: localGameState.buildings,
};
}
function calculateRSSDifference(currentVal, tradePlayer, tradeOffer, examinedPlayer, rssIndex) {
const result = tradePlayer == examinedPlayer ?
((currentVal + tradeOffer.offer[rssIndex]) - tradeOffer.payment[rssIndex]) :
tradeOffer.recievePlayer == examinedPlayer ?
((currentVal + tradeOffer.payment[rssIndex]) - tradeOffer.offer[rssIndex]) : 0;
return result;
}
// returns the game state after an attempted trade offer
function attemptTradeOffer(localGameState, player, nextPlayer, tradeResponse, tradeOffer) {
// this doesn't work because of compiler errors but it's how it should work
if (!tradeResponse) {
return {
winner: localGameState.winner,
roll: localGameState.roll,
round: localGameState.round,
turn: nextPlayer, // next player assuming trading is the last step
phase: RESOURCE_GEN, // transitions to the next phase, which is rss generation
buildings: localGameState.buildings,
resources: localGameState.resources,
};
}
else {
const aliceRss = array(UInt, [
calculateRSSDifference(localGameState.resources[0][WHEAT], player, tradeOffer, 0, WHEAT),
0,//calculateRSSDifference(localGameState.resources[0][ORE], player, tradeOffer, 0, ORE),
0, //localGameState.resources[0][WOOD] + 1000,// + calculateRSSDifference(0, WOOD),
localGameState.resources[0][BRICK] + 1000,// + calculateRSSDifference(0, BRICK)
]);
const bobRss = array(UInt, [
calculateRSSDifference(localGameState.resources[1][WHEAT], player, tradeOffer, 1, WHEAT),
0, //calculateRSSDifference(localGameState.resources[1][ORE], player, tradeOffer, 1, ORE),
0, //localGameState.resources[1][WOOD] + calculateRSSDifference(1, WOOD),
0, //localGameState.resources[1][BRICK] + calculateRSSDifference(1, BRICK)
]);
const carlRss = array(UInt, [
0, //calculateRSSDifference(localGameState.resources[2][WHEAT], 2, WHEAT),
0, //localGameState.resources[2][ORE] + calculateRSSDifference(2, ORE),
0, //localGameState.resources[2][WOOD] + calculateRSSDifference(2, WOOD),
0, //localGameState.resources[2][BRICK] + calculateRSSDifference(2, BRICK)
]);
return {
winner: localGameState.winner,
roll: localGameState.roll,
round: localGameState.round,
turn: nextPlayer, // next player assuming trading is the last step
phase: RESOURCE_GEN, // transitions to next phase
buildings: localGameState.buildings,
resources: array(Array(UInt, RSS_ENUM_SIZE), [aliceRss, bobRss, carlRss])
};
}
}
// ALICE: Dice Roll Phase
A.interact.log("Dice Roll Phase");
const gameState1 = diceRollPhase(gameState, pALICE);
letPlayersSeeGameState(gameState1);
// ALICE: Building Phase
A.interact.log("Building Phase");
A.only(() => {
const aBuilding = declassify(interact.placeBuilding());
});
A.publish(aBuilding);
commit();
const gameState2 = attemptBuildingPhase(gameState1, pALICE, aBuilding);
letPlayersSeeGameState(gameState2);
// ALICE: Trade Deal Phase
A.only(() => {
const aTrade = declassify(interact.offerTrade());
interact.log(aTrade);
});
A.publish(aTrade);
const aTradeAllowed =
!aTrade.skip &&
isPlayer(aTrade.recievePlayer) &&
ensurePlayerHasResources(gameState2, pALICE, aTrade.offer) &&
ensurePlayerHasResources(gameState2, aTrade.recievePlayer, aTrade.payment);
A.interact.log(aTradeAllowed);
const bobCanTrade = aTrade.recievePlayer == pBOB;
A.interact.log(bobCanTrade);
const carlCanTrade = aTrade.recievePlayer == pCARL;
A.interact.log(carlCanTrade);
commit();
B.only(() => {
const _bTradeResponse = aTradeAllowed && bobCanTrade ? interact.recieveTradeOffer({
offerPlayer: pALICE,
offer: aTrade.offer,
payment: aTrade.payment
}) : false;
const bTradeResponse = declassify(_bTradeResponse);
});
B.publish(bTradeResponse);
const gameState3B = attemptTradeOffer(
gameState2,
pALICE, // the person who offered it
pBOB, // the next player
aTradeAllowed && bobCanTrade ? bTradeResponse : false,
aTrade);
commit();
C.only(() => {
const _cTradeResponse = aTradeAllowed && carlCanTrade ? interact.recieveTradeOffer({
offerPlayer: pALICE,
offer: aTrade.offer,
payment: aTrade.payment
}) : false;
const cTradeResponse = declassify(_cTradeResponse);
});
C.publish(cTradeResponse);
const gameState3C = attemptTradeOffer(
gameState2,
pALICE, // the person who offered it
pBOB, // the next player
aTradeAllowed && carlCanTrade ? cTradeResponse : false,
aTrade);
commit();
const gameState3 = bobCanTrade ? gameState3B : gameState3C;
letPlayersSeeGameState(gameState3);
// repeat the last four steps with bob
// repeat again with carl
// check to see if anyone is a winner (haven't finished yet)
// probably will just check for a summation of buildings
A.only(() => {
const test = "test";
});
A.publish(test);
gameState = gameState3;
continue;
}
commit();
//#endregion
// alice always wins in this scenario (pending actual logic)
A.only(() => {
const Apotatoes = declassify(interact.testaroonie);
});
A.publish(Apotatoes);
transfer(wager * PLAYER_COUNT).to(
gameState.winner == pALICE ? A :
gameState.winner == pBOB ? B : C
);
commit();
exit();
}
);