-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
489 lines (440 loc) · 16.4 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
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
<!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>Mine</title>
<style>
body {
margin: 10px;
}
#main-container {
font-size: 32px;
font-family: Ubuntu;
max-width: 1200px;
margin: auto;
}
.mining-timer {
top: 137px;
}
#table-container {
margin-top: 10px;
}
#table-data {
width: 100%;
}
.table {
width: 100%;
font-size: 14px;
border-collapse: collapse;
}
.table tr th, .table tr td {
border: 1px solid black;
padding: 5px;
text-align: center;
width: 50%;
}
.other-info {
font-size: 26px
}
.other-info .account-info span {
float: right;
}
.other-info .account-info:nth-child(4) {
margin-top: 20px;
}
.small-info {
text-align: right;
font-size: 14px;
}
.data-container {
max-width: 800px;
width: 100%;
display: flex;
justify-content: space-evenly;
margin: auto;
}
.data-container > div {
min-width: 200px;
}
#table-container {
flex-direction: column;
}
#table-container .nav-tab {
display: flex;
margin-bottom: 10px;
justify-content: flex-end;
gap: 10px;
}
#table-container .nav-tab .btn-tab {
flex-grow: 1;
}
.btn-full {
padding: 5px 10px;
cursor: pointer;
}
@media (max-width: 550px) {
.data-container {
display: block;
}
.main-information {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.main-information .mining-timer {
text-align: right;
}
}
</style>
</head>
<body>
<div id="main-container">
<div id="error">Something wrong</div>
<div id="mining-graph" style="display: none;">
<div id="graph-container" class="data-container">
<div class="main-information">
<div class="information">
<div id="planet"></div>
<div id="value">0</div>
</div>
<div class="information mining-timer">
<div>Mining</div>
<div id="cooldown"></div>
<audio src="alarm.wav" id="alarm"></audio>
</div>
</div>
<div class="other-info">
<div class="account-info">
CPU <span id="cpu-usage"></span>
</div>
<div class="account-info">
USED <span id="cpu-use"></span>
</div>
<div class="account-info">
MAX <span id="cpu-max"></span>
</div>
<div class="account-info">
TLM <span id="tlm-fetch">0 TLM</span>
</div>
<div class="account-info">
MINE <span id="mine-count">0</span>
</div>
</div>
</div>
<div id="table-container" class="data-container">
<div class="nav-tab">
<button target="#table-data" class="btn-tab btn-full">
Mine Table
</button>
<button target="#cpu-data" class="btn-tab btn-full">
CPU Table
</button>
</div>
<div id="table-data" class="table-tab">
<table class="table" id="template">
<thead>
<tr>
<th>Time</th>
<th>TLM</th>
</tr>
</thead>
<tbody>
<tr>
<td class="d-time"></td>
<td class="d-tlm"></td>
</tr>
</tbody>
</table>
</div>
<div id="cpu-data" class="table-tab" style="display: none;">
<table class="table" id="cpu-table">
<thead>
<tr>
<th>Time</th>
<th>CPU Max</th>
</tr>
</thead>
<tbody>
<tr>
<td class="d-time"></td>
<td class="d-cpu"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
const WAX_HOST = "https://wax.pink.gg";
const LOG_ENDPOINT = 'https://api.alienworlds.io/v1/alienworlds/mines'
function fetchLog(wallet, date, success) {
let prev = new Date(date);
prev.setDate(prev.getDate() - 1);
let m = String(prev.getMonth() + 1).padStart(2, 0);
let d = String(prev.getDate()).padStart(2, 0);
let strDate = prev.getFullYear() + `-${m}-${d}`;
$.ajax({
type: "GET",
url: LOG_ENDPOINT,
data: {
miner: wallet,
limit: 400,
sort: 'desc',
from: strDate + 'T17:00:00.000Z',
to: date + 'T16:59:59.999Z'
},
dataType: 'json',
crossDomain: true,
async: false,
success: success
})
}
function fetchAccount(wallet, success) {
$.ajax({
type: 'POST',
url: WAX_HOST + "/v1/chain/get_account",
data: JSON.stringify({"account_name":wallet}),
dataType: 'json',
headers: {'content-type': 'application/json'},
crossDomain: true,
async: false,
success: success
})
}
function fetchTableRow(wallet, scope, table, success) {
$.ajax({
type: "POST",
url: WAX_HOST + "/v1/chain/get_table_rows",
data: JSON.stringify({
"json":true,
"code":"m.federation",
"scope":scope,
"table":table,
"table_key":"",
"lower_bound":wallet,
"upper_bound":wallet,
"index_position":1,
"key_type":"",
"limit":10,
"reverse":false,
"show_payer":false
}),
dataType: 'json',
headers: {'content-type': 'application/json'},
crossDomain: true,
async: false,
success: success
});
}
function fetchAsset(id, success) {
$.ajax({
url: "https://wax.api.atomicassets.io/atomicassets/v1/assets/" + id,
dataType: 'json',
type: "GET",
success: success,
crossDomain: true,
async: false,
})
}
function getWorld(world, success) {
return fetchTableRow("", world + '.world', 'state3', success);
}
function playAlarm() {
let player = document.getElementById("alarm");
player.pause();
player.currentTime = 0;
player.play();
}
const QUERY = new URLSearchParams(window.location.search);
const USERNAME = QUERY.get('user');
const LIMIT_ALARM = 2;
const MAX_COUNTER = 35;
const ALL_CPU = [];
var CURRENT_PLANET = "";
var WARN_COUNTER = 0;
var LAND_DELAY = 0;
var ITEM_DELAY = 0;
var LAST_MINE = null;
var UPDATE_TABLE = true;
var UPDATE_GRAPH = false;
var CPU_STAKED_TH = 0;
const strBodyTable = $("#template tbody tr").prop('outerHTML');
$("#template tbody").empty();
const strBodyCpuTable = $("#cpu-table tbody tr").prop('outerHTML');
$("#cpu-table tbody").empty();
function refreshCPUTable() {
$("#cpu-table tbody").empty();
let MIN_TH = CPU_STAKED_TH / 2;
for (let cpu of ALL_CPU) {
let bd = $(strBodyCpuTable);
let strMineTime = cpu.time.toLocaleTimeString();
bd.find(".d-cpu").html(cpu.cpu.toLocaleString());
bd.find(".d-time").html(strMineTime);
let color_indicator = 512;
if (cpu.cpu <= MIN_TH) {
color_indicator = 0;
} else if (cpu.cpu >= CPU_STAKED_TH) {
color_indicator = 512;
} else {
let gap_cpu = cpu.cpu - MIN_TH;
color_indicator = Math.round(gap_cpu / MIN_TH * 512);
}
let color_decider = color_indicator - 256;
let color_red = 255;
let color_green = 255;
if (color_decider > 0) {
color_red -= color_decider;
} else if (color_decider < 0) {
color_green += color_decider;
}
bd.find(".d-cpu").css({'backgroundColor': `rgb(${color_red}, ${color_green}, 0)`})
$("#cpu-table tbody").append(bd);
}
}
function updateAccount() {
fetchAccount(USERNAME, r => {
let _cpu = r.cpu_limit;
let _net = r.net_limit;
$("#cpu-usage").html(Math.ceil(_cpu.used / _cpu.max * 100) + " %");
$("#cpu-use").html(_cpu.used.toLocaleString());
$("#cpu-max").html(_cpu.max.toLocaleString());
CPU_STAKED_TH = parseInt(r.cpu_weight) / 1000000;
ALL_CPU.unshift({
time: new Date(),
cpu: _cpu.max
});
if (ALL_CPU.length > 100) {
ALL_CPU.pop();
}
setTimeout(updateAccount, 1500);
});
}
function updateGain() {
UPDATE_TABLE = false;
let now = new Date();
let strDate = now.getFullYear();
strDate += "-" + String(now.getMonth() + 1).padStart(2, 0);
strDate += "-" + String(now.getDate()).padStart(2, 0);
fetchLog(USERNAME, strDate, r => {
$("#template tbody").empty();
$("#mine-count").html(r.results.length);
let totalMine = 0;
for (let mining of r.results) {
let bd = $(strBodyTable);
let mine_time = new Date(mining.block_timestamp);
let strMineTime = mine_time.toLocaleTimeString();
bd.find(".d-tlm").html(String(mining.bounty / 10000).padEnd(6, 0) + " TLM");
bd.find(".d-time").html(strMineTime);
$("#template tbody").append(bd);
totalMine += mining.bounty;
}
if (r.results.length == 0) {
$("#template tbody").html('<tr><td colspan=2 style="text-align:center">No Data</td></tr>');
}
$("#tlm-fetch").html(String(totalMine / 10000));
});
}
function runTlmPot() {
if (!UPDATE_GRAPH) {
setTimeout(runTlmPot, 1000);
return true;
}
getWorld(CURRENT_PLANET.toLowerCase(), function(r) {
let data = r.rows[0];
$("#value").html(data.mine_bucket);
setTimeout(runTlmPot, 500);
});
}
function setGlobalWorld(r) {
let landName = r.data.name;
let [land, planet] = landName.split(' on ');
LAND_DELAY = r.data.data.delay / 10;
CURRENT_PLANET = planet;
$("#planet").html(planet);
runTlmPot();
}
function runTimer() {
if (LAST_MINE === null || ITEM_DELAY === 0 || LAND_DELAY === 0) {
setTimeout(runTimer, 500);
return false;
}
let currentTime = new Date();
let offsetDelay = ITEM_DELAY * LAND_DELAY * 1000;
let dtLastMine = new Date(LAST_MINE + "+0000");
dtLastMine.setTime(dtLastMine.getTime() + offsetDelay);
let cooldown = Math.floor((dtLastMine.getTime() - currentTime.getTime()) / 1000);
if (cooldown <= 0) {
document.getElementById('cooldown').innerText = '00:00:00';
if (WARN_COUNTER < LIMIT_ALARM) playAlarm();
UPDATE_GRAPH = true;
UPDATE_TABLE = true;
WARN_COUNTER++;
fetchTableRow(USERNAME, 'm.federation', 'miners', function(r) {
let data = r.rows[0];
LAST_MINE = data.last_mine;
setTimeout(runTimer, 3000);
});
} else {
WARN_COUNTER = 0;
UPDATE_GRAPH = false;
if (UPDATE_TABLE) updateGain();
let hour_left = String(Math.floor(cooldown / 3600)).padStart(2, '0');
let min_left = String(Math.floor((cooldown % 3600) / 60)).padStart(2, '0');
let sec_left = String(Math.floor(cooldown % 60)).padStart(2, '0');
let string_left = `${hour_left}:${min_left}:${sec_left}`;
document.getElementById('cooldown').innerText = string_left;
setTimeout(runTimer, 500);
}
}
$(".btn-tab").on('click', function() {
let target = $(this).attr('target');
$(".table-tab").hide();
$(target).show();
if (target === "#cpu-data") refreshCPUTable();
});
(() => {
if (USERNAME === null) return false;
document.getElementById("error").style = 'display: none;'
document.getElementById("mining-graph").style = 'display: inherit;'
fetchTableRow(USERNAME, 'm.federation', 'miners', function(r) {
let data = r.rows[0];
let landID = data.current_land;
fetchAsset(landID, setGlobalWorld);
LAST_MINE = data.last_mine;
});
fetchTableRow(USERNAME, 'm.federation', 'bags', function(r) {
let data = r.rows[0];
let promises = [];
for (let itemId of data.items) {
promises.push(new Promise((resolve) => {
fetchAsset(itemId, resolve);
}))
}
Promise.all(promises).then((assets) => {
let delays = [];
for (let asset of assets) {
delays.push(asset.data.data.delay);
}
if (delays.length == 0) {
ITEM_DELAY = 0;
} else if (delays.length == 1) {
ITEM_DELAY = delays[0];
} else {
delays.sort((a, b) => a - b).reverse();
halving = (delays.length == 2) ? 0.5 : 1;
if (delays.length > 2) delays = delays.slice(0, 2);
ITEM_DELAY = delays.reduce((a, b) => a + (b * halving));
}
});
});
runTimer();
updateGain();
updateAccount();
})()
</script>
</body>
</html>