Merge remote-tracking branch 'origin/feature/#313-effects' into feature/map-refactor
This commit is contained in:
commit
b3ac6d34b8
@ -46,8 +46,11 @@ export type AssetData = {
|
|||||||
|
|
||||||
export type WorldSettings = {
|
export type WorldSettings = {
|
||||||
date: Date
|
date: Date
|
||||||
isRainEnabled: boolean
|
weatherState: WeatherState
|
||||||
isFogEnabled: boolean
|
}
|
||||||
|
|
||||||
|
export type WeatherState = {
|
||||||
|
rainPercentage: number
|
||||||
fogDensity: number
|
fogDensity: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,15 +6,9 @@ export class BaseWorld extends BaseEntity {
|
|||||||
@PrimaryKey()
|
@PrimaryKey()
|
||||||
date = new Date()
|
date = new Date()
|
||||||
|
|
||||||
@Property()
|
|
||||||
isRainEnabled = false
|
|
||||||
|
|
||||||
@Property()
|
@Property()
|
||||||
rainPercentage = 0
|
rainPercentage = 0
|
||||||
|
|
||||||
@Property()
|
|
||||||
isFogEnabled = false
|
|
||||||
|
|
||||||
@Property()
|
@Property()
|
||||||
fogDensity = 0
|
fogDensity = 0
|
||||||
|
|
||||||
@ -27,15 +21,6 @@ export class BaseWorld extends BaseEntity {
|
|||||||
return this.date
|
return this.date
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsRainEnabled(isRainEnabled: boolean) {
|
|
||||||
this.isRainEnabled = isRainEnabled
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
getIsRainEnabled() {
|
|
||||||
return this.isRainEnabled
|
|
||||||
}
|
|
||||||
|
|
||||||
setRainPercentage(rainPercentage: number) {
|
setRainPercentage(rainPercentage: number) {
|
||||||
this.rainPercentage = rainPercentage
|
this.rainPercentage = rainPercentage
|
||||||
return this
|
return this
|
||||||
@ -45,15 +30,6 @@ export class BaseWorld extends BaseEntity {
|
|||||||
return this.rainPercentage
|
return this.rainPercentage
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsFogEnabled(isFogEnabled: boolean) {
|
|
||||||
this.isFogEnabled = isFogEnabled
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
getIsFogEnabled() {
|
|
||||||
return this.isFogEnabled
|
|
||||||
}
|
|
||||||
|
|
||||||
setFogDensity(fogDensity: number) {
|
setFogDensity(fogDensity: number) {
|
||||||
this.fogDensity = fogDensity
|
this.fogDensity = fogDensity
|
||||||
return this
|
return this
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { BaseEvent } from '#application/base/baseEvent'
|
import { BaseEvent } from '#application/base/baseEvent'
|
||||||
import WeatherManager from '#managers/weatherManager'
|
import WeatherManager from '#managers/weatherManager'
|
||||||
import CharacterRepository from '#repositories/characterRepository'
|
|
||||||
import ChatService from '#services/chatService'
|
import ChatService from '#services/chatService'
|
||||||
|
|
||||||
type TypePayload = {
|
type TypePayload = {
|
||||||
@ -20,7 +19,10 @@ export default class ToggleFogCommand extends BaseEvent {
|
|||||||
// Check if character exists and is GM
|
// Check if character exists and is GM
|
||||||
if (!(await this.isCharacterGM())) return
|
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) {
|
} catch (error: any) {
|
||||||
this.logger.error('command error', error.message)
|
this.logger.error('command error', error.message)
|
||||||
callback(false)
|
callback(false)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { BaseEvent } from '#application/base/baseEvent'
|
import { BaseEvent } from '#application/base/baseEvent'
|
||||||
import WeatherManager from '#managers/weatherManager'
|
import WeatherManager from '#managers/weatherManager'
|
||||||
import CharacterRepository from '#repositories/characterRepository'
|
|
||||||
import ChatService from '#services/chatService'
|
import ChatService from '#services/chatService'
|
||||||
|
|
||||||
type TypePayload = {
|
type TypePayload = {
|
||||||
@ -20,7 +19,10 @@ export default class ToggleRainCommand extends BaseEvent {
|
|||||||
// Check if character exists and is GM
|
// Check if character exists and is GM
|
||||||
if (!(await this.isCharacterGM())) return
|
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) {
|
} catch (error: any) {
|
||||||
this.logger.error('command error', error.message)
|
this.logger.error('command error', error.message)
|
||||||
callback(false)
|
callback(false)
|
||||||
|
@ -6,9 +6,7 @@ import SocketManager from '#managers/socketManager'
|
|||||||
import WorldRepository from '#repositories/worldRepository'
|
import WorldRepository from '#repositories/worldRepository'
|
||||||
|
|
||||||
type WeatherState = {
|
type WeatherState = {
|
||||||
isRainEnabled: boolean
|
|
||||||
rainPercentage: number
|
rainPercentage: number
|
||||||
isFogEnabled: boolean
|
|
||||||
fogDensity: number
|
fogDensity: number
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,9 +24,7 @@ class WeatherManager {
|
|||||||
private intervalId: NodeJS.Timeout | null = null
|
private intervalId: NodeJS.Timeout | null = null
|
||||||
|
|
||||||
private weatherState: WeatherState = {
|
private weatherState: WeatherState = {
|
||||||
isRainEnabled: false,
|
|
||||||
rainPercentage: 0,
|
rainPercentage: 0,
|
||||||
isFogEnabled: false,
|
|
||||||
fogDensity: 0
|
fogDensity: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,18 +39,31 @@ class WeatherManager {
|
|||||||
return { ...this.weatherState }
|
return { ...this.weatherState }
|
||||||
}
|
}
|
||||||
|
|
||||||
public async toggleRain(): Promise<void> {
|
public randomWeatherValue(type: 'rain' | 'fog' ) {
|
||||||
this.updateWeatherProperty('rain')
|
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()
|
await this.saveAndEmitWeather()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async toggleFog(): Promise<void> {
|
public async setFogValue(value : number | null): Promise<void> {
|
||||||
this.updateWeatherProperty('fog')
|
if (value === null) {
|
||||||
await this.saveAndEmitWeather()
|
value = this.randomWeatherValue('fog')
|
||||||
}
|
}
|
||||||
|
|
||||||
public cleanup(): void {
|
this.updateWeatherProperty('fog', value)
|
||||||
this.intervalId && clearInterval(this.intervalId)
|
await this.saveAndEmitWeather()
|
||||||
}
|
}
|
||||||
|
|
||||||
private async loadWeather(): Promise<void> {
|
private async loadWeather(): Promise<void> {
|
||||||
@ -63,9 +72,7 @@ class WeatherManager {
|
|||||||
const world = await worldRepository.getFirst()
|
const world = await worldRepository.getFirst()
|
||||||
if (world) {
|
if (world) {
|
||||||
this.weatherState = {
|
this.weatherState = {
|
||||||
isRainEnabled: world.isRainEnabled,
|
|
||||||
rainPercentage: world.rainPercentage,
|
rainPercentage: world.rainPercentage,
|
||||||
isFogEnabled: world.isFogEnabled,
|
|
||||||
fogDensity: world.fogDensity
|
fogDensity: world.fogDensity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,22 +90,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'): void {
|
private updateWeatherProperty(type: 'rain' | 'fog', value: number): void {
|
||||||
if (type === 'rain') {
|
if (type === 'rain') {
|
||||||
this.weatherState.isRainEnabled = !this.weatherState.isRainEnabled
|
this.weatherState.rainPercentage = value
|
||||||
this.weatherState.rainPercentage = this.weatherState.isRainEnabled ? this.getRandomNumber(WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.min, WeatherManager.CONFIG.RAIN_PERCENTAGE_RANGE.max) : 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'fog') {
|
if (type === 'fog') {
|
||||||
this.weatherState.isFogEnabled = !this.weatherState.isFogEnabled
|
this.weatherState.fogDensity = value
|
||||||
this.weatherState.fogDensity = this.weatherState.isFogEnabled ? this.getRandomNumber(WeatherManager.CONFIG.FOG_DENSITY_RANGE.min, WeatherManager.CONFIG.FOG_DENSITY_RANGE.max) : 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,10 +123,9 @@ class WeatherManager {
|
|||||||
let world = await worldRepository.getFirst()
|
let world = await worldRepository.getFirst()
|
||||||
if (!world) world = new World()
|
if (!world) world = new World()
|
||||||
|
|
||||||
|
//the data model still contains the booleans
|
||||||
await world
|
await world
|
||||||
.setIsRainEnabled(this.weatherState.isRainEnabled)
|
|
||||||
.setRainPercentage(this.weatherState.rainPercentage)
|
.setRainPercentage(this.weatherState.rainPercentage)
|
||||||
.setIsFogEnabled(this.weatherState.isFogEnabled)
|
|
||||||
.setFogDensity(this.weatherState.fogDensity)
|
.setFogDensity(this.weatherState.fogDensity)
|
||||||
.save()
|
.save()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -38,7 +38,7 @@ class ChatService extends BaseService {
|
|||||||
|
|
||||||
public getArgs(command: string, message: string): string[] | undefined {
|
public getArgs(command: string, message: string): string[] | undefined {
|
||||||
if (!this.isCommand(message, command)) return
|
if (!this.isCommand(message, command)) return
|
||||||
return message.split(`/${command} `)[1].split(' ')
|
return message.slice(`/${command} `.length).split(' ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user