#184: Allow GMs to set time

This commit is contained in:
2024-11-05 23:07:23 +01:00
parent ae0241fecb
commit 26dbaa45a7
2 changed files with 88 additions and 4 deletions

View File

@ -0,0 +1,66 @@
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'
import DateManager from '../../../managers/dateManager'
type TypePayload = {
message: string
}
export default class SetTimeCommand {
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, 'time')) {
return
}
// Check if character exists
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
}
// 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.`)
callback(false)
return
}
// Get arguments
const args = getArgs('time', data.message)
if (!args) {
callback(false)
return
}
const time = args[0] // 24h time, e.g. 17:34
if (!time) {
callback(false)
return
}
await DateManager.setTime(time)
callback(true)
} catch (error: any) {
gameLogger.error('command error', error.message)
callback(false)
}
}
}