diff --git a/src/lib/data/ships.json b/src/lib/data/ships.json new file mode 100644 index 0000000..af942c5 --- /dev/null +++ b/src/lib/data/ships.json @@ -0,0 +1,57 @@ +[ + { + "id": "transporter", + "capacity": { + "solid": 10000, + "liquid": 10000, + "gas": 10000 + }, + "requirements": { + "buildings": [ + { + "id": "shipyard", + "level": 1 + } + ], + "research": [], + "resources": { + "iron": 1000, + "gold": 500 + } + }, + "time": 40, + "structure": { + "hitpoints": 1000, + "defense": 10, + "attack": 0 + }, + "speed": 10 + }, { + "id": "fighter", + "capacity": { + "solid": 100, + "liquid": 100, + "gas": 100 + }, + "requirements": { + "buildings": [ + { + "id": "shipyard", + "level": 1 + } + ], + "research": [], + "resources": { + "iron": 500, + "gold": 200 + } + }, + "time": 15, + "structure": { + "hitpoints": 800, + "defense": 20, + "attack": 20 + }, + "speed": 12 + } +] \ No newline at end of file diff --git a/src/lib/db/users.ts b/src/lib/db/users.ts index 6dd029e..b28d63e 100644 --- a/src/lib/db/users.ts +++ b/src/lib/db/users.ts @@ -7,6 +7,7 @@ import { ObjectId } from 'mongodb'; import { hash } from 'argon2' import { createInitialResources } from '../utils/resourceManager'; import type Research from '../../types/Research'; +import type Ship from '../../types/Ship'; export const getAllUsers = async () => { const users = await Users(); @@ -117,4 +118,31 @@ export const createOrUpgradeResearch = async (username: string, research: Resear { $push: { research } } ); } +} + +export const getUserShips = async (user: User): Promise> => { + const ships: Array = []; + + if (user?.ships !== undefined) { + user?.ships.forEach((ship: Ship) => { + ships.push(ship); + }); + } + + return ships; +} + +export const createOrUpgradeShip = async (user: User, ship: Ship) => { + const users = await Users(); + + const result = await users.updateOne( + { username: user.username, "ships.id": ship.id }, + { $set: { "ships.$.amount": ship.amount } } + ); + + if (result.modifiedCount === 0) { + await users.updateOne({ username: user.username }, + { $push: { ships: ship } } + ); + } } \ No newline at end of file diff --git a/src/lib/lang/en/ships.json b/src/lib/lang/en/ships.json new file mode 100644 index 0000000..a18c256 --- /dev/null +++ b/src/lib/lang/en/ships.json @@ -0,0 +1,12 @@ +{ + "Label": { + "transporter": { + "name": "Transporter", + "description": "Transporter can move resources between planets." + }, + "fighter": { + "name": "Fighter", + "description": "Fighter can attack enemy ships and planets." + } + } +} \ No newline at end of file diff --git a/src/pages/game/ships.astro b/src/pages/game/ships.astro new file mode 100644 index 0000000..9fad385 --- /dev/null +++ b/src/pages/game/ships.astro @@ -0,0 +1,136 @@ +--- +import Layout from '../../layouts/Layout.astro'; +import NavBar from '../../components/NavBar.astro'; +import { getUserByAccessToken } from '../../lib/db/users'; +import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver'; +import ResourceBar from '../../components/ResourceBar.astro'; + +import ships from '../../lib/data/ships.json'; + +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 locale = getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); +const langShips = await getLocales(locale, 'ships'); + +const playerShips = checkUser.ships; +console.log(playerShips) +--- + + + + + + {ships.map(ship => ( +
+
+ ({playerShips.find((s => s.id === ship.id))?.amount ?? 0}) {ship.id} +
+
+ ))} +
+ + + \ No newline at end of file diff --git a/src/types/Ship.ts b/src/types/Ship.ts new file mode 100644 index 0000000..3515b7f --- /dev/null +++ b/src/types/Ship.ts @@ -0,0 +1,4 @@ +export default interface Ship { + id: string; + amount: number; +} \ No newline at end of file diff --git a/src/types/User.ts b/src/types/User.ts index 8203bc4..fa7f6c0 100644 --- a/src/types/User.ts +++ b/src/types/User.ts @@ -2,6 +2,7 @@ import type { ObjectId } from "mongodb"; import type Building from "./Building"; import type DBResource from "./DBResource"; import type Research from "./Research"; +import type Ship from "./Ship"; export default interface User { _id: ObjectId; @@ -12,6 +13,7 @@ export default interface User { resources: Array; buildings: Array; research: Array; + ships: Array createdAt: Date; updatedAt: Date; } \ No newline at end of file