52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import Logger, { LoggerType } from '#application/logger'
|
|
import { UUID } from '#application/types'
|
|
import { Zone } from '#entities/zone'
|
|
import LoadedZone from '#models/loadedZone'
|
|
import ZoneCharacter from '#models/zoneCharacter'
|
|
import ZoneRepository from '#repositories/zoneRepository'
|
|
|
|
class ZoneManager {
|
|
private readonly zones = new Map<UUID, LoadedZone>()
|
|
private logger = Logger.type(LoggerType.GAME)
|
|
|
|
public async boot(): Promise<void> {
|
|
const zones = await ZoneRepository.getAll()
|
|
await Promise.all(zones.map((zone) => this.loadZone(zone)))
|
|
|
|
this.logger.info(`Zone manager loaded with ${this.zones.size} zones`)
|
|
}
|
|
|
|
public async loadZone(zone: Zone): Promise<void> {
|
|
const loadedZone = new LoadedZone(zone)
|
|
this.zones.set(zone.id, loadedZone)
|
|
this.logger.info(`Zone ID ${zone.id} loaded`)
|
|
}
|
|
|
|
public unloadZone(zoneId: UUID): void {
|
|
this.zones.delete(zoneId)
|
|
this.logger.info(`Zone ID ${zoneId} unloaded`)
|
|
}
|
|
|
|
public getLoadedZones(): LoadedZone[] {
|
|
return Array.from(this.zones.values())
|
|
}
|
|
|
|
public getZoneById(zoneId: UUID): LoadedZone | undefined {
|
|
return this.zones.get(zoneId)
|
|
}
|
|
|
|
public getCharacterById(characterId: UUID): 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: UUID): void {
|
|
this.zones.forEach((zone) => zone.removeCharacter(characterId))
|
|
}
|
|
}
|
|
|
|
export default new ZoneManager()
|