-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.js
188 lines (162 loc) · 5.17 KB
/
watcher.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
/** @typedef {{
* offsets:{
* monsterKills:number,
* monsterDeaths:number,
* trapDeaths:number
* },
* ordering:{
* monsters:string[],
* traps: string[],
* items: string[],
* levelVariants: string[],
* worldNames: string[],
* },
* killers:{
* firstMonster:number,
* firstTrap:number,
* otherValues:{[x:string]:string}
* }
* }} config */
/** @typedef {{
* plays:number,
* wins:number,
* deaths:number,
* score:number,
* levelName:[number,number,number],
* killerName:["MONSTER"|"TRAP"|"OTHER",number],
* items:number[],
* kills: [number,number][]
* }} levelEvent
*/
const fs = require("fs");
const events = require("events");
class SpelunkyWatcher extends events.EventEmitter {
constructor (configLocation,gameLocation){
super();
this.configLocation = configLocation;
this.gameLocation = gameLocation;
/** @type {config} */
this.config;
this.monsterKills = Array(56).fill(0);
this.curPlays = 0;
this.starting = undefined;
}
async startUp(fireFirst=false){
if (this.starting){
return this.starting;
}
this.starting = (async ()=>{
await this.updateFromConfig();
if (fireFirst){
this.handler();
}
})();
return this.starting;
}
async updateFromConfig(){
const rawConfig = await fs.promises.readFile("FileConfig.json");
this.config = JSON.parse(rawConfig);
fs.watch(this.gameLocation,()=>setTimeout(()=>this.handler(),1000));
}
async handler() {
let buffer;
try {
buffer = await fs.promises.readFile(this.gameLocation);
} catch (e){
console.error(e);
return;
}
const data = new Int32Array(buffer.buffer,buffer.byteOffset,buffer.byteLength / Uint32Array.BYTES_PER_ELEMENT)
const [
plays,deaths,wins,c0_1,
c16,c1_1,c1_2,BestScore,
BestTime,AvgScore,LastLevel,LevelVariant,
score,KilledBy,flg1,flg2,
flg3,flg4,c1_3,c0_4
] = data.subarray(0,20);
if (plays == this.curPlays){
return
}
this.curPlays = plays;
//Raw Data for debugging
console.log({
plays,deaths,wins,BestScore,
BestTime,AvgScore,LastLevel,LevelVariant,
LastScore: score,KilledBy,
flg1: fFlag(flg1),
flg2: fFlag(flg2),
flg3: fFlag(flg3),
flg4: fFlag(flg4)
})
const not0 = [c0_1,c0_4];
if (not0.some(x=>x != 0)) console.log("0s",not0)
const not1 = [c1_1,c1_2,c1_3];
if (not1.some(x=>x != 1)) console.log("1s",not1)
if (c16 != 16) console.log("c16",c16);
const items = extractItemIds(flg1,flg2,flg3,flg4);
const levelName = this.findLevelName(LastLevel,LevelVariant);
const killerName = this.findKiller(KilledBy);
const curMonsterKills = data.subarray(this.config.offsets.monsterKill, this.config.offsets.monsterKill + this.config.ordering.monsters.length)
const diffMonsterKills = findDiffIds(this.monsterKills, curMonsterKills);
this.monsterKills = Array.from(curMonsterKills)
this.emit("level",{
plays, wins, deaths,
score, levelName,
killerName,
items,
kills: diffMonsterKills
});
}
findLevelName(levelNumber,levelVariant){
levelNumber -= 1
const world = Math.floor(levelNumber/4)+1
const level = (levelNumber%4)+1
let name;
if (levelVariant == 0){
name = world-1
} else {
name = levelVariant+4;
}
return [world,level,name];
}
findKiller(id){
if (id in this.config.killers.otherValues){
return ["OTHER",id]
}
if (this.config.killers.firstMonster <= id &&
id < this.config.killers.firstMonster + this.config.ordering.monsters.length
){
return ["MONSTER",id-this.config.killers.firstMonster];
}
if (this.config.killers.firstTrap <= id &&
id < this.config.killers.firstTrap + this.config.ordering.traps.length
){
return ["TRAP",id-this.config.killers.firstTrap];
}
}
}
/**
* @method
* @name SpelunkyWatcher#on
* @param {"level"} event
* @param {LevelEvent} args
* @returns {void}
*/
function fFlag(n){
const c = (n >>> 0).toString(16).padStart(8,"0").toUpperCase();
return c.substr(0,2)+"-"+c.substr(2,2)+"-"+c.substr(4,2)+"-"+c.substr(6,2);
}
function extractItemIds(...flags){
return [].concat(...flags.map((flag,n)=>
[0xFF000000,0xFF0000,0xFF00,0xFF]
.map((a,b)=>[a,b+n*4])
.filter(x=>flag&x[0])
.map(x=>x[1])
))
}
function findDiffIds(oldArray,newArray){
return oldArray
.map((k,i)=>[i,newArray[i]-k])
.filter(x=>x[1] > 0)
}
module.exports = SpelunkyWatcher;