diff --git a/src/core/Ranks.js b/src/core/Ranks.js index 03fc283..d05b438 100644 --- a/src/core/Ranks.js +++ b/src/core/Ranks.js @@ -17,22 +17,28 @@ import { MINUTE } from './constants'; import { DailyCron } from '../utils/cron'; class Ranks { - ranks; // Array - constructor() { - this.updateRanking = this.updateRanking.bind(this); this.resetDailyRanking = this.resetDailyRanking.bind(this); - this.prevTop = []; this.ranks = { dailyRanking: [], ranking: [], + prevTop: [], }; + /* + * we go through socketEvents for sharding + */ + socketEvents.on('rankingListUpdate', (rankings) => { + this.ranks = { + ...this.ranks, + ...rankings, + }; + }); } async initialize() { - this.prevTop = await loadDailyTop(); - await this.updateRanking(); - setInterval(this.updateRanking, 1 * MINUTE); + this.ranks.prevTop = await loadDailyTop(); + await Ranks.updateRanking(); + setInterval(Ranks.updateRanking, 5 * MINUTE); DailyCron.hook(this.resetDailyRanking); } @@ -74,11 +80,13 @@ class Ranks { return rawRanks; } - async updateRanking() { - if (socketEvents.amIImportant()) { - // TODO do this only in main shard + static async updateRanking() { + /* + * only main shard updates and sends it to others + */ + if (!socketEvents.amIImportant()) { + return; } - // populate dictionaries const ranking = await Ranks.populateRanking( await getRanks( false, @@ -91,18 +99,21 @@ class Ranks { 1, 100, )); - this.ranks.ranking = ranking; - this.ranks.dailyRanking = dailyRanking; + socketEvents.rankingListUpdate({ ranking, dailyRanking }); } async resetDailyRanking() { + /* + * only main shard updates and sends it to others + */ if (!socketEvents.amIImportant()) { return; } - this.prevTop = await saveDailyTop(this.ranks.dailyRanking); + const prevTop = await saveDailyTop(this.ranks.dailyRanking); + socketEvents.rankingListUpdate({ prevTop }); logger.info('Resetting Daily Ranking'); await resetDailyRanks(); - await this.updateRanking(); + await Ranks.updateRanking(); } } diff --git a/src/core/draw.js b/src/core/draw.js index c6dfab5..d0aa44b 100644 --- a/src/core/draw.js +++ b/src/core/draw.js @@ -128,7 +128,7 @@ export async function drawByOffsets( throw new Error(7); } } - if (canvas.req === 'top' && !rankings.prevTop.includes(user.id)) { + if (canvas.req === 'top' && !rankings.ranks.prevTop.includes(user.id)) { // not in top ten throw new Error(12); } diff --git a/src/socket/MessageBroker.js b/src/socket/MessageBroker.js index 7d96142..bdddb9f 100644 --- a/src/socket/MessageBroker.js +++ b/src/socket/MessageBroker.js @@ -70,8 +70,8 @@ class MessageBroker extends SocketEvents { * straight sent over websocket */ if (~comma) { - console.log('CLUSTER: Broadcast', message); const key = message.slice(message.indexOf(':') + 1, comma); + console.log('CLUSTER: Broadcast', key); const val = JSON.parse(message.slice(comma + 1)); super.emit(key, ...val); return; @@ -181,6 +181,9 @@ class MessageBroker extends SocketEvents { this.onlineCounter = newCounter; } + /* + * intercept all events and distribute them to others + */ emit(key, ...args) { super.emit(key, ...args); if (key === 'recvChatMessage') { diff --git a/src/socket/SockEvents.js b/src/socket/SockEvents.js index 2e0a498..69ffc40 100644 --- a/src/socket/SockEvents.js +++ b/src/socket/SockEvents.js @@ -183,6 +183,18 @@ class SocketEvents extends EventEmitter { this.emit('remChatChannel', userId, channelId); } + /* + * broadcast ranking list updates + * @param { + * dailyRanking?: daily pixel raking top 100, + * ranking?: total pixel ranking top 100, + * prevTop?: top 10 of the previous day, + * } + */ + rankingListUpdate(rankings) { + this.emit('rankingListUpdate', rankings); + } + /* * reload user on websocket to get changes */