-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
189 lines (141 loc) · 5.49 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
const resultsDiv = document.querySelector('#results');
const loadingDiv = document.querySelector('#loading');
const statisticsDiv = document.querySelector('#statistics');
const seedInput = document.querySelector('#seed_input');
const updateButton = document.querySelector('#update_button');
const crashesCount = document.querySelector('#crashes_count');
const clientSeed = "0000000000000000000415ebb64b0d51ccee0bb55826e43846e5bea777d91966";
const amountInput = document.querySelector('#amount_input');
const goodValueInput = document.querySelector('#good_value_input');
let storageAmount = localStorage.getItem('amount');
amountInput.value = storageAmount ? storageAmount : 100;
let storageGoodValue = localStorage.getItem('goodValue');
goodValueInput.value = storageGoodValue ? storageGoodValue : 2;
let timeout = null;
seedInput.addEventListener('keyup', ev => {
if (ev.key == 'Enter') {
ev.preventDefault();
OnInputChange();
}
});
$(resultsDiv).selectable({
stop: UpdateSelectionWindow
});
seedInput.addEventListener('input', (ev) => { OnInputChange() });
amountInput.addEventListener('input', (ev) => { OnInputChange() });
goodValueInput.addEventListener('input', (ev) => { OnInputChange() });
updateButton.addEventListener('click', (ev) => { OnInputChange(true) } );
function OnInputChange(byButton = false) {
if (!seedInput.value) {
loadingDiv.innerHTML = '';
return;
}
let seed = seedInput.value;
let amount = parseInt(amountInput.value);
if (!amount && amount !== 0) amount = 100;
HandleUpdateButtonVisibility(amount);
if (amount >= 5000 && !byButton) return;
let goodValue = parseFloat(goodValueInput.value);
if (!goodValue && goodValue !== 0) goodValue = 2;
if (amount < 800) {
GetChain(seed, amount, goodValue);
} else {
clearTimeout(timeout);
loadingDiv.innerHTML = 'Loading...';
timeout = setTimeout(() => {
GetChain(seed, amount, goodValue);
loadingDiv.innerHTML = '';
}, 500);
}
UpdateSelectionWindow();
localStorage.setItem('amount', amount);
localStorage.setItem('goodValue', goodValue);
}
function GetChain(seed, amount = 1000, goodValue = 2) {
resultsDiv.innerHTML = '';
let chain = [seed];
amount -= 1;
if (amount < 0) chain = [];
for (let i = 0; i < amount; i++) {
chain.push(
CryptoJS.algo.SHA256.create()
.update(chain[chain.length - 1])
.finalize().toString(CryptoJS.enc.Hex)
)
}
chain = chain.map((seed, index) => {
const hash = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, seed)
.update(clientSeed)
.finalize().toString(CryptoJS.enc.Hex)
const divisible = (hash, mod) => {
let val = 0;
let o = hash.length % 4;
for (let i = o > 0 ? o - 4 : 0; i < hash.length; i += 4) {
val =
((val << 16) + parseInt(hash.substring(i, i + 4), 16)) %
mod;
}
return val === 0;
};
const getPoint = (hash) => {
// In 1 of 15 games the game crashes instantly.
if (divisible(hash, 15)) return 0;
// Use the most significant 52-bit from the hash to calculate the crash point
let h = parseInt(hash.slice(0, 52 / 4), 16);
let e = Math.pow(2, 52);
const point = (
Math.floor((100 * e - h) / (e - h)) / 100
).toFixed(2);
return point;
}
const point = getPoint(hash);
return parseFloat(point);
});
let goodCount = 0;
let totalCount = chain.length;
for (let value of chain) {
let multiplier = value == 0 ? 1.0 : value;
const isGood = multiplier >= goodValue;
if (isGood) goodCount++;
const div = document.createElement('div');
div.textContent = multiplier.toFixed(2) + 'X';
div.className = `crash ${isGood ? 'bom' : 'ruim'}`;
resultsDiv.appendChild(div);
}
UpdateStatistics(totalCount, goodCount);
}
function UpdateStatistics(totalCount = 0, goodCount = 0) {
if (!totalCount) {
statisticsDiv.classList.add('hide');
return;
}
statisticsDiv.classList.remove('hide');
const lossCount = totalCount - goodCount;
const goodPercentage = ( (goodCount / totalCount) * 100 ).toFixed(2);
const lossPercentage = ( (lossCount / totalCount) * 100 ).toFixed(2);
statisticsDiv.innerHTML = `<span class="good">${goodCount}</span>/${totalCount} Wins (<span class="good">${goodPercentage}%</span> Chance) • <span class="bad">${lossCount}</span>/${totalCount} Losses (<span class="bad">${lossPercentage}%</span> Chance)`;
}
function UpdateSelectionWindow() {
let selectedElements = document.querySelectorAll('.ui-selected');
// console.log(selectedElements);
if (selectedElements.length == 0) {
crashesCount.classList.add('hide');
return;
} else {
crashesCount.classList.remove('hide');
}
let goodElements = document.querySelectorAll('.ui-selected.bom');
let count = selectedElements.length;
let goodCount = goodElements.length;
crashesCount.innerHTML = `
<div>${count} selected</div>
<div><span class="good">${goodCount}</span>/${count}</div>
`
}
function HandleUpdateButtonVisibility(amount) {
if (amount >= 5000) {
updateButton.classList.remove('hide');
} else {
updateButton.classList.add('hide');
}
}