-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathbubble-shooter.html
More file actions
493 lines (428 loc) · 13.7 KB
/
bubble-shooter.html
File metadata and controls
493 lines (428 loc) · 13.7 KB
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
<!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=1.0" />
<title>Bubble Shooter verify</title>
<link rel="stylesheet" href="./lib/main.css" />
<link rel="stylesheet" href="./lib/bootstrap/css/bootstrap.min.css" />
<script src="./lib/vue.min.js"></script>
<script src="./lib/crypto-js.js"></script>
<script src="./lib/tools.js"></script>
<script src="./lib/utils.js"></script>
<style>
body {
font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}
.main {
max-width: 760px;
margin: 24px auto;
}
.muted {
color: #6c757d;
}
</style>
</head>
<body>
<div id="app" class="main">
<h1 class="text-center pb-3">Bubble Shooter verify</h1>
<hr />
<!-- Inputs -->
<form class="py-3">
<h2 class="text-center">Input</h2>
<div class="form-group">
<label>Server Seed</label>
<input class="form-control" placeholder="Server Seed" :value="server_seed"
@input="server_seed = $event.target.value" />
</div>
<div class="form-group">
<label>Client Seed</label>
<input class="form-control" placeholder="Client Seed" :value="client_seed"
@input="client_seed = $event.target.value" />
</div>
<div class="form-group">
<label>Nonce</label>
<input class="form-control" placeholder="Nonce" :value="nonce"
@input="nonce = normalizeInt($event.target.value, 0)" />
</div>
<div class="form-group">
<label>ColorId: {{ computedColorId }}</label>
<input class="form-control" placeholder="ColorId" :value="colorId"
@input="colorId = normalizeInt($event.target.value, 0)" />
</div>
</form>
<hr />
<!-- Outputs -->
<form class="py-3">
<h2 class="text-center">Output</h2>
<div class="form-group">
<label>Game result</label>
<input class="form-control" readonly :value="returnGameResult" />
</div>
<div class="form-group">
<label>Payout</label>
<input class="form-control" readonly placeholder="Payout" :value="returnPayout"
@input="returnPayout = normalizeInt($event.target.value, 0)" />
</div>
<div class="form-group" v-if="isRainbowColorId">
<label>Random number: {{ computedColorId }}</label>
<input class="form-control" readonly placeholder="Random number" :value="returnRandomNumber"
@input="returnRandomNumber = normalizeInt($event.target.value, 0)" />
</div>
</form>
<hr />
<!-- Explanation -->
<section class="py-3">
<h2 class="text-center">Payout Table</h2>
<div class="mt-2">
<div v-for="(data, key) in tableData" :key="key" class="table-container w-full mb-4 px-2 py-2">
<h3 class="table-title text-center">{{ key }}</h3>
<table class="w-full " style="width: 100%;" border="1">
<thead class="w-full" style="width: 100%;">
<tr class="w-full">
<th class=" w-24 text-center">Odds</th>
<th class=" w-24 text-center">Range</th>
</tr>
</thead>
<tbody class="w-full" style="width: 100%;">
<tr v-for="(item, index) in data" :key="index"
:style="key === computedColorId && item.payout === randomPayout ? 'background-color: rgb(26, 169, 100);' : ''">
<td class="text-center w-24 px-12 ">{{ item.payout / 100 }}x</td>
<td class="text-center w-24 px-12">
<span v-if="index === 0">≤ {{ item.max }}</span>
<span v-else>{{ data[index-1].max + 1 }} - {{ item.max }}</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>
<script>
const qs = tools.queryString();
new Vue({
el: '#app',
data: {
server_seed: qs.s || '',
client_seed: qs.c || '',
nonce: Number.isFinite(parseInt(qs.n)) ? parseInt(qs.n) : 0,
colorId: Number.isFinite(parseInt(qs.cd)) ? parseInt(qs.cd) : 0,
difficultly: Number.isFinite(parseInt(qs.dy)) ? parseInt(qs.dy) : 0,
gameResult: Number.isFinite(parseInt(qs.gr)) ? parseInt(qs.gr) : 0,
randomNumber: Number.isFinite(parseInt(qs.rn)) ? parseInt(qs.rn) : 0,
payout: Number.isFinite(parseInt(qs.py)) ? parseInt(qs.py) : 0,
getrtp: 0.98,
DEFAULT_RTP: 99,
MULTI_RTP: 100,
tableData: {
"RAINBOW_GREEN": [
{ "max": 20, "payout": 120 },
{ "max": 30, "payout": 130 },
{ "max": 40, "payout": 150 },
{ "max": 94, "payout": 200 },
{ "max": 98, "payout": 500 },
{ "max": 100, "payout": 1000 }
],
"RAINBOW_YELLOW": [
{ "max": 20, "payout": 200 },
{ "max": 30, "payout": 200 },
{ "max": 40, "payout": 300 },
{ "max": 94, "payout": 500 },
{ "max": 98, "payout": 1000 },
{ "max": 100, "payout": 5000 }
],
"RAINBOW_BLUE": [
{ "max": 20, "payout": 200 },
{ "max": 30, "payout": 200 },
{ "max": 40, "payout": 300 },
{ "max": 94, "payout": 500 },
{ "max": 98, "payout": 1000 },
{ "max": 100, "payout": 5000 }
],
"JACKPOT": [
{ "max": 10, "payout": 300 },
{ "max": 30, "payout": 500 },
{ "max": 40, "payout": 700 },
{ "max": 94, "payout": 1000 },
{ "max": 98, "payout": 1500 },
{ "max": 100, "payout": 10000 }
]
}
},
computed: {
hmacHex() {
if (!this.server_seed || !this.client_seed) return '';
return CryptoJS.HmacSHA256(`${this.client_seed}:${this.nonce}`, this.server_seed).toString();
},
returnGameResult() {
const rtp = this.MULTI_RTP * this.getrtp;
const value = calcByRtp(this.server_seed, this.client_seed, this.nonce, Math.min(
rtp,
this.DEFAULT_RTP,
));
return value;
},
returnRandomNumber() {
if (!this.isRainbowColorId) {
return 0
}
const value = getRandomNumber(this.server_seed, this.client_seed, this.nonce, 0);
return value;
},
returnPayout() {
const value = getOdds(this.payout, this.returnGameResult, this.randomPayout);
return Math.floor(value * 100) / 10000 + 'x';
},
computedColorId() {
return this.getColorId(this.colorId);
},
isRainbowColorId() {
return [5, 6, 7, 8].includes(Number(this.colorId));
},
colorTableList() {
return this.tableData?.[this.computedColorId] ?? [];
},
randomPayout() {
const random = this.isRainbowColorId ? this.colorTableList.find(item => item.max >= this.returnRandomNumber)?.payout : this.payout;
return random;
},
},
methods: {
normalizeInt(v, def) {
const n = parseInt(v, 10);
return Number.isFinite(n) ? n : def;
},
getColorId(colorId) {
const ColorId = {
0: 'PLACEHOLDER',
1: 'GREEN',
2: 'YELLOW',
3: 'BLUE',
4: 'PURPLE',
5: 'RAINBOW_GREEN',
6: 'RAINBOW_YELLOW',
7: 'RAINBOW_BLUE',
8: 'JACKPOT',
}
return ColorId[colorId];
}
}
});
/**
* 将 hex 字符串转换为字节数组(用于 salt 分支)
*/
function hexToBytes(hex) {
const bytes = [];
for (let i = 0; i < hex.length; i += 2) {
bytes.push(parseInt(hex.substr(i, 2), 16));
}
return bytes;
}
/**
* 等价于 Java: calcByRtp(String serverSeed, String clientSeed, long nonce, Integer rtp)
*/
function calcByRtp(serverSeed, clientSeed, nonce, rtp) {
const key = `${clientSeed}:${nonce}`;
const hash = CryptoJS.HmacSHA256(key, serverSeed).toString();
return pointFromHashAndRtp(hash, null, rtp);
}
/**
* 等价于 Java: pointFromHashAndRtp(String seed, String salt, Integer rtp)
* 使用 BigInt 实现
*/
function pointFromHashAndRtp(seed, salt, rtp) {
const nBits = 52;
// 如果提供了 salt,进行二次 HMAC
if (salt != null && salt !== '') {
const seedBytes = hexToBytes(seed);
seed = CryptoJS.HmacSHA256(seedBytes, salt).toString();
}
// 取前 52 bit = 13 个十六进制字符 (52 / 4 = 13)
const hexPart = seed.substring(0, nBits / 4);
// 使用 BigInt 解析十六进制
const r = BigInt('0x' + hexPart);
// 2^52 作为 BigInt
const twoPow52 = 1n << 52n; // 或者 BigInt(2**52)
// x = 1 - (r / 2^52)
// 为了避免浮点数,我们保持整数运算:
// x = (twoPow52 - r) / twoPow52
// 计算 (rtp % 100) / x = (rtp % 100) * twoPow52 / (twoPow52 - r)
const rtpMod100 = BigInt(rtp % 100);
// 计算分子和分母
const numerator = rtpMod100 * twoPow52;
const denominator = twoPow52 - r;
// 执行整数除法(相当于 Math.floor)
let x = numerator / denominator;
// 转换为 Number(因为结果不会超过安全整数范围)
const result = Number(x);
// Java: Math.max(100L, x.longValue())
return Math.max(100, result);
}
// 辅助函数:十六进制字符串转字节数组(保持原样)
function hexToBytes(hex) {
const bytes = [];
for (let i = 0; i < hex.length; i += 2) {
bytes.push(parseInt(hex.substr(i, 2), 16));
}
return bytes;
}
function getOdds(payout, gameValue, randomPayOut) {
// payout 取 random 还是固定的
if (randomPayOut === 0) {
// 非彩色球
if (BigInt(payout) > gameValue) {
return "0.0";
}
// 使用 BigInt 进行高精度除法
return divideWithPrecision(BigInt(payout), 100n, 1);
} else {
// 彩色球
if (BigInt(payout) > gameValue) {
return "0.0";
}
// 使用 BigInt 进行高精度除法
return divideWithPrecision(BigInt(randomPayOut), 100n, 1);
}
}
/**
* 高精度除法函数,模拟 Java 的 BigDecimal.divide
* @param {bigint} dividend - 被除数
* @param {bigint} divisor - 除数
* @param {number} scale - 小数位数
* @returns {string} 保留指定小数位数的字符串结果
*/
function divideWithPrecision(dividend, divisor, scale) {
// 乘以 10^scale 来保留小数
const multiplier = 10n ** BigInt(scale);
const result = (dividend * multiplier * 100n) / divisor; // 乘以100对应原逻辑
// 转换为字符串并插入小数点
const resultStr = result.toString();
if (resultStr.length <= scale) {
// 如果结果长度小于等于小数位数,前面补0
return '0.' + resultStr.padStart(scale, '0');
} else {
// 插入小数点
const integerPart = resultStr.slice(0, -scale);
const decimalPart = resultStr.slice(-scale);
return integerPart + '.' + decimalPart;
}
}
// ---- 统一读取区间并返回赔率 ----
function getPayoutByRandom(colorId, randomNumber) {
const config = PAYOUT_TABLE.get(colorId);
if (!config) return 0;
// 找到第一个大于等于 randomNumber 的键
let resultPayout = 0;
for (const [maxValue, payout] of config) {
if (randomNumber <= maxValue) {
resultPayout = payout;
break;
}
}
return resultPayout;
}
// 如果需要更高效的查找(假设配置已排序)
function getPayoutByRandomOptimized(colorId, randomNumber) {
// 常量定义
const RAINBOW_GREEN = 5;
const RAINBOW_YELLOW = 6;
const RAINBOW_BLUE = 7;
const JACKPOT = 8;
// 颜色赔率区间配置
// key = colorId
// value = Map<最大随机值, payout>
const PAYOUT_TABLE = new Map();
// ---- 静态加载所有区间定义 ----
// RAINBOW_GREEN → 期望 2x
const rainbowGreen = new Map([
[20, 120],
[30, 130],
[40, 150],
[94, 200],
[98, 500],
[100, 1000]
]);
PAYOUT_TABLE.set(RAINBOW_GREEN, rainbowGreen);
// RAINBOW_YELLOW & RAINBOW_BLUE → 期望 5x
const rainbowYellowBlue = new Map([
[20, 200],
[30, 200],
[40, 300],
[94, 500],
[98, 1000],
[100, 5000]
]);
PAYOUT_TABLE.set(RAINBOW_YELLOW, rainbowYellowBlue);
PAYOUT_TABLE.set(RAINBOW_BLUE, rainbowYellowBlue);
// JACKPOT → 期望 10x
const jackpot = new Map([
[10, 300],
[30, 500],
[40, 700],
[94, 1000],
[98, 1500],
[100, 10000]
]);
PAYOUT_TABLE.set(JACKPOT, jackpot);
const config = PAYOUT_TABLE.get(colorId);
if (!config) return 0;
// 将 Map 转换为数组并排序
const entries = Array.from(config.entries()).sort((a, b) => a[0] - b[0]);
for (const [maxValue, payout] of entries) {
if (randomNumber <= maxValue) {
return payout;
}
}
return 0;
}
/**
* 生成随机数(1-100)
* @param {string} serverSeed - 服务器种子
* @param {string} clientSeed - 客户端种子
* @param {bigint} nonce - 随机数
* @param {bigint} round - 轮次
* @returns {number} 1-100之间的随机整数
*/
function getRandomNumber(serverSeed, clientSeed, nonce, round) {
const key = `${clientSeed}:${nonce}:${round}`;
const hash = CryptoJS.HmacSHA256(key, serverSeed).toString(); // 假设已有 hmacSHA256 实现
// random [1,100]
const randomNumber = slideWindowNumber(hash, 100) + 1;
return randomNumber;
}
/**
* 从指定索引计算数字
* @param {string} hash - 哈希字符串
* @param {number} beginIndex - 起始索引
* @param {number} events - 事件数量
* @returns {number} 计算结果
*/
function calNumberFromIndex(hash, beginIndex, events) {
// 使用 BigInt 处理大整数,避免精度丢失
const hexStr = hash.substring(beginIndex, beginIndex + 14);
const numBigInt = BigInt('0x' + hexStr) >> 3n;
// 转换为浮点数进行计算
const num = Number(numBigInt);
const MAX_BORDER = 1 / Math.pow(2, 53);
const result = num * MAX_BORDER;
return Math.floor(result * events);
}
/**
* 滑动窗口算法
* @param {string} hash - 哈希字符串
* @param {number} events - 事件数量
* @returns {number} 随机数
*/
function slideWindowNumber(hash, events) {
// 使用 BigInt 解析前15个十六进制字符
const hexStr = hash.substring(0, 15);
let indexBigInt = BigInt('0x' + hexStr);
// 使用质数让影响结果的字符更分散
indexBigInt = indexBigInt % 47n;
return calNumberFromIndex(hash, Number(indexBigInt), events);
}
</script>
</body>
</html>