forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixCharactersMissingHeads.ts
More file actions
115 lines (98 loc) · 3.06 KB
/
Copy pathfixCharactersMissingHeads.ts
File metadata and controls
115 lines (98 loc) · 3.06 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
import pool from "../db";
import {
CHARACTER_GENDERS,
CHARACTER_RACES,
GENDER_ID_MAP,
RACE_ID_MAP,
getAllowedAppearance,
} from "../lib/characterCreation";
type AffectedCharacterRow = {
id: string;
name: string;
id_raza: number;
id_genero: number;
id_head: number;
id_last_head: number;
};
const KNOWN_INVALID_DEFAULT_HEAD_IDS = [150, 200, 250, 300, 350, 400, 450];
const raceKeyById = new Map(
CHARACTER_RACES.map((raceKey) => [RACE_ID_MAP[raceKey], raceKey] as const),
);
const genderKeyById = new Map(
CHARACTER_GENDERS.map(
(genderKey) => [GENDER_ID_MAP[genderKey], genderKey] as const,
),
);
function resolveDefaultHeadId(idRaza: number, idGenero: number): number {
const raceKey = raceKeyById.get(Number(idRaza));
const genderKey = genderKeyById.get(Number(idGenero));
if (!raceKey || !genderKey) {
throw new Error(
`No se pudo resolver cabeza default para raza ${idRaza} y genero ${idGenero}`,
);
}
return getAllowedAppearance(raceKey, genderKey).startHeadId;
}
async function main() {
const client = await pool.connect();
try {
await client.query("BEGIN");
const affectedResult = await client.query<AffectedCharacterRow>(
`
SELECT id, name, id_raza, id_genero, id_head, id_last_head
FROM characters
WHERE deleted_at IS NULL
AND (
(COALESCE(id_head, 0) <= 0 AND COALESCE(id_last_head, 0) <= 0)
OR (
COALESCE(id_head, 0) = ANY($1::int[])
AND COALESCE(id_last_head, 0) = ANY($1::int[])
)
)
ORDER BY created_at ASC, id ASC
`,
[KNOWN_INVALID_DEFAULT_HEAD_IDS],
);
if (affectedResult.rows.length === 0) {
await client.query("COMMIT");
console.log(
"No se encontraron personajes con id_head = 0 e id_last_head = 0.",
);
return;
}
let updatedCount = 0;
for (const character of affectedResult.rows) {
const defaultHeadId = resolveDefaultHeadId(
character.id_raza,
character.id_genero,
);
await client.query(
`
UPDATE characters
SET id_head = $2,
id_last_head = $2,
updated_at = NOW()
WHERE id = $1
`,
[character.id, defaultHeadId],
);
updatedCount += 1;
console.log(
`Personaje reparado: ${character.name} (${character.id}) ${character.id_head}/${character.id_last_head} -> cabeza ${defaultHeadId}`,
);
}
await client.query("COMMIT");
console.log(`Personajes reparados: ${updatedCount}`);
} catch (error) {
await client.query("ROLLBACK");
throw error;
} finally {
client.release();
await pool.end();
}
}
main().catch(async (error) => {
console.error(error);
await pool.end().catch(() => undefined);
process.exit(1);
});