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';
|
import { DailyCron } from '../utils/cron';
|
||||||
|
|
||||||
class Ranks {
|
class Ranks {
|
||||||
ranks; // Array
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.updateRanking = this.updateRanking.bind(this);
|
|
||||||
this.resetDailyRanking = this.resetDailyRanking.bind(this);
|
this.resetDailyRanking = this.resetDailyRanking.bind(this);
|
||||||
this.prevTop = [];
|
|
||||||
this.ranks = {
|
this.ranks = {
|
||||||
dailyRanking: [],
|
dailyRanking: [],
|
||||||
ranking: [],
|
ranking: [],
|
||||||
|
prevTop: [],
|
||||||
};
|
};
|
||||||
|
/*
|
||||||
|
* we go through socketEvents for sharding
|
||||||
|
*/
|
||||||
|
socketEvents.on('rankingListUpdate', (rankings) => {
|
||||||
|
this.ranks = {
|
||||||
|
...this.ranks,
|
||||||
|
...rankings,
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async initialize() {
|
async initialize() {
|
||||||
this.prevTop = await loadDailyTop();
|
this.ranks.prevTop = await loadDailyTop();
|
||||||
await this.updateRanking();
|
await Ranks.updateRanking();
|
||||||
setInterval(this.updateRanking, 1 * MINUTE);
|
setInterval(Ranks.updateRanking, 5 * MINUTE);
|
||||||
DailyCron.hook(this.resetDailyRanking);
|
DailyCron.hook(this.resetDailyRanking);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,11 +80,13 @@ class Ranks {
|
||||||
return rawRanks;
|
return rawRanks;
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateRanking() {
|
static async updateRanking() {
|
||||||
if (socketEvents.amIImportant()) {
|
/*
|
||||||
// TODO do this only in main shard
|
* only main shard updates and sends it to others
|
||||||
|
*/
|
||||||
|
if (!socketEvents.amIImportant()) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// populate dictionaries
|
|
||||||
const ranking = await Ranks.populateRanking(
|
const ranking = await Ranks.populateRanking(
|
||||||
await getRanks(
|
await getRanks(
|
||||||
false,
|
false,
|
||||||
|
@ -91,18 +99,21 @@ class Ranks {
|
||||||
1,
|
1,
|
||||||
100,
|
100,
|
||||||
));
|
));
|
||||||
this.ranks.ranking = ranking;
|
socketEvents.rankingListUpdate({ ranking, dailyRanking });
|
||||||
this.ranks.dailyRanking = dailyRanking;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async resetDailyRanking() {
|
async resetDailyRanking() {
|
||||||
|
/*
|
||||||
|
* only main shard updates and sends it to others
|
||||||
|
*/
|
||||||
if (!socketEvents.amIImportant()) {
|
if (!socketEvents.amIImportant()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.prevTop = await saveDailyTop(this.ranks.dailyRanking);
|
const prevTop = await saveDailyTop(this.ranks.dailyRanking);
|
||||||
|
socketEvents.rankingListUpdate({ prevTop });
|
||||||
logger.info('Resetting Daily Ranking');
|
logger.info('Resetting Daily Ranking');
|
||||||
await resetDailyRanks();
|
await resetDailyRanks();
|
||||||
await this.updateRanking();
|
await Ranks.updateRanking();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,7 +128,7 @@ export async function drawByOffsets(
|
||||||
throw new Error(7);
|
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
|
// not in top ten
|
||||||
throw new Error(12);
|
throw new Error(12);
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,8 +70,8 @@ class MessageBroker extends SocketEvents {
|
||||||
* straight sent over websocket
|
* straight sent over websocket
|
||||||
*/
|
*/
|
||||||
if (~comma) {
|
if (~comma) {
|
||||||
console.log('CLUSTER: Broadcast', message);
|
|
||||||
const key = message.slice(message.indexOf(':') + 1, comma);
|
const key = message.slice(message.indexOf(':') + 1, comma);
|
||||||
|
console.log('CLUSTER: Broadcast', key);
|
||||||
const val = JSON.parse(message.slice(comma + 1));
|
const val = JSON.parse(message.slice(comma + 1));
|
||||||
super.emit(key, ...val);
|
super.emit(key, ...val);
|
||||||
return;
|
return;
|
||||||
|
@ -181,6 +181,9 @@ class MessageBroker extends SocketEvents {
|
||||||
this.onlineCounter = newCounter;
|
this.onlineCounter = newCounter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* intercept all events and distribute them to others
|
||||||
|
*/
|
||||||
emit(key, ...args) {
|
emit(key, ...args) {
|
||||||
super.emit(key, ...args);
|
super.emit(key, ...args);
|
||||||
if (key === 'recvChatMessage') {
|
if (key === 'recvChatMessage') {
|
||||||
|
|
|
@ -183,6 +183,18 @@ class SocketEvents extends EventEmitter {
|
||||||
this.emit('remChatChannel', userId, channelId);
|
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
|
* reload user on websocket to get changes
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue
Block a user