-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtracking.js
124 lines (105 loc) · 2.42 KB
/
tracking.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
const crypto = require('node:crypto')
const BASE_URL = 'https://dw.kvn.wtf'
/** @type {Map<string, string>} */
const seenChunks = new Map()
/** @type {Set<string>} */
const seenObjects = new Set()
/**
* @param chunk {number[][]}
* @returns {string}
*/
function hashChunk(chunk) {
return crypto
.createHash('sha1')
.update(JSON.stringify(chunk))
.digest('hex')
}
/**
* @param {Record<string, number[][]>} chunks
* @returns {Promise<void>}
*/
async function onSeenChunks(chunks) {
const requests = []
for (const [chunkName, chunk] of Object.entries(chunks)) {
const hash = hashChunk(chunk)
if ((seenChunks.get(chunkName) ?? '') === hash) {
continue
}
seenChunks.set(chunkName, hash)
requests.push(fetch(
`${BASE_URL}/log/chunk?name=${encodeURIComponent(chunkName)}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(chunk)
}
))
}
await Promise.allSettled(requests)
}
/**
* @param {any[]} entities
* @returns {Promise<void>}
*/
async function onSeenObjects(entities) {
const requests = []
for (const entity of entities) {
if (!('ai' in entity) || entity.hp !== entity.maxHp || !!entity.fx?.hpMore) {
continue
}
const key = `${entity.level}+${entity.r ?? 0} ${entity.md}`
if (seenObjects.has(key)) {
continue
}
seenObjects.add(key)
requests.push(fetch(
`${BASE_URL}/log/mob`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
level: entity.level,
r: entity.r ?? 0,
md: entity.md,
terrain: entity.terrain,
hp: entity.maxHp,
})
}
))
}
await Promise.allSettled(requests)
}
/**
* @param {{ item: { mods: Record<string, number>, n?: number} }[]} entries
* @returns {Promise<void>}
*/
async function onLoot(entries){
const requests = []
for (const entry of entries) {
if (!entry.item.mods) {
continue
}
const item = { ...entry.item }
delete item.n
requests.push(fetch(
`${BASE_URL}/log/loot`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(item)
}
))
}
await Promise.allSettled(requests)
}
module.exports = {
onSeenChunks,
onSeenObjects,
onLoot,
}