From 564a3942a30dfabd69f5acb8498fb3d5e078bb85 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Tue, 30 Jan 2024 10:00:43 +0100 Subject: [PATCH] Implement (somewhat) functional resource bar --- src/components/ResourceBar.astro | 172 ++++++++++++++++++++++++++++--- src/lib/data/resources.json | 49 +++++++++ src/lib/db/users.ts | 4 +- src/lib/lang/en/resources.json | 14 ++- src/pages/game/buildings.astro | 27 ++--- src/pages/game/galaxyView.astro | 6 +- src/pages/game/index.astro | 32 ++---- src/pages/game/profile.astro | 6 +- src/pages/login.astro | 9 +- src/pages/logout.astro | 8 ++ src/pages/register.astro | 9 +- src/types/Resource.ts | 5 + src/types/Resources.ts | 8 ++ src/types/User.ts | 2 +- 14 files changed, 277 insertions(+), 74 deletions(-) create mode 100644 src/lib/data/resources.json create mode 100644 src/types/Resource.ts diff --git a/src/components/ResourceBar.astro b/src/components/ResourceBar.astro index bc6850e..065f296 100644 --- a/src/components/ResourceBar.astro +++ b/src/components/ResourceBar.astro @@ -1,32 +1,174 @@ --- +import { ObjectId } from 'mongodb'; +import { getUserResources } from '../lib/db/users'; import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver'; -interface Props { - loggedIn: string; + +import resourceTypes from '../lib/data/resources.json'; + +const resourceLang = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resources'); + +const resources = await getUserResources(new ObjectId(Astro.cookies.get('userid')?.value ?? '')); + +const resourceArray = []; +for(const key in resources) { + resourceArray.push({ + name: key, + amount: resources[key as never] + }); } - -const lang = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resourcebar'); - -// const { loggedIn } = Astro.props; --- -
-
- -
coal
+
+
+
+
+ {resourceArray.map(res => +
x.name === res.name)?.type ?? "solid"} style={(resourceTypes.find(x => x.name === res.name)?.type ?? "solid") === "solid" ? "" : "display: none;"}> +
+ x.name === res.name)?.icon ?? "#"} alt={res.name} /> +
+
+
{resourceLang[`Label_${res.name}`]}
+
{res.amount}
+
+
+ )}
\ No newline at end of file +} + +.resourcebar-item-icon { + width: 50px; + height: 50px; + margin-right: 8px; + margin-top: 8px; + margin-bottom: 8px; +} + +.resourcebar-item-icon img { + width: 100%; +} + +.resourcebar-item-text-wrapper { + display: flex; + flex-direction: column; + justify-content: center; +} + +.resourcebar-item-text { + font-size: 1.5em; +} + +.resourcebar-item-amount { + font-size: 1.2em; +} + + + \ No newline at end of file diff --git a/src/lib/data/resources.json b/src/lib/data/resources.json new file mode 100644 index 0000000..41f9e94 --- /dev/null +++ b/src/lib/data/resources.json @@ -0,0 +1,49 @@ +[ + { + "name": "coal", + "type": "solid", + "icon": "https://gamepedia.cursecdn.com/minecraft_gamepedia/5/58/Coal_JE4_BE3.png" + }, + { + "name": "iron", + "type": "solid", + "icon": "https://vignette.wikia.nocookie.net/minecraft/images/e/e8/New_Iron_IngotB.png/revision/latest?cb=20190520101024" + }, + { + "name": "gold", + "type": "solid", + "icon": "https://gamepedia.cursecdn.com/minecraft_gamepedia/5/57/Gold_Ingot_JE3_BE2.png" + }, + + { + "name": "water", + "type": "liquid", + "icon": "https://ael.ovh/uranium.png" + }, + { + "name": "sulfuricAcid", + "type": "liquid", + "icon": "https://ael.ovh/uranium.png" + }, + { + "name": "liquidNitrogen", + "type": "liquid", + "icon": "https://ael.ovh/uranium.png" + }, + + { + "name": "hydrogen", + "type": "gas", + "icon": "https://ael.ovh/uranium.png" + }, + { + "name": "oxygen", + "type": "gas", + "icon": "https://ael.ovh/uranium.png" + }, + { + "name": "helium3", + "type": "gas", + "icon": "https://ael.ovh/uranium.png" + } +] \ No newline at end of file diff --git a/src/lib/db/users.ts b/src/lib/db/users.ts index bd0d938..56d3043 100644 --- a/src/lib/db/users.ts +++ b/src/lib/db/users.ts @@ -39,9 +39,9 @@ export const getUserByAccessToken = async(accessToken: string | AccessToken): Pr } else return getUserById(accessToken.user as ObjectId) } -export const getUserResources = async (username: string): Promise => { +export const getUserResources = async (id: ObjectId): Promise => { const users = await Users(); - const user = await users.findOne({ username }); + const user = await users.findOne({ _id: id }); const defaultResources: Resources = { coal: 0, diff --git a/src/lib/lang/en/resources.json b/src/lib/lang/en/resources.json index eddd436..2768675 100644 --- a/src/lib/lang/en/resources.json +++ b/src/lib/lang/en/resources.json @@ -1,7 +1,15 @@ { "Label": { - "coal": "Coal", - "iron": "Iron", - "gold": "Gold" + "coal": "Coal (C)", + "iron": "Iron (Fe)", + "gold": "Gold (Au)", + + "water": "Water (H₂O)", + "sulfuricAcid": "Sulfuric Acid (H₂SO₄)", + "liquidNitrogen": "Liquid Nitrogen (N₂)", + + "hydrogen": "Hydrogen (H₂)", + "oxygen": "Oxygen (O₂)", + "helium3": "Helium-3 (³He)" } } \ No newline at end of file diff --git a/src/pages/game/buildings.astro b/src/pages/game/buildings.astro index 56b48c3..6aece36 100644 --- a/src/pages/game/buildings.astro +++ b/src/pages/game/buildings.astro @@ -1,7 +1,7 @@ --- import Layout from '../../layouts/Layout.astro'; import NavBar from '../../components/NavBar.astro'; -import { getUserResources } from '../../lib/db/users'; +import { getUserByAccessToken, getUserResources } from '../../lib/db/users'; import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver'; import ResourceBar from '../../components/ResourceBar.astro'; @@ -9,10 +9,12 @@ const buildingsList = (await import('../../lib/data/buildings.json')).default; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const username = Astro.cookies.get('username')?.value ?? ""; +if(loggedToken === null || username === "") return Astro.redirect('/logout'); -if(loggedToken === null || username === "") return Astro.redirect('/'); +const checkUser = await getUserByAccessToken(loggedToken); +if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); -const resources = await getUserResources(username); +const resources = await getUserResources(checkUser._id); const locale = getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); @@ -23,12 +25,7 @@ const langBuildings = await getLocales(locale, 'buildings'); - -
    -
  • {langResources['Label_coal']}: {resources.coal * 2}
  • -
  • {langResources['Label_iron']}: {resources.iron * 3}
  • -
  • {langResources['Label_gold']}: {resources.gold * 4}
  • -
+ {buildingsList.map(cat => (
@@ -119,18 +116,6 @@ const langBuildings = await getLocales(locale, 'buildings'); } \ No newline at end of file + \ No newline at end of file diff --git a/src/pages/game/profile.astro b/src/pages/game/profile.astro index 8eebe32..4c647d3 100644 --- a/src/pages/game/profile.astro +++ b/src/pages/game/profile.astro @@ -1,15 +1,17 @@ --- import Layout from '../../layouts/Layout.astro'; import NavBar from '../../components/NavBar.astro'; -import { getUserByNickOrEmail, getUserResources, updateUserResources } from '../../lib/db/users'; +import { getUserByAccessToken, getUserByNickOrEmail } from '../../lib/db/users'; import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver'; import ResourceBar from '../../components/ResourceBar.astro'; import format from '../../lib/utils/format'; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const username = Astro.cookies.get('username')?.value ?? ""; +if(loggedToken === null || username === "") return Astro.redirect('/logout'); -if(loggedToken === null || username === "") return Astro.redirect('/'); +const checkUser = await getUserByAccessToken(loggedToken); +if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); const locale = getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); diff --git a/src/pages/login.astro b/src/pages/login.astro index 6518faa..8a59aee 100644 --- a/src/pages/login.astro +++ b/src/pages/login.astro @@ -57,6 +57,13 @@ if(Astro.request.method === "POST") { secure: true }); + Astro.cookies.set("userid", user._id?.toString() as string, { + path: "/", + maxAge: sessionTime, + sameSite: "lax", + secure: true + }); + return Astro.redirect("/game"); } else { error = "invalid username or password"; @@ -66,7 +73,7 @@ if(Astro.request.method === "POST") { --- - +


diff --git a/src/pages/logout.astro b/src/pages/logout.astro index 2e50cf2..fc95f78 100644 --- a/src/pages/logout.astro +++ b/src/pages/logout.astro @@ -3,5 +3,13 @@ if(Astro.cookies.has('sessionToken')) { Astro.cookies.delete('sessionToken'); } +if(Astro.cookies.has('username')) { + Astro.cookies.delete('uesrname'); +} + +if(Astro.cookies.has('userid')) { + Astro.cookies.delete('userid'); +} + return Astro.redirect('/'); --- \ No newline at end of file diff --git a/src/pages/register.astro b/src/pages/register.astro index 72c95d3..33043c4 100644 --- a/src/pages/register.astro +++ b/src/pages/register.astro @@ -104,13 +104,20 @@ if(Astro.request.method === "POST") { secure: true }); + Astro.cookies.set("userid", user._id?.toString() as string, { + path: "/", + maxAge: sessionTime, + sameSite: "lax", + secure: true + }) + return Astro.redirect("/game"); } } --- - +

diff --git a/src/types/Resource.ts b/src/types/Resource.ts new file mode 100644 index 0000000..4836014 --- /dev/null +++ b/src/types/Resource.ts @@ -0,0 +1,5 @@ +export default interface Resource { + name: string; + type: "solid" | "liquid" | "gas"; + amount: number; +} \ No newline at end of file diff --git a/src/types/Resources.ts b/src/types/Resources.ts index f3891d1..c986ec6 100644 --- a/src/types/Resources.ts +++ b/src/types/Resources.ts @@ -2,4 +2,12 @@ export default interface Resources { coal: number; iron: number; gold: number; + + water: number; + sulfuricAcid: number; + liquidNitrogen: number; + + hydrogen: number; + oxygen: number; + helium3: number; } diff --git a/src/types/User.ts b/src/types/User.ts index 852849f..3f023b8 100644 --- a/src/types/User.ts +++ b/src/types/User.ts @@ -3,7 +3,7 @@ import type Resources from "./Resources"; import type Building from "./Building"; export default interface User { - _id?: ObjectId; + _id: ObjectId; username: string; email: string; password: string;