-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
257 lines (246 loc) · 8.55 KB
/
index.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
Vue.component("v-warning", {
props: ["text"],
template: '<div class="wrong-box"><img src="./assets/images/tips.png" alt=""><span class="wrong-text">{{text}}</span></div>'
})
const app = new Vue({
el: "#root",
data: {
//type true single false group
lotteryType: false,
serverValue: "",
clientValue: "",
uidValue: "",
oddsValue: 0,
ethHashValue: "",
participantValue: 0,
winnerValue: 0,
myParticipantValue: "",
serverSeedHash: "",
generate: false,
stop: 10,
look: false,
isShowWarning: false
},
created: function () {
this.getUrlParams();
},
methods: {
groupVerify: async function () {
let {
serverValue,
ethHashValue,
participantValue,
winnerValue,
serverSeedHash,
myParticipantValue
} = this;
if (!serverValue ||
!ethHashValue ||
participantValue <= 0 ||
winnerValue <= 0 ||
myParticipantValue < 0 ||
myParticipantValue === ""
) {
this.isShowWarning = true;
return false
}
const renderGroupView = [
{
title: "> ServerSeed",
value: serverValue.split(""),
id: 0
},
{
title: "> ServerSeed Hash",
value: serverSeedHash ? serverSeedHash.split("") : "null".split(""),
id: 1
},
{
title: "> ClientSeed(ETH block hash)",
value: ethHashValue.split(""),
id: 2
},
{
title: "> Number of Participants",
value: ("" + participantValue).split(""),
id: 3
},
{
title: "> Number of Winners",
value: ("" + winnerValue).split(""),
id: 4
},
{
title: "> PID",
value: ("" + myParticipantValue).split(""),
id: 5
},
]
const arr = notInstantDrawWinner(ethHashValue, serverValue, 0, +participantValue, winnerValue);
console.log("notInstant", arr);
let isWin = arr.includes(+myParticipantValue);
this.renderView(renderGroupView, isWin);
return false;
},
singleVerify() {
let {
serverValue,
clientValue,
uidValue,
oddsValue,
serverSeedHash
} = this;
if (!serverValue ||
!clientValue ||
!uidValue ||
oddsValue <= 0 ||
oddsValue > 100
) {
this.isShowWarning = true;
return false
}
const renderSingleView = [
{
title: "> ServerSeed",
value: serverValue.split(""),
id: 0
},
{
title: "> ServerSeed Hash",
value: serverSeedHash ? serverSeedHash.split("") : "null".split(""),
id: 1
},
{
title: "> ClientSeed",
value: clientValue.split(""),
id: 2
},
{
title: "> PID",
value: uidValue.split(""),
id: 3
},
{
title: "> Chance of Winning",
value: (parseFloat(oddsValue) + "%").split(""),
id: 4
},
{
title: "> HMAC_SHA256(ClinetSeed:PID,ServerSeed)",
value: Encrypt(`${clientValue}:${uidValue}`, serverValue).split(""),
id: 5
}
]
const isWin = instantDrawWinner(oddsValue / 100, `${clientValue}:${uidValue}`, serverValue);
console.log("instant", isWin);
this.renderView(renderSingleView, isWin);
return false;
},
renderView: async function (data, isWin) {
if (this.look) return;
this.look = true
if (this.generate === false) {
this.generate = true;
}
const view = document.getElementById("view");
view.innerHTML = "";
const line1 = document.createElement("div");
const line2 = document.createElement("div");
const caculating = document.createElement("h2");
const success = document.createElement("h2");
const error = document.createElement("h2");
line1.className = "line";
line2.className = "line";
caculating.innerText = "> Caculating...";
success.innerHTML = ">>> CONGRATS! YOU’VE WON THE DRAW!<i>_</i> ";
error.innerHTML = ">>> <span class='red'>OOPS! YOU LOST THE DRAW</span><i>_</i>";
await this.generateResult(data);
view.appendChild(line1)
view.appendChild(caculating);
view.appendChild(line2);
isWin ?
view.appendChild(success)
:
view.appendChild(error);
this.look = false
},
generateResult: async function (all) {
if (!all.length) return;
const cur = all.splice(0, 1)[0];
const view = document.getElementById("view");
if (cur.title) {
const h2 = document.createElement("h2");
h2.innerText = cur.title;
view.appendChild(h2);
}
if (cur.value) {
const p = document.createElement("p");
p.id = cur.id;
view.appendChild(p);
await this.stopRender(cur.id, cur.value);
await this.generateResult(all);
}
},
stopRender: async function (id, arr) {
if (!Array.isArray(arr) && arr.length == 0) return;
const ele = document.getElementById(id);
let curText = "";
const render = async () => {
return new Promise((res, rej) => {
try {
curText += arr.splice(0, 1).join("");
ele.innerText = curText;
if (arr.length > 0) {
setTimeout(async () => {
if (arr.length === 1) {
res(true)
}
res(await render())
}, this.$data.stop);
}
else {
res(true)
}
}
catch (err) {
this.look = false;
rej(err);
}
})
}
return await render();
},
getUrlParams: function () {
let params = {
ss: "",
ssh: "",
cs: "",
uid: "",
wo: 0,
ebh: "",
pa: 0,
wa: 0,
ypa: ""
};
let search = window.location.search;
if (!search) return params;
const paramsArr = search.split("?")[1].split("&");
paramsArr.map(item => {
const [key, value] = item.split("=");
params[key] = value;
})
if (params.uid != "" || params.wo != "") {
this.lotteryType = true;
}
this.serverValue = params.ss;
this.clientValue = params.cs;
this.serverSeedHash = params.ssh;
this.uidValue = params.uid;
this.oddsValue = parseFloat(params.wo) ? parseFloat(params.wo) * 100 : 0;
this.ethHashValue = params.ebh;
this.participantValue = parseInt(params.pa) ? parseInt(params.pa) : 0;
this.winnerValue = parseInt(params.wa) ? parseInt(params.wa) : 0;
this.myParticipantValue = parseInt(params.ypa);
},
}
})