1
0
forked from noxious/server

#174: Refactor character manager into zoneManager for better DX, major refactor of time and weather system (data is stored in DB now instead of JSON file), npm update, npm format, many other improvements

This commit is contained in:
2024-11-13 13:21:01 +01:00
parent 628b3bf1fa
commit d4e0cbe398
43 changed files with 465 additions and 461 deletions

View File

@ -3,53 +3,53 @@ import ZoneRepository from '../repositories/zoneRepository'
import ZoneService from '../services/zoneService'
import LoadedZone from '../models/loadedZone'
import { gameLogger } from '../utilities/logger'
import ZoneCharacter from '../models/zoneCharacter'
class ZoneManager {
private loadedZones: LoadedZone[] = []
private readonly zones = new Map<number, LoadedZone>()
// Method to initialize zoneEditor manager
public async boot() {
public async boot(): Promise<void> {
// Create first zone if it doesn't exist
if (!(await ZoneRepository.getById(1))) {
const zoneService = new ZoneService()
await zoneService.createDemoZone()
await new ZoneService().createDemoZone()
}
const zones = await ZoneRepository.getAll()
await Promise.all(zones.map((zone) => this.loadZone(zone)))
for (const zone of zones) {
await this.loadZone(zone)
}
gameLogger.info('Zone manager loaded')
gameLogger.info(`Zone manager loaded with ${this.zones.size} zones`)
}
// Method to handle individual zoneEditor loading
public async loadZone(zone: Zone) {
public async loadZone(zone: Zone): Promise<void> {
const loadedZone = new LoadedZone(zone)
this.loadedZones.push(loadedZone)
this.zones.set(zone.id, loadedZone)
gameLogger.info(`Zone ID ${zone.id} loaded`)
}
// Method to handle individual zoneEditor unloading
public unloadZone(zoneId: number) {
this.loadedZones = this.loadedZones.filter((loadedZone) => loadedZone.getZone().id !== zoneId)
public unloadZone(zoneId: number): void {
this.zones.delete(zoneId)
gameLogger.info(`Zone ID ${zoneId} unloaded`)
}
// Getter for loaded zones
public getLoadedZones(): LoadedZone[] {
return this.loadedZones
return Array.from(this.zones.values())
}
// Getter for zone by id
public getZoneById(zoneId: number): LoadedZone | undefined {
return this.loadedZones.find((loadedZone) => loadedZone.getZone().id === zoneId)
return this.zones.get(zoneId)
}
}
export interface ZoneAssets {
tiles: string[]
objects: string[]
public getCharacter(characterId: number): ZoneCharacter | undefined {
for (const zone of this.zones.values()) {
const character = zone.getCharactersInZone().find((char) => char.character.id === characterId)
if (character) return character
}
return undefined
}
public removeCharacter(characterId: number): void {
this.zones.forEach((zone) => zone.removeCharacter(characterId))
}
}
export default new ZoneManager()