47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { BaseEvent } from '#application/base/baseEvent'
|
|
import MapManager from '#managers/mapManager'
|
|
import MapRepository from '#repositories/mapRepository'
|
|
import ChatService from '#services/chatService'
|
|
|
|
type TypePayload = {
|
|
message: string
|
|
}
|
|
|
|
export default class ChatMessageEvent extends BaseEvent {
|
|
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 || ChatService.isCommand(data.message)) {
|
|
return callback(false)
|
|
}
|
|
|
|
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
|
|
if (!mapCharacter) {
|
|
this.logger.error('chat:message error', 'Character not found')
|
|
return callback(false)
|
|
}
|
|
|
|
const character = mapCharacter.character
|
|
|
|
const mapRepository = new MapRepository()
|
|
const map = await mapRepository.getById(character.map.id)
|
|
if (!map) {
|
|
this.logger.error('chat:message error', 'Map not found')
|
|
return callback(false)
|
|
}
|
|
|
|
if (await ChatService.sendMapMessage(character.getId(), map.getId(), data.message)) {
|
|
return callback(true)
|
|
}
|
|
|
|
callback(false)
|
|
} catch (error: any) {
|
|
this.logger.error('chat:message error', error.message)
|
|
callback(false)
|
|
}
|
|
}
|
|
}
|