From 5f305c8983afca3b1dd7d612dcc27bd5393333ed Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Sun, 7 Jul 2024 10:55:43 +0200 Subject: [PATCH] Modify language driver to accept array of multiple types --- src/components/NavBar.astro | 26 +++++++++++++------------- src/components/ResourceBar.astro | 11 +++++------ src/lib/utils/langDriver.ts | 13 ++++++++----- src/pages/game/buildings.astro | 17 +++++++---------- src/pages/game/profile.astro | 26 +++++++++++++------------- src/pages/game/research.astro | 19 ++++++++----------- src/pages/game/ships.astro | 12 +++++++----- 7 files changed, 61 insertions(+), 63 deletions(-) diff --git a/src/components/NavBar.astro b/src/components/NavBar.astro index 5c0bc41..2664d67 100644 --- a/src/components/NavBar.astro +++ b/src/components/NavBar.astro @@ -19,88 +19,88 @@ interface NavElement { dropdowns?: Array; } -const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'navbar'); +const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), ['navbar']); const listOfElements: Array = [{ id: "home", - title: lang["Link_home"], + title: lang["navbar"]["Link_home"], type: "simple", url: "/", show: "always", position: "top" }, { id: "about", - title: lang["Link_about"], + title: lang["navbar"]["Link_about"], type: "simple", url: "#", show: "always", position: "top" }, { id: "api", - title: lang["Link_api"], + title: lang["navbar"]["Link_api"], type: "simple", url: "#", show: "always", position: "top" }, { id: "login", - title: lang["Link_login"], + title: lang["navbar"]["Link_login"], type: "simple", url: "/login", show: "notLoggedInOnly", position: "bottom" }, { id: "register", - title: lang["Link_register"], + title: lang["navbar"]["Link_register"], type: "simple", url: "/register", show: "notLoggedInOnly", position: "bottom" },{ id: "overview", - title: lang["Link_overview"], + title: lang["navbar"]["Link_overview"], type: "simple", url: "/game", show: "loggedInOnly", position: "bottom" }, { id: "buildings", - title: lang["Link_buildings"], + title: lang["navbar"]["Link_buildings"], type: "simple", url: "/game/buildings", show: "loggedInOnly", position: "bottom" }, { id: "research", - title: lang["Link_research"], + title: lang["navbar"]["Link_research"], type: "simple", url: "/game/research", show: "loggedInOnly", position: "bottom" }, { id: "ships", - title: lang["Link_ships"], + title: lang["navbar"]["Link_ships"], type: "simple", url: "/game/ships", show: "loggedInOnly", position: "bottom" }, { id: "fleet", - title: lang["Link_fleet"], + title: lang["navbar"]["Link_fleet"], type: "simple", url: "/game/fleet", show: "loggedInOnly", position: "bottom" }, { id: "galaxyView", - title: lang["Link_galaxyView"], + title: lang["navbar"]["Link_galaxyView"], type: "simple", url: "/game/galaxyView", show: "loggedInOnly", position: "bottom" }, { id: "profile", - title: lang["Link_profile"], + title: lang["navbar"]["Link_profile"], type: "simple", url: "/game/profile", show: "loggedInOnly", diff --git a/src/components/ResourceBar.astro b/src/components/ResourceBar.astro index e051905..d56ea57 100644 --- a/src/components/ResourceBar.astro +++ b/src/components/ResourceBar.astro @@ -5,8 +5,7 @@ import { getHighestWeightedLanguage, getLocales } from '../lib/utils/langDriver' import resourceTypes from '../lib/data/resources.json'; -const resourceLang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resources'); -const resBarLang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resourcebar'); +const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), ['resourcebar', 'resources']); const resources = await calculateCurrentAvailableResources(new ObjectId(Astro.cookies.get('planetid')?.value ?? '')); @@ -31,13 +30,13 @@ for(const key in resources) { x.name === res.name)?.icon ?? "#"} alt={res.name} />
-
{resourceLang[`Label_${res.name}`]}
+
{lang["resources"][`Label_${res.name}`]}
[fetching]
-
{resBarLang['Label_avaliable']} - {Math.floor(res.amount).toString()}
-
{resBarLang['Label_production']} - {res.perHourMiningRate.toString()}
-
{resBarLang['Label_capacity']} - {'21372137'}
+
{lang["resourcebar"]['Label_avaliable']} - {Math.floor(res.amount).toString()}
+
{lang["resourcebar"]['Label_production']} - {res.perHourMiningRate.toString()}
+
{lang["resourcebar"]['Label_capacity']} - {'21372137'}
)} diff --git a/src/lib/utils/langDriver.ts b/src/lib/utils/langDriver.ts index b321052..a27e440 100644 --- a/src/lib/utils/langDriver.ts +++ b/src/lib/utils/langDriver.ts @@ -32,18 +32,21 @@ export async function getHighestWeightedLanguage(header: string | null): Promise return highestWeightedLang; } -export async function getLocales(language: string, type: string) { +export async function getLocales(language: string, types: string[]) { if(!(await getSupportedLanguages()).includes(language)) { console.log(await getSupportedLanguages(), language) return null; } - const lang = await (await fetch(`http://localhost:4321/lang/${language}/${type}.json`)).json(); const out: any = {}; - for(const category in lang) { - for(const element in lang[category]) { - out[`${category}_${element}`] = lang[category][element]; + for(const type of types) { + const lang = await (await fetch(`http://localhost:4321/lang/${language}/${type}.json`)).json(); + out[type] = {}; + for(const category in lang) { + for(const element in lang[category]) { + out[type][`${category}_${element}`] = lang[category][element]; + } } } diff --git a/src/pages/game/buildings.astro b/src/pages/game/buildings.astro index b267869..d3e0bd2 100644 --- a/src/pages/game/buildings.astro +++ b/src/pages/game/buildings.astro @@ -16,10 +16,7 @@ if(checkUser === null || checkUser.username !== username) return Astro.redirect( const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); -const langResources = await getLocales(locale, 'resources'); -const langGame = await getLocales(locale, 'game'); -const langBuildings = await getLocales(locale, 'buildings'); -const langResearch = await getLocales(locale, 'research'); +const lang = await getLocales(locale, ['resources', 'game', 'buildings', 'research']); --- @@ -28,19 +25,19 @@ const langResearch = await getLocales(locale, 'research'); {buildingsList.map(cat => (
-

{langBuildings[`Label_${cat.category}`]}

+

{lang["buildings"][`Label_${cat.category}`]}

{cat.buildings.map(building => ( <> -

{langBuildings[`Label_${building.id}`]}

+

{lang["buildings"][`Label_${building.id}`]}

{building.requirements.resources.map(res => ( -
{langResources[`Label_${res.name}`]}: {res.amount}
+
{lang["resources"][`Label_${res.name}`]}: {res.amount}
))} {building.requirements.buildings.map(b => ( -
{langBuildings[`Label_${b.id}`]}: {b.level}
+
{lang["buildings"][`Label_${b.id}`]}: {b.level}
))} {building.requirements.research.map(t => ( -
{langResearch[`Label_${t.id}`].name}: {t.level}
+
{lang["research"][`Label_${t.id}`].name}: {t.level}
))} - {langGame['Link_build']} + {lang["game"]['Link_build']} ))}
))} diff --git a/src/pages/game/profile.astro b/src/pages/game/profile.astro index d86261d..22a5fdd 100644 --- a/src/pages/game/profile.astro +++ b/src/pages/game/profile.astro @@ -17,7 +17,7 @@ const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accep const user = await getUserByNickOrEmail(username); -const langGame = await getLocales(locale, 'game'); +const lang = await getLocales(locale, ['game']); --- @@ -25,24 +25,24 @@ const langGame = await getLocales(locale, 'game');
-

{format(langGame['Label_userCreationDate'], user?.createdAt.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ").toString() ?? "")}

- {langGame['Link_logout']} +

{format(lang["game"]['Label_userCreationDate'], user?.createdAt.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ").toString() ?? "")}

+ {lang["game"]['Link_logout']}
- - - + + +
- - - + + +
- - - - + + + +
diff --git a/src/pages/game/research.astro b/src/pages/game/research.astro index 133063f..ac63d97 100644 --- a/src/pages/game/research.astro +++ b/src/pages/game/research.astro @@ -20,10 +20,7 @@ if(checkUser === null || checkUser.username !== username) return Astro.redirect( const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); -const langResources = await getLocales(locale, 'resources'); -const langGame = await getLocales(locale, 'game'); -const langBuildings = await getLocales(locale, 'buildings'); -const langResearch = await getLocales(locale, 'research'); +const lang = await getLocales(locale, ['game', 'resources', 'buildings', 'research']); const researchDetails: ResearchDetail[] = []; //TODO: Add union type for cost keys researchList.forEach(element => { @@ -48,21 +45,21 @@ researchList.forEach(element => { {researchDetails.map(research => (
-

{langResearch[`Label_${research.id}`].name}

+

{lang["research"][`Label_${research.id}`].name}

- {langResearch[`Label_${research.id}`].description}
+ {lang["research"][`Label_${research.id}`].description}
- {langGame['Link_research']} + {lang["game"]['Link_research']}
-
{langResearch['Label_required']}:
+
{lang["research"]['Label_required']}:
{research.requiredResearch.length !== 0 ? research.requiredResearch.map(req => (
- {langResearch[`Label_${req.id}`].name} - {req.level} + {lang["research"][`Label_${req.id}`].name} - {req.level}
- )) : langResearch['Label_none']} + )) : lang["research"]['Label_none']}
@@ -71,7 +68,7 @@ researchList.forEach(element => {
{Object.entries(research.cost).map(([key, value]) =>
- {langResources[`Label_${key}`]}: {value} || + {lang["resources"][`Label_${key}`]}: {value} ||
)}
diff --git a/src/pages/game/ships.astro b/src/pages/game/ships.astro index 3b1f32e..be8ecab 100644 --- a/src/pages/game/ships.astro +++ b/src/pages/game/ships.astro @@ -6,6 +6,8 @@ import { getHighestWeightedLanguage, getLocales } from '../../lib/utils/langDriv import ResourceBar from '../../components/ResourceBar.astro'; import ships from '../../lib/data/ships.json'; +import { getPlanetById } from '../../lib/db/planets'; +import { ObjectId } from 'mongodb'; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const username = Astro.cookies.get('username')?.value ?? ""; @@ -14,11 +16,11 @@ 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 = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); -const langShips = await getLocales(locale, 'ships'); +const planet = await getPlanetById(new ObjectId(Astro.cookies.get('planetid')?.value)); -const playerShips = checkUser.ships; -console.log(playerShips) +if(!planet) return; + +const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), ['ships']); --- @@ -28,7 +30,7 @@ console.log(playerShips) {ships.map(ship => (
- ({playerShips.find((s => s.id === ship.id))?.amount ?? 0}) {ship.id} + ({planet.ships.find((s => s.id === ship.id))?.amount ?? 0}) {ship.id}
))}