summaryrefslogtreecommitdiff
path: root/src/routers/vault/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/routers/vault/utils')
-rw-r--r--src/routers/vault/utils/claim.js27
-rw-r--r--src/routers/vault/utils/game_accounts.js123
2 files changed, 136 insertions, 14 deletions
diff --git a/src/routers/vault/utils/claim.js b/src/routers/vault/utils/claim.js
index b3dbe9d..d28e076 100644
--- a/src/routers/vault/utils/claim.js
+++ b/src/routers/vault/utils/claim.js
@@ -1,4 +1,6 @@
const { Op } = require("sequelize");
+const LegacyAccount = require("../types/LegacyAccount.js");
+const LegacyChar = require("../types/LegacyChar.js");
// claim by email // TODO: DRY this
const claim_accounts = async (req, email, vault_id, session = null) => {
@@ -49,27 +51,24 @@ const claim_accounts = async (req, email, vault_id, session = null) => {
});
if (session !== null) {
- const chars = [];
const chars_ = await locals.legacy.char.findAll({
where: {accountId: acc.accountId},
});
+ const legacy_account = new LegacyAccount(acc.accountId, acc.userid);
+ legacy_account.revoltId = acc.revoltId;
+
for (const char of chars_) {
- chars.push({
- name: char.name,
- charId: char.charId,
- revoltId: char.revoltId,
- level: char.baseLevel,
- sex: char.sex,
- });
+ const legacy_char = new LegacyChar(legacy_account, char.charId, char.name);
+ legacy_char.revoltId = char.revoltId;
+ legacy_char.baseLevel = char.baseLevel;
+ legacy_char.gender = char.sex;
+
+ legacy_account.chars.push(legacy_char);
}
+
// add to session cache
- session.legacyAccounts.push({
- name: acc.userid,
- accountId: acc.accountId,
- revoltId: acc.revoltId,
- chars,
- });
+ session.legacyAccounts.push(legacy_account);
}
locals.logger.info(`Vault.legacy.account: linked Legacy account ${acc.accountId} to Vault account {${vault_id}} [${req.ip}]`);
diff --git a/src/routers/vault/utils/game_accounts.js b/src/routers/vault/utils/game_accounts.js
new file mode 100644
index 0000000..c19feb5
--- /dev/null
+++ b/src/routers/vault/utils/game_accounts.js
@@ -0,0 +1,123 @@
+const LegacyAccount = require("../types/LegacyAccount.js");
+const LegacyChar = require("../types/LegacyChar.js");
+const EvolAccount = require("../types/EvolAccount.js");
+const EvolChar = require("../types/EvolChar.js");
+
+/**
+ * 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}} [${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;
+
+ 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}} [${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,
+};