forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacterSettings.ts
More file actions
282 lines (242 loc) · 7 KB
/
Copy pathcharacterSettings.ts
File metadata and controls
282 lines (242 loc) · 7 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
import { z } from "zod";
import pool from "../db";
import type { AuthSessionRecord } from "../types";
const HOTKEY_ACTIONS = [
"moveUp",
"moveLeft",
"moveDown",
"moveRight",
"toggleWorldMap",
"toggleSeguro",
"toggleClanSeguro",
"toggleHiddenSkill",
"pickupItem",
"attackOrTarget",
"meditate",
"equipItem",
"useItem",
"dropItem",
] as const;
const MACRO_SLOTS = 8;
const MAX_LABEL_LENGTH = 70;
const MAX_KEYCODE_LENGTH = 70;
type HotkeyAction = (typeof HOTKEY_ACTIONS)[number];
export type HotkeySettings = Record<HotkeyAction, string[]>;
export type StoredMacro = {
keyCode: string;
targetType: "item" | "spell" | "command";
label: string;
targetSlot?: number;
targetId?: number;
command?: string;
grhIndex?: number;
};
export type CharacterSettingsResponse = {
characterId: string;
hotkeys: HotkeySettings;
macros: Array<StoredMacro | null>;
};
const DEFAULT_HOTKEY_SETTINGS: HotkeySettings = {
moveUp: ["KeyW"],
moveLeft: ["KeyA"],
moveDown: ["KeyS"],
moveRight: ["KeyD"],
toggleWorldMap: ["KeyM"],
toggleSeguro: ["KeyK"],
toggleClanSeguro: ["KeyJ"],
toggleHiddenSkill: ["KeyO"],
pickupItem: ["KeyQ"],
attackOrTarget: ["Space"],
meditate: ["KeyN"],
equipItem: ["KeyE"],
useItem: ["KeyU"],
dropItem: ["KeyT"],
};
const hotkeyCodeSchema = z.string().trim().min(1).max(MAX_KEYCODE_LENGTH);
const macroLabelSchema = z.string().trim().min(1).max(MAX_LABEL_LENGTH);
const hotkeyCodesSchema = z.array(hotkeyCodeSchema).max(2);
const macroCommandSchema = z
.string()
.trim()
.regex(/^\/?[a-zA-Z]{1,15}$/, "El comando del macro solo admite letras de la A a la Z y hasta 15 caracteres")
.transform((value) => {
const normalized = value.startsWith("/") ? value.slice(1) : value;
return `/${normalized.toLowerCase()}`;
});
const storedMacroSchema = z.discriminatedUnion("targetType", [
z.object({
keyCode: hotkeyCodeSchema,
targetType: z.literal("item"),
label: macroLabelSchema,
targetSlot: z.number().int(),
targetId: z.number().int(),
grhIndex: z.number().int().optional(),
}),
z.object({
keyCode: hotkeyCodeSchema,
targetType: z.literal("spell"),
label: macroLabelSchema,
targetSlot: z.number().int(),
targetId: z.number().int(),
}),
z.object({
keyCode: hotkeyCodeSchema,
targetType: z.literal("command"),
label: macroLabelSchema,
command: macroCommandSchema,
}),
]);
const hotkeySettingsSchema = z.object(
Object.fromEntries(
HOTKEY_ACTIONS.map((action) => [action, hotkeyCodesSchema]),
) as Record<HotkeyAction, typeof hotkeyCodesSchema>,
);
const characterSettingsSchema = z.object({
hotkeys: hotkeySettingsSchema,
macros: z.array(storedMacroSchema.nullable()).length(MACRO_SLOTS),
});
function cloneDefaultHotkeySettings(): HotkeySettings {
return HOTKEY_ACTIONS.reduce((acc, action) => {
acc[action] = [...DEFAULT_HOTKEY_SETTINGS[action]];
return acc;
}, {} as HotkeySettings);
}
function normalizeHotkeySettings(value: unknown): HotkeySettings {
const fallback = cloneDefaultHotkeySettings();
const hasStoredMeditateBinding =
!!value &&
typeof value === "object" &&
Array.isArray((value as Partial<Record<HotkeyAction, unknown>>).meditate);
if (!value || typeof value !== "object") {
return fallback;
}
const normalized = HOTKEY_ACTIONS.reduce((acc, action) => {
const parsedCodes = hotkeyCodesSchema.safeParse(
(value as Partial<Record<HotkeyAction, unknown>>)[action],
);
acc[action] = parsedCodes.success ? [...parsedCodes.data] : [...fallback[action]];
return acc;
}, {} as HotkeySettings);
if (
!hasStoredMeditateBinding &&
HOTKEY_ACTIONS.some(
(action) => action !== "meditate" && normalized[action].includes("KeyN"),
)
) {
normalized.meditate = [];
}
return normalized;
}
function normalizeMacros(value: unknown): Array<StoredMacro | null> {
if (!Array.isArray(value)) {
return Array.from({ length: MACRO_SLOTS }, () => null);
}
return Array.from({ length: MACRO_SLOTS }, (_unused, index) => {
const parsed = storedMacroSchema.nullable().safeParse(value[index] ?? null);
return parsed.success ? parsed.data : null;
});
}
async function getSessionWithSelectedCharacter(token: string): Promise<AuthSessionRecord | null> {
const sessionResult = await pool.query<AuthSessionRecord>(
`
SELECT token, account_id, selected_character_id, created_at, expires_at
FROM auth_sessions
WHERE token = $1
AND expires_at > NOW()
LIMIT 1
`,
[token],
);
const session = sessionResult.rows[0] ?? null;
if (!session?.selected_character_id) {
return null;
}
return session;
}
async function assertSelectedCharacterBelongsToSession(
accountId: string,
characterId: string,
): Promise<boolean> {
const result = await pool.query<{ id: string }>(
`
SELECT id
FROM characters
WHERE id = $1
AND account_id = $2
AND deleted_at IS NULL
LIMIT 1
`,
[characterId, accountId],
);
return Boolean(result.rows[0]);
}
export async function getCharacterSettingsBySessionToken(
token: string,
): Promise<CharacterSettingsResponse | null> {
const session = await getSessionWithSelectedCharacter(token);
if (!session?.selected_character_id) {
return null;
}
const characterBelongsToSession = await assertSelectedCharacterBelongsToSession(
session.account_id,
session.selected_character_id,
);
if (!characterBelongsToSession) {
return null;
}
const settingsResult = await pool.query<{
hotkeys: unknown;
macros: unknown;
}>(
`
SELECT hotkeys, macros
FROM character_settings
WHERE character_id = $1
LIMIT 1
`,
[session.selected_character_id],
);
const row = settingsResult.rows[0];
return {
characterId: session.selected_character_id,
hotkeys: normalizeHotkeySettings(row?.hotkeys),
macros: normalizeMacros(row?.macros),
};
}
export async function saveCharacterSettingsBySessionToken(
token: string,
payload: unknown,
): Promise<CharacterSettingsResponse | null> {
const session = await getSessionWithSelectedCharacter(token);
if (!session?.selected_character_id) {
return null;
}
const characterBelongsToSession = await assertSelectedCharacterBelongsToSession(
session.account_id,
session.selected_character_id,
);
if (!characterBelongsToSession) {
return null;
}
const parsed = characterSettingsSchema.parse(payload);
await pool.query(
`
INSERT INTO character_settings (character_id, hotkeys, macros)
VALUES ($1, $2::jsonb, $3::jsonb)
ON CONFLICT (character_id)
DO UPDATE SET hotkeys = EXCLUDED.hotkeys,
macros = EXCLUDED.macros,
updated_at = NOW()
`,
[
session.selected_character_id,
JSON.stringify(parsed.hotkeys),
JSON.stringify(parsed.macros),
],
);
return {
characterId: session.selected_character_id,
hotkeys: normalizeHotkeySettings(parsed.hotkeys),
macros: normalizeMacros(parsed.macros),
};
}