forked from noxious/server
Updated several events to new event format
This commit is contained in:
@ -2,27 +2,47 @@ import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../utilities/types'
|
||||
import { getArgs, isCommand } from '../../../utilities/chat'
|
||||
import CharacterRepository from '../../../repositories/characterRepository'
|
||||
import { gameLogger } from '../../../utilities/logger'
|
||||
|
||||
type TypePayload = {
|
||||
message: string
|
||||
}
|
||||
|
||||
export default function (io: Server, socket: TSocket) {
|
||||
socket.on('chat:send_message', async (data: TypePayload, callback: (response: boolean) => void) => {
|
||||
export default class AlertCommandEvent {
|
||||
constructor(
|
||||
private readonly io: Server,
|
||||
private readonly socket: TSocket
|
||||
) {}
|
||||
|
||||
public listen(): void {
|
||||
this.socket.on('chat:send_message', this.handleAlertCommand.bind(this))
|
||||
}
|
||||
|
||||
private async handleAlertCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
||||
try {
|
||||
if (!isCommand(data.message, 'alert')) return
|
||||
if (!isCommand(data.message, 'alert')) {
|
||||
return
|
||||
}
|
||||
|
||||
const args = getArgs('alert', data.message)
|
||||
|
||||
if (!args) return
|
||||
if (!args) {
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.characterId as number)
|
||||
if (!character) return
|
||||
const character = await CharacterRepository.getByUserAndId(this.socket.user?.id as number, this.socket.characterId as number)
|
||||
if (!character) {
|
||||
gameLogger.error('chat:alert_command error', 'Character not found')
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
io.emit('notification', { title: 'Message from GM', message: args.join(' ') })
|
||||
this.io.emit('notification', { title: 'Message from GM', message: args.join(' ') })
|
||||
callback(true)
|
||||
} catch (error: any) {
|
||||
console.log(`---Error sending message: ${error.message}`)
|
||||
gameLogger.error('chat:alert_command error', error.message)
|
||||
callback(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user