forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyBalanceToCharacters.ts
More file actions
132 lines (118 loc) · 3.28 KB
/
Copy pathapplyBalanceToCharacters.ts
File metadata and controls
132 lines (118 loc) · 3.28 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
import pool from "../db";
import type { CharacterRecord } from "../types";
import {
clampLevel,
getLegacyExpNextLevelForLevel,
getMaxHitForLevel,
getMaxHpForLevel,
getMaxManaForLevel,
getMinHitForLevel,
} from "../lib/characterCreation";
const RACE_BASE_STATS: Record<
number,
{
fuerza: number;
agilidad: number;
inteligencia: number;
constitucion: number;
}
> = {
1: { fuerza: 19, agilidad: 19, inteligencia: 18, constitucion: 20 },
2: { fuerza: 18, agilidad: 20, inteligencia: 20, constitucion: 19 },
3: { fuerza: 20, agilidad: 19, inteligencia: 19, constitucion: 19 },
4: { fuerza: 21, agilidad: 18, inteligencia: 15, constitucion: 21 },
5: { fuerza: 16, agilidad: 21, inteligencia: 22, constitucion: 18 },
};
function getExpectedStats(character: CharacterRecord) {
const base = RACE_BASE_STATS[character.id_raza];
if (!base) {
throw new Error(
`Raza invalida para character ${character.id}: ${character.id_raza}`,
);
}
const level = clampLevel(character.level || 1);
const maxHp = getMaxHpForLevel(character.id_clase, base.constitucion, level);
const maxMana = getMaxManaForLevel(
character.id_clase,
base.inteligencia,
level,
);
return {
level,
expNextLevel: getLegacyExpNextLevelForLevel(level),
attrFuerza: base.fuerza,
attrAgilidad: base.agilidad,
attrInteligencia: base.inteligencia,
attrConstitucion: base.constitucion,
maxHp,
hp: character.dead
? 0
: Math.min(Math.max(Number(character.hp ?? maxHp), 0), maxHp),
maxMana,
mana: Math.min(Math.max(Number(character.mana ?? maxMana), 0), maxMana),
minHit: getMinHitForLevel(character.id_clase, level),
maxHit: getMaxHitForLevel(character.id_clase, level),
};
}
async function main() {
const client = await pool.connect();
try {
await client.query("BEGIN");
const result = await client.query<CharacterRecord>(
"SELECT * FROM characters ORDER BY created_at ASC, id ASC",
);
let updatedCount = 0;
for (const character of result.rows) {
const next = getExpectedStats(character);
await client.query(
`
UPDATE characters
SET
level = $2,
exp_next_level = $3,
attr_fuerza = $4,
attr_agilidad = $5,
attr_inteligencia = $6,
attr_constitucion = $7,
hp = $8,
max_hp = $9,
mana = $10,
max_mana = $11,
min_hit = $12,
max_hit = $13,
updated_at = NOW()
WHERE id = $1
`,
[
character.id,
next.level,
next.expNextLevel,
next.attrFuerza,
next.attrAgilidad,
next.attrInteligencia,
next.attrConstitucion,
next.hp,
next.maxHp,
next.mana,
next.maxMana,
next.minHit,
next.maxHit,
],
);
updatedCount += 1;
}
await client.query("COMMIT");
console.log(`Characters actualizados: ${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);
});