-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
243 lines (203 loc) · 7.99 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width,initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no">
<meta name="x5-fullscreen" content="true">
<meta name="full-screen" content="yes">
<link rel="stylesheet" href="style.css">
<title>Lottery game</title>
</head>
<body>
<div id="app">
<div class="title">Klottery Game</div>
<div v-if="initializing">
Please wait while game is initializing...
</div>
<div v-else>
<button v-if="!waiting" class="play-btn" @click="joinLottery">Play now!</button>
<div class="result" v-else>Waiting...</div>
<div class="result" v-if="result">{{ result }}</div>
<div class="ball-row">
<ball
v-for="(player, i) in currentPlayers"
:player="player"
:key="i"
/>
</div>
</div>
<hr>
<div id="result-area">
<h2>Game history</h2>
<h3 v-if="loadGames">Loading...</h3>
<div v-if="!loadGames">
<div v-if="games.length > 0">
<div v-for="game in games" :key="game.id">
<b>Game ID: {{ game.id }}. Players: {{game.index}}</b>
<ul>
<li v-for="player in game.players" class="player" :class="player === game.winner ? 'winner' : ''">
{{ player }}
</li>
</ul>
</div>
</div>
<span v-else>Empty</span>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script src=lib/nebPay.js></script>
<script src=lib/nebulas.js></script>
<script type="x-template" id="ball">
<div class="ball-box" :style="randStyle()">
{{ player }}
</div>
</script>
<script>
// Const register
//const SMART_CONSTRACT_ADDR = 'n1npz11XKh1g71WfuhM3r18T2ADRJnX1E6f' // Test net
const SMART_CONSTRACT_ADDR = 'n1m5TiDJX7q23XsaDKhSCcdonLbqbppQCa6' // Main net
const HOST_ADDR = 'n1GCcs6AaciCTci5wXuQgC4Y2z5HRwXz4zq'
const INTERVAL_TIME = 5000
const MAX_PER_ROUND = 5
// NebPay init
var NebPay = require("nebpay");
var nebPay = new NebPay();
var serialNumber
var callbackUrl = NebPay.config.testnetUrl;
var lastNas = 0.1
// Nebulas init
const nebulas = require("nebulas")
const Account = nebulas.Account
const neb = new nebulas.Neb();
neb.setRequest(new nebulas.HttpRequest("https://mainnet.nebulas.io"));
// Ball component
const ball = Vue.component('ball', {
template: '#ball',
props: ['player'],
methods: {
randColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
},
randStyle() {
return {
backgroundColor: this.randColor()
}
}
}
})
// Main component
window.app = new Vue({
el: '#app',
data: {
initializing: true,
waiting: false,
result: null,
games: [],
loadGames: false,
currentGameID: null,
currentPlayers: Array(MAX_PER_ROUND).fill('Empty'),
MAX_PER_ROUND: MAX_PER_ROUND,
lastPlayerAddress: null,
},
components: { ball },
mounted() {
this.getHistory().finally(() => this.initializing = false)
},
methods: {
joinLottery() {
var to = SMART_CONSTRACT_ADDR;
var value = 0.1;
var callFunction = "joinLottery"
var callArgs = JSON.stringify([])
const serialNumber = nebPay.call(to, value, callFunction, callArgs, {
callback: callbackUrl
});
this.waiting = true
this.intervalQuery = setInterval(() => {
this.funcIntervalQuery(serialNumber);
}, INTERVAL_TIME);
},
funcIntervalQuery(serialNumber) {
var options = {
callback: callbackUrl
}
nebPay.queryPayInfo(serialNumber,options)
.then((resp) => {
var respObject = JSON.parse(resp)
if(respObject.data.status === 1){
const execute_result = JSON.parse(respObject.data.execute_result)
this.currentGameID = execute_result.game_id
const playerIndex = this.currentPlayers.findIndex(c => c === 'Empty')
this.currentPlayers[playerIndex] = execute_result.player_address
this.lastPlayerAddress = execute_result.player_address
this.result = `Join game ${this.currentGameID} successfully! Waiting for other player..`
this.intervalGetHistoryQuery = setInterval(() => {
this.getHistory();
}, INTERVAL_TIME);
clearInterval(this.intervalQuery);
} else if (respObject.data.status === 0) {
this.result = respObject.data.execute_error
clearInterval(this.intervalQuery);
}
})
.catch(err => console.log(err))
},
getHistory() {
this.loadGames = true
const from = HOST_ADDR;
const gas_price = "1000000"
const gas_limit = "2000000"
const callFunction = "getHistory";
const callArgs = JSON.stringify([]);
const contract = {
"function": callFunction,
"args": callArgs
}
return neb.api.call(from,SMART_CONSTRACT_ADDR,"0","0",gas_price,gas_limit,contract)
.then((res) => {
this.cbGetHistory(res)
this.loadGames = false
})
.catch((err) => {
console.log({err})
this.loadGames = false
clearInterval(this.intervalGetHistoryQuery)
})
},
cbGetHistory(res) {
var result = JSON.parse(res.result)
result = result.filter(a => a !== null).reverse()
const currentGame = this.currentGameID !== null ?
result.find(a => a.id === this.currentGameID) :
(result[0].index >= MAX_PER_ROUND ? null : result[0])
this.processGame(currentGame)
this.games = result
},
processGame(game) {
if (!game) return
// Run spin
game.players.forEach((p, i) => this.currentPlayers[i] = p)
if (game.index === MAX_PER_ROUND) {
this.waiting = false
this.result = `Winner of game ${game.id} is: ${game.winner}. `
if (this.lastPlayerAddress !== null) {
if (game.winner === this.lastPlayerAddress) {
this.result += 'You WIN!'
} else {
this.result += 'You LOOSE!'
}
}
}
}
}
});
</script>
</body>
</html>