Replaced all event names with numbers for less bandwidth usage

This commit is contained in:
Dennis Postma 2025-02-11 23:12:41 +01:00
parent 8b51f6e16a
commit 9e55ac7990
57 changed files with 193 additions and 98 deletions

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { Server } from 'socket.io' import { Server } from 'socket.io'
import type { TSocket } from '#application/types' import type { TSocket } from '#application/types'
@ -39,14 +40,14 @@ export abstract class BaseEvent {
} }
protected emitError(message: string): void { protected emitError(message: string): void {
this.socket.emit('notification', { title: 'Server message', message }) this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Server message', message })
this.logger.error('Base event error', `Player ${this.socket.userId}: ${message}`) this.logger.error('Base event error', `Player ${this.socket.userId}: ${message}`)
} }
protected handleError(context: string, error: unknown): void { protected handleError(context: string, error: unknown): void {
console.log(error) console.log(error)
const errorMessage = error instanceof Error ? error.message : error && typeof error === 'object' && 'toString' in error ? error.toString() : String(error) const errorMessage = error instanceof Error ? error.message : error && typeof error === 'object' && 'toString' in error ? error.toString() : String(error)
this.socket.emit('notification', { title: 'Server message', message: `Server error occured. Please contact the server administrator.` }) this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Server message', message: `Server error occured. Please contact the server administrator.` })
this.logger.error('Base event error', errorMessage) this.logger.error('Base event error', errorMessage)
} }
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import * as readline from 'readline' import * as readline from 'readline'
export class ConsolePrompt { export class ConsolePrompt {
@ -10,7 +11,7 @@ export class ConsolePrompt {
output: process.stdout output: process.stdout
}) })
this.rl.on('close', () => { this.rl.on(SocketEvent.CLOSE, () => {
this.isClosed = true this.isClosed = true
}) })
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import * as fs from 'fs' import * as fs from 'fs'
import * as path from 'path' import * as path from 'path'
@ -60,7 +61,7 @@ export class LogReader {
end: newPosition end: newPosition
}) })
stream.on('data', (data) => { stream.on(SocketEvent.DATA, (data) => {
console.log(`[${filename}]`) console.log(`[${filename}]`)
console.log(data.toString()) // console.log(data.toString()) //
}) })

View File

@ -1,17 +1,57 @@
export enum SocketEvent { export enum SocketEvent {
CHARACTER_CONNECT = 1, CLOSE = '52',
CHARACTER_MOVE = 2, DATA = '51',
CHARACTER_MOVE_ERROR = 3, CHARACTER_CONNECT = '50',
CHARACTER_TELEPORT = 4, CHARACTER_CREATE = '49',
MAP_CHARACTER_LEAVE = 5, CHARACTER_DELETE = '48',
MAP_CHARACTER_JOIN = 6, CHARACTER_LIST = '47',
MAP_CHARACTER_LIST = 7, GM_CHARACTERHAIR_CREATE = '46',
MAP_CHARACTER_DELETE = 8, GM_CHARACTERHAIR_REMOVE = '45',
MAP_CHARACTER_CREATE = 9, GM_CHARACTERHAIR_LIST = '44',
MAP_CHARACTER_UPDATE = 10, GM_CHARACTERHAIR_UPDATE = '43',
MAP_CHARACTER_HAIR_UPDATE = 11, GM_CHARACTERTYPE_CREATE = '42',
MAP_CHARACTER_HAIR_LIST = 12, GM_CHARACTERTYPE_REMOVE = '41',
MAP_CHARACTER_TELEPORT = 13 GM_CHARACTERTYPE_LIST = '40',
GM_CHARACTERTYPE_UPDATE = '39',
GM_ITEM_CREATE = '38',
GM_ITEM_REMOVE = '37',
GM_ITEM_LIST = '36',
GM_ITEM_UPDATE = '35',
GM_MAPOBJECT_LIST = '34',
GM_MAPOBJECT_REMOVE = '33',
GM_MAPOBJECT_UPDATE = '32',
GM_MAPOBJECT_UPLOAD = '31',
GM_SPRITE_COPY = '30',
GM_SPRITE_CREATE = '29',
GM_SPRITE_DELETE = '28',
GM_SPRITE_LIST = '27',
GM_SPRITE_UPDATE = '26',
GM_TILE_DELETE = '25',
GM_TILE_LIST = '24',
GM_TILE_UPDATE = '23',
GM_TILE_UPLOAD = '22',
GM_MAP_CREATE = '21',
GM_MAP_DELETE = '20',
GM_MAP_REQUEST = '19',
GM_MAP_UPDATE = '18',
MAP_CHARACTER_MOVEERROR = '17',
DISCONNECT = '16',
USER_DISCONNECT = '15',
LOGIN = '14',
LOGGED_IN = '13',
NOTIFICATION = '12',
DATE = '11',
FAILED = '10',
COMPLETED = '9',
CONNECTION = '8',
WEATHER = '7',
CHARACTER_DISCONNECT = '6',
MAP_CHARACTER_ATTACK = '5',
MAP_CHARACTER_TELEPORT = '4',
MAP_CHARACTER_JOIN = '3',
MAP_CHARACTER_LEAVE = '2',
MAP_CHARACTER_MOVE = '1',
CHAT_MESSAGE = '0',
} }
export enum ItemType { export enum ItemType {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { Server } from 'socket.io' import { Server } from 'socket.io'
import { BaseCommand } from '#application/base/baseCommand' import { BaseCommand } from '#application/base/baseCommand'
@ -8,6 +9,6 @@ export default class AlertCommand extends BaseCommand {
public execute(input: CommandInput): void { public execute(input: CommandInput): void {
const message: string = input.join(' ') ?? null const message: string = input.join(' ') ?? null
if (!message) return console.log('message is required') if (!message) return console.log('message is required')
this.io.emit('notification', { message: message }) this.io.emit(SocketEvent.NOTIFICATION, { message: message })
} }
} }

View File

@ -1,4 +1,4 @@
import fs from 'fs' wimport fs from 'fs'
import sharp from 'sharp' import sharp from 'sharp'

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -16,7 +17,7 @@ export default class CharacterConnectEvent extends BaseEvent {
private readonly characterRepository = new CharacterRepository() private readonly characterRepository = new CharacterRepository()
public listen(): void { public listen(): void {
this.socket.on('character:connect', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHARACTER_CONNECT, this.handleEvent.bind(this))
} }
private async handleEvent(data: CharacterConnectPayload, callback: (response: any) => void): Promise<void> { private async handleEvent(data: CharacterConnectPayload, callback: (response: any) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { ZodError, z } from 'zod' import { ZodError, z } from 'zod'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -17,7 +18,7 @@ export default class CharacterCreateEvent extends BaseEvent {
private readonly mapRepository: MapRepository = new MapRepository() private readonly mapRepository: MapRepository = new MapRepository()
public listen(): void { public listen(): void {
this.socket.on('character:create', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHARACTER_CREATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: z.infer<typeof ZCharacterCreate>, callback: (success: boolean) => void): Promise<void> { private async handleEvent(data: z.infer<typeof ZCharacterCreate>, callback: (success: boolean) => void): Promise<void> {
@ -61,7 +62,7 @@ export default class CharacterCreateEvent extends BaseEvent {
await newCharacter.setName(data.name).setUser(user).setMap(map).setCharacterType(characterType).save() await newCharacter.setName(data.name).setUser(user).setMap(map).setCharacterType(characterType).save()
characters = await this.characterRepository.getByUserId(user.getId()) characters = await this.characterRepository.getByUserId(user.getId())
this.socket.emit('character:list', characters) this.socket.emit(SocketEvent.CHARACTER_LIST, characters)
this.logger.info('character:create success') this.logger.info('character:create success')
} }
@ -76,7 +77,7 @@ export default class CharacterCreateEvent extends BaseEvent {
errorMessage = error.message errorMessage = error.message
} }
this.socket.emit('notification', { this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Error', title: 'Error',
message: errorMessage message: errorMessage
}) })

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -14,7 +15,7 @@ type TypeResponse = {
export default class CharacterDeleteEvent extends BaseEvent { export default class CharacterDeleteEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('character:delete', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHARACTER_DELETE, this.handleEvent.bind(this))
} }
private async handleEvent(data: TypePayload, callback: (response: TypeResponse) => void): Promise<any> { private async handleEvent(data: TypePayload, callback: (response: TypeResponse) => void): Promise<any> {
@ -23,9 +24,9 @@ export default class CharacterDeleteEvent extends BaseEvent {
await (await characterRepository.getByUserAndId(this.socket.userId!, data.characterId))?.delete() await (await characterRepository.getByUserAndId(this.socket.userId!, data.characterId))?.delete()
const characters: Character[] = await characterRepository.getByUserId(this.socket.userId!) const characters: Character[] = await characterRepository.getByUserId(this.socket.userId!)
this.socket.emit('character:list', characters) this.socket.emit(SocketEvent.CHARACTER_LIST, characters)
} catch (error: any) { } catch (error: any) {
return this.socket.emit('notification', { message: 'Character delete failed. Please try again.' }) return this.socket.emit(SocketEvent.NOTIFICATION, { message: 'Character delete failed. Please try again.' })
} }
} }
} }

View File

@ -1,10 +1,11 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { Character } from '#entities/character' import { Character } from '#entities/character'
import CharacterRepository from '#repositories/characterRepository' import CharacterRepository from '#repositories/characterRepository'
export default class CharacterListEvent extends BaseEvent { export default class CharacterListEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('character:list', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHARACTER_LIST, this.handleEvent.bind(this))
} }
private async handleEvent(data: any): Promise<void> { private async handleEvent(data: any): Promise<void> {
@ -12,7 +13,7 @@ export default class CharacterListEvent extends BaseEvent {
const characterRepository = new CharacterRepository() const characterRepository = new CharacterRepository()
let characters: Character[] = await characterRepository.getByUserId(this.socket.userId!) let characters: Character[] = await characterRepository.getByUserId(this.socket.userId!)
this.socket.emit('character:list', characters) this.socket.emit(SocketEvent.CHARACTER_LIST, characters)
} catch (error: any) { } catch (error: any) {
this.logger.error('character:list error', error.message) this.logger.error('character:list error', error.message)
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import CharacterRepository from '#repositories/characterRepository' import CharacterRepository from '#repositories/characterRepository'
import ChatService from '#services/chatService' import ChatService from '#services/chatService'
@ -8,7 +9,7 @@ type TypePayload = {
export default class AlertCommandEvent extends BaseEvent { export default class AlertCommandEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('chat:message', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHAT_MESSAGE, this.handleEvent.bind(this))
} }
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
@ -25,7 +26,7 @@ export default class AlertCommandEvent extends BaseEvent {
return callback(false) return callback(false)
} }
this.io.emit('notification', { title: 'Message from GM', message: args.join(' ') }) this.io.emit(SocketEvent.NOTIFICATION, { title: 'Message from GM', message: args.join(' ') })
return callback(true) return callback(true)
} catch (error: any) { } catch (error: any) {
this.logger.error('chat:alert_command error', error.message) this.logger.error('chat:alert_command error', error.message)

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import DateManager from '#managers/dateManager' import DateManager from '#managers/dateManager'
import CharacterRepository from '#repositories/characterRepository' import CharacterRepository from '#repositories/characterRepository'
@ -9,7 +10,7 @@ type TypePayload = {
export default class SetTimeCommand extends BaseEvent { export default class SetTimeCommand extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('chat:message', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHAT_MESSAGE, this.handleEvent.bind(this))
} }
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -12,7 +13,7 @@ type TypePayload = {
export default class TeleportCommandEvent extends BaseEvent { export default class TeleportCommandEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('chat:message', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHAT_MESSAGE, this.handleEvent.bind(this))
} }
private async handleEvent(data: TypePayload, callback: (response: boolean) => void) { private async handleEvent(data: TypePayload, callback: (response: boolean) => void) {
@ -29,7 +30,7 @@ export default class TeleportCommandEvent extends BaseEvent {
const args = ChatService.getArgs('teleport', data.message) const args = ChatService.getArgs('teleport', data.message)
if (!args || args.length === 0 || args.length > 3) { if (!args || args.length === 0 || args.length > 3) {
this.socket.emit('notification', { this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message', title: 'Server message',
message: 'Usage: /teleport <mapId> [x] [y]' message: 'Usage: /teleport <mapId> [x] [y]'
}) })
@ -41,7 +42,7 @@ export default class TeleportCommandEvent extends BaseEvent {
const targetY = args[2] ? parseInt(args[2], 10) : 0 const targetY = args[2] ? parseInt(args[2], 10) : 0
if (!mapId || isNaN(targetX) || isNaN(targetY)) { if (!mapId || isNaN(targetX) || isNaN(targetY)) {
this.socket.emit('notification', { this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message', title: 'Server message',
message: 'Invalid parameters. X and Y coordinates must be numbers.' message: 'Invalid parameters. X and Y coordinates must be numbers.'
}) })
@ -51,7 +52,7 @@ export default class TeleportCommandEvent extends BaseEvent {
const mapRepository = new MapRepository() const mapRepository = new MapRepository()
const map = await mapRepository.getById(mapId) const map = await mapRepository.getById(mapId)
if (!map) { if (!map) {
this.socket.emit('notification', { this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message', title: 'Server message',
message: 'Map not found' message: 'Map not found'
}) })
@ -59,7 +60,7 @@ export default class TeleportCommandEvent extends BaseEvent {
} }
if (character.map.id === map.id && targetX === character.positionX && targetY === character.positionY) { if (character.map.id === map.id && targetX === character.positionX && targetY === character.positionY) {
this.socket.emit('notification', { this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message', title: 'Server message',
message: 'You are already at that location' message: 'You are already at that location'
}) })
@ -74,20 +75,20 @@ export default class TeleportCommandEvent extends BaseEvent {
}) })
if (!success) { if (!success) {
return this.socket.emit('notification', { return this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message', title: 'Server message',
message: 'Failed to teleport' message: 'Failed to teleport'
}) })
} }
this.socket.emit('notification', { this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message', title: 'Server message',
message: `Teleported to ${map.name} (${targetX}, ${targetY})` message: `Teleported to ${map.name} (${targetX}, ${targetY})`
}) })
this.logger.info('teleport', `Character ${character.id} teleported to map ${map.id} at position (${targetX}, ${targetY})`) this.logger.info('teleport', `Character ${character.id} teleported to map ${map.id} at position (${targetX}, ${targetY})`)
} catch (error: any) { } catch (error: any) {
this.logger.error(`Error in teleport command: ${error.message}`) this.logger.error(`Error in teleport command: ${error.message}`)
this.socket.emit('notification', { this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message', title: 'Server message',
message: 'An error occurred while teleporting' message: 'An error occurred while teleporting'
}) })

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import WeatherManager from '#managers/weatherManager' import WeatherManager from '#managers/weatherManager'
import ChatService from '#services/chatService' import ChatService from '#services/chatService'
@ -8,7 +9,7 @@ type TypePayload = {
export default class ToggleFogCommand extends BaseEvent { export default class ToggleFogCommand extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('chat:message', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHAT_MESSAGE, this.handleEvent.bind(this))
} }
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import WeatherManager from '#managers/weatherManager' import WeatherManager from '#managers/weatherManager'
import ChatService from '#services/chatService' import ChatService from '#services/chatService'
@ -8,7 +9,7 @@ type TypePayload = {
export default class ToggleRainCommand extends BaseEvent { export default class ToggleRainCommand extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('chat:message', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHAT_MESSAGE, this.handleEvent.bind(this))
} }
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import MapManager from '#managers/mapManager' import MapManager from '#managers/mapManager'
import MapRepository from '#repositories/mapRepository' import MapRepository from '#repositories/mapRepository'
@ -9,7 +10,7 @@ type TypePayload = {
export default class ChatMessageEvent extends BaseEvent { export default class ChatMessageEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('chat:message', this.handleEvent.bind(this)) this.socket.on(SocketEvent.CHAT_MESSAGE, this.handleEvent.bind(this))
} }
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,9 +1,10 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import MapManager from '#managers/mapManager' import MapManager from '#managers/mapManager'
export default class DisconnectEvent extends BaseEvent { export default class DisconnectEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('disconnect', this.handleEvent.bind(this)) this.socket.on(SocketEvent.DISCONNECT, this.handleEvent.bind(this))
} }
private async handleEvent(): Promise<void> { private async handleEvent(): Promise<void> {
@ -13,7 +14,7 @@ export default class DisconnectEvent extends BaseEvent {
return return
} }
this.io.emit('user:disconnect', this.socket.userId) this.io.emit(SocketEvent.USER_DISCONNECT, this.socket.userId)
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!) const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
if (!mapCharacter) { if (!mapCharacter) {

View File

@ -1,9 +1,10 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { CharacterHair } from '#entities/characterHair' import { CharacterHair } from '#entities/characterHair'
export default class CharacterHairCreateEvent extends BaseEvent { export default class CharacterHairCreateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterHair:create', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERHAIR_CREATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: undefined, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: undefined, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -9,7 +10,7 @@ interface IPayload {
export default class CharacterHairDeleteEvent extends BaseEvent { export default class CharacterHairDeleteEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterHair:remove', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERHAIR_REMOVE, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { CharacterHair } from '#entities/characterHair' import { CharacterHair } from '#entities/characterHair'
import CharacterHairRepository from '#repositories/characterHairRepository' import CharacterHairRepository from '#repositories/characterHairRepository'
@ -6,7 +7,7 @@ interface IPayload {}
export default class characterHairListEvent extends BaseEvent { export default class characterHairListEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterHair:list', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERHAIR_LIST, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: CharacterHair[]) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: CharacterHair[]) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -15,7 +16,7 @@ type Payload = {
export default class CharacterHairUpdateEvent extends BaseEvent { export default class CharacterHairUpdateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterHair:update', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERHAIR_UPDATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> {

View File

@ -1,9 +1,10 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { CharacterType } from '#entities/characterType' import { CharacterType } from '#entities/characterType'
export default class CharacterTypeCreateEvent extends BaseEvent { export default class CharacterTypeCreateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterType:create', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERTYPE_CREATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: undefined, callback: (response: boolean, characterType?: any) => void): Promise<void> { private async handleEvent(data: undefined, callback: (response: boolean, characterType?: any) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -9,7 +10,7 @@ interface IPayload {
export default class CharacterTypeDeleteEvent extends BaseEvent { export default class CharacterTypeDeleteEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterType:remove', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERTYPE_REMOVE, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { CharacterType } from '#entities/characterType' import { CharacterType } from '#entities/characterType'
import CharacterTypeRepository from '#repositories/characterTypeRepository' import CharacterTypeRepository from '#repositories/characterTypeRepository'
@ -6,7 +7,7 @@ interface IPayload {}
export default class CharacterTypeListEvent extends BaseEvent { export default class CharacterTypeListEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterType:list', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERTYPE_LIST, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: CharacterType[]) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: CharacterType[]) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -16,7 +17,7 @@ type Payload = {
export default class CharacterTypeUpdateEvent extends BaseEvent { export default class CharacterTypeUpdateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:characterType:update', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_CHARACTERTYPE_UPDATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { ItemRarity, ItemType } from '#application/enums' import { ItemRarity, ItemType } from '#application/enums'
import { Item } from '#entities/item' import { Item } from '#entities/item'
@ -5,7 +6,7 @@ import SpriteRepository from '#repositories/spriteRepository'
export default class ItemCreateEvent extends BaseEvent { export default class ItemCreateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:item:create', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_ITEM_CREATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: undefined, callback: (response: boolean, item?: any) => void): Promise<void> { private async handleEvent(data: undefined, callback: (response: boolean, item?: any) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -9,7 +10,7 @@ interface IPayload {
export default class ItemDeleteEvent extends BaseEvent { export default class ItemDeleteEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:item:remove', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_ITEM_REMOVE, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { Item } from '#entities/item' import { Item } from '#entities/item'
import ItemRepository from '#repositories/itemRepository' import ItemRepository from '#repositories/itemRepository'
@ -6,7 +7,7 @@ interface IPayload {}
export default class ItemListEvent extends BaseEvent { export default class ItemListEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:item:list', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_ITEM_LIST, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: Item[]) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: Item[]) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -17,7 +18,7 @@ type Payload = {
export default class ItemUpdateEvent extends BaseEvent { export default class ItemUpdateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:item:update', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_ITEM_UPDATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { MapObject } from '#entities/mapObject' import { MapObject } from '#entities/mapObject'
import MapObjectRepository from '#repositories/mapObjectRepository' import MapObjectRepository from '#repositories/mapObjectRepository'
@ -6,7 +7,7 @@ interface IPayload {}
export default class MapObjectListEvent extends BaseEvent { export default class MapObjectListEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:mapObject:list', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAPOBJECT_LIST, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: MapObject[]) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: MapObject[]) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import fs from 'fs' import fs from 'fs'
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
@ -12,7 +13,7 @@ interface IPayload {
export default class MapObjectRemoveEvent extends BaseEvent { export default class MapObjectRemoveEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:mapObject:remove', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAPOBJECT_REMOVE, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -16,7 +17,7 @@ type Payload = {
export default class MapObjectUpdateEvent extends BaseEvent { export default class MapObjectUpdateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:mapObject:update', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAPOBJECT_UPDATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> {
@ -40,7 +41,7 @@ export default class MapObjectUpdateEvent extends BaseEvent {
return callback(true) return callback(true)
} catch (error) { } catch (error) {
this.socket.emit('notification', { title: 'Error', message: 'Failed to update mapObject.' }) this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Error', message: 'Failed to update mapObject.' })
return callback(false) return callback(false)
} }
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import fs from 'fs/promises' import fs from 'fs/promises'
import { writeFile } from 'node:fs/promises' import { writeFile } from 'node:fs/promises'
@ -13,7 +14,7 @@ interface IObjectData {
export default class MapObjectUploadEvent extends BaseEvent { export default class MapObjectUploadEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:mapObject:upload', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAPOBJECT_UPLOAD, this.handleEvent.bind(this))
} }
private async handleEvent(data: IObjectData, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: IObjectData, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -10,7 +11,7 @@ interface CopyPayload {
export default class SpriteCopyEvent extends BaseEvent { export default class SpriteCopyEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:sprite:copy', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_SPRITE_COPY, this.handleEvent.bind(this))
} }
private async handleEvent(payload: CopyPayload, callback: (success: boolean) => void): Promise<void> { private async handleEvent(payload: CopyPayload, callback: (success: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import fs from 'fs/promises' import fs from 'fs/promises'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -6,7 +7,7 @@ import { Sprite } from '#entities/sprite'
export default class SpriteCreateEvent extends BaseEvent { export default class SpriteCreateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:sprite:create', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_SPRITE_CREATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: undefined, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: undefined, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import fs from 'fs' import fs from 'fs'
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
@ -12,7 +13,7 @@ type Payload = {
export default class GMSpriteDeleteEvent extends BaseEvent { export default class GMSpriteDeleteEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:sprite:delete', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_SPRITE_DELETE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { Sprite } from '#entities/sprite' import { Sprite } from '#entities/sprite'
import SpriteRepository from '#repositories/spriteRepository' import SpriteRepository from '#repositories/spriteRepository'
@ -6,7 +7,7 @@ interface IPayload {}
export default class SpriteListEvent extends BaseEvent { export default class SpriteListEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:sprite:list', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_SPRITE_LIST, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: Sprite[]) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: Sprite[]) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import fs from 'fs' import fs from 'fs'
import sharp from 'sharp' import sharp from 'sharp'
@ -44,7 +45,7 @@ type Payload = {
export default class SpriteUpdateEvent extends BaseEvent { export default class SpriteUpdateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:sprite:update', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_SPRITE_UPDATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import fs from 'fs/promises' import fs from 'fs/promises'
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
@ -12,7 +13,7 @@ type Payload = {
export default class GMTileDeleteEvent extends BaseEvent { export default class GMTileDeleteEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:tile:delete', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_TILE_DELETE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { Tile } from '#entities/tile' import { Tile } from '#entities/tile'
import TileRepository from '#repositories/tileRepository' import TileRepository from '#repositories/tileRepository'
@ -6,7 +7,7 @@ interface IPayload {}
export default class TileListEven extends BaseEvent { export default class TileListEven extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:tile:list', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_TILE_LIST, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: Tile[]) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: Tile[]) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -11,7 +12,7 @@ type Payload = {
export default class TileUpdateEvent extends BaseEvent { export default class TileUpdateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:tile:update', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_TILE_UPDATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (success: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import fs from 'fs/promises' import fs from 'fs/promises'
import { writeFile } from 'node:fs/promises' import { writeFile } from 'node:fs/promises'
@ -11,7 +12,7 @@ interface ITileData {
export default class TileUploadEvent extends BaseEvent { export default class TileUploadEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:tile:upload', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_TILE_UPLOAD, this.handleEvent.bind(this))
} }
private async handleEvent(data: ITileData, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: ITileData, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { MapCacheT } from '#entities/map' import type { MapCacheT } from '#entities/map'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -11,7 +12,7 @@ type Payload = {
export default class MapCreateEvent extends BaseEvent { export default class MapCreateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:map:create', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAP_CREATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (response: MapCacheT | false) => void): Promise<void> { private async handleEvent(data: Payload, callback: (response: MapCacheT | false) => void): Promise<void> {
@ -21,12 +22,12 @@ export default class MapCreateEvent extends BaseEvent {
this.logger.info(`GM ${(await this.getCharacter())!.getId()} has created a new map via map editor.`) this.logger.info(`GM ${(await this.getCharacter())!.getId()} has created a new map via map editor.`)
if (data.name === '') { if (data.name === '') {
this.socket.emit('notification', { title: 'Error', message: 'Map name cannot be empty.' }) this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Error', message: 'Map name cannot be empty.' })
return callback(false) return callback(false)
} }
if (data.width < 1 || data.height < 1) { if (data.width < 1 || data.height < 1) {
this.socket.emit('notification', { title: 'Error', message: 'Map width and height must be greater than 0.' }) this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Error', message: 'Map width and height must be greater than 0.' })
return callback(false) return callback(false)
} }
@ -41,7 +42,7 @@ export default class MapCreateEvent extends BaseEvent {
return callback(await map.cache()) return callback(await map.cache())
} catch (error: any) { } catch (error: any) {
this.logger.error('gm:map:create error', error.message) this.logger.error('gm:map:create error', error.message)
this.socket.emit('notification', { message: 'Failed to create map.' }) this.socket.emit(SocketEvent.NOTIFICATION, { message: 'Failed to create map.' })
return callback(false) return callback(false)
} }
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -9,7 +10,7 @@ type Payload = {
export default class MapDeleteEvent extends BaseEvent { export default class MapDeleteEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:map:delete', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAP_DELETE, this.handleEvent.bind(this))
} }
private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -10,7 +11,7 @@ interface IPayload {
export default class MapRequestEvent extends BaseEvent { export default class MapRequestEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:map:request', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAP_REQUEST, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -34,7 +35,7 @@ interface IPayload {
export default class MapUpdateEvent extends BaseEvent { export default class MapUpdateEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('gm:map:update', this.handleEvent.bind(this)) this.socket.on(SocketEvent.GM_MAP_UPDATE, this.handleEvent.bind(this))
} }
private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> {

View File

@ -1,9 +1,10 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import UserRepository from '#repositories/userRepository' import UserRepository from '#repositories/userRepository'
export default class LoginEvent extends BaseEvent { export default class LoginEvent extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('login', this.handleEvent.bind(this)) this.socket.on(SocketEvent.LOGIN, this.handleEvent.bind(this))
} }
private async handleEvent() { private async handleEvent() {
@ -14,7 +15,7 @@ export default class LoginEvent extends BaseEvent {
} }
const userRepository = new UserRepository() const userRepository = new UserRepository()
this.socket.emit('logged_in', { user: userRepository.getById(this.socket.userId) }) this.socket.emit(SocketEvent.LOGGED_IN, { user: userRepository.getById(this.socket.userId) })
this.logger.info(`User logged in: ${this.socket.userId}`) this.logger.info(`User logged in: ${this.socket.userId}`)
} catch (error: any) { } catch (error: any) {
this.logger.error('login error: ' + error.message) this.logger.error('login error: ' + error.message)

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import CharacterAttackService from '#services/characterAttackService' import CharacterAttackService from '#services/characterAttackService'
@ -5,7 +6,7 @@ export default class CharacterMove extends BaseEvent {
private readonly characterAttackService = CharacterAttackService private readonly characterAttackService = CharacterAttackService
public listen(): void { public listen(): void {
this.socket.on('map:character:attack', this.handleEvent.bind(this)) this.socket.on(SocketEvent.MAP_CHARACTER_ATTACK, this.handleEvent.bind(this))
} }
private async handleEvent(data: any, callback: (response: any) => void): Promise<void> { private async handleEvent(data: any, callback: (response: any) => void): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { MapEventTileWithTeleport } from '#application/types' import type { MapEventTileWithTeleport } from '#application/types'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
@ -12,7 +13,7 @@ export default class CharacterMove extends BaseEvent {
private readonly MOVEMENT_THROTTLE = 230 // Minimum time between movement requests private readonly MOVEMENT_THROTTLE = 230 // Minimum time between movement requests
public listen(): void { public listen(): void {
this.socket.on('map:character:move', this.handleEvent.bind(this)) this.socket.on(SocketEvent.MAP_CHARACTER_MOVE, this.handleEvent.bind(this))
} }
private async handleEvent({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> { private async handleEvent({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
@ -47,7 +48,7 @@ export default class CharacterMove extends BaseEvent {
const path = await this.characterService.calculatePath(mapCharacter.character, positionX, positionY) const path = await this.characterService.calculatePath(mapCharacter.character, positionX, positionY)
if (!path?.length) { if (!path?.length) {
this.io.in(mapCharacter.character.map.id).emit('map:character:moveError', 'No valid path found') this.io.in(mapCharacter.character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVEERROR, 'No valid path found')
return return
} }
@ -98,7 +99,7 @@ export default class CharacterMove extends BaseEvent {
character.setPositionX(end.positionX).setPositionY(end.positionY) character.setPositionX(end.positionX).setPositionY(end.positionY)
// Then emit with the same properties // Then emit with the same properties
this.io.in(character.map.id).emit('map:character:move', { this.io.in(character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVE, {
characterId: character.id, characterId: character.id,
positionX: character.getPositionX(), positionX: character.getPositionX(),
positionY: character.getPositionY(), positionY: character.getPositionY(),
@ -128,7 +129,7 @@ export default class CharacterMove extends BaseEvent {
private finalizeMovement(mapCharacter: MapCharacter): void { private finalizeMovement(mapCharacter: MapCharacter): void {
mapCharacter.isMoving = false mapCharacter.isMoving = false
this.io.in(mapCharacter.character.map.id).emit('map:character:move', { this.io.in(mapCharacter.character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVE, {
characterId: mapCharacter.character.id, characterId: mapCharacter.character.id,
positionX: mapCharacter.character.positionX, positionX: mapCharacter.character.positionX,
positionY: mapCharacter.character.positionY, positionY: mapCharacter.character.positionY,

View File

@ -1,15 +1,16 @@
import { SocketEvent } from '#application/enums';
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import WeatherManager from '#managers/weatherManager' import WeatherManager from '#managers/weatherManager'
export default class Weather extends BaseEvent { export default class Weather extends BaseEvent {
public listen(): void { public listen(): void {
this.socket.on('weather', this.handleEvent.bind(this)) this.socket.on(SocketEvent.WEATHER, this.handleEvent.bind(this))
} }
private async handleEvent(): Promise<void> { private async handleEvent(): Promise<void> {
try { try {
const weather = WeatherManager.getWeatherState() const weather = WeatherManager.getWeatherState()
this.socket.emit('weather', weather) this.socket.emit(SocketEvent.WEATHER, weather)
} catch (error: any) { } catch (error: any) {
this.logger.error('weather error: ' + error.message) this.logger.error('weather error: ' + error.message)
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { Server as SocketServer } from 'socket.io' import { Server as SocketServer } from 'socket.io'
import type { TSocket } from '#application/types' import type { TSocket } from '#application/types'
@ -8,9 +9,9 @@ export default class SomeJob {
async execute(io: SocketServer, socket?: TSocket) { async execute(io: SocketServer, socket?: TSocket) {
// Handle the event // Handle the event
if (socket) { if (socket) {
socket.emit('notification', { message: 'Something happened with socket' }) socket.emit(SocketEvent.NOTIFICATION, { message: 'Something happened with socket' })
} }
// Use io for broadcasting if needed // Use io for broadcasting if needed
io.emit('notification', { message: 'Something happened' }) io.emit(SocketEvent.NOTIFICATION, { message: 'Something happened' })
} }
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { Server } from 'socket.io' import { Server } from 'socket.io'
import Logger, { LoggerType } from '#application/logger' import Logger, { LoggerType } from '#application/logger'
@ -84,7 +85,7 @@ class DateManager {
private emitDate(): void { private emitDate(): void {
const io = SocketManager.getIO() const io = SocketManager.getIO()
io?.emit('date', this.currentDate) io?.emit(SocketEvent.DATE, this.currentDate)
} }
private async saveDate(): Promise<void> { private async saveDate(): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { Server } from 'socket.io' import { Server } from 'socket.io'
import Logger, { LoggerType } from '#application/logger' import Logger, { LoggerType } from '#application/logger'
@ -112,7 +113,7 @@ class WeatherManager {
private emitWeather(): void { private emitWeather(): void {
const io = SocketManager.getIO() const io = SocketManager.getIO()
io?.emit('weather', this.weatherState) io?.emit(SocketEvent.WEATHER, this.weatherState)
} }
private async saveWeather(): Promise<void> { private async saveWeather(): Promise<void> {

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import { Server } from 'socket.io' import { Server } from 'socket.io'
import type { TSocket, UUID } from '#application/types' import type { TSocket, UUID } from '#application/types'
@ -44,11 +45,11 @@ class MapCharacter {
MapManager.removeCharacter(this.character.id) MapManager.removeCharacter(this.character.id)
// Notify map players // Notify map players
io.in(this.character.map.id).emit('map:character:leave', this.character.id) io.in(this.character.map.id).emit(SocketEvent.MAP_CHARACTER_LEAVE, this.character.id)
} }
// Notify all players // Notify all players
io.emit('character:disconnect', this.character.id) io.emit(SocketEvent.CHARACTER_DISCONNECT, this.character.id)
} catch (error) { } catch (error) {
console.error(`Error disconnecting character ${this.character.id}:`, error) console.error(`Error disconnecting character ${this.character.id}:`, error)
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseService } from '#application/base/baseService' import { BaseService } from '#application/base/baseService'
@ -27,7 +28,7 @@ class CharacterAttackService extends BaseService {
} }
// Emit attack event // Emit attack event
io.in(character.character.map.id).emit('map:character:attack', character.character.id) io.in(character.character.map.id).emit(SocketEvent.MAP_CHARACTER_ATTACK, character.character.id)
return true return true
} }
} }

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import Logger, { LoggerType } from '#application/logger' import Logger, { LoggerType } from '#application/logger'
@ -61,7 +62,7 @@ class CharacterTeleportService {
// If the current map is the target map and we are not joining, send move event // If the current map is the target map and we are not joining, send move event
if (currentMapId === options.targetMapId && !options.isInitialJoin) { if (currentMapId === options.targetMapId && !options.isInitialJoin) {
// If the current map is the target map, send move event // If the current map is the target map, send move event
io.in(currentMapId).emit('map:character:move', { io.in(currentMapId).emit(SocketEvent.MAP_CHARACTER_MOVE, {
characterId: mapCharacter.character.id, characterId: mapCharacter.character.id,
positionX: options.targetX, positionX: options.targetX,
positionY: options.targetY, positionY: options.targetY,
@ -75,7 +76,7 @@ class CharacterTeleportService {
if (currentMapId) { if (currentMapId) {
socket.leave(currentMapId) socket.leave(currentMapId)
MapManager.removeCharacter(characterId) MapManager.removeCharacter(characterId)
io.in(currentMapId).emit('map:character:leave', characterId) io.in(currentMapId).emit(SocketEvent.MAP_CHARACTER_LEAVE, characterId)
} }
// Join new map // Join new map
@ -86,8 +87,8 @@ class CharacterTeleportService {
await mapRepository.getEntityManager().populate(map!, mapRepository.POPULATE_TELEPORT as any) await mapRepository.getEntityManager().populate(map!, mapRepository.POPULATE_TELEPORT as any)
// Notify clients // Notify clients
io.in(options.targetMapId).emit('map:character:join', mapCharacter) io.in(options.targetMapId).emit(SocketEvent.MAP_CHARACTER_JOIN, mapCharacter)
socket.emit('map:character:teleport', { socket.emit(SocketEvent.MAP_CHARACTER_TELEPORT, {
mapId: options.targetMapId, mapId: options.targetMapId,
characters: targetMap.getCharactersInMap() characters: targetMap.getCharactersInMap()
}) })

View File

@ -1,3 +1,4 @@
import { SocketEvent } from '#application/enums';
import type { UUID } from '#application/types' import type { UUID } from '#application/types'
import { BaseService } from '#application/base/baseService' import { BaseService } from '#application/base/baseService'
@ -22,7 +23,7 @@ class ChatService extends BaseService {
await chat.setCharacter(character).setMap(map).setMessage(message).save() await chat.setCharacter(character).setMap(map).setMessage(message).save()
const io = SocketManager.getIO() const io = SocketManager.getIO()
io.to(mapId).emit('chat:message', chat) io.to(mapId).emit(SocketEvent.CHAT_MESSAGE, chat)
return true return true
} catch (error: any) { } catch (error: any) {
this.logger.error(`Failed to save chat message: ${error instanceof Error ? error.message : String(error)}`) this.logger.error(`Failed to save chat message: ${error instanceof Error ? error.message : String(error)}`)