forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntimeSettings.ts
More file actions
318 lines (268 loc) · 8.71 KB
/
Copy pathruntimeSettings.ts
File metadata and controls
318 lines (268 loc) · 8.71 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
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
import pool from "../db";
import type { RuntimeTimingConfig } from "../types";
const TIMING_SETTINGS_KEY = "timing";
export const DEFAULT_RUNTIME_TIMING: RuntimeTimingConfig = {
gameplayTickMs: 50,
walkStepMs: 200,
playerStatusTickMs: 500,
fishingTickMs: 4000,
npcThinkMs: 650,
idleCharacterTimeoutMs: 900000,
idleCharacterSweepMs: 10000,
cleanupClosedCharactersMs: 60000,
onlineStatsSnapshotMs: 60000,
worldSaveMs: 1800000,
statusDurations: {
crowdControlUserMs: 8000,
crowdControlNpcMs: 50000,
fuerzaAgilidadBuffMs: 90000,
invisibilitySpellMs: 20000,
npcAttackMs: 2000,
},
actionCooldowns: {
dialogMs: 500,
clickMs: 150,
doorToggleMs: 250,
meleeMs: 950,
rangeMs: 950,
spellMs: 850,
meleeToSpellMs: 550,
spellToMeleeMs: 550,
useItemMs: 250,
meleeToUseItemMs: 550,
dropItemMs: 500,
equipToggleMs: 125,
},
visualEffects: {
invisibilityFadeOutMs: 1250,
invisibilityFadeInMs: 3000,
invisibilityMinAlpha: 0,
invisibilityMaxAlpha: 0.85,
},
};
const ALLOWED_TIMING_PATHS = new Set([
"gameplayTickMs",
"walkStepMs",
"playerStatusTickMs",
"fishingTickMs",
"npcThinkMs",
"idleCharacterTimeoutMs",
"idleCharacterSweepMs",
"cleanupClosedCharactersMs",
"onlineStatsSnapshotMs",
"worldSaveMs",
"statusDurations.crowdControlUserMs",
"statusDurations.crowdControlNpcMs",
"statusDurations.fuerzaAgilidadBuffMs",
"statusDurations.invisibilitySpellMs",
"statusDurations.npcAttackMs",
"actionCooldowns.dialogMs",
"actionCooldowns.clickMs",
"actionCooldowns.doorToggleMs",
"actionCooldowns.meleeMs",
"actionCooldowns.rangeMs",
"actionCooldowns.spellMs",
"actionCooldowns.meleeToSpellMs",
"actionCooldowns.spellToMeleeMs",
"actionCooldowns.useItemMs",
"actionCooldowns.meleeToUseItemMs",
"actionCooldowns.dropItemMs",
"actionCooldowns.equipToggleMs",
"visualEffects.invisibilityFadeOutMs",
"visualEffects.invisibilityFadeInMs",
"visualEffects.invisibilityMinAlpha",
"visualEffects.invisibilityMaxAlpha",
]);
type RuntimeSettingRecord = {
key: string;
value: RuntimeTimingConfig | null;
};
function cloneDefaultTiming(): RuntimeTimingConfig {
return JSON.parse(
JSON.stringify(DEFAULT_RUNTIME_TIMING),
) as RuntimeTimingConfig;
}
function normalizeLegacyUseItemTiming(value: unknown): unknown {
if (!isPlainObject(value)) {
return value;
}
const next = { ...value };
const actionCooldowns = isPlainObject(next.actionCooldowns)
? { ...next.actionCooldowns }
: null;
if (!actionCooldowns) {
return next;
}
if (actionCooldowns.useItemMs == null) {
const clickMs = Number(actionCooldowns.useItemClickMs);
const uMs = Number(actionCooldowns.useItemUMs);
if (
Number.isFinite(clickMs) &&
clickMs > 0 &&
Number.isFinite(uMs) &&
uMs > 0
) {
actionCooldowns.useItemMs = Math.round(
1000 / (1000 / clickMs + 1000 / uMs),
);
} else if (Number.isFinite(clickMs) && clickMs > 0) {
actionCooldowns.useItemMs = Math.round(clickMs);
} else if (Number.isFinite(uMs) && uMs > 0) {
actionCooldowns.useItemMs = Math.round(uMs);
}
}
if (actionCooldowns.meleeToUseItemMs == null) {
const meleeToUseItemUMs = Number(actionCooldowns.meleeToUseItemUMs);
if (Number.isFinite(meleeToUseItemUMs) && meleeToUseItemUMs > 0) {
actionCooldowns.meleeToUseItemMs = Math.round(meleeToUseItemUMs);
}
}
delete actionCooldowns.useItemClickMs;
delete actionCooldowns.useItemUMs;
delete actionCooldowns.meleeToUseItemUMs;
next.actionCooldowns = actionCooldowns;
return next;
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function mergeTiming(
base: Record<string, unknown>,
patch: Record<string, unknown>,
): Record<string, unknown> {
const next = { ...base };
for (const [key, value] of Object.entries(patch)) {
const current = next[key];
if (isPlainObject(current) && isPlainObject(value)) {
next[key] = mergeTiming(current, value);
continue;
}
next[key] = value;
}
return next;
}
function isAlphaTimingPath(path: string): boolean {
return (
path === "visualEffects.invisibilityMinAlpha" ||
path === "visualEffects.invisibilityMaxAlpha"
);
}
function sanitizeTimingValue(value: unknown, path: string): number {
const numericValue = Number(value);
if (!Number.isFinite(numericValue)) {
throw new Error(`El valor para ${path} debe ser un numero valido`);
}
if (isAlphaTimingPath(path)) {
if (numericValue < 0 || numericValue > 1) {
throw new Error(`El valor para ${path} debe estar entre 0 y 1`);
}
return Math.round(numericValue * 1000) / 1000;
}
if (numericValue <= 0) {
throw new Error(
`El valor para ${path} debe ser un numero positivo en milisegundos`,
);
}
return Math.round(numericValue);
}
function sanitizeTimingTree(
value: unknown,
defaults: Record<string, unknown>,
prefix = "",
): Record<string, unknown> {
const source = isPlainObject(value) ? value : {};
const result: Record<string, unknown> = {};
for (const [key, defaultValue] of Object.entries(defaults)) {
const path = prefix ? `${prefix}.${key}` : key;
const rawValue = source[key];
if (isPlainObject(defaultValue)) {
result[key] = sanitizeTimingTree(rawValue, defaultValue, path);
continue;
}
result[key] = sanitizeTimingValue(rawValue ?? defaultValue, path);
}
return result;
}
function setNestedValue(
target: Record<string, unknown>,
path: string,
value: number,
): void {
const segments = path.split(".");
let current: Record<string, unknown> = target;
for (let index = 0; index < segments.length - 1; index += 1) {
const segment = segments[index];
const nextValue = current[segment];
if (!isPlainObject(nextValue)) {
current[segment] = {};
}
current = current[segment] as Record<string, unknown>;
}
current[segments[segments.length - 1] as string] = value;
}
async function saveTimingConfig(
timing: RuntimeTimingConfig,
): Promise<RuntimeTimingConfig> {
await pool.query(
`
INSERT INTO runtime_settings (key, value, updated_at)
VALUES ($1, $2::jsonb, NOW())
ON CONFLICT (key)
DO UPDATE SET value = EXCLUDED.value,
updated_at = NOW()
`,
[TIMING_SETTINGS_KEY, JSON.stringify(timing)],
);
return timing;
}
export async function getRuntimeTimingConfig(): Promise<RuntimeTimingConfig> {
const defaultTiming = cloneDefaultTiming();
const result = await pool.query<RuntimeSettingRecord>(
`
SELECT key, value
FROM runtime_settings
WHERE key = $1
LIMIT 1
`,
[TIMING_SETTINGS_KEY],
);
if (!result.rowCount) {
return saveTimingConfig(defaultTiming);
}
const mergedTiming = mergeTiming(
defaultTiming as unknown as Record<string, unknown>,
normalizeLegacyUseItemTiming(
(result.rows[0]?.value ?? {}) as unknown as Record<string, unknown>,
) as Record<string, unknown>,
);
const sanitizedTiming = sanitizeTimingTree(
mergedTiming,
defaultTiming as unknown as Record<string, unknown>,
) as RuntimeTimingConfig;
return saveTimingConfig(sanitizedTiming);
}
export async function updateRuntimeTimingValue(
path: string,
value: unknown,
): Promise<RuntimeTimingConfig> {
const normalizedPath = path.trim();
if (!ALLOWED_TIMING_PATHS.has(normalizedPath)) {
throw new Error("Intervalo invalido");
}
const nextTiming = cloneDefaultTiming() as unknown as Record<
string,
unknown
>;
const currentTiming = await getRuntimeTimingConfig();
const mergedTiming = mergeTiming(
nextTiming,
currentTiming as unknown as Record<string, unknown>,
);
const sanitizedValue = sanitizeTimingValue(value, normalizedPath);
setNestedValue(mergedTiming, normalizedPath, sanitizedValue);
const sanitizedTiming = sanitizeTimingTree(
mergedTiming,
DEFAULT_RUNTIME_TIMING as unknown as Record<string, unknown>,
) as RuntimeTimingConfig;
return saveTimingConfig(sanitizedTiming);
}