-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameButtonListener.js
263 lines (199 loc) · 8.66 KB
/
gameButtonListener.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
const helper = require("./helpers.js");
const btn0 = document.getElementById("btn0");
const btn1 = document.getElementById("btn1");
const btn2 = document.getElementById("btn2");
const btn3 = document.getElementById("btn3");
const defaultNumPennies = 500;
class Game{
play(p1, p2){
let players = new Array(p1, p2);
var nbOfFlip = players[0].choice();
while (!(players[0].isBankrupt() || players[1].isBankrupt())){
var player_bet = players[(nbOfFlip + 1) % 2].choice();
var player_flip = players[nbOfFlip % 2].flip();
if (player_flip == player_bet){
players[(nbOfFlip + 1) % 2].addOnePennie();
players[nbOfFlip % 2].substractOnePennie();
}else{
players[(nbOfFlip + 1) % 2].substractOnePennie();
players[nbOfFlip % 2].addOnePennie();
}
nbOfFlip += 1;
}
return players;
}
}
class Player{
constructor(numberOfPennies){
this.numberOfPennies = numberOfPennies;
}
addOnePennie(){
this.numberOfPennies += 1;
}
substractOnePennie(){
this.numberOfPennies -= 1;
}
isBankrupt(){
return this.numberOfPennies == 0;
}
getNumberOfPennies(){
return this.numberOfPennies;
}
// Randomly returns a 0 or a 1.
choice(){
return Math.floor(Math.random() * 2);
}
flip(){
return this.choice();
}
}
async function play(playerNbrPennies, smartContractNbrPennies, playerBetAmount, contractBetAmount){
let player = new Player(playerNbrPennies);
let smartContract = new Player(smartContractNbrPennies);
let game = new Game();
let result = game.play(player, smartContract);
try{
// The player lost
if(result[0].isBankrupt()){
document.getElementById("loss-amount").innerHTML= " " + parseFloat(playerBetAmount).toFixed(4);
await helper.substractLost(playerBetAmount);
} // The player won
else {
document.getElementById("win-amount").innerHTML= " " + parseFloat(contractBetAmount).toFixed(4);
await helper.addGain(contractBetAmount);
}
}catch(err){
manageSpinnerOff();
console.log(err);
}
}
function manageSpinnerOn(){
$('#cover-spin').show(0);
}
function manageSpinnerOff(){
$('#cover-spin').hide(0);
}
async function isContractSolvableToBet(playerBet, multiplierReward) {
// Verifies if the contract has enough budget to reward the player if he wins.
let budgetOfContract = await helper.getCurrentBudgetOfContract();
bool = (budgetOfContract > (playerBet * multiplierReward));
return bool;
}
async function handleBet(multiplierGameMode) {
var desiredPercentageToBet = document.getElementById("percentBetInput").value;
var currentBalanceUser = await helper.getBalance();
var playerBet = currentBalanceUser * desiredPercentageToBet;
var isContractSolvable = await isContractSolvableToBet(playerBet, multiplierGameMode);
if ( ! isContractSolvable) {
window.alert("Sorry. The contract doesn't have the funds required to bet against you. Please withdraw your funds or chose another game mode if the contract still has funds.");
} else {
manageSpinnerOn();
await play(defaultNumPennies, defaultNumPennies * multiplierGameMode, playerBet, playerBet * multiplierGameMode);
// verify if user still has funds to bet or no. And hide the playing interface if not.
helper.startGameInit();
}
}
btn0.addEventListener('click', async function handleClick(){
// easy mode
// The player has 2/3 chances to win. And his reward is equal to 0.5x the amount of his bet.
try {
handleBet(0.5);
}catch(err){
console.log(err);
}
});
btn1.addEventListener('click', async function handleClick(){
// fair mode
// The player has 50 % chances to win. And his reward is equal to 1x the amount of his bet.
try {
handleBet(1);
}catch(err){
console.log(err);
}
});
btn2.addEventListener('click', async function handleClick(){
// hard mode
// The player has 40 % chances to win. And his reward is equal to 1.5x the amount of his bet.
try {
handleBet(1.5);
}catch(err){
console.log(err);
}
});
btn3.addEventListener('click', async function handleClick(){
// very hard mode
// The player has 1/3 chances to win. And his reward is equal to 2x the amount of his bet.
try {
handleBet(2);
}catch(err){
console.log(err);
}
});
// btn0.addEventListener('click', async function handleClick(){
// // easy mode
// // The player has 2/3 chances to win. And his reward is equal to 0.5x the amount of his bet.
// var desiredPercentageToBet = document.getElementById("percentBetInput").value;
// var currentBalanceUser = await helper.getBalance();
// var playerBet = currentBalanceUser * desiredPercentageToBet;
// var boolContractSolvable = await isContractSolvableToBet(playerBet, 0.5);
// if ( ! boolContractSolvable) {
// window.alert("Sorry. The contract doesn't have the funds required to bet against you. Please withdraw your funds or chose another game mode if the contract still has funds.");
// } else {
// window.Click = "btn0";
// manageSpinnerOn();
// await play(defaultNumPennies, defaultNumPennies * 0.5, playerBet, playerBet*0.5);
// // verify if user still has funds to bet or no. And hide the playing interface if not.
// helper.startGameInit();
// }
// });
// btn1.addEventListener('click', async function handleClick(){
// // fair mode
// // The player has 50 % chances to win. And his reward is equal to 1x the amount of his bet.
// var desiredPercentageToBet = document.getElementById("percentBetInput").value;
// var currentBalanceUser = await helper.getBalance();
// var playerBet = currentBalanceUser * desiredPercentageToBet;
// var boolContractSolvable = await isContractSolvableToBet(playerBet, 1);
// if ( ! boolContractSolvable) {
// window.alert("Sorry. The contract doesn't have the funds required to bet against you. Please withdraw your funds or chose another game mode if the contract still has funds.");
// } else {
// window.Click = "btn1";
// manageSpinnerOn();
// await play(defaultNumPennies, defaultNumPennies, playerBet, playerBet);
// // verify if user still has funds to bet or no. And hide the playing interface if not.
// helper.startGameInit();
// }
// });
// btn2.addEventListener('click', async function handleClick(){
// // hard mode
// // The player has 40 % chances to win. And his reward is equal to 1.5x the amount of his bet.
// var desiredPercentageToBet = document.getElementById("percentBetInput").value;
// var currentBalanceUser = await helper.getBalance();
// var playerBet = currentBalanceUser * desiredPercentageToBet;
// var boolContractSolvable = await isContractSolvableToBet(playerBet, 1.5);
// if ( ! boolContractSolvable) {
// window.alert("Sorry. The contract doesn't have the funds required to bet against you. Please withdraw your funds or chose another game mode if the contract still has funds.");
// } else {
// window.Click = "btn2";
// manageSpinnerOn();
// await play(defaultNumPennies, defaultNumPennies * 1.5, playerBet, playerBet * 1.5);
// // verify if user still has funds to bet or no. And hide the playing interface if not.
// helper.startGameInit();
// }
// });
// btn3.addEventListener('click', async function handleClick(){
// // very hard mode
// // The player has 1/3 chances to win. And his reward is equal to 2x the amount of his bet.
// var desiredPercentageToBet = document.getElementById("percentBetInput").value;
// var currentBalanceUser = await helper.getBalance();
// var playerBet = currentBalanceUser * desiredPercentageToBet;
// var boolContractSolvable = await isContractSolvableToBet(playerBet, 2);
// if ( ! boolContractSolvable) {
// window.alert("Sorry. The contract doesn't have the funds required to bet against you. Please withdraw your funds or chose another game mode if the contract still has funds.");
// } else {
// window.Click = "btn3";
// manageSpinnerOn();
// await play(defaultNumPennies, defaultNumPennies * 2, playerBet, playerBet * 2);
// // verify if user still has funds to bet or no. And hide the playing interface if not.
// helper.startGameInit();
// }
// });