Skip to content

Commit

Permalink
fix: restore clan games points
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Nov 23, 2024
1 parent 5ec848d commit debbddc
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/tasks/cleanup.tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class CleanupTasksService {
@Inject(Collections.PLAYER_LINKS)
private readonly linksEntity: Collection<PlayerLinksEntity>,
@Inject(Collections.CLAN_GAMES_POINTS)
private readonly clanGamesPointsEntity: Collection<PlayerLinksEntity>,
private readonly clanGamesPointsEntity: Collection<any>,

@Inject(Tokens.CLASH_CLIENT) private readonly clashClient: ClashClient,
@Inject(Tokens.REDIS) private readonly redis: RedisClient,
Expand Down Expand Up @@ -137,4 +137,52 @@ export class CleanupTasksService {
this.logger.log('Clan games points cleanup completed');
return { status: 'success' };
}

private async onModuleInit() {
let lastCursor = null;
let loop = 0;
let hasMore = true;

do {
const cursor = this.clanGamesPointsEntity
.find({ season: '2024-11', ...(lastCursor ? { _id: { $gt: lastCursor } } : {}) })
.sort({ _id: 1 })
.limit(1000);

loop++;
hasMore = false;

for await (const doc of cursor) {
hasMore = true;
let initial = doc.initial;
lastCursor = doc._id;

const { clans } = doc;
const cleanedUpLogs = clans.filter((clan) => new Date(clan.timestamp).getDate() >= 22);

const fake = clans.reduce((total, clan) => {
if (new Date(clan.timestamp).getDate() < 22) total += clan.score;
return total;
}, 0);

if (!fake) continue;
initial = initial + fake;

await this.clanGamesPointsEntity.updateOne(
{ _id: doc._id },
{
$set: {
initial,
completedAt: null,
clans: cleanedUpLogs,
},
},
);
}

console.log(`loop: ${loop}`);
} while (hasMore);

console.log('done');
}
}

0 comments on commit debbddc

Please sign in to comment.