fix chat message storing for shards

dont show NULL in rankings
This commit is contained in:
HF 2022-09-11 12:03:18 +02:00
parent fc602934dc
commit 971531a3f6
4 changed files with 46 additions and 36 deletions

View File

@ -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([

View File

@ -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) {

View File

@ -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,
});

View File

@ -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}`])