-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbettinggame.html
631 lines (492 loc) · 24 KB
/
bettinggame.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
<!DOCTYPE html>
<html class="language_chosen" lang="cz" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width"> <!-- setting the view on multiple devices -->
<meta name="description" content="Web page about blockchain technology">
<meta name="keywords" content="blockchain, ethereum, bitcoin, programování, kurzy, Solidity">
<meta name="author" content="Jiří Musil">
<title>Musil Blockchain | About</title>
<link rel="stylesheet" href="./css/style3.css"> <!-- connecting it with a CSS stylesheet -->
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/jquery.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/web3.min.js"></script>
<script src="script.js"></script>
<script type="text/javascript">
/* Autogenerated - do not fiddle */
if(typeof(Contracts)==="undefined") var Contracts={};
(function(module, Contracts) {
var data={
address: "0x9fc6561a6f54e96a3d53b27f03ec81a9dfdcea4b",
network: "rinkeby",
endpoint: "https://rinkeby.infura.io/",
abi: [{"constant":false,"inputs":[],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"idToPlayer","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getPlayer","outputs":[{"name":"","type":"address"},{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_player","type":"address"}],"name":"getName","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_player","type":"address"}],"name":"getRecord","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getLastGeneratedNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_player","type":"address"}],"name":"getNumberOfTrials","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_number","type":"uint256"},{"name":"_bet","type":"uint256"}],"name":"random","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"startAgain","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"createPlayer","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"getAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"players","outputs":[{"name":"owner","type":"address"},{"name":"id","type":"uint256"},{"name":"name","type":"string"},{"name":"balance","type":"uint256"},{"name":"record","type":"uint256"},{"name":"numberOfTrials","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_player","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getNumberOfPlayers","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"}]
};
Contracts["BettingGame"]=data;
module.exports=data;
})((typeof(module)==="undefined"?{}:module), Contracts);
(function (Contract) {
var web3_instance;
var instance;
var accounts;
var address;
var recordy = [5, 6];
function init(cb) {
$("#noMetamask").show();
web3_instance = new Web3(
(window.web3 && window.web3.currentProvider) ||
new Web3.providers.HttpProvider(Contract.endpoint));
accounts = web3.eth.accounts;
var contract_interface = web3_instance.eth.contract(Contract.abi);
instance = contract_interface.at(Contract.address);
cb();
}
function getAddress() {
var address;
address = accounts[0].toString();
if (address.length > 0) {
$("#address").html(address);
$("#noMetamask").hide();
$("#metamask").show();
}
else {
$("#address").html("Please login into metamask");
}
}
function checkRegistered(address) {
instance.players(address, function (error, result) {
if (error) {
alert(error);
}
else {
if (result[2] != 0) {
$("#registering").hide();
$("#registered").show();
}
else {
$("#registering").show();
$("#registered").hide();
}
}
});
}
function getBalance(address) {
instance.getBalance(address, function (error, result) {
if (error) {
alert(error);
}
else {
$("#balance").html(result.toString());
}
});
}
function getNumberOfPlayers() {
instance.getNumberOfPlayers(function (error, result) {
var count;
if (error) {
alert(error);
}
else {
$("#number_of_players").html(result.toString());
}
});
}
function getName(address, cb) {
instance.getName(address, function (error, result) {
if (error) {
alert(error);
}
else {
// $("#get_nick").html(result);
cb(result);
}
});
}
function getRecord(address, cb) {
instance.getRecord(address, function (error, result) {
if (error) {
alert(error);
}
else {
//$("#record").html(result.toString());
cb(result);
}
});
}
function getGeneratedNumber() {
instance.getLastGeneratedNumber(function (error, result) {
if (error) {
alert(error);
}
else {
$("#generated_number").html(result.toString());
}
});
}
function waitForReceipt(txHash, cb) {
web3_instance.eth.getTransactionReceipt(txHash, function (error, receipt) {
if (error) {
alert(error);
}
else if (receipt !== null) {
cb(receipt);
}
else {
window.setTimeout(function () {
waitForReceipt(txHash, cb)
}, 5000);
}
});
}
function register() {
var name = $("#nick").val();
if (name.length > 0 && document.getElementById('agreement').checked) {
// GAS CONSUMED = 130 000
instance.createPlayer.sendTransaction(name, { from: accounts[0], gas: 300000, value: 1000000000000000 }, function (error, txHash) {
if (error) {
alert("Transaction didn't get through - try again!");
}
else {
waitForReceipt(txHash, function (receipt) {
if (receipt.status === "0x1") {
getNumberOfPlayers();
getAddress();
getName(accounts[0], function (result) {
$("#get_nick").html(result.toString());
});
getBalance();
getRecord(accounts[0], function (result) {
$("#record").html(result.toString());;
});
clearTable();
getAllPlayers();
}
else {
alert("Receive status failed");
}
});
}
});
}
else if (!document.getElementById('agreement').checked && !name.length > 0) {
alert("Please state that you are older than 21 and pick a non-zero name");
}
else if (!document.getElementById('agreement').checked) {
alert(name + ", please state that you are older than 21");
}
else if (!name.length > 0) {
alert("Please pick a non-zero name");
}
else {
alert("Something went wrong, check the input data");
}
}
function random() {
var number = parseInt($("#number").val());
var bet = parseInt($("#bet").val());
if (number > 0 && number <= 100) {
// GAS CONSUMED = 35 000
instance.random.sendTransaction(number, bet, { from: accounts[0], gas: 300000 }, function (error, txHash) {
if (error) {
alert("Transaction didn't get through - try again!");
}
else {
waitForReceipt(txHash, function (receipt) {
if (receipt.status === "0x1") {
getBalance(accounts[0]);
getRecord(accounts[0], function (result) {
$("#record").html(result.toString());;
});
getGeneratedNumber();
clearTable();
getArrayOfRecords();
}
else {
alert("Receive status failed");
}
});
}
});
}
else {
alert("Requirements for betting not met, check it once more! Choose a number from 1 to 100");
}
}
function startAgain() {
// GAS CONSUMED = 35 000
instance.startAgain.sendTransaction({ from: accounts[0], gas: 300000, value: 1000000000000000 }, function (error, txHash) {
if (error) {
alert("Transaction didn't get through - try again!");
}
else {
waitForReceipt(txHash, function (receipt) {
if (receipt.status === "0x1") {
getBalance(accounts[0]);
}
else {
alert("Receive status failed");
}
});
}
});
}
// This will prepare the table to be updated - it deletes the old content
function clearTable() {
let x = document.getElementById("myTable").rows.length;
for (let i = 0; i < x - 1; i++) {
document.getElementById("myTable").deleteRow(-1);
}
}
function getAllPlayers() {
instance.getNumberOfPlayers(function (error, numberOfPlayers) {
if (error) {
alert(error);
}
else {
for (let i = 1; i <= numberOfPlayers; i++) {
instance.getAddress(i, function (error, address) {
if (error) {
alert(error);
}
else {
printPlayerDetails(address);
}
});
}
}
}
);
}
function getArrayOfRecords() {
let records = [];
let sortedRecords = [];
let indexes = [];
let x = 5;
instance.getNumberOfPlayers(function (error, numberOfPlayers) {
if (error) {
alert(error);
}
else {
for (let i = 1; i <= numberOfPlayers; i++) {
instance.getAddress(i, function (error, address) {
if (error) {
alert(error);
}
else {
instance.getRecord(address, function (error, record) {
if (error) {
alert(error);
}
else {
//recordsArray(records, record);
// x = record;
//recordy.push(77);
//alert(record);
// let y = "#record" + i;
// $(y).html(record.toString());
// alert("before");
records[i - 1] = record;
}
});
}
});
}
}
});
setTimeout(function () {
// It is not possible to write sortedRecords = records ... then the records would change as well
records.forEach(function (value) {
sortedRecords.push(value);
});
sortedRecords.sort(function (a, b) { return b - a });
sortedRecords.forEach(function (value) {
let x = records.indexOf(value);
indexes.push(x + 1);
});
//Making it TOP10 - reducing the size of the array to 10 (generaly any number)
let number = 10;
while (indexes.length > number) {
indexes.pop();
}
getPlayerAddresses(indexes);
}, 2000);
}
function getPlayerAddresses(ids) {
// Making an empty table of a right size - to be able to fill arbitrary row any time
for (let i = 1; i <= ids.length; i++) {
let table = document.getElementById("myTable");
let row = table.insertRow(i);
}
ids.forEach(function (value) {
instance.getAddress(value, function (error, _address) {
if (error) {
alert(error);
}
else {
address = _address;
let table = document.getElementById("myTable");
let x = table.rows.length;
// xx is to ensure it will get recorded on the right row - due to big latency
let xx = ids.indexOf(value) + 1;
let row = table.insertRow(xx);
let cell1 = row.insertCell(0);
let cell2 = row.insertCell(1);
let cell3 = row.insertCell(2);
let cell4 = row.insertCell(3);
if(address == accounts[0]){
cell1.innerHTML = "**" + xx.toString() + "**";
cell3.innerHTML = "**" + address.toString() + "**";
getName(address, function (result) {
cell2.innerHTML = "**" + result + "**";
});
getRecord(address, function (result) {
cell4.innerHTML = "**" + result.toString() + "**";
});
}
else {
cell1.innerHTML = xx.toString();
cell3.innerHTML = address.toString();
getName(address, function (result) {
cell2.innerHTML = result;
});
getRecord(address, function (result) {
cell4.innerHTML = result.toString();
});
}
}
});
});
}
$(document).ready(function () {
init(function () {
getAddress();
checkRegistered(accounts[0]);
getArrayOfRecords();
getNumberOfPlayers();
getName(accounts[0], function (result) {
$("#get_nick").html(result.toString());
});
getBalance(accounts[0]);
getRecord(accounts[0], function (result) {
$("#record").html(result.toString());
});
getGeneratedNumber();
});
$("#register").click(function () {
register();
});
$("#submit_bet").click(function () {
random();
});
$("#start_again").click(function () {
startAgain();
});
$("#addTable").click(function () {
addTable();
});
});
})(Contracts['BettingGame']);
</script>
<style type="text/css">
body {
background-color: midnightblue;
color: darksalmon;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
text-align: center;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
</head>
<header>
<div class="container">
<div id="branding">
<h1><span class="highlight">Musil Blockchain</span> </h1>
</div>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="sources.html">Sources</a></li>
<li><a href="services.html">Services</a></li>
<li class="current"><a href="bettinggame.html">Betting game</a></li>
<li class="language"><img src="./img/cz.png" alt="Přejít do češtiny" width="58" height="42"> </li>
<li><a href="index.html">Domů</a></li>
<li><a href="about.html">O mně</a></li>
<li><a href="sources.html">Zdroje</a></li>
<li><a href="services.html">Služby</a></li>
<li class="current"><a href="bettinggame.html">Betting game</a></li>
<li class="language"><img src="./img/gb.png" alt="Switch into english" width="58" height="42"> </li>
</ul>
</nav>
</div>
</header>
<!--
<section id="newsletter">
<div class="container">
<h1>Subscribe to our newsletter</h1>
<form>
<input type="email" placeholder="Enter Email...">
<button type="submit" class="button1">Subscribe</button>
</form>
</div>
</section>
-->
<body>
<div style="text-align:center; padding: 10px;">
<h1>Betting game</h1>
<p>Description: This is my first try to create a decentralized application, in the form of a betting game. The game is running on <strong>Ethereum Rinkeby Test Network</strong>.</p>
<p>Popis: Toto je můj první pokus o stvoření decentralizované aplikace, ve formě sázecí hry. Hra běží na <strong>Ethereum Rinkeby Test Network</strong>.</p>
<p>Rules: Players are betting on choosing higher number than randomly generated number from 1 to 100. Possible win is proportionate to the risk player takes- the smaller number player bets on, the more he can win - however, with a smaller chance. </p>
<p>Pravidla: Hráč vsází na zvolení vyššího čísla než náhodně vygenerované číslo od 1 do 100. Možná výhra je přímo úměrná riziku, které hráč podstoupí - na čím menší číslo si vsadí, tím více může vyhrát - ale s menší šancí. </p>
<p>Source code on Github - link.</p>
<p>Zdrojový kód na GitHubu - odkaz.</p>
<div id="noMetamask" style="display: none;">
<h3 style="color:red"><strong>WARNING: In order to play this game, you need to download Metamask or other Ethereum browser extension, connect it on Rinkeby Test Network and get some test ether.</strong></h3>
<h3 style="color:red"><strong>Varování: Pro hraní této hry a interakci s ní je nezbytné mít nainstalovaný Metamask nebo jiné prohlížečové rozšíření komunikující s ethereovým blockchainem, připojt se k Rinkeby Test Network a vlastnit testovací ether.</strong></h3>
</div>
<div id="metamask" style="display: none">
<div id="registering" style="display: none">
<br>
<input type="text" id="nick" placeholder="Write your nick..."/>
<input type="checkbox" id="agreement"> I am over 21
<button id="register">Start playing!</button>
</div>
<h2>ETH address: <span id="address"></span></h2>
<div id="registered" style="display: none">
<h2>Nick: <span id="get_nick"></span></h2>
<h2>My balance: <span id="balance"></span></h2>
<button id="start_again">Start again with 1000</button>
<h2>My record: <span id="record"></span></h2>
<input type="number" id="number" placeholder="Write your number..."/>
<input type="number" id="bet" placeholder="Write your bet..."/>
<button id="submit_bet">Submit bet</button>
<h2>Generated number: <span id="generated_number"></span></h2>
</div>
<br>
<h3>Leaderboard - TOP10</h3>
<table id="myTable">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Address</th>
<th>Record</th>
</tr>
</thead>
</table>
<h2>Number of players: <span id="number_of_players"></span></h2>
</div>
</div>
<footer>
<p>Jiří Musil, Copyright © 2019</p>
</footer>
</body>
</html>