180 lines
5.2 KiB
TypeScript
180 lines
5.2 KiB
TypeScript
import { ObjectId } from "mongodb";
|
|
import PlanetManager, { Planet } from "./PlanetManager";
|
|
import { getAllGalaxies } from "../../db/galaxies";
|
|
import { getSectorById } from "../../db/sectors";
|
|
import { getSystemById } from "../../db/systems";
|
|
import { getAllUsers } from "../../db/users";
|
|
import User from "../User";
|
|
|
|
export type Galaxy = {
|
|
_id: ObjectId,
|
|
name: string,
|
|
sectors: Array<Sector>
|
|
coords: {
|
|
x: number,
|
|
y: number
|
|
}
|
|
}
|
|
|
|
export type Sector = {
|
|
_id: ObjectId,
|
|
galaxy: Galaxy,
|
|
name: string,
|
|
expedition: null,
|
|
systems: Array<System>
|
|
}
|
|
|
|
export type System = {
|
|
_id: ObjectId,
|
|
sector: Sector,
|
|
name: string,
|
|
ownedBy: User,
|
|
planets: PlanetManager
|
|
}
|
|
|
|
class LocationManager {
|
|
private static instance: LocationManager;
|
|
|
|
private constructor() {}
|
|
|
|
public static getInstance(): LocationManager {
|
|
if (!LocationManager.instance) {
|
|
LocationManager.instance = new LocationManager();
|
|
}
|
|
return LocationManager.instance;
|
|
}
|
|
|
|
galaxies: Galaxy[] = [];
|
|
users: User[] = [];
|
|
|
|
async init() {
|
|
const users = await getAllUsers();
|
|
users.forEach(async user => {
|
|
this.users.push(new User(user._id, user.username, user.email, user.createdAt, user.updatedAt, user.lastLogin));
|
|
});
|
|
|
|
console.log(`Loaded ${users.length} users from database`);
|
|
|
|
const galaxies = await getAllGalaxies();
|
|
for(const galaxy of galaxies) {
|
|
const galaxyObject: Galaxy = {
|
|
_id: galaxy._id,
|
|
name: galaxy.name,
|
|
sectors: [],
|
|
coords: galaxy.coords
|
|
};
|
|
|
|
for(const sectorId of galaxy.sectors) {
|
|
const sectorData = await getSectorById(sectorId);
|
|
|
|
const sectorObject: Sector = {
|
|
_id: sectorData._id,
|
|
galaxy: galaxyObject,
|
|
name: sectorData.name,
|
|
expedition: null,
|
|
systems: []
|
|
};
|
|
|
|
for(const systemId of sectorData.systems) {
|
|
const systemData = await getSystemById(systemId);
|
|
const user = this.users.find(user => user.id.equals(systemData.ownedBy));
|
|
|
|
if(!user) throw new Error("User not found");
|
|
|
|
const systemObject: System = {
|
|
_id: systemData._id,
|
|
sector: sectorObject,
|
|
name: systemData.name,
|
|
ownedBy: user,
|
|
planets: new PlanetManager(user)
|
|
};
|
|
|
|
await systemObject.planets.fillData(systemObject, systemData.planets);
|
|
|
|
sectorObject.systems.push(systemObject);
|
|
};
|
|
|
|
console.log(`Loaded ${sectorObject.systems.length} systems from sector ${sectorObject.name}`);
|
|
|
|
galaxyObject.sectors.push(sectorObject);
|
|
};
|
|
|
|
console.log(`Loaded ${galaxyObject.sectors.length} sectors from galaxy ${galaxyObject.name}`);
|
|
|
|
this.galaxies.push(galaxyObject);
|
|
};
|
|
|
|
console.log(`Loaded ${this.galaxies.length} galaxies from database`);
|
|
|
|
for(const user of this.users) {
|
|
await user.init();
|
|
const userDB = users.find(u => u._id.equals(user.id));
|
|
const mainPlanet = this.getPlanet(userDB?.mainPlanet as ObjectId);
|
|
|
|
if(!mainPlanet) throw new Error("Main planet not found");
|
|
|
|
user.mainPlanet = mainPlanet;
|
|
};
|
|
|
|
return this;
|
|
}
|
|
|
|
getGalaxy(_id: ObjectId) {
|
|
return this.galaxies.find(galaxy => galaxy._id.equals(_id));
|
|
}
|
|
|
|
getSector(_id: ObjectId) {
|
|
let foundSector: Sector | undefined;
|
|
this.galaxies.find(galaxy => galaxy.sectors.find(sector => {
|
|
if(sector._id.equals(_id)) foundSector = sector;
|
|
}));
|
|
return foundSector;
|
|
}
|
|
|
|
getSystem(_id: ObjectId) {
|
|
let foundSystem: System | undefined;
|
|
this.galaxies.find(galaxy => galaxy.sectors.find(sector => sector.systems.find(system => {
|
|
if(system._id.equals(_id)) foundSystem = system
|
|
})));
|
|
return foundSystem
|
|
}
|
|
|
|
getSystemsOwnedBy(id: ObjectId) {
|
|
const systems: System[] = [];
|
|
|
|
for(const galaxy of this.galaxies) {
|
|
for(const sector of galaxy.sectors) {
|
|
for(const system of sector.systems) {
|
|
if(system.ownedBy.id.equals(id)) systems.push(system);
|
|
}
|
|
}
|
|
}
|
|
|
|
return systems;
|
|
}
|
|
|
|
getPlanet(_id: ObjectId) {
|
|
let foundPlanet: Planet | undefined;
|
|
|
|
for(const galaxy of this.galaxies) {
|
|
for(const sector of galaxy.sectors) {
|
|
for(const system of sector.systems) {
|
|
foundPlanet = system.planets.getPlanetById(_id);
|
|
if(foundPlanet) break;
|
|
}
|
|
if(foundPlanet) break;
|
|
}
|
|
if(foundPlanet) break;
|
|
}
|
|
|
|
return foundPlanet;
|
|
}
|
|
|
|
getUser(_id: ObjectId) {
|
|
return this.users.find(user => user.id.equals(_id));
|
|
}
|
|
}
|
|
|
|
const locationManager = LocationManager.getInstance();
|
|
locationManager.init();
|
|
export default locationManager; |