summaryrefslogtreecommitdiff
path: root/src/routers/vault/utils/game_accounts.js
blob: a5df144258eb855ea64691d9f7b84eb2b23540fd (plain) (blame)
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
const LegacyAccount = require("../types/LegacyAccount.js");
const LegacyChar = require("../types/LegacyChar.js");
const EvolAccount = require("../types/EvolAccount.js");
const EvolChar = require("../types/EvolChar.js");
const { Op } = require("sequelize");

/**
 * fetch the legacy game accounts and cache in the Session
 * @param {*} req - the express request
 * @param {Session} session - the Session
 * @return {Promise<LegacyAccount[]>} a promise resolving to an array of LegacyAccount
 */
const get_legacy_accounts = async (req, session) => {
    const accounts = [];
    const claimed = await req.app.locals.vault.claimed_legacy_accounts.findAll({
        where: {vaultId: session.vault},
    });

    for (const acc_ of claimed) {
        const acc = await req.app.locals.legacy.login.findByPk(acc_.accountId);

        if (acc === null || acc === undefined) {
            // unexpected: account was deleted
            console.info(`Vault.legacy.account: unlinking deleted account ${acc_.accountId} <${session.vault}@vault> [${req.ip}]`);
            await acc_.destroy(); // un-claim the account
            continue;
        }

        const account = new LegacyAccount(acc.accountId, acc.userid);
        account.revoltId = acc.revoltId;

        const chars = await req.app.locals.legacy.char.findAll({
            where: {accountId: acc.accountId},
        });

        for (const char of chars) {
            const char_ = new LegacyChar(account, char.charId, char.name);
            char_.baseLevel = char.baseLevel;
            char_.gender = char.sex;
            char_.revoltId = char.revoltId;

            const char_vars = await req.app.locals.legacy.char_reg.findAll({
                where: {
                    charId: char.charId,
                    [Op.or]: [
                        {name: "TUT_var"},
                        {name: "BOSS_POINTS"},
                    ],
                },
                limit: 2, // for now we only use these 2 vars ^
            });

            for (const var_ of char_vars) {
                if (var_.name === "TUT_var") {
                    char_.creationTime = var_.value > 0xFF ? var_.value : 0;
                } else if (var_.name === "BOSS_POINTS") {
                    char_.bossPoints = Math.max(0, var_.value);
                }

                // in the future maybe here set the vars in a Map<name, value>
            }

            account.chars.push(char_);
        }

        accounts.push(account);
    }

    session.legacyAccounts = accounts;
    return accounts;
};

/**
 * fetch the evol game accounts and cache in the Session
 * @param {*} req - the express request
 * @param {Session} session - the Session
 * @return {Promise<EvolAccount[]>} a promise resolving to an array of EvolAccount
 */
const get_account_list = async (req, session) => {
    const accounts = [];
    const claimed = await req.app.locals.vault.claimed_game_accounts.findAll({
        where: {vaultId: session.vault},
    });

    for (const acc_ of claimed) {
        const acc = await req.app.locals.evol.login.findByPk(acc_.accountId);

        if (acc === null || acc === undefined) {
            // unexpected: account was deleted
            console.info(`Vault.evol.account: unlinking deleted account ${acc_.accountId} <${session.vault}@vault> [${req.ip}]`);
            await acc_.destroy(); // un-claim the account
            continue;
        }

        const account = new EvolAccount(acc.accountId, acc.userid);

        // check if this is an imported account
        for (const legacy_acc of session.legacyAccounts) {
            if (legacy_acc.revoltId === account.accountId) {
                account.legacyId = legacy_acc.accountId;

                // two-way binding
                account.legacyAccount = legacy_acc;
                legacy_acc.revoltAccount = account;
                break;
            }
        }

        const chars = await req.app.locals.evol.char.findAll({
            where: {accountId: acc.accountId},
        });

        for (const char of chars) {
            const char_ = new EvolChar(account, char.charId, char.name);
            char_.baseLevel = char.baseLevel;
            char_.gender = char.sex;

            // check if this is an imported char
            for (const legacy_acc of session.legacyAccounts) {
                for (const legacy_char of legacy_acc.chars) {
                    if (legacy_char.revoltId === char_.charId) {
                        char_.legacyId = legacy_char.charId;

                        // two-way binding
                        char_.legacyChar = legacy_char;
                        legacy_char.revoltChar = char_;
                        break;
                    }
                }
            }

            account.chars.push(char_);
        }

        accounts.push(account);
    }

    session.gameAccounts = accounts;
    return accounts;
};

module.exports = {
    get_evol: get_account_list,
    get_legacy: get_legacy_accounts,
};