diff --git a/src/core/ChatMessageBuffer.js b/src/core/ChatMessageBuffer.js index 81e1127..5356eb5 100644 --- a/src/core/ChatMessageBuffer.js +++ b/src/core/ChatMessageBuffer.js @@ -11,12 +11,15 @@ import { Message, Channel } from '../data/sql'; const MAX_BUFFER_TIME = 120000; class ChatMessageBuffer { - constructor() { + constructor(socketEvents) { this.buffer = new Map(); this.timestamps = new Map(); this.cleanBuffer = this.cleanBuffer.bind(this); this.cleanLoop = setInterval(this.cleanBuffer, 3 * 60 * 1000); + this.addMessage = this.addMessage.bind(this); + this.socketEvents = socketEvents; + socketEvents.on('chatMessage', this.addMessage); } async getMessages(cid, limit = 30) { @@ -50,13 +53,17 @@ class ChatMessageBuffer { ); } - async addMessage( + async broadcastChatMessage( name, message, cid, uid, - flag, + flag = 'xx', + sendapi = true, ) { + if (message.length > 200) { + return; + } Message.create({ name, flag, @@ -71,6 +78,27 @@ class ChatMessageBuffer { id: cid, }, }); + /* + * goes through socket events and then comes + * back at addMessage + */ + this.socketEvents.broadcastChatMessage( + name, + message, + cid, + uid, + flag, + sendapi, + ); + } + + async addMessage( + name, + message, + cid, + uid, + flag, + ) { const messages = this.buffer.get(cid); if (messages) { messages.push([ diff --git a/src/core/ChatProvider.js b/src/core/ChatProvider.js index 4ba0592..1643eb3 100644 --- a/src/core/ChatProvider.js +++ b/src/core/ChatProvider.js @@ -74,7 +74,7 @@ export class ChatProvider { }, ]; this.mutedCountries = []; - this.chatMessageBuffer = new ChatMessageBuffer(); + this.chatMessageBuffer = new ChatMessageBuffer(socketEvents); this.clearOldMessages = this.clearOldMessages.bind(this); socketEvents.on('recvChatMessage', async (user, message, channelId) => { @@ -503,32 +503,8 @@ export class ChatProvider { return null; } - broadcastChatMessage( - name, - message, - channelId, - id, - country = 'xx', - sendapi = true, - ) { - if (message.length > 250) { - return; - } - this.chatMessageBuffer.addMessage( - name, - message, - channelId, - id, - country, - ); - socketEvents.broadcastChatMessage( - name, - message, - channelId, - id, - country, - sendapi, - ); + broadcastChatMessage(...args) { + return this.chatMessageBuffer.broadcastChatMessage(...args); } static async checkIfMuted(uid) { diff --git a/src/core/ranking.js b/src/core/ranking.js index be46ac4..30ed66f 100644 --- a/src/core/ranking.js +++ b/src/core/ranking.js @@ -33,17 +33,19 @@ class Ranks { } async updateRanking() { - logger.info('Update pixel rankings'); if (socketEvents.amIImportant()) { + logger.info('Update pixel rankings in SQL'); // recalculate ranking column await sequelize.query( // eslint-disable-next-line max-len - 'SET @r=0; UPDATE Users SET ranking= @r:= (@r + 1) ORDER BY totalPixels DESC;', + 'SET @r=0; UPDATE Users SET ranking= @r:= (@r + 1) WHERE totalPixels IS NOT NULL ORDER BY totalPixels DESC;', ); await sequelize.query( // eslint-disable-next-line max-len - 'SET @r=0; UPDATE Users SET dailyRanking= @r:= (@r + 1) ORDER BY dailyTotalPixels DESC;', + 'SET @r=0; UPDATE Users SET dailyRanking= @r:= (@r + 1) WHERE dailyTotalPixels IS NOT NULL ORDER BY dailyTotalPixels DESC;', ); + } else { + logger.info('Get pixel rankings from SQL'); } // populate dictionaries const ranking = await RegUser.findAll({ @@ -64,7 +66,9 @@ class Ranks { ], ], limit: 100, - where: { id: { [Sequelize.Op.notIn]: [51, 1] } }, + where: { + ranking: { [Sequelize.Op.not]: null }, + }, order: ['ranking'], raw: true, }); @@ -86,7 +90,9 @@ class Ranks { ], ], limit: 100, - where: { id: { [Sequelize.Op.notIn]: [51, 1] } }, + where: { + dailyRanking: { [Sequelize.Op.not]: null }, + }, order: ['dailyRanking'], raw: true, }); diff --git a/src/ssr/Main.jsx b/src/ssr/Main.jsx index 3e3afbd..6146edc 100644 --- a/src/ssr/Main.jsx +++ b/src/ssr/Main.jsx @@ -42,7 +42,7 @@ function generateMainPage(req) { const ssvR = { ...ssv, shard: (host.startsWith(`${socketEvents.thisShard}.`)) - ? '' : socketEvents.getLowestActiveShard(), + ? null : socketEvents.getLowestActiveShard(), lang: lang === 'default' ? 'en' : lang, }; const scripts = (assets[`client-${lang}`])