Changed toggle functions to set, and refactored the random weather value gen

This commit is contained in:
Andrei 2025-01-22 20:33:11 -06:00
parent 020f2bd3c5
commit dae0d365d5
3 changed files with 26 additions and 19 deletions

View File

@ -22,7 +22,7 @@ export default class ToggleFogCommand extends BaseEvent {
const args = ChatService.getArgs('fog', data.message) const args = ChatService.getArgs('fog', data.message)
await WeatherManager.toggleFog(Number(args![0])) await WeatherManager.setFogValue(Number(args![0]))
} catch (error: any) { } catch (error: any) {
this.logger.error('command error', error.message) this.logger.error('command error', error.message)

View File

@ -22,7 +22,8 @@ export default class ToggleRainCommand extends BaseEvent {
let args = ChatService.getArgs('rain', data.message) let args = ChatService.getArgs('rain', data.message)
await WeatherManager.toggleRain(Number(args![0])) //if no arguments are given, should default to random
await WeatherManager.setRainValue(Number(args![0]))
} catch (error: any) { } catch (error: any) {
this.logger.error('command error', error.message) this.logger.error('command error', error.message)

View File

@ -39,12 +39,29 @@ class WeatherManager {
return { ...this.weatherState } return { ...this.weatherState }
} }
public async toggleRain(value? : number): Promise<void> { public randomWeatherValue(type: 'rain' | 'fog' ) {
switch (type) {
case 'rain':
return this.getRandomNumber(WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.min, WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.max)
case 'fog':
return this.getRandomNumber(WeatherManager.CONFIG.FOG_DENSITY_RANGE.min, WeatherManager.CONFIG.FOG_DENSITY_RANGE.max)
}
}
public async setRainValue(value? : number): Promise<void> {
if (value === undefined) {
value = this.weatherState.fogDensity > 0 ? 0 : this.randomWeatherValue('fog')
}
this.updateWeatherProperty('rain', value) this.updateWeatherProperty('rain', value)
await this.saveAndEmitWeather() await this.saveAndEmitWeather()
} }
public async toggleFog(value? : number): Promise<void> { public async setFogValue(value? : number): Promise<void> {
if (value === undefined) {
value = this.weatherState.fogDensity > 0 ? 0 : this.randomWeatherValue('fog')
}
this.updateWeatherProperty('fog', value) this.updateWeatherProperty('fog', value)
await this.saveAndEmitWeather() await this.saveAndEmitWeather()
} }
@ -77,32 +94,20 @@ class WeatherManager {
private updateRandomWeather(): void { private updateRandomWeather(): void {
if (Math.random() < WeatherManager.CONFIG.RAIN_CHANCE) { if (Math.random() < WeatherManager.CONFIG.RAIN_CHANCE) {
this.updateWeatherProperty('rain') this.updateWeatherProperty('rain', this.randomWeatherValue('rain') )
} }
if (Math.random() < WeatherManager.CONFIG.FOG_CHANCE) { if (Math.random() < WeatherManager.CONFIG.FOG_CHANCE) {
this.updateWeatherProperty('fog') this.updateWeatherProperty('fog', this.randomWeatherValue('fog'))
} }
} }
private updateWeatherProperty(type: 'rain' | 'fog', value?: number): void { private updateWeatherProperty(type: 'rain' | 'fog', value: number): void {
if (type === 'rain') { if (type === 'rain') {
if (value !== undefined) {
this.weatherState.rainPercentage = value this.weatherState.rainPercentage = value
}
else {
this.weatherState.rainPercentage = this.weatherState.rainPercentage > 0 ? 0 :
this.weatherState.rainPercentage = this.getRandomNumber(WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.min, WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.max)
}
} }
if (type === 'fog') { if (type === 'fog') {
if (value !== undefined) {
this.weatherState.fogDensity = value this.weatherState.fogDensity = value
}
else {
this.weatherState.rainPercentage = this.weatherState.fogDensity > 0 ? 0 :
this.weatherState.fogDensity = this.getRandomNumber(WeatherManager.CONFIG.FOG_DENSITY_RANGE.min, WeatherManager.CONFIG.FOG_DENSITY_RANGE.max)
}
} }
} }
@ -122,6 +127,7 @@ class WeatherManager {
let world = await worldRepository.getFirst() let world = await worldRepository.getFirst()
if (!world) world = new World() if (!world) world = new World()
//left them in here because the
await world await world
.setRainPercentage(this.weatherState.rainPercentage) .setRainPercentage(this.weatherState.rainPercentage)
.setIsRainEnabled(this.weatherState.rainPercentage > 0) .setIsRainEnabled(this.weatherState.rainPercentage > 0)