1
0
forked from noxious/server

#184: Added commands to toggle rain / fog, command bug fix, minor improvements

This commit is contained in:
Dennis Postma 2024-11-05 23:15:56 +01:00
parent 26dbaa45a7
commit c4a42066ab
6 changed files with 160 additions and 12 deletions

View File

@ -31,6 +31,32 @@ class WeatherManager {
appLogger.info('Weather manager loaded')
}
public async toggleRain(): Promise<void> {
this.weatherState.isRainEnabled = !this.weatherState.isRainEnabled
this.weatherState.rainPercentage = this.weatherState.isRainEnabled
? Math.floor(Math.random() * 50) + 50 // 50-100%
: 0
// Save weather
await this.saveWeather()
// Emit weather
this.emitWeather()
}
public async toggleFog(): Promise<void> {
this.weatherState.isFogEnabled = !this.weatherState.isFogEnabled
this.weatherState.fogDensity = this.weatherState.isFogEnabled
? Math.random() * 0.7 + 0.3 // 0.3-1.0
: 0
// Save weather
await this.saveWeather()
// Emit weather
this.emitWeather()
}
private async loadWeather(): Promise<void> {
try {
this.weatherState.isRainEnabled = await readJsonValue<boolean>(this.getWorldFilePath(), 'isRainEnabled')
@ -50,7 +76,9 @@ class WeatherManager {
this.intervalId = setInterval(() => {
this.updateWeather()
this.emitWeather()
this.saveWeather()
this.saveWeather().catch((error) => {
appLogger.error(`Failed to save weather: ${error instanceof Error ? error.message : String(error)}`)
})
}, WeatherManager.UPDATE_INTERVAL)
}

View File

@ -24,16 +24,24 @@ export default class AlertCommandEvent {
return
}
const args = getArgs('alert', data.message)
if (!args) {
// 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
}
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')
// 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
}
const args = getArgs('alert', data.message)
if (!args) {
callback(false)
return
}

View File

@ -1,9 +1,10 @@
import { Server } from 'socket.io'
import { TSocket } from '../../../utilities/types'
import { ExtendedCharacter, TSocket } from '../../../utilities/types'
import { getArgs, isCommand } from '../../../utilities/chat'
import ZoneRepository from '../../../repositories/zoneRepository'
import CharacterManager from '../../../managers/characterManager'
import { gameMasterLogger } from '../../../utilities/logger'
import { gameLogger, gameMasterLogger } from '../../../utilities/logger'
import CharacterRepository from '../../../repositories/characterRepository'
type TypePayload = {
message: string
@ -21,9 +22,18 @@ export default class TeleportCommandEvent {
private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
try {
const character = CharacterManager.getCharacterFromSocket(this.socket)
// Check if character exists
const character = await CharacterRepository.getByUserAndId(this.socket.user?.id as number, this.socket.characterId as number) as ExtendedCharacter
if (!character) {
this.socket.emit('notification', { title: 'Server message', message: 'Character not found' })
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
}

View File

@ -0,0 +1,51 @@
import { Server } from 'socket.io'
import { TSocket } from '../../../utilities/types'
import { isCommand } from '../../../utilities/chat'
import CharacterRepository from '../../../repositories/characterRepository'
import { gameLogger } from '../../../utilities/logger'
import WeatherManager from '../../../managers/weatherManager'
type TypePayload = {
message: string
}
export default class ToggleFogCommand {
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, 'fog')) {
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
}
await WeatherManager.toggleFog()
callback(true)
} catch (error: any) {
gameLogger.error('command error', error.message)
callback(false)
}
}
}

View File

@ -0,0 +1,51 @@
import { Server } from 'socket.io'
import { TSocket } from '../../../utilities/types'
import { isCommand } from '../../../utilities/chat'
import CharacterRepository from '../../../repositories/characterRepository'
import { gameLogger } from '../../../utilities/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:send_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.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
}
await WeatherManager.toggleRain()
callback(true)
} catch (error: any) {
gameLogger.error('command error', error.message)
callback(false)
}
}
}

View File

@ -1,6 +1,6 @@
export function isCommand(message: string, command?: string) {
if (command) {
return message.startsWith(`/${command} `)
return message === `/${command}` || message.startsWith(`/${command} `)
}
return message.startsWith('/')
}