From a5d4431f5212cfa8a545cc4a8f3f1f85c5436626 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Thu, 3 Oct 2024 12:23:40 +0200 Subject: [PATCH] Add planet and system switcher --- src/components/NavBar.astro | 7 ++ src/lib/classes/managers/LocationManager.ts | 6 +- src/lib/classes/managers/PlanetManager.ts | 5 +- src/pages/game/index.astro | 12 +++ src/pages/game/systemManager/index.astro | 94 +++++++++++++++++++++ src/pages/game/systemManager/select.astro | 77 +++++++++++++++++ 6 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 src/pages/game/systemManager/index.astro create mode 100644 src/pages/game/systemManager/select.astro diff --git a/src/components/NavBar.astro b/src/components/NavBar.astro index 8afcc74..7721bd9 100644 --- a/src/components/NavBar.astro +++ b/src/components/NavBar.astro @@ -98,6 +98,13 @@ const listOfElements: Array = [{ url: "/game/galaxyView", show: "loggedInOnly", position: "bottom" +}, { + id: "systemManager", + title: getName(lang, "general", "nav-system-manager"), + type: "simple", + url: "/game/systemManager", + show: "loggedInOnly", + position: "bottom" }, { id: "profile", title: getName(lang, "general", "nav-profile"), diff --git a/src/lib/classes/managers/LocationManager.ts b/src/lib/classes/managers/LocationManager.ts index 7a26265..b115887 100644 --- a/src/lib/classes/managers/LocationManager.ts +++ b/src/lib/classes/managers/LocationManager.ts @@ -89,14 +89,18 @@ class LocationManager { planets: new PlanetManager(user) }; - await systemObject.planets.fillData(systemData.planets); + 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); }; diff --git a/src/lib/classes/managers/PlanetManager.ts b/src/lib/classes/managers/PlanetManager.ts index 8f04ca1..53d5e32 100644 --- a/src/lib/classes/managers/PlanetManager.ts +++ b/src/lib/classes/managers/PlanetManager.ts @@ -4,6 +4,7 @@ import { getPlanetById } from "../../db/planets"; import ResourceManager from "./ResourceManager"; import User from "../User"; import ShipManager from "./ShipManager"; +import { System } from "./LocationManager"; export type Planet = { _id: ObjectId; @@ -18,12 +19,14 @@ export type Planet = { export default class PlanetManager { planets: Array = []; owner: User; + system!: System; constructor(user: User) { this.owner = user; } - async fillData(planets: ObjectId[]) { + async fillData(system: System, planets: ObjectId[]) { + this.system = system; await Promise.all(planets.map(async planetId => { const planet = await getPlanetById(planetId); diff --git a/src/pages/game/index.astro b/src/pages/game/index.astro index 5cd2a17..6514e48 100644 --- a/src/pages/game/index.astro +++ b/src/pages/game/index.astro @@ -3,6 +3,8 @@ import Layout from '../../layouts/Layout.astro'; import NavBar from '../../components/NavBar.astro'; import ResourceBar from '../../components/ResourceBar.astro'; import { getUserByAccessToken } from '../../lib/db/users'; +import locationManager from '../../lib/classes/managers/LocationManager'; +import { ObjectId } from 'mongodb'; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const username = Astro.cookies.get('username')?.value ?? ""; @@ -10,11 +12,21 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout'); const checkUser = await getUserByAccessToken(loggedToken); if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); + +const currentPlanetId = Astro.cookies.get('planetid')?.value ?? null; +if(currentPlanetId === null) return Astro.redirect('/game/logout'); +const currentPlanet = locationManager.getPlanet(new ObjectId(currentPlanetId)); +if(currentPlanet === undefined) { + Astro.cookies.delete('planetid'); + return Astro.redirect('/game/logout'); +} --- + +

{currentPlanet.name} in {currentPlanet.manager.system.name}

\ No newline at end of file diff --git a/src/pages/game/systemManager/select.astro b/src/pages/game/systemManager/select.astro new file mode 100644 index 0000000..cbf700d --- /dev/null +++ b/src/pages/game/systemManager/select.astro @@ -0,0 +1,77 @@ +--- +import NavBar from "../../../components/NavBar.astro"; +import ResourceBar from "../../../components/ResourceBar.astro"; +import Layout from "../../../layouts/Layout.astro"; +import locationManager from "../../../lib/classes/managers/LocationManager"; +import { getUserByAccessToken } from "../../../lib/db/users"; + +const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; +const username = Astro.cookies.get('username')?.value ?? ""; +if(loggedToken === null || username === "") return Astro.redirect('/logout'); + +const checkUser = await getUserByAccessToken(loggedToken); +if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); + +if(Astro.request.method === "POST") { + const selectedSystemId = (await Astro.request.formData()).get('systemId') as string | null; + + if(selectedSystemId !== null) { + Astro.cookies.set('currentSystem', selectedSystemId, { + path: '/', + expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7), + sameSite: 'lax', + secure: true + }); + + return Astro.redirect('/game/systemManager'); + } +} + +const systems = locationManager.getSystemsOwnedBy(checkUser._id); +--- + + + + +
+ {systems.map(system => ( +
+

{system.name}

+
+
+ ))} +
+
+ \ No newline at end of file