-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs v0.1
71 lines (60 loc) · 2.09 KB
/
index.mjs v0.1
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
import { loadStdlib, ask } from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs'; // import backend
const stdlib = loadStdlib();
function sleep(milliseconds){
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
console.log(`Welcome to BlackJack`);
sleep(2000);
console.log(`Have a seat card Shark`);
sleep(2000);
console.log(`This is a heads up game, Shark vs Dealer`);
sleep(2000);
console.log(`Both start with 1000 balance`);
sleep(2000);
const startingBalance = stdlib.parseCurrency(1000);
const accShark = await stdlib.newTestAccount(startingBalance);
const accDealer = await stdlib.newTestAccount(startingBalance);
const fmt = (x) => stdlib.formatCurrency(x, 4);
const getBalance = async (who) => fmt(await stdlib.balanceOf(who));
const beforeShark = await getBalance(accShark);
const beforeDealer = await getBalance(accDealer);
const ctcShark = accShark.contract(backend);
const ctcDealer = accDealer.contract(backend, ctcShark.getInfo());
const CARD = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace'];
const OUTCOME = ['Card Shark wins!', 'Dealer wins!', 'Draw'];
const Player = (Who) => ({
...stdlib.hasRandom,
getCard: async () => {
const card = Math.floor(Math.random() * 13);
console.log(`${Who} drew a ${CARD[card]}`)
return card;
},
seeOutcome: (outcome) => {
console.log(`${Who} saw outcome ${OUTCOME[outcome]}`);
},
informTimeout: () => {
console.log(`${Who} observed a timeout`);
},
});
await Promise.all([
ctcShark.p.Shark({
...Player('Shark'),
wager: stdlib.parseCurrency(5),
deadline: 10,
}),
ctcDealer.p.Dealer({
...Player('Dealer'),
acceptWager: async (amount) => {
console.log(`Dealer accepts the wager of ${fmt(amount)}`);
},
}),
]);
const afterShark = await getBalance(accShark);
const afterDealer = await getBalance(accDealer);
console.log(`Card Shark went from ${beforeShark} to ${afterShark}.`);
console.log(`Dealer went from ${beforeDealer} to ${afterDealer}.`);