forked from noxious/server
#184: Allow GMs to set time
This commit is contained in:
66
src/socketEvents/chat/gameMaster/setTimeCommand.ts
Normal file
66
src/socketEvents/chat/gameMaster/setTimeCommand.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user