From 5ea71d01b4cc3eec3241300359959f9111fc4cb0 Mon Sep 17 00:00:00 2001 From: HF Date: Fri, 30 Sep 2022 00:31:21 +0200 Subject: [PATCH] allow top 10 users to place on top maps, even when they are Private fix #36 --- src/core/Ranks.js | 40 +++++++++++++++++++++++----------------- src/core/draw.js | 2 +- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/core/Ranks.js b/src/core/Ranks.js index b5357c0..ee65962 100644 --- a/src/core/Ranks.js +++ b/src/core/Ranks.js @@ -44,29 +44,19 @@ class Ranks { // pixels placed by day pDailyStats: [], }; + this.prevTopIds = []; /* * we go through socketEvents for sharding */ socketEvents.on('rankingListUpdate', (rankings) => { - this.ranks = { - ...this.ranks, - ...rankings, - }; + this.mergeIntoRanks(rankings); }); } async initialize() { try { - let someRanks = await Ranks.dailyUpdateRanking(); - this.ranks = { - ...this.ranks, - ...someRanks, - }; - someRanks = await Ranks.hourlyUpdateRanking(); - this.ranks = { - ...this.ranks, - ...someRanks, - }; + this.mergeIntoRanks(await Ranks.dailyUpdateRanking()); + this.mergeIntoRanks(await Ranks.hourlyUpdateRanking()); await Ranks.updateRanking(); } catch (err) { // eslint-disable-next-line no-console @@ -77,6 +67,21 @@ class Ranks { DailyCron.hook(Ranks.setDailyRanking); } + mergeIntoRanks(newRanks) { + if (!newRanks) { + return; + } + const { prevTopIds } = newRanks; + if (prevTopIds) { + this.prevTopIds = prevTopIds; + delete newRanks.prevTopIds; + } + this.ranks = { + ...this.ranks, + ...newRanks, + }; + } + static async updateRanking() { // only main shard does it if (!socketEvents.amIImportant()) { @@ -121,9 +126,9 @@ class Ranks { } static async dailyUpdateRanking() { - const prevTop = await populateRanking( - await getPrevTop(), - ); + const prevTopData = await getPrevTop(); + const prevTopIds = prevTopData.map((d) => d.id); + const prevTop = await populateRanking(prevTopData); const pDailyStats = await getDailyPixelStats(); const histStats = await getTopDailyHistory(); histStats.users = await populateRanking(histStats.users); @@ -134,6 +139,7 @@ class Ranks { prevTop, pDailyStats, histStats, + prevTopIds, }; if (socketEvents.amIImportant()) { // only main shard sends to others diff --git a/src/core/draw.js b/src/core/draw.js index 0170a31..f082c8d 100644 --- a/src/core/draw.js +++ b/src/core/draw.js @@ -126,7 +126,7 @@ export default async function drawByOffsets( } } if (canvas.req === 'top' - && !rankings.ranks.prevTop.find((r) => r.id === user.id) + && !rankings.prevTopIds.includes(user.id) ) { // not in top ten throw new Error(12);