1
0
forked from noxious/server

Refactor send chat logic

This commit is contained in:
Dennis Postma 2025-01-01 21:57:24 +01:00
parent 45e756fcd3
commit e61aeed691
2 changed files with 6 additions and 10 deletions

View File

@ -32,7 +32,7 @@ export default class ChatMessageEvent extends BaseEvent {
return callback(false)
}
if (await ChatService.sendZoneMessage(this.io, this.socket, data.message, character.id, zone.id)) {
if (await ChatService.sendZoneMessage(character.id, zone.id, data.message)) {
return callback(true)
}

View File

@ -6,9 +6,10 @@ import { Chat } from '#entities/chat'
import CharacterRepository from '#repositories/characterRepository'
import ChatRepository from '#repositories/chatRepository'
import ZoneRepository from '#repositories/zoneRepository'
import SocketManager from '#managers/socketManager'
class ChatService extends BaseService {
async sendZoneMessage(io: Server, socket: TSocket, message: string, characterId: UUID, zoneId: UUID): Promise<boolean> {
async sendZoneMessage(characterId: UUID, zoneId: UUID, message: string): Promise<boolean> {
try {
const character = await CharacterRepository.getById(characterId)
if (!character) return false
@ -16,15 +17,10 @@ class ChatService extends BaseService {
const zone = await ZoneRepository.getById(zoneId)
if (!zone) return false
const newChat = new Chat()
newChat.setCharacter(character).setZone(zone).setMessage(message)
await newChat.save()
const chat = await ChatRepository.getById(newChat.id)
if (!chat) return false
const chat = new Chat()
await chat.setCharacter(character).setZone(zone).setMessage(message).save()
const io = SocketManager.getIO()
io.to(zoneId).emit('chat:message', chat)
return true
} catch (error: any) {