forked from noxious/server
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../../utilities/types'
|
|
import ZoneRepository from '../../repositories/zoneRepository'
|
|
import { isCommand } from '../../utilities/chat'
|
|
import { gameLogger } from '../../utilities/logger'
|
|
import ZoneManager from '../../managers/zoneManager'
|
|
import ChatService from '../../services/chatService'
|
|
|
|
type TypePayload = {
|
|
message: string
|
|
}
|
|
|
|
export default class ChatMessageEvent {
|
|
constructor(
|
|
private readonly io: Server,
|
|
private readonly socket: TSocket
|
|
) {}
|
|
|
|
public listen(): void {
|
|
this.socket.on('chat:message', this.handleEvent.bind(this))
|
|
}
|
|
|
|
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
|
try {
|
|
if (!data.message || isCommand(data.message)) {
|
|
return callback(false)
|
|
}
|
|
|
|
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
|
if (!zoneCharacter) {
|
|
gameLogger.error('chat:message error', 'Character not found')
|
|
return callback(false)
|
|
}
|
|
|
|
const character = zoneCharacter.character
|
|
|
|
const zone = await ZoneRepository.getById(character.zoneId)
|
|
if (!zone) {
|
|
gameLogger.error('chat:message error', 'Zone not found')
|
|
return callback(false)
|
|
}
|
|
|
|
const chatService = new ChatService()
|
|
if (await chatService.sendZoneMessage(this.io, this.socket, data.message, character.id, zone.id)) {
|
|
return callback(true)
|
|
}
|
|
|
|
callback(false)
|
|
} catch (error: any) {
|
|
gameLogger.error('chat:message error', error.message)
|
|
callback(false)
|
|
}
|
|
}
|
|
}
|