From 9f0a78f1ea1665996e57d71f13849b847bceaed0 Mon Sep 17 00:00:00 2001 From: HF Date: Fri, 6 Nov 2020 21:00:47 +0100 Subject: [PATCH] make info and event messages belong to actual user --- src/components/PasswordReset.jsx | 6 +-- src/controls/PixelPainterControls.js | 3 ++ src/core/ChatProvider.js | 55 ++++++++++++++++++++++++---- src/core/constants.js | 5 +++ src/core/event.js | 41 ++++++++++----------- src/socket/APISocketServer.js | 12 +++--- src/socket/WebSocketEvents.js | 8 +++- src/socket/websockets.js | 4 +- 8 files changed, 93 insertions(+), 41 deletions(-) diff --git a/src/components/PasswordReset.jsx b/src/components/PasswordReset.jsx index 646eb01..f1f4d6b 100644 --- a/src/components/PasswordReset.jsx +++ b/src/components/PasswordReset.jsx @@ -47,9 +47,9 @@ export default function getPasswordResetHtml(name, code, message = null) { : ; const index = `${ ReactDOM.renderToStaticMarkup()}`; return index; } diff --git a/src/controls/PixelPainterControls.js b/src/controls/PixelPainterControls.js index 3b60bf1..b6cf147 100644 --- a/src/controls/PixelPainterControls.js +++ b/src/controls/PixelPainterControls.js @@ -181,6 +181,7 @@ class PixelPlainterControls { onTouchStart(event: TouchEvent) { event.preventDefault(); + event.stopPropagation(); document.activeElement.blur(); this.clickTabStartTime = Date.now(); @@ -212,6 +213,7 @@ class PixelPlainterControls { onTouchEnd(event: TouchEvent) { event.preventDefault(); + event.stopPropagation(); this.clearTabTimeout(); if (event.changedTouches.length < 2) { @@ -240,6 +242,7 @@ class PixelPlainterControls { onTouchMove(event: TouchEvent) { event.preventDefault(); + event.stopPropagation(); const multiTouch = (event.touches.length > 1); diff --git a/src/core/ChatProvider.js b/src/core/ChatProvider.js index 5b9b054..955a40d 100644 --- a/src/core/ChatProvider.js +++ b/src/core/ChatProvider.js @@ -5,16 +5,19 @@ import logger from './logger'; import redis from '../data/redis'; import User from '../data/models/User'; import webSockets from '../socket/websockets'; -import { Channel } from '../data/models'; +import { Channel, RegUser } from '../data/models'; import ChatMessageBuffer from './ChatMessageBuffer'; -import { CHAT_CHANNELS } from './constants'; +import { CHAT_CHANNELS, EVENT_USER_NAME, INFO_USER_NAME } from './constants'; export class ChatProvider { constructor() { this.defaultChannels = []; this.defaultChannelIds = []; + this.enChannelId = 0; this.intChannelId = 0; + this.infoUserId = 1; + this.eventUserId = 1; this.caseCheck = /^[A-Z !.]*$/; this.cyrillic = new RegExp('[\u0436-\u043B]'); this.filters = [ @@ -68,12 +71,44 @@ export class ChatProvider { if (name === 'int') { this.intChannelId = id; } + if (name === 'en') { + this.enChannelId = id; + } this.defaultChannels.push([ id, name, ]); this.defaultChannelIds.push(id); } + // find or create default users + let name = INFO_USER_NAME; + const infoUser = await RegUser.findOrCreate({ + attributes: [ + 'id', + ], + where: { name }, + defaults: { + name, + verified: 3, + email: 'info@example.com', + }, + raw: true, + }); + [this.infoUserId] = infoUser; + name = EVENT_USER_NAME; + const eventUser = await RegUser.findOrCreate({ + attributes: [ + 'id', + ], + where: { name }, + defaults: { + name, + verified: 3, + email: 'event@example.com', + }, + raw: true, + }); + [this.eventUserId] = eventUser; } userHasChannelAccess(user, cid, write = false) { @@ -181,6 +216,7 @@ export class ChatProvider { 'info', `Country ${cc} has been muted`, channelId, + this.infoUserId, ); return null; } if (cmd === 'unmutec' && args[0]) { @@ -193,6 +229,7 @@ export class ChatProvider { 'info', `Country ${cc} has been unmuted`, channelId, + this.infoUserId, ); return null; } @@ -226,8 +263,8 @@ export class ChatProvider { broadcastChatMessage( name, message, - channelId: number = 1, - id = 1, + channelId, + id, country: string = 'xx', sendapi: boolean = true, ) { @@ -251,12 +288,13 @@ export class ChatProvider { ); } - static automute(name, channelId = 1) { + static automute(name, channelId) { ChatProvider.mute(name, channelId, 60); webSockets.broadcastChatMessage( 'info', `${name} has been muted for spam for 60min`, channelId, + this.infoUserId, ); } @@ -266,7 +304,7 @@ export class ChatProvider { return ttl; } - static async mute(plainName, channelId = 1, timeMin = null) { + static async mute(plainName, channelId, timeMin = null) { const name = (plainName.startsWith('@')) ? plainName.substr(1) : plainName; const id = await User.name2Id(name); if (!id) { @@ -281,6 +319,7 @@ export class ChatProvider { 'info', `${name} has been muted for ${timeMin}min`, channelId, + this.infoUserId, ); } } else { @@ -289,13 +328,14 @@ export class ChatProvider { 'info', `${name} has been muted forever`, channelId, + this.infoUserId, ); } logger.info(`Muted user ${id}`); return null; } - static async unmute(plainName, channelId = 1) { + static async unmute(plainName, channelId) { const name = (plainName.startsWith('@')) ? plainName.substr(1) : plainName; const id = await User.name2Id(name); if (!id) { @@ -310,6 +350,7 @@ export class ChatProvider { 'info', `${name} has been unmuted`, channelId, + this.infoUserId, ); logger.info(`Unmuted user ${id}`); return null; diff --git a/src/core/constants.js b/src/core/constants.js index 551d7a6..ed2f858 100644 --- a/src/core/constants.js +++ b/src/core/constants.js @@ -90,6 +90,8 @@ export const DAY = 24 * HOUR; export const MONTH = 30 * DAY; // available public Chat Channels +// at least 'en' and 'int' have to be present, +// as they get used in core/ChatProvider export const CHAT_CHANNELS = [ { name: 'en', @@ -99,3 +101,6 @@ export const CHAT_CHANNELS = [ ]; export const MAX_CHAT_MESSAGES = 100; + +export const EVENT_USER_NAME = 'event'; +export const INFO_USER_NAME = 'info'; diff --git a/src/core/event.js b/src/core/event.js index 7e19d43..bc0469c 100644 --- a/src/core/event.js +++ b/src/core/event.js @@ -18,7 +18,7 @@ import { import Void from './Void'; import { protectCanvasArea } from './Image'; import { setPixelByOffset } from './setPixel'; -import { TILE_SIZE } from './constants'; +import { TILE_SIZE, EVENT_USER_NAME } from './constants'; import chatProvider from './ChatProvider'; import { HOURLY_EVENT } from './config'; // eslint-disable-next-line import/no-unresolved @@ -147,8 +147,7 @@ class Event { const timestamp = await nextEvent(); const x = i * TILE_SIZE - canvasSize / 2; const y = j * TILE_SIZE - canvasSize / 2; - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( `Suspicious activity spotted in ${Event.getDirection(x, y)}`, ); drawCross([i, j], 19, 0, 13); @@ -156,6 +155,15 @@ class Event { return timestamp; } + static broadcastChatMessage(message) { + chatProvider.broadcastChatMessage( + EVENT_USER_NAME, + message, + chatProvider.enChannelId, + chatProvider.eventUserId, + ); + } + async runEventLoop() { const { eventState, @@ -181,8 +189,7 @@ class Event { if (eventState !== 3 && eventState !== 4) { this.eventState = 3; const [x, y] = this.eventArea; - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( `Unstable area at ${Event.getDirection(x, y)} at concerning level`, ); } @@ -215,8 +222,7 @@ class Event { const rand = Math.random() * 3000 - 500; return Math.floor(z + TILE_SIZE * 1.5 + rand); }); - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( `Alert! Threat is rising in 2min near #d,${xNear},${yNear},30`, ); } @@ -232,8 +238,7 @@ class Event { // 1min till Event: blinking solid cross red small fase if (eventState !== 9 && eventState !== 10) { this.eventState = 9; - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( 'Alert! Danger!', ); } @@ -251,8 +256,7 @@ class Event { const [x, y, w, h] = this.eventArea; await protectCanvasArea(CANVAS_ID, x, y, w, h, false); logger.info(`Starting Event at ${x},${y} now`); - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( 'Fight starting!', ); this.void = new Void(this.eventCenterC); @@ -262,8 +266,7 @@ class Event { if (percent === 100) { // event lost logger.info(`Event got lost after ${Math.abs(eventMinutes)} min`); - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( 'Threat couldn\'t be contained, abandon area', ); this.success = 2; @@ -274,8 +277,7 @@ class Event { } else { const now = Date.now(); if (now > this.chatTimeout) { - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( `Clown Void reached ${percent}% of its max size`, ); this.chatTimeout = now + 40000; @@ -292,8 +294,7 @@ class Event { if (this.void.checkStatus() !== 100) { // event won logger.info('Event got won! Cooldown sitewide now half.'); - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( 'Threat successfully defeated. Good work!', ); this.success = 1; @@ -312,8 +313,7 @@ class Event { // 5min after last Event // end debuff if lost if (this.success === 2) { - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( 'Void seems to leave again.', ); this.success = 0; @@ -330,8 +330,7 @@ class Event { logger.info('Restoring old event area'); await clearOldEvent(); if (this.success === 1) { - chatProvider.broadcastChatMessage( - 'event', + Event.broadcastChatMessage( 'Celebration time over, get back to work.', ); this.success = 0; diff --git a/src/socket/APISocketServer.js b/src/socket/APISocketServer.js index a003b6c..19528c7 100644 --- a/src/socket/APISocketServer.js +++ b/src/socket/APISocketServer.js @@ -253,16 +253,16 @@ class APISocketServer extends WebSocketEvents { chatProvider.broadcastChatMessage( chatname, msg, - 1, - 1, + chatProvider.enChannelId, + chatProvider.infoUserId, 'xx', false, ); this.broadcastChatMessage( chatname, msg, - 1, - 1, + chatProvider.enChannelId, + chatProvider.infoUserId, 'xx', true, ws, @@ -275,7 +275,7 @@ class APISocketServer extends WebSocketEvents { name, msg, channelId, - 1, + chatProvider.infoUserId, country, false, ); @@ -283,7 +283,7 @@ class APISocketServer extends WebSocketEvents { name, msg, channelId, - 1, + chatProvider.infoUserId, country, true, ws, diff --git a/src/socket/WebSocketEvents.js b/src/socket/WebSocketEvents.js index 3e07b85..55a4827 100644 --- a/src/socket/WebSocketEvents.js +++ b/src/socket/WebSocketEvents.js @@ -14,8 +14,12 @@ class WebSocketEvents { broadcastPixelBuffer(canvasId: number, chunkid: number, buffer: Buffer) { } - broadcastChatMessage(name: string, message: string, channelId: number) { - } + broadcastChatMessage( + name: string, + message: string, + channelId: number, + userId: number, + ) {} broadcastMinecraftLink(name: string, minecraftid: string, accepted: boolean) { } diff --git a/src/socket/websockets.js b/src/socket/websockets.js index d198d29..fe1eadb 100644 --- a/src/socket/websockets.js +++ b/src/socket/websockets.js @@ -71,8 +71,8 @@ class WebSockets { broadcastChatMessage( name: string, message: string, - channelId: number = 1, - id: number = 1, + channelId: number, + id: number, country: string = 'xx', sendapi: boolean = true, ) {