diff --git a/src/lib/classes/FleetShip.ts b/src/lib/classes/FleetShip.ts new file mode 100644 index 0000000..3106310 --- /dev/null +++ b/src/lib/classes/FleetShip.ts @@ -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; + } +} \ No newline at end of file diff --git a/src/lib/classes/User.ts b/src/lib/classes/User.ts index 1b58122..2cebc7e 100644 --- a/src/lib/classes/User.ts +++ b/src/lib/classes/User.ts @@ -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() { diff --git a/src/lib/classes/managers/FleetManager.ts b/src/lib/classes/managers/FleetManager.ts new file mode 100644 index 0000000..72707fb --- /dev/null +++ b/src/lib/classes/managers/FleetManager.ts @@ -0,0 +1,13 @@ +import FleetShip from "../FleetShip"; +import User from "../User"; + +export default class FleetManager { + user: User; + fleet: Array = []; + + constructor(user: User) { + this.user = user; + } + + +} \ No newline at end of file diff --git a/src/lib/db/fleet.ts b/src/lib/db/fleet.ts new file mode 100644 index 0000000..b7979e0 --- /dev/null +++ b/src/lib/db/fleet.ts @@ -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; +} + +export const getAllFleetByUser = async (userId: ObjectId) => { + const planets = await (await Planets()).find({ owner: userId }).toArray(); + + const fleets = new Map(); + + 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); +} \ No newline at end of file diff --git a/src/lib/db/mongodb.ts b/src/lib/db/mongodb.ts index bd5449c..45d82f6 100644 --- a/src/lib/db/mongodb.ts +++ b/src/lib/db/mongodb.ts @@ -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 [ diff --git a/src/pages/game/fleet.astro b/src/pages/game/fleet.astro new file mode 100644 index 0000000..dc9ce67 --- /dev/null +++ b/src/pages/game/fleet.astro @@ -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'))); +--- + + + + + + {fleet.map(f => <> +
+ {locationManager.getPlanet(f.source)?.name ?? "?"} => {locationManager.getPlanet(f.destination)?.name ?? "?"}
+ Containing: {f.ships.map(s => { + const ship = ships.find(ship => ship.id === s.id); + return `${getName(lang, 'ships', s.id)} x${s.amount}`; + }).join(', ')}
+
+ )} +
+ +