Merge remote-tracking branch 'origin/feature/#313-effects' into feature/map-refactor

This commit is contained in:
Dennis Postma 2025-01-25 15:34:40 +01:00
commit b3ac6d34b8
6 changed files with 41 additions and 54 deletions

View File

@ -46,8 +46,11 @@ export type AssetData = {
export type WorldSettings = {
date: Date
isRainEnabled: boolean
isFogEnabled: boolean
weatherState: WeatherState
}
export type WeatherState = {
rainPercentage: number
fogDensity: number
}

View File

@ -6,15 +6,9 @@ export class BaseWorld extends BaseEntity {
@PrimaryKey()
date = new Date()
@Property()
isRainEnabled = false
@Property()
rainPercentage = 0
@Property()
isFogEnabled = false
@Property()
fogDensity = 0
@ -27,15 +21,6 @@ export class BaseWorld extends BaseEntity {
return this.date
}
setIsRainEnabled(isRainEnabled: boolean) {
this.isRainEnabled = isRainEnabled
return this
}
getIsRainEnabled() {
return this.isRainEnabled
}
setRainPercentage(rainPercentage: number) {
this.rainPercentage = rainPercentage
return this
@ -45,15 +30,6 @@ export class BaseWorld extends BaseEntity {
return this.rainPercentage
}
setIsFogEnabled(isFogEnabled: boolean) {
this.isFogEnabled = isFogEnabled
return this
}
getIsFogEnabled() {
return this.isFogEnabled
}
setFogDensity(fogDensity: number) {
this.fogDensity = fogDensity
return this

View File

@ -1,6 +1,5 @@
import { BaseEvent } from '#application/base/baseEvent'
import WeatherManager from '#managers/weatherManager'
import CharacterRepository from '#repositories/characterRepository'
import ChatService from '#services/chatService'
type TypePayload = {
@ -20,7 +19,10 @@ export default class ToggleFogCommand extends BaseEvent {
// Check if character exists and is GM
if (!(await this.isCharacterGM())) return
await WeatherManager.toggleFog()
const args = ChatService.getArgs('fog', data.message)
await WeatherManager.setFogValue(args![0] ? Number(args![0]) : null);
callback(true)
} catch (error: any) {
this.logger.error('command error', error.message)
callback(false)

View File

@ -1,6 +1,5 @@
import { BaseEvent } from '#application/base/baseEvent'
import WeatherManager from '#managers/weatherManager'
import CharacterRepository from '#repositories/characterRepository'
import ChatService from '#services/chatService'
type TypePayload = {
@ -20,7 +19,10 @@ export default class ToggleRainCommand extends BaseEvent {
// Check if character exists and is GM
if (!(await this.isCharacterGM())) return
await WeatherManager.toggleRain()
let args = ChatService.getArgs('rain', data.message)
await WeatherManager.setRainValue(args![0] ? Number(args![0]) : null);
callback(true)
} catch (error: any) {
this.logger.error('command error', error.message)
callback(false)

View File

@ -6,9 +6,7 @@ import SocketManager from '#managers/socketManager'
import WorldRepository from '#repositories/worldRepository'
type WeatherState = {
isRainEnabled: boolean
rainPercentage: number
isFogEnabled: boolean
fogDensity: number
}
@ -26,9 +24,7 @@ class WeatherManager {
private intervalId: NodeJS.Timeout | null = null
private weatherState: WeatherState = {
isRainEnabled: false,
rainPercentage: 0,
isFogEnabled: false,
fogDensity: 0
}
@ -43,18 +39,31 @@ class WeatherManager {
return { ...this.weatherState }
}
public async toggleRain(): Promise<void> {
this.updateWeatherProperty('rain')
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 | null): Promise<void> {
if (value === null) {
value = this.randomWeatherValue('rain')
}
this.updateWeatherProperty('rain', value)
await this.saveAndEmitWeather()
}
public async toggleFog(): Promise<void> {
this.updateWeatherProperty('fog')
await this.saveAndEmitWeather()
}
public async setFogValue(value : number | null): Promise<void> {
if (value === null) {
value = this.randomWeatherValue('fog')
}
public cleanup(): void {
this.intervalId && clearInterval(this.intervalId)
this.updateWeatherProperty('fog', value)
await this.saveAndEmitWeather()
}
private async loadWeather(): Promise<void> {
@ -63,9 +72,7 @@ class WeatherManager {
const world = await worldRepository.getFirst()
if (world) {
this.weatherState = {
isRainEnabled: world.isRainEnabled,
rainPercentage: world.rainPercentage,
isFogEnabled: world.isFogEnabled,
fogDensity: world.fogDensity
}
}
@ -83,22 +90,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'): void {
private updateWeatherProperty(type: 'rain' | 'fog', value: number): void {
if (type === 'rain') {
this.weatherState.isRainEnabled = !this.weatherState.isRainEnabled
this.weatherState.rainPercentage = this.weatherState.isRainEnabled ? this.getRandomNumber(WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.min, WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.max) : 0
this.weatherState.rainPercentage = value
}
if (type === 'fog') {
this.weatherState.isFogEnabled = !this.weatherState.isFogEnabled
this.weatherState.fogDensity = this.weatherState.isFogEnabled ? this.getRandomNumber(WeatherManager.CONFIG.FOG_DENSITY_RANGE.min, WeatherManager.CONFIG.FOG_DENSITY_RANGE.max) : 0
this.weatherState.fogDensity = value
}
}
@ -118,10 +123,9 @@ class WeatherManager {
let world = await worldRepository.getFirst()
if (!world) world = new World()
//the data model still contains the booleans
await world
.setIsRainEnabled(this.weatherState.isRainEnabled)
.setRainPercentage(this.weatherState.rainPercentage)
.setIsFogEnabled(this.weatherState.isFogEnabled)
.setFogDensity(this.weatherState.fogDensity)
.save()
} catch (error) {

View File

@ -38,7 +38,7 @@ class ChatService extends BaseService {
public getArgs(command: string, message: string): string[] | undefined {
if (!this.isCommand(message, command)) return
return message.split(`/${command} `)[1].split(' ')
return message.slice(`/${command} `.length).split(' ')
}
}