server/src/socketEvents/chat/gameMaster/toggleRainCommand.ts

48 lines
1.4 KiB
TypeScript

import { Server } from 'socket.io'
import { TSocket } from '#application/types'
import { isCommand } from '#application/chat'
import CharacterRepository from '#repositories/characterRepository'
import { gameLogger } from '#application/logger'
import WeatherManager from '#managers/weatherManager'
type TypePayload = {
message: string
}
export default class ToggleRainCommand {
constructor(
private readonly io: Server,
private readonly socket: TSocket
) {}
public listen(): void {
this.socket.on('chat:message', this.handleAlertCommand.bind(this))
}
private async handleAlertCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
try {
if (!isCommand(data.message, 'rain')) {
return
}
// Check if character exists
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!)
if (!character) {
gameLogger.error('chat:alert_command error', 'Character not found')
return
}
// Check if the user is the GM
if (character.role !== 'gm') {
gameLogger.info(`User ${character.id} tried to set time but is not a game master.`)
return
}
await WeatherManager.toggleRain()
} catch (error: any) {
gameLogger.error('command error', error.message)
callback(false)
}
}
}