#161: Set default value for createdAt field, store zone chats into database
This commit is contained in:
57
src/socketEvents/chat/message.ts
Normal file
57
src/socketEvents/chat/message.ts
Normal file
@ -0,0 +1,57 @@
|
||||
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:send_message', this.handleChatMessage.bind(this))
|
||||
}
|
||||
|
||||
private async handleChatMessage(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
||||
try {
|
||||
if (!data.message || isCommand(data.message)) {
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
||||
if (!zoneCharacter) {
|
||||
gameLogger.error('chat:send_message error', 'Character not found')
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
const character = zoneCharacter.character
|
||||
|
||||
const zone = await ZoneRepository.getById(character.zoneId)
|
||||
if (!zone) {
|
||||
gameLogger.error('chat:send_message error', 'Zone not found')
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
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:send_message error', error.message)
|
||||
callback(false)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user