From bc95e101a5ef832c3e0b0bbdc51e4488dbd7c638 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Fri, 7 Feb 2025 12:46:49 +0100 Subject: [PATCH] Fix register script --- src/pages/register.astro | 73 ++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 18 deletions(-) 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({