Fix register script
This commit is contained in:
parent
e3be290d3d
commit
1e8e88636e
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 });
|
||||
|
|
|
@ -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({
|
||||
|
|
Loading…
Reference in New Issue