diff --git a/src/lib/classes/User.ts b/src/lib/classes/User.ts index 1b58122..5bf723d 100644 --- a/src/lib/classes/User.ts +++ b/src/lib/classes/User.ts @@ -1,6 +1,7 @@ import { ObjectId } from "mongodb"; import ResearchManager from "./managers/ResearchManager"; import { Planet } from "./managers/PlanetManager"; +import { updateUserMainPlanet } from "../db/users"; export default class User { id: ObjectId; @@ -26,7 +27,8 @@ export default class User { await this.research.init(); } - addMainPlanet(planet: Planet) { + async addMainPlanet(planet: Planet) { this.mainPlanet = planet; + await updateUserMainPlanet(this.id, planet._id); } } \ No newline at end of file diff --git a/src/lib/db/users.ts b/src/lib/db/users.ts index 602a7d8..2726b30 100644 --- a/src/lib/db/users.ts +++ b/src/lib/db/users.ts @@ -30,6 +30,11 @@ export const createUser = async (id: ObjectId, username: string, email: string, return user; } +export const updateUserMainPlanet = async (id: ObjectId, mainPlanet: ObjectId) => { + const users = await Users(); + users.updateOne({ _id: id }, { $set: { mainPlanet } }); +} + export const deleteUser = async (id: ObjectId) => { const users = await Users(); return users.deleteOne({ _id: id }); diff --git a/src/pages/register.astro b/src/pages/register.astro index 286f254..6fefbb1 100644 --- a/src/pages/register.astro +++ b/src/pages/register.astro @@ -10,10 +10,13 @@ import config from '../../config.json'; import { getAllGalaxies } from '../lib/db/galaxies'; import { createPlanet } from '../lib/db/planets'; import { addSystemToSector } from '../lib/db/sectors'; -import { createSystem } from '../lib/db/systems'; +import { addPlanetToSystem, createSystem } from '../lib/db/systems'; import DBPlanet from '../types/db/DBPlanet'; import DBSystem from '../types/db/DBSystem'; import locationManager from '../lib/classes/managers/LocationManager'; +import SystemManager, { System } from '../lib/classes/managers/SystemManager'; +import User from '../lib/classes/User'; +import ResearchManager from '../lib/classes/managers/ResearchManager'; let error = ""; @@ -39,38 +42,73 @@ if(Astro.request.method === "POST") { const user = await createUser(userId, username, email, password, planetId); + const userObject = new User(user._id, user.username, user.email, user.createdAt, user.updatedAt, user.lastLogin); + const sessionTime = config.SESSION_TIME_MINUTES * 60; const galaxyIndex = Math.floor(Math.random() * 4); const sectorIndex = Math.floor(Math.random() * 8); - - const planetData: DBPlanet = { - _id: planetId, - owner: user._id, - name: `${username}'s home planet`, - fields: 100, - buildings: [], - ships: [], - resources: [] - } - - await createPlanet(planetData); + const galaxies = await getAllGalaxies(); + const sectorId = galaxies[galaxyIndex].sectors[sectorIndex]; + const sector = locationManager.getSector(sectorId); + if(!sector) throw new Error("FATAL: Sector was not found in location manager."); const systemData: DBSystem = { _id: new ObjectId(), name: `${username}'s home system`, ownedBy: user._id, + planets: [], structures: [], - planets: [planetData._id], + resources: [], + ships: [], + defenses: [], + asteroids: [] } + const planetData: DBPlanet = { + _id: new ObjectId(), + name: `${username}'s home planet`, + owner: systemData.ownedBy, + fields: 40, + resources: [], + buildings: [], + ships: [], + defenses: [] + } + + systemData.planets.push(planetData._id); + await createSystem(systemData); + await createPlanet(planetData); + await addPlanetToSystem(systemData._id, planetData._id); - const galaxies = await getAllGalaxies(); - const sectorId = galaxies[galaxyIndex].sectors[sectorIndex]; - + const systemObject: System = { + _id: systemData._id, + sector: sector, + name: systemData.name, + ownedBy: userObject, + //@ts-ignore + structures: null, + //@ts-ignore + resources: null, + //@ts-ignore + ships: null, + //@ts-ignore + defenses: null, + planets: [], + asteroids: [] + } + + const systemManager = await new SystemManager(systemObject).fillData(systemData); + const planet = systemManager.planets.find(p => p._id.equals(planetData._id)); + if(!planet) throw new Error("FATAL: Planet was not created properly in system manager."); + + await userObject.addMainPlanet(planet); + sector.systems.push(systemManager); await addSystemToSector(sectorId, systemData._id); + locationManager.users.push(userObject); + const cookieOptions: AstroCookieSetOptions = { path: "/", maxAge: sessionTime, @@ -83,7 +121,6 @@ if(Astro.request.method === "POST") { Astro.cookies.set("currentPlanet", planetData._id.toString(), cookieOptions); Astro.cookies.set("currentSystem", systemData._id.toString(), cookieOptions); - await locationManager.init(); const res = await fetch(`${Astro.url.origin}/api/auth/generateAccessToken`, { method: 'POST', body: JSON.stringify({