forked from nyaoouo/GBFR-ACT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact_ws.html
442 lines (415 loc) · 16.9 KB
/
act_ws.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remote</title>
<script>
function cdnError(el) {
alert(`${el.src || el.href} is not loaded, check your network or try to refresh the page`)
}
</script>
<link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css" onerror="cdnError(this)"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/css/bootstrap.min.css" onerror="cdnError(this)"/>
<script src="https://cdn.jsdelivr.net/npm/chart.js" onerror="cdnError(this)"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/js/bootstrap.bundle.min.js" onerror="cdnError(this)"></script>
<script src="https://unpkg.com/vue@3/dist/vue.global.js" onerror="cdnError(this)"></script>
<script src="https://unpkg.com/element-plus" onerror="cdnError(this)"></script>
<script src="https://unpkg.com/@element-plus/icons-vue" onerror="cdnError(this)"></script>
</head>
<style>
body {
font-family: -apple-system,
'Nimbus Roman No9 L',
'PingFang SC Regular',
'Hiragino Sans GB',
'Microsoft Yahei',
'WenQuanYi Micro Hei',
'ST Heiti', sans-serif
}
::-webkit-scrollbar {
width: 5px;
height: 5px;
background-color: #F5F5F5;
}
::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgb(93, 93, 93);
border-radius: 10px;
background-color: #F5F5F5;
}
::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(166, 166, 166, 0.3);
background-color: rgb(234, 234, 234);
}
.main-0 {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.main-1 {
display: table-cell;
vertical-align: middle;
}
.main-2 {
width: 1000px;
max-width: 100%;
margin-left: auto;
margin-right: auto;
}
</style>
<body>
<div id="app" class="main-0">
<div class="main-1">
<div class="main-2 shadow rounded p-3">
<div v-if="!is_socket_connected" class="p-3">
<el-alert title="socket is not connect, check inject status" type="error" effect="dark" :closable="false"></el-alert>
</div>
<el-tabs
v-model="view_record_value"
type="card"
closable @tab-remove="remove_record"
>
<el-tab-pane
v-for="item in records" :key="item.name" :label="item.name" :name="item.name"
>
<el-table :data="Object.values(item.actors)" style="width: 100%" :default-sort="{ prop: 'dps', order: 'descending' }">
<el-table-column prop="name" label="name">
<template #default="scope">
<span>
<el-icon :style="{'color':scope.row.color}"><Avatar/></el-icon> {{ scope.row.name }}
</span>
</template>
</el-table-column>
<el-table-column prop="damage" label="damage" sortable></el-table-column>
<el-table-column prop="damage_in_minute" label="damage_in_minute" sortable></el-table-column>
<el-table-column prop="dps" label="dps" sortable></el-table-column>
<el-table-column prop="dps_in_minute" label="dps_in_minute" sortable></el-table-column>
<el-table-column type="expand">
<template #default="props">
{{props.row.targets}}
</template>
</el-table-column>
</el-table>
</el-table>
<div v-if="item.show_chart">
<canvas :ref="el => { item.canvas = el }" height="500"></canvas>
</div>
</el-tab-pane>
</el-tabs>
</div>
</div>
</div>
</body>
<script>
const _with_time = f => (...args) => {
const now = new Date();
f(`[${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}]`, ...args);
}
let log_evt = false
let log_render = false
const log = _with_time(console.log);
const err = _with_time(console.error);
const flag_offs = (val) => Array.from(Array(val.toString(2).length).keys())
.filter(off => val & (1 << off));
const actor_names = {
0x9498420d: "姬塔",
0x26a4848a: "古兰",
0xc3155079: "赛达",
0x34d4fd8f: "卡塔莉娜",
0xf8d73d33: "拉卡姆",
0x7b5934ad: "伊欧",
0x443d46bb: "欧根",
0xa9d6569e: "萝赛塔",
0x2b4aa114: "夏洛特",
0xbcc238de: "冈达葛萨",
0xfba6615d: "菲莉",
0x601aa977: "娜露梅",
0x63a7c3f0: "兰斯洛特",
0xf96a90c2: "巴恩",
0x28ac1108: "珀西瓦尔",
0x94e2514e: "齐格飞",
0x6fdd6932: "卡莉奥丝特罗",
0xc97f3365: "尤达哈拉",
0xd16cfbde: "巴萨拉卡",
0x8056abcd: "伊德",
0xf5755c0e: "伊德(龙人化)",
}
var socket = null;
function numberToHexStringWithZFill(number, minLength) {
let hexString = number.toString(16);
while (hexString.length < minLength) {
hexString = '0' + hexString;
}
return hexString;
}
const colors = [
'#55ff55',
'#ff5555',
'#5555ff',
'#ffff55',
'#ff55ff',
'#55ffff',
]
class DpsChart {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.datas = {
datasets: []
};
this.chart = new Chart(this.ctx, {
type: 'line',
data: this.datas,
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
type: 'linear',
position: 'bottom',
},
y: {
//beginAtZero: true,
position: 'left',
}
},
animation: {
y: {
duration: 0
}
},
elements: {
point: {
radius: 0
}
}
}
});
this.period = 5 * 60;
}
set_key_color(key, color) {
const dataset = this.datas.datasets.find(ds => ds.label === key);
if (dataset) {
dataset.borderColor = color;
dataset.backgroundColor = color;
} else {
this.datas.datasets.push({
label: key,
data: [],
borderColor: color,
backgroundColor: color,
fill: false,
});
}
}
append_frame(timestamp, datas) {
if (this.period > 0) {
const min_time = timestamp - this.period;
for (const dataset of this.datas.datasets) {
const cutoff_idx = dataset.data.findIndex(d => d.x > min_time);
if (cutoff_idx >= 0) dataset.data = dataset.data.slice(cutoff_idx);
}
}
Object.entries(datas).forEach(([key, value]) => {
let dataset = this.datas.datasets.find(ds => ds.label === key);
if (!dataset) {
dataset = {
label: key,
data: [],
borderColor: '#000000',
backgroundColor: '#000000',
fill: false,
}
this.datas.datasets.push(dataset);
}
dataset.data.push({x: timestamp, y: value});
});
if (log_render) log('log_render', this.datas);
this.chart.update();
}
}
const dpsCharts = {};
const App = Vue.createApp({
setup() {
const is_socket_connected = Vue.ref(false);
const drawer = Vue.ref(false);
const records = Vue.ref([]);
const view_record_value = Vue.ref("");
var want_new = false;
var update_period = 0;
const remove_record = (name) => {
console.log('remove_record', name);
records.value = records.value.filter((item) => item.name !== name);
if (view_record_value.value === name) {
if (records.value.length === 0) new_record();
view_record_value.value = records.value[records.value.length - 1].name;
}
if (name in dpsCharts) {
dpsCharts[name].chart.destroy();
delete dpsCharts[name];
}
}
const fmt_time = (ms) => {
if (ms < 1000) return ms + "ms";
const sec = Math.floor(ms / 1000);
if (sec < 60) return sec + "s";
const min = Math.floor(sec / 60);
return min + "m";
}
const new_record = () => {
const now = new Date();
records.value.push({
name: `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`,
time: now.getTime(),
last_record_at: 0,
actors: {},
events_in_minute: [],
canvas: null,
show_chart: false,
});
view_record_value.value = records.value[records.value.length - 1].name;
want_new = false;
}
const get_latest_record = () => {
if (records.value.length === 0) new_record();
return records.value[records.value.length - 1];
}
const get_actor = (source_idx, source_id, party_idx) => {
const current_record = get_latest_record();
if (!current_record.actors[source_idx]) {
current_record.actors[source_idx] = {
name: `[${party_idx}]${actor_names[source_id] || numberToHexStringWithZFill(source_id, 8)}#${source_idx}`,
color: colors[party_idx], // assume no more than 6 actors
damage: 0,
damage_in_minute: 0,
dps: 0,
dps_in_minute: 0,
targets: {},
};
}
return current_record.actors[source_idx];
}
const on_damage = (evt) => {
const {source, target, damage, flags} = evt;
const [source_type, source_idx, source_id, source_party_idx, ..._] = source;
const [target_type, target_idx, target_id, target_party_idx, ...__] = target;
// if (target_type === "BehaviorAppBase") return;
// if (source_type !== "BehaviorPlayerBase") return;
const now = new Date().getTime()
const current_record = get_latest_record();
current_record.last_record_at = now;
if (source_party_idx === -1) return;
const actor = get_actor(source_idx, source_id, source_party_idx);
current_record.events_in_minute.push({
time: now,
type: "damage",
data: evt
});
actor.damage += damage;
actor.damage_in_minute += damage;
const target_key = `${numberToHexStringWithZFill(target_id, 8)}[${target_idx}]`;
if (!(target_key in actor.targets)) actor.targets[target_key] = 0;
actor.targets[target_key] += damage;
}
const process_old_records = (current_record) => {
const min_time = new Date().getTime() - 60000;
while (current_record.events_in_minute.length > 0) {
const first_evt = current_record.events_in_minute[0];
if (first_evt.time > min_time) break;
current_record.events_in_minute.shift();
switch (first_evt.type) {
case "damage":
const {source, target, damage, flags} = first_evt.data;
const [source_type, source_idx, source_id, ..._] = source;
const actor = get_actor(source_idx, source_id);
actor.damage_in_minute -= damage;
break;
}
}
}
let last_chart_evt = 0
const calc_damages = (current_record) => {
const full_time = (current_record.last_record_at - current_record.time) / 1000;
const real_time = (new Date().getTime() - current_record.time) / 1000
const full_time_in_minute = Math.min(60, real_time);
for (const actor of Object.values(current_record.actors)) {
actor.dps = Math.floor(actor.damage / full_time);
actor.dps_in_minute = Math.floor(actor.damage_in_minute / full_time_in_minute);
}
if (real_time > 10) {
current_record.show_chart = true;
if (current_record.canvas && last_chart_evt !== current_record.last_record_at) {
if (!dpsCharts[current_record.name]) dpsCharts[current_record.name] = new DpsChart(current_record.canvas);
const chart = dpsCharts[current_record.name];
for (const [idx, actor] of Object.entries(current_record.actors)) {
chart.set_key_color(actor.name, actor.color);
}
chart.append_frame(real_time, Object.fromEntries(Object.entries(current_record.actors).map(([idx, actor]) => [actor.name, actor.dps_in_minute])));
last_chart_evt = current_record.last_record_at;
}
}
}
const update = () => {
const current_record = get_latest_record();
if (current_record.last_record_at > 0) {
process_old_records(current_record);
calc_damages(current_record);
}
if (update_period > 0) setTimeout(update, update_period);
}
const create_socket = () => {
const socket_ = new WebSocket("ws://localhost:24399");
socket_.addEventListener("message", evt_ => {
if (log_evt) log(evt_.data);
const evt = JSON.parse(evt_.data);
switch (evt.type) {
case "enter_area":
want_new = true;
break;
case "damage":
if (want_new) new_record();
on_damage(evt.data);
break;
}
});
socket_.addEventListener("open", () => {
is_socket_connected.value = true;
socket = socket_;
});
socket_.addEventListener("close", () => {
is_socket_connected.value = false;
socket = null;
});
socket_.addEventListener("error", evt => {
console.error("Ws Error", evt);
is_socket_connected.value = false;
socket = null;
if (update_period > 0)
setTimeout(create_socket, update_period);
});
}
Vue.onMounted(() => {
update_period = 1000;
create_socket();
update();
});
Vue.onUnmounted(() => {
update_period = -1;
if (socket) socket.close();
});
return {
view_record_value,
drawer,
is_socket_connected,
records,
fmt_time,
remove_record,
}
}
})
App.use(ElementPlus);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) App.component(key, component);
App.mount('#app');
</script>
</html>