forked from noxious/server
Changed toggle functions to set, and refactored the random weather value gen
This commit is contained in:
parent
020f2bd3c5
commit
dae0d365d5
@ -22,7 +22,7 @@ export default class ToggleFogCommand extends BaseEvent {
|
||||
|
||||
const args = ChatService.getArgs('fog', data.message)
|
||||
|
||||
await WeatherManager.toggleFog(Number(args![0]))
|
||||
await WeatherManager.setFogValue(Number(args![0]))
|
||||
|
||||
} catch (error: any) {
|
||||
this.logger.error('command error', error.message)
|
||||
|
@ -22,7 +22,8 @@ export default class ToggleRainCommand extends BaseEvent {
|
||||
|
||||
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) {
|
||||
this.logger.error('command error', error.message)
|
||||
|
@ -39,12 +39,29 @@ class WeatherManager {
|
||||
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)
|
||||
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)
|
||||
await this.saveAndEmitWeather()
|
||||
}
|
||||
@ -77,32 +94,20 @@ class WeatherManager {
|
||||
|
||||
private updateRandomWeather(): void {
|
||||
if (Math.random() < WeatherManager.CONFIG.RAIN_CHANCE) {
|
||||
this.updateWeatherProperty('rain')
|
||||
this.updateWeatherProperty('rain', this.randomWeatherValue('rain') )
|
||||
}
|
||||
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 (value !== undefined) {
|
||||
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 (value !== undefined) {
|
||||
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()
|
||||
if (!world) world = new World()
|
||||
|
||||
//left them in here because the
|
||||
await world
|
||||
.setRainPercentage(this.weatherState.rainPercentage)
|
||||
.setIsRainEnabled(this.weatherState.rainPercentage > 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user