forked from noxious/server
77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
import { Zone, ZoneEventTileTeleport, ZoneEventTileType } from '@prisma/client'
|
|
import ZoneRepository from '../repositories/zoneRepository'
|
|
import ZoneService from '../services/zoneService'
|
|
import logger from '../utilities/logger'
|
|
import LoadedZone from '../models/loadedZone'
|
|
import zoneRepository from '../repositories/zoneRepository'
|
|
import { beforeEach } from 'node:test'
|
|
import prisma from '../utilities/prisma'
|
|
|
|
class ZoneManager {
|
|
private loadedZones: LoadedZone[] = []
|
|
|
|
// Method to initialize zoneEditor manager
|
|
public async boot() {
|
|
if (!(await ZoneRepository.getById(1))) {
|
|
const zoneService = new ZoneService()
|
|
await zoneService.createDemoZone()
|
|
}
|
|
|
|
const zones = await ZoneRepository.getAll()
|
|
|
|
for (const zone of zones) {
|
|
await this.loadZone(zone)
|
|
}
|
|
|
|
logger.info('Zone manager loaded')
|
|
}
|
|
|
|
public async getZoneAssets(zone: Zone): Promise<ZoneAssets> {
|
|
const tiles: string[] = this.getUnique(
|
|
(JSON.parse(JSON.stringify(zone.tiles)) as string[][]).reduce((acc, val) => [...acc, ...val])
|
|
);
|
|
const objects = await zoneRepository.getObjects(zone.id);
|
|
const mappedObjects = this.getUnique(objects.map(x => x.objectId));
|
|
|
|
return {
|
|
tiles: tiles,
|
|
objects: mappedObjects,
|
|
} as ZoneAssets;
|
|
}
|
|
|
|
private getUnique<T>(array: T[]) {
|
|
return [...new Set<T>(array)]
|
|
}
|
|
|
|
// Method to handle individual zoneEditor loading
|
|
public async loadZone(zone: Zone) {
|
|
const loadedZone = new LoadedZone(zone)
|
|
this.loadedZones.push(loadedZone)
|
|
await this.getZoneAssets(zone);
|
|
logger.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)
|
|
logger.info(`Zone ID ${zoneId} unloaded`)
|
|
}
|
|
|
|
// Getter for loaded zones
|
|
public getLoadedZones(): LoadedZone[] {
|
|
return this.loadedZones
|
|
}
|
|
|
|
// Getter for zone by id
|
|
public getZoneById(zoneId: number): LoadedZone | undefined {
|
|
return this.loadedZones.find((loadedZone) => loadedZone.getZone().id === zoneId)
|
|
}
|
|
}
|
|
|
|
export interface ZoneAssets {
|
|
tiles: string[]
|
|
objects: string[]
|
|
}
|
|
|
|
export default new ZoneManager()
|