forked from noxious/server
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import type { UUID } from '#application/types'
|
|
|
|
import Logger, { LoggerType } from '#application/logger'
|
|
import { Map } from '#entities/map'
|
|
import LoadedMap from '#models/loadedMap'
|
|
import MapCharacter from '#models/mapCharacter'
|
|
import MapRepository from '#repositories/mapRepository'
|
|
|
|
class MapManager {
|
|
private readonly maps: Record<UUID, LoadedMap> = {}
|
|
private logger = Logger.type(LoggerType.GAME)
|
|
|
|
public async boot(): Promise<void> {
|
|
const mapRepository = new MapRepository()
|
|
const maps = await mapRepository.getAll()
|
|
await mapRepository.getEntityManager().populate(maps, mapRepository.POPULATE_ALL as never[])
|
|
|
|
await Promise.all(maps.map((map) => this.loadMap(map)))
|
|
|
|
this.logger.info(`Map manager loaded with ${Object.keys(this.maps).length} maps`)
|
|
}
|
|
|
|
public async loadMap(map: Map): Promise<void> {
|
|
this.maps[map.id] = new LoadedMap(map)
|
|
this.logger.info(`Map ID ${map.id} loaded`)
|
|
}
|
|
|
|
public unloadMap(mapId: UUID): void {
|
|
delete this.maps[mapId]
|
|
this.logger.info(`Map ID ${mapId} unloaded`)
|
|
}
|
|
|
|
public getLoadedMaps(): LoadedMap[] {
|
|
return Object.values(this.maps)
|
|
}
|
|
|
|
public getMapById(mapId: UUID): LoadedMap | undefined {
|
|
return this.maps[mapId]
|
|
}
|
|
|
|
public getCharacterById(characterId: UUID): MapCharacter | undefined {
|
|
for (const map of Object.values(this.maps)) {
|
|
const character = map.getCharactersInMap().find((char) => char.character.id === characterId)
|
|
if (character) return character
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
public removeCharacter(characterId: UUID): void {
|
|
Object.values(this.maps).forEach((map) => map.removeCharacter(characterId))
|
|
}
|
|
}
|
|
|
|
export default new MapManager()
|