Skip to content

Commit

Permalink
Use archived score tables rather than banned score tables for account…
Browse files Browse the repository at this point in the history
… transfers
  • Loading branch information
Rian8337 committed Dec 12, 2024
1 parent 1bd1968 commit 09956a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/database/official/OfficialDatabaseTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ config();
export enum OfficialDatabaseTables {
user = "user",
score = "score",
archivedScore = "score_archived",
bestScore = "score_best",
archivedBestScore = "score_best_archived",
bannedScore = "score_banned",
bestBannedScore = "score_best_banned",
}
Expand Down
46 changes: 32 additions & 14 deletions src/performAccountTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ DatabaseManager.init().then(async () => {
OfficialDatabaseTables.bannedScore,
);

const archivedScoreTable = constructOfficialDatabaseTable(
OfficialDatabaseTables.archivedScore,
);

const bestScoreTable = constructOfficialDatabaseTable(
OfficialDatabaseTables.bestScore,
);
Expand All @@ -99,6 +103,10 @@ DatabaseManager.init().then(async () => {
OfficialDatabaseTables.bestBannedScore,
);

const archivedBestScoreTable = constructOfficialDatabaseTable(
OfficialDatabaseTables.archivedBestScore,
);

console.log("Transfering", transfers.size, "accounts.");

for (const transfer of transfers.values()) {
Expand Down Expand Up @@ -126,7 +134,7 @@ DatabaseManager.init().then(async () => {
const targetUidScores = await getScores(transfer.transferUid);
const uidTransferScores = await getScores(uidToTransfer);

const scoreIdsToBan: number[] = [];
const scoreIdsToArchive: number[] = [];

for (const [hash, transferScore] of uidTransferScores) {
const targetScore = targetUidScores.get(hash);
Expand All @@ -136,11 +144,11 @@ DatabaseManager.init().then(async () => {
}

if (targetScore.score > transferScore.score) {
// The target uid's score is better, so we move the transfer score to the banned table.
scoreIdsToBan.push(transferScore.id);
// The target uid's score is better, so we move the transfer score to the archived table.
scoreIdsToArchive.push(transferScore.id);
} else {
// The transfer score is better, so we move the target uid's score to the banned table.
scoreIdsToBan.push(targetScore.id);
// The transfer score is better, so we move the target uid's score to the archived table.
scoreIdsToArchive.push(targetScore.id);
}
}

Expand All @@ -149,9 +157,9 @@ DatabaseManager.init().then(async () => {
try {
await connection.beginTransaction();

for (const scoreId of scoreIdsToBan) {
for (const scoreId of scoreIdsToArchive) {
await connection.query(
`INSERT INTO ${bannedScoreTable} SELECT * FROM ${scoreTable} WHERE id = ?`,
`INSERT INTO ${archivedScoreTable} SELECT * FROM ${scoreTable} WHERE id = ?`,
[scoreId],
);

Expand All @@ -176,7 +184,7 @@ DatabaseManager.init().then(async () => {
);
const uidTransferBestScores = await getBestScores(uidToTransfer);

scoreIdsToBan.length = 0;
scoreIdsToArchive.length = 0;

for (const [hash, transferScore] of uidTransferBestScores) {
const targetScore = targetUidBestScores.get(hash);
Expand All @@ -186,11 +194,11 @@ DatabaseManager.init().then(async () => {
}

if (targetScore.pp > transferScore.pp) {
// The target uid's score is better, so we move the transfer score to the banned table.
scoreIdsToBan.push(transferScore.id);
// The target uid's score is better, so we move the transfer score to the archived table.
scoreIdsToArchive.push(transferScore.id);
} else {
// The transfer score is better, so we move the target uid's score to the banned table.
scoreIdsToBan.push(targetScore.id);
// The transfer score is better, so we move the target uid's score to the archived table.
scoreIdsToArchive.push(targetScore.id);
}
}

Expand All @@ -199,9 +207,9 @@ DatabaseManager.init().then(async () => {
try {
await connection.beginTransaction();

for (const scoreId of scoreIdsToBan) {
for (const scoreId of scoreIdsToArchive) {
await connection.query(
`INSERT INTO ${bestBannedScoreTable} SELECT * FROM ${bestScoreTable} WHERE id = ?`,
`INSERT INTO ${archivedBestScoreTable} SELECT * FROM ${bestScoreTable} WHERE id = ?`,
[scoreId],
);

Expand Down Expand Up @@ -236,11 +244,21 @@ DatabaseManager.init().then(async () => {
[transfer.transferUid, uidToTransfer],
);

await connection.query(
`UPDATE ${archivedScoreTable} SET uid = ? WHERE uid = ?`,
[transfer.transferUid, uidToTransfer],
);

await connection.query(
`UPDATE ${bestScoreTable} SET uid = ? WHERE uid = ?`,
[transfer.transferUid, uidToTransfer],
);

await connection.query(
`UPDATE ${archivedBestScoreTable} SET uid = ? WHERE uid = ?`,
[transfer.transferUid, uidToTransfer],
);

await connection.query(
`UPDATE ${bestBannedScoreTable} SET uid = ? WHERE uid = ?`,
[transfer.transferUid, uidToTransfer],
Expand Down

0 comments on commit 09956a0

Please sign in to comment.