forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter-settings.ts
More file actions
101 lines (85 loc) · 2.62 KB
/
Copy pathcharacter-settings.ts
File metadata and controls
101 lines (85 loc) · 2.62 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
import {
DEFAULT_HOTKEY_SETTINGS,
normalizeHotkeySettings,
type HotkeySettings,
} from "./hotkeys";
export const MACRO_SLOTS = 8;
export type MacroTargetType = "item" | "spell" | "command";
export type StoredMacro = {
keyCode: string;
targetType: MacroTargetType;
label: string;
targetSlot?: number;
targetId?: number;
command?: string;
grhIndex?: number;
};
export type CharacterSettingsResponse = {
characterId: string;
hotkeys: HotkeySettings;
macros: Array<StoredMacro | null>;
};
export function createEmptyMacros(): Array<StoredMacro | null> {
return Array.from({ length: MACRO_SLOTS }, () => null);
}
export function normalizeMacros(value: unknown): Array<StoredMacro | null> {
if (!Array.isArray(value)) {
return createEmptyMacros();
}
return Array.from({ length: MACRO_SLOTS }, (_, index) => {
const entry = value[index];
if (!entry || typeof entry !== "object") {
return null;
}
const macro = entry as Partial<StoredMacro>;
if (
typeof macro.keyCode !== "string" ||
(macro.targetType !== "item" &&
macro.targetType !== "spell" &&
macro.targetType !== "command") ||
typeof macro.label !== "string"
) {
return null;
}
if (macro.targetType === "command") {
if (typeof macro.command !== "string" || !macro.command.trim()) {
return null;
}
return {
keyCode: macro.keyCode,
targetType: "command",
label: macro.label,
command: macro.command,
};
}
if (
typeof macro.targetSlot !== "number" ||
typeof macro.targetId !== "number"
) {
return null;
}
return {
keyCode: macro.keyCode,
targetType: macro.targetType,
targetSlot: macro.targetSlot,
targetId: macro.targetId,
label: macro.label,
grhIndex:
typeof macro.grhIndex === "number" ? macro.grhIndex : undefined,
};
});
}
export function normalizeCharacterSettings(
value: Partial<CharacterSettingsResponse> | null | undefined,
): CharacterSettingsResponse | null {
if (!value?.characterId || typeof value.characterId !== "string") {
return null;
}
return {
characterId: value.characterId,
hotkeys: normalizeHotkeySettings(
value.hotkeys ?? DEFAULT_HOTKEY_SETTINGS,
),
macros: normalizeMacros(value.macros),
};
}