This commit is contained in:
2024-08-20 01:50:05 +02:00
parent 706107ba4d
commit cb90e0cdf8
4 changed files with 56 additions and 19 deletions

View File

@ -2,6 +2,7 @@ import { Server } from 'socket.io'
import { TSocket } from '../../utilities/Types'
import CharacterRepository from '../../repositories/CharacterRepository'
import ZoneRepository from '../../repositories/ZoneRepository'
import { isCommand } from '../../utilities/Chat'
type TypePayload = {
message: string
@ -9,28 +10,19 @@ type TypePayload = {
export default function (socket: TSocket, io: Server) {
socket.on('chat:send_message', async (data: TypePayload, callback: (response: boolean) => void) => {
console.log(`---User ${socket.character?.id} has sent a message via chat.`)
if (!data.message) {
console.log(`---Message not provided.`)
return
}
try {
if (!data.message) return
if (isCommand(data.message)) return
const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.character?.id as number)
if (!character) {
console.log(`---Character not found.`)
return
}
if (!character) return
const zone = await ZoneRepository.getById(character.zoneId)
if (!zone) {
console.log(`---Zone not found.`)
return
}
if (!zone) return
// send over zone and characters to socket
callback(true)
io.to(zone.id.toString()).emit('chat:message', {
character: character,
message: data.message

View File

@ -0,0 +1,33 @@
import { Server } from 'socket.io'
import { TSocket } from '../../../utilities/Types'
import { getArgs, isCommand } from '../../../utilities/Chat'
import CharacterRepository from '../../../repositories/CharacterRepository'
type TypePayload = {
message: string
}
export default function (socket: TSocket, io: Server) {
socket.on('chat:send_message', async (data: TypePayload, callback: (response: boolean) => void) => {
try {
if (!isCommand(data.message, 'alert')) return
const args = getArgs('alert', data.message)
if (!args) return
const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.character?.id as number)
if (!character) return
// When 1 arg is provided, only send message. 2 includes a title
if (args.length === 1) {
return io.emit('notification', { message: args[0] })
}
io.emit('notification', { title: args[0], message: args.slice(1).join(' ') })
callback(true)
} catch (error: any) {
console.log(`---Error sending message: ${error.message}`)
}
})
}