Skip to content

Commit

Permalink
Add account transfer script
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Sep 22, 2024
1 parent 3000299 commit 36cb86f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/database/official/OfficialDatabaseTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ config();
export enum OfficialDatabaseTables {
user = "user",
score = "score",
bestScore = "score_best",
bannedScore = "score_banned",
bestBannedScore = "score_best_banned",
}

/**
Expand All @@ -17,7 +20,7 @@ export enum OfficialDatabaseTables {
* @returns The constructed table name.
*/
export function constructOfficialDatabaseTable(
table: OfficialDatabaseTables
table: OfficialDatabaseTables,
): string {
return `${process.env.OFFICIAL_DB_PREFIX}${table}`;
}
81 changes: 81 additions & 0 deletions src/performAccountTransfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { DatabaseManager } from "@alice-database/DatabaseManager";
import { officialPool } from "@alice-database/official/OfficialDatabasePool";
import {
constructOfficialDatabaseTable,
OfficialDatabaseTables,
} from "@alice-database/official/OfficialDatabaseTables";

Promise.all([DatabaseManager.init(), officialPool.connect()]).then(async () => {
const dbManager = DatabaseManager.aliceDb.collections.accountTransfer;
const transfers = await dbManager.get(
"discordId",
{ transferDone: { $ne: true } },
{ projection: { _id: 0 } },
);

const userTable = constructOfficialDatabaseTable(
OfficialDatabaseTables.user,
);

const scoreTables = [
OfficialDatabaseTables.score,
OfficialDatabaseTables.bestScore,
OfficialDatabaseTables.bannedScore,
OfficialDatabaseTables.bestBannedScore,
].map(constructOfficialDatabaseTable);

for (const transfer of transfers.values()) {
const connection = await officialPool.getConnection();

await connection.beginTransaction();

for (const uidToTransfer of transfer.transferList) {
// Mark the uid as archived.
await connection.query(
`UPDATE ${userTable} SET archived = 1 WHERE id = ?`,
[uidToTransfer],
);

// Transfer the scores.
for (const table of scoreTables) {
await connection.query(
`UPDATE ${table} SET uid = ? WHERE uid = ?`,
[transfer.transferUid, uidToTransfer],
);
}
}

const scoreTable = scoreTables[0];
const valuesArr = [transfer.transferUid, transfer.transferUid];

await connection.query(
`UPDATE ${userTable} SET score = (SELECT SUM(score) FROM ${scoreTable} WHERE uid = ?) WHERE id = ?`,
valuesArr,
);

await connection.query(
`UPDATE ${userTable} SET accuracy = (SELECT SUM(accuracy) FROM ${scoreTable} WHERE uid = ?) WHERE id = ?`,
valuesArr,
);

await connection.query(
`UPDATE ${userTable} SET playcount = (SELECT COUNT(*) FROM ${scoreTable} WHERE uid = ? AND score > 0) WHERE id = ?`,
valuesArr,
);

await connection.commit();

connection.release();

await dbManager.updateOne(
{ discordId: transfer.discordId },
{ $set: { transferDone: true } },
);

console.log(`Transfer for ${transfer.discordId} has been completed.`);
}

console.log("All transfers have been completed.");

process.exit(0);
});
5 changes: 5 additions & 0 deletions src/structures/database/aliceDb/DatabaseAccountTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ export interface DatabaseAccountTransfer extends BaseDocument {
* The list of uids of osu!droid accounts to transfer scores from.
*/
readonly transferList: number[];

/**
* Whether the transfer is done.
*/
transferDone?: boolean;
}

0 comments on commit 36cb86f

Please sign in to comment.