forked from noxious/server
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
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: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.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
|
|
}
|
|
|
|
// Get arguments
|
|
const args = getArgs('time', data.message)
|
|
|
|
if (!args) {
|
|
return
|
|
}
|
|
|
|
const time = args[0] // 24h time, e.g. 17:34
|
|
|
|
if (!time) {
|
|
return
|
|
}
|
|
|
|
await DateManager.setTime(time)
|
|
} catch (error: any) {
|
|
gameLogger.error('command error', error.message)
|
|
callback(false)
|
|
}
|
|
}
|
|
}
|