share ranking over shards
This commit is contained in:
parent
1dd6700834
commit
29cc314b14
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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') {
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue
Block a user