Implement fleet manager
This commit is contained in:
parent
37097bb361
commit
5711506579
|
@ -0,0 +1,12 @@
|
|||
import DBShip from "../../types/db/DBShip";
|
||||
|
||||
export default class FleetShip {
|
||||
stored: Array<{ id: string, amount: number }> = [];
|
||||
hitpoints: number;
|
||||
data: DBShip;
|
||||
|
||||
constructor(data: DBShip, hitpoints: number) {
|
||||
this.data = data;
|
||||
this.hitpoints = hitpoints;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import { ObjectId } from "mongodb";
|
||||
import ResearchManager from "./managers/ResearchManager";
|
||||
import { Planet } from "./managers/PlanetManager";
|
||||
import FleetManager from "./managers/FleetManager";
|
||||
|
||||
export default class User {
|
||||
id: ObjectId;
|
||||
|
@ -10,6 +11,7 @@ export default class User {
|
|||
updatedAt: Date;
|
||||
lastLogin: Date;
|
||||
research: ResearchManager;
|
||||
fleet: FleetManager;
|
||||
mainPlanet!: Planet;
|
||||
|
||||
constructor(id: ObjectId, username: string, email: string, createdAt: Date, updatedAt: Date, lastLogin: Date) {
|
||||
|
@ -20,6 +22,7 @@ export default class User {
|
|||
this.updatedAt = updatedAt;
|
||||
this.lastLogin = lastLogin;
|
||||
this.research = new ResearchManager(this);
|
||||
this.fleet = new FleetManager(this);
|
||||
}
|
||||
|
||||
async init() {
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import FleetShip from "../FleetShip";
|
||||
import User from "../User";
|
||||
|
||||
export default class FleetManager {
|
||||
user: User;
|
||||
fleet: Array<FleetShip> = [];
|
||||
|
||||
constructor(user: User) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
import { ObjectId } from 'mongodb';
|
||||
import DBFleet from '../../types/db/DBFleet';
|
||||
import { Planet } from '../classes/managers/PlanetManager';
|
||||
import { Fleet, Planets } from '../db/mongodb';
|
||||
|
||||
export const getAllFleet = async () => {
|
||||
return (await Fleet()).find({}).toArray() as unknown as Array<DBFleet>;
|
||||
}
|
||||
|
||||
export const getAllFleetByUser = async (userId: ObjectId) => {
|
||||
const planets = await (await Planets()).find({ owner: userId }).toArray();
|
||||
|
||||
const fleets = new Map<string, DBFleet>();
|
||||
|
||||
for(const planet of planets) {
|
||||
const fleet = await (await Fleet()).find({ $or: [
|
||||
{ source: planet._id },
|
||||
{ destination: planet._id }
|
||||
] }).toArray() as unknown as DBFleet[];
|
||||
|
||||
for(const f of fleet) {
|
||||
fleets.set(f._id.toString(), f);
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(fleets.values());
|
||||
}
|
||||
|
||||
export const createFleet = async (source: Planet, destination: Planet, mission: string, ships: Array<{ id: string, amount: number }>) => {
|
||||
const fleet = {
|
||||
source: source._id,
|
||||
destination: destination._id,
|
||||
finished: false,
|
||||
returning: false,
|
||||
mission,
|
||||
ships
|
||||
}
|
||||
|
||||
await (await Fleet()).insertOne(fleet);
|
||||
}
|
|
@ -71,6 +71,11 @@ export const Ships = async() => {
|
|||
return db.collection('ships');
|
||||
}
|
||||
|
||||
export const Fleet = async() => {
|
||||
const db = await getDB();
|
||||
return db.collection('fleet');
|
||||
}
|
||||
|
||||
export const Lang = async (language = "en") => {
|
||||
const db = await getDB(`${config.MONGODB_DB}_${language}`);
|
||||
return [
|
||||
|
|
|
@ -0,0 +1,154 @@
|
|||
---
|
||||
import Layout from '../../layouts/Layout.astro';
|
||||
import NavBar from '../../components/NavBar.astro';
|
||||
import { getUserByAccessToken } from '../../lib/db/users';
|
||||
import { getHighestWeightedLanguage, getLocales, getName, getObj } from '../../lib/utils/langDriver';
|
||||
import ResourceBar from '../../components/ResourceBar.astro';
|
||||
import locationManager from '../../lib/classes/managers/LocationManager';
|
||||
import { getAllFleetByUser } from '../../lib/db/fleet';
|
||||
import { getAllShips } from '../../lib/db/ships';
|
||||
|
||||
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 user = locationManager.getUser(checkUser._id);
|
||||
if(!user) return Astro.redirect('/logout');
|
||||
|
||||
const ships = await getAllShips();
|
||||
|
||||
const fleet = await getAllFleetByUser(user.id);
|
||||
|
||||
const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')));
|
||||
---
|
||||
|
||||
<Layout title="Fleet">
|
||||
<NavBar loggedIn="true" active="fleet" />
|
||||
<ResourceBar />
|
||||
|
||||
{fleet.map(f => <>
|
||||
<div class="ship-cards">
|
||||
{locationManager.getPlanet(f.source)?.name ?? "?"} => {locationManager.getPlanet(f.destination)?.name ?? "?"} <br />
|
||||
Containing: {f.ships.map(s => {
|
||||
const ship = ships.find(ship => ship.id === s.id);
|
||||
return `${getName(lang, 'ships', s.id)} x${s.amount}`;
|
||||
}).join(', ')} <br />
|
||||
</div>
|
||||
</>)}
|
||||
</Layout>
|
||||
|
||||
<style>
|
||||
* {
|
||||
color: white;
|
||||
}
|
||||
|
||||
main {
|
||||
margin: auto;
|
||||
padding: 1rem;
|
||||
width: 800px;
|
||||
max-width: calc(100% - 2rem);
|
||||
color: white;
|
||||
font-size: 20px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.astro-a {
|
||||
position: absolute;
|
||||
top: -32px;
|
||||
left: 50%;
|
||||
transform: translatex(-50%);
|
||||
width: 220px;
|
||||
height: auto;
|
||||
z-index: -1;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 2rem;
|
||||
font-weight: 700;
|
||||
line-height: 1;
|
||||
text-align: center;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.text-gradient {
|
||||
background-image: var(--accent-gradient);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
background-size: 400%;
|
||||
background-position: 0%;
|
||||
}
|
||||
|
||||
.instructions {
|
||||
margin-bottom: 2rem;
|
||||
border: 1px solid rgba(var(--accent-light), 25%);
|
||||
background: linear-gradient(rgba(var(--accent-dark), 66%), rgba(var(--accent-dark), 33%));
|
||||
padding: 1.5rem;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.instructions code {
|
||||
font-size: 0.8em;
|
||||
font-weight: bold;
|
||||
background: rgba(var(--accent-light), 12%);
|
||||
color: rgb(var(--accent-light));
|
||||
border-radius: 4px;
|
||||
padding: 0.3em 0.4em;
|
||||
}
|
||||
|
||||
.instructions strong {
|
||||
color: rgb(var(--accent-light));
|
||||
}
|
||||
|
||||
.link-card-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));
|
||||
gap: 2rem;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.a-button {
|
||||
text-decoration: none;
|
||||
color: green;
|
||||
height: fit-content;
|
||||
}
|
||||
|
||||
.a-button:hover {
|
||||
color: lime;
|
||||
}
|
||||
|
||||
.ship-cards {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
row-gap: 40px;
|
||||
column-gap: 2%;
|
||||
margin-top: 40px;
|
||||
}
|
||||
|
||||
#ship-modal-background {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
#ship-modal-details {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 80%;
|
||||
max-width: 800px;
|
||||
background: rgba(0, 0, 0, 0.9);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
z-index: 101;
|
||||
}
|
|
@ -3,6 +3,7 @@ import { ObjectId } from "mongodb";
|
|||
export default interface DBPlanet {
|
||||
_id: ObjectId;
|
||||
name: string;
|
||||
owner: ObjectId; // shouldn't be here
|
||||
fields: number;
|
||||
resources: Array<{ id: string, amount: number, lastUpdated: Date, perHourMiningRate: number }>;
|
||||
buildings: Array<{ id: string, level: number }>;
|
||||
|
|
Loading…
Reference in New Issue