forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpcRespawnCooldowns.ts
More file actions
172 lines (141 loc) · 5.04 KB
/
Copy pathnpcRespawnCooldowns.ts
File metadata and controls
172 lines (141 loc) · 5.04 KB
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
import fs from "fs";
import path from "path";
export type NpcRespawnEntry = {
mapNum: number;
x: number;
y: number;
npcIndex: number;
};
type PersistedNpcRespawnCooldown = NpcRespawnEntry & {
respawnAt: number;
};
const NPC_RESPAWN_COOLDOWNS_PATH = path.resolve(__dirname, "../jsons/npcRespawnCooldowns.json");
const persistedCooldowns = new Map<string, PersistedNpcRespawnCooldown>();
const scheduledRespawns = new Map<string, NodeJS.Timeout>();
export function buildNpcRespawnKey(entry: NpcRespawnEntry): string {
return `${entry.mapNum}:${entry.x}:${entry.y}:${entry.npcIndex}`;
}
function persistNpcRespawnCooldowns(): void {
const rows = [...persistedCooldowns.values()].sort((left, right) => left.respawnAt - right.respawnAt);
fs.writeFileSync(NPC_RESPAWN_COOLDOWNS_PATH, `${JSON.stringify(rows, null, 2)}\n`, "utf8");
}
function clearScheduledRespawn(key: string): void {
const timer = scheduledRespawns.get(key);
if (!timer) {
return;
}
clearTimeout(timer);
scheduledRespawns.delete(key);
}
function scheduleNpcRespawn(cooldown: PersistedNpcRespawnCooldown, callback: (entry: NpcRespawnEntry) => void): void {
const key = buildNpcRespawnKey(cooldown);
const delayMs = Math.max(0, cooldown.respawnAt - Date.now());
console.log(delayMs);
clearScheduledRespawn(key);
scheduledRespawns.set(
key,
setTimeout(() => {
const activeCooldown = persistedCooldowns.get(key);
if (!activeCooldown || activeCooldown.respawnAt !== cooldown.respawnAt) {
return;
}
persistedCooldowns.delete(key);
scheduledRespawns.delete(key);
persistNpcRespawnCooldowns();
callback({
mapNum: cooldown.mapNum,
x: cooldown.x,
y: cooldown.y,
npcIndex: cooldown.npcIndex,
});
}, delayMs),
);
}
function loadPersistedNpcRespawnCooldowns(): void {
persistedCooldowns.clear();
if (!fs.existsSync(NPC_RESPAWN_COOLDOWNS_PATH)) {
persistNpcRespawnCooldowns();
return;
}
try {
const raw = fs.readFileSync(NPC_RESPAWN_COOLDOWNS_PATH, "utf8");
const parsed = JSON.parse(raw) as PersistedNpcRespawnCooldown[];
for (const entry of Array.isArray(parsed) ? parsed : []) {
const normalized = {
mapNum: Number(entry.mapNum ?? 0),
x: Number(entry.x ?? 0),
y: Number(entry.y ?? 0),
npcIndex: Number(entry.npcIndex ?? 0),
respawnAt: Number(entry.respawnAt ?? 0),
};
if (
normalized.mapNum <= 0 ||
normalized.x <= 0 ||
normalized.y <= 0 ||
normalized.npcIndex <= 0 ||
normalized.respawnAt <= 0
) {
continue;
}
persistedCooldowns.set(buildNpcRespawnKey(normalized), normalized);
}
} catch (error) {
console.warn(
`[NPC RESPAWN] No se pudo leer npcRespawnCooldowns.json: ${error instanceof Error ? error.message : "error desconocido"}.`,
);
persistedCooldowns.clear();
}
}
export function initializeNpcRespawnCooldowns(callback: (entry: NpcRespawnEntry) => void): Set<string> {
loadPersistedNpcRespawnCooldowns();
const loadedKeys = new Set<string>(persistedCooldowns.keys());
const now = Date.now();
let removedExpired = false;
for (const [key, cooldown] of persistedCooldowns.entries()) {
if (cooldown.respawnAt > now) {
scheduleNpcRespawn(cooldown, callback);
continue;
}
callback({
mapNum: cooldown.mapNum,
x: cooldown.x,
y: cooldown.y,
npcIndex: cooldown.npcIndex,
});
persistedCooldowns.delete(key);
removedExpired = true;
}
if (removedExpired) {
persistNpcRespawnCooldowns();
}
return loadedKeys;
}
export function getNpcRespawnCooldown(entry: NpcRespawnEntry): PersistedNpcRespawnCooldown | null {
return persistedCooldowns.get(buildNpcRespawnKey(entry)) ?? null;
}
export function setNpcRespawnCooldown(
entry: NpcRespawnEntry,
cooldownMs: number,
callback: (respawnEntry: NpcRespawnEntry) => void,
): PersistedNpcRespawnCooldown {
const normalizedCooldown = {
mapNum: Number(entry.mapNum),
x: Number(entry.x),
y: Number(entry.y),
npcIndex: Number(entry.npcIndex),
respawnAt: Date.now() + Math.max(0, cooldownMs),
};
persistedCooldowns.set(buildNpcRespawnKey(normalizedCooldown), normalizedCooldown);
persistNpcRespawnCooldowns();
scheduleNpcRespawn(normalizedCooldown, callback);
return normalizedCooldown;
}
export function clearNpcRespawnCooldown(entry: NpcRespawnEntry): void {
const key = buildNpcRespawnKey(entry);
if (!persistedCooldowns.has(key)) {
return;
}
clearScheduledRespawn(key);
persistedCooldowns.delete(key);
persistNpcRespawnCooldowns();
}