Add planet and system switcher

This commit is contained in:
Aelita4 2024-10-03 12:23:40 +02:00
parent 38960ef308
commit a5d4431f52
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
6 changed files with 199 additions and 2 deletions

View File

@ -98,6 +98,13 @@ const listOfElements: Array<NavElement> = [{
url: "/game/galaxyView", url: "/game/galaxyView",
show: "loggedInOnly", show: "loggedInOnly",
position: "bottom" position: "bottom"
}, {
id: "systemManager",
title: getName(lang, "general", "nav-system-manager"),
type: "simple",
url: "/game/systemManager",
show: "loggedInOnly",
position: "bottom"
}, { }, {
id: "profile", id: "profile",
title: getName(lang, "general", "nav-profile"), title: getName(lang, "general", "nav-profile"),

View File

@ -89,14 +89,18 @@ class LocationManager {
planets: new PlanetManager(user) planets: new PlanetManager(user)
}; };
await systemObject.planets.fillData(systemData.planets); await systemObject.planets.fillData(systemObject, systemData.planets);
sectorObject.systems.push(systemObject); sectorObject.systems.push(systemObject);
}; };
console.log(`Loaded ${sectorObject.systems.length} systems from sector ${sectorObject.name}`);
galaxyObject.sectors.push(sectorObject); galaxyObject.sectors.push(sectorObject);
}; };
console.log(`Loaded ${galaxyObject.sectors.length} sectors from galaxy ${galaxyObject.name}`);
this.galaxies.push(galaxyObject); this.galaxies.push(galaxyObject);
}; };

View File

@ -4,6 +4,7 @@ import { getPlanetById } from "../../db/planets";
import ResourceManager from "./ResourceManager"; import ResourceManager from "./ResourceManager";
import User from "../User"; import User from "../User";
import ShipManager from "./ShipManager"; import ShipManager from "./ShipManager";
import { System } from "./LocationManager";
export type Planet = { export type Planet = {
_id: ObjectId; _id: ObjectId;
@ -18,12 +19,14 @@ export type Planet = {
export default class PlanetManager { export default class PlanetManager {
planets: Array<Planet> = []; planets: Array<Planet> = [];
owner: User; owner: User;
system!: System;
constructor(user: User) { constructor(user: User) {
this.owner = user; this.owner = user;
} }
async fillData(planets: ObjectId[]) { async fillData(system: System, planets: ObjectId[]) {
this.system = system;
await Promise.all(planets.map(async planetId => { await Promise.all(planets.map(async planetId => {
const planet = await getPlanetById(planetId); const planet = await getPlanetById(planetId);

View File

@ -3,6 +3,8 @@ import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro'; import NavBar from '../../components/NavBar.astro';
import ResourceBar from '../../components/ResourceBar.astro'; import ResourceBar from '../../components/ResourceBar.astro';
import { getUserByAccessToken } from '../../lib/db/users'; 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 loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
const username = Astro.cookies.get('username')?.value ?? ""; const username = Astro.cookies.get('username')?.value ?? "";
@ -10,11 +12,21 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout');
const checkUser = await getUserByAccessToken(loggedToken); const checkUser = await getUserByAccessToken(loggedToken);
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); 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');
}
--- ---
<Layout title="Overview"> <Layout title="Overview">
<NavBar loggedIn="true" active="overview" /> <NavBar loggedIn="true" active="overview" />
<ResourceBar /> <ResourceBar />
<h1>{currentPlanet.name} in {currentPlanet.manager.system.name}</h1>
</Layout> </Layout>
<style> <style>

View File

@ -0,0 +1,94 @@
---
import { ObjectId } from "mongodb";
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');
const currentSystemId = Astro.cookies.get('currentSystem')?.value ?? null;
if(currentSystemId === null) return Astro.redirect('/game/systemManager/select');
const currentSystem = locationManager.getSystem(new ObjectId(currentSystemId));
if(currentSystem === undefined) {
Astro.cookies.delete('currentSystem');
return Astro.redirect('/game/systemManager/select');
}
if(Astro.request.method === "POST") {
const selectedPlanetId = (await Astro.request.formData()).get('planetId') as string | null;
if(selectedPlanetId !== null) {
Astro.cookies.set('planetid', selectedPlanetId, {
path: '/',
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7),
sameSite: 'lax',
secure: true
});
}
}
---
<Layout title="System Manager">
<NavBar loggedIn="true" active="systemManager" />
<ResourceBar />
<h1>Selected system: {currentSystem.name} <a href="/game/systemManager/select">(change)</a></h1>
<div class="planetList">
{currentSystem.planets.planets.length === 0 ? <span>No planets in this sector</span> : currentSystem.planets.planets.map(planet => (
<div class="planetCard">
<h2>{planet.name}<div><form method="post"><input type="hidden" name="planetId" value={planet._id.toString()} /><input type="submit" value="Select" /></form></div></h2>
<p>Fields: {planet.fields}</p>
<h3>Buildings:</h3>
<div>
{planet.buildings.buildings.length === 0 ? <span>None</span> : planet.buildings.buildings.map(building => (
<p>{building.data.id} - {building.level}</p>
))}
</div>
<h3>Ships: </h3>
<div>
{planet.ships.ships.length === 0 ? <span>None</span> : planet.ships.ships.map(ship => (
<p>{ship.data.id} - {ship.amount}</p>
))}
</div>
</div>
))}
</div>
</Layout>
<style>
h1 {
text-align: center;
color: white;
}
.planetList {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
color: white;
}
.system-card input {
background-color: #555;
color: white;
border: none;
padding: 0.5rem;
border-radius: 5px;
cursor: pointer;
}
.system-card div {
margin-top: auto;
margin-bottom: auto;
margin-left: 2rem;
}
</style>

View File

@ -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);
---
<Layout title="System Manager">
<NavBar loggedIn="true" active="systemManager" />
<ResourceBar />
<div class="systems-list">
{systems.map(system => (
<div class="system-card">
<h2>{system.name}</h2>
<div><form method="post"><input type="hidden" name="systemId" value={system._id.toString()} /><input type="submit" value="Select" /></form></div>
</div>
))}
</div>
</Layout>
<style>
.systems-list {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: center;
}
.system-card {
background-color: #333;
color: white;
padding: 1rem;
margin: 1rem;
border-radius: 5px;
display: flex;
flex-direction: row;
}
.system-card input {
background-color: #555;
color: white;
border: none;
padding: 0.5rem;
border-radius: 5px;
cursor: pointer;
}
.system-card div {
margin-top: auto;
margin-bottom: auto;
margin-left: 2rem;
}
</style>