diff --git a/public/images/buildings/shipyard.jpeg b/public/images/buildings/shipyard.jpeg new file mode 100644 index 0000000..c14b9c8 Binary files /dev/null and b/public/images/buildings/shipyard.jpeg differ diff --git a/public/images/ships/fighter.jpeg b/public/images/ships/fighter.jpeg new file mode 100644 index 0000000..6089d5e Binary files /dev/null and b/public/images/ships/fighter.jpeg differ diff --git a/public/images/ships/transporter.jpeg b/public/images/ships/transporter.jpeg new file mode 100644 index 0000000..a6a099d Binary files /dev/null and b/public/images/ships/transporter.jpeg differ diff --git a/src/components/ResourceBar.astro b/src/components/ResourceBar.astro index ab39313..76cb44a 100644 --- a/src/components/ResourceBar.astro +++ b/src/components/ResourceBar.astro @@ -2,8 +2,9 @@ import { ObjectId } from 'mongodb'; import { calculateCurrentAvailableResources } from '../lib/utils/resourceManager'; import { getHighestWeightedLanguage, getLocales, getName } from '../lib/utils/langDriver'; +import { getAllResources } from '../lib/db/resources'; -import resourceTypes from '../lib/data/resources.json'; +const resourceTypes = await getAllResources(); const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); @@ -22,13 +23,13 @@ for(const key in resources) {
{resourceArray.map(res =>
x.name === res.name)?.type ?? "solid"} + data-res-type={resourceTypes.find(x => x.id === res.name)?.type ?? "solid"} data-res-amount={res.amount} data-res-mining-rate={res.perHourMiningRate} - style={(resourceTypes.find(x => x.name === res.name)?.type ?? "solid") === "solid" ? "" : "display: none;"} + style={(resourceTypes.find(x => x.id === res.name)?.type ?? "solid") === "solid" ? "" : "display: none;"} >
- x.name === res.name)?.icon ?? "#"} alt={res.name} /> + x.id === res.name)?.icon ?? "#"} alt={res.name} />
{getName(lang, 'resources', res.name)}
diff --git a/src/lib/data/research.json b/src/lib/data/research.json deleted file mode 100644 index 991486c..0000000 --- a/src/lib/data/research.json +++ /dev/null @@ -1,144 +0,0 @@ -[ - { - "id": "basic-engine", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 1 - } - ], - "research": [], - "resources": { - "iron": 1000, - "gold": 500 - } - }, - "time": 60, - "multiplier": 2 - }, { - "id": "advanced-engine", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 5 - } - ], - "research": [ - { - "id": "basic-engine", - "level": 1 - } - ], - "resources": { - "iron": 2000, - "gold": 1000 - } - }, - "time": 120, - "multiplier": 3 - }, { - "id": "combat-utilities", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 1 - } - ], - "research": [], - "resources": { - "iron": 1000, - "gold": 500 - } - }, - "time": 60, - "multiplier": 2 - }, { - "id": "defensive-utilities", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 1 - } - ], - "research": [], - "resources": { - "iron": 1000, - "gold": 500 - } - }, - "time": 60, - "multiplier": 2 - }, { - "id": "oxygen-production", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 1 - } - ], - "research": [], - "resources": { - "iron": 1000, - "gold": 500 - } - }, - "time": 200, - "multiplier": 2 - }, { - "id": "terraforming", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 1 - } - ], - "research": [], - "resources": { - "iron": 1000, - "gold": 500 - } - }, - "time": 500, - "multiplier": 3 - }, { - "id": "advanced-technologies", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 6 - } - ], - "research": [], - "resources": { - "iron": 1000, - "gold": 500 - } - }, - "time": 1500, - "multiplier": 4 - }, { - "id": "nuclear-power", - "requirements": { - "buildings": [ - { - "id": "research-lab", - "level": 10 - } - ], - "research": [], - "resources": { - "iron": 1000, - "gold": 500 - } - }, - "time": 3000, - "multiplier": 5 - } -] \ No newline at end of file diff --git a/src/lib/data/resources.json b/src/lib/data/resources.json deleted file mode 100644 index 41f9e94..0000000 --- a/src/lib/data/resources.json +++ /dev/null @@ -1,49 +0,0 @@ -[ - { - "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/data/ships.json b/src/lib/data/ships.json deleted file mode 100644 index af942c5..0000000 --- a/src/lib/data/ships.json +++ /dev/null @@ -1,57 +0,0 @@ -[ - { - "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/mongodb.ts b/src/lib/db/mongodb.ts index 9eb1094..6d97a5e 100644 --- a/src/lib/db/mongodb.ts +++ b/src/lib/db/mongodb.ts @@ -45,6 +45,16 @@ export const Research = async() => { return db.collection('research'); } +export const Resources = async() => { + const db = await getDB(); + return db.collection('resources'); +} + +export const Ships = async() => { + const db = await getDB(); + return db.collection('ships'); +} + export const Lang = async (language = "en") => { const db = await getDB(`${config.MONGODB_DB}_${language}`); return [ diff --git a/src/lib/db/planets.ts b/src/lib/db/planets.ts index cd45cd2..1a8b341 100644 --- a/src/lib/db/planets.ts +++ b/src/lib/db/planets.ts @@ -5,7 +5,7 @@ import type Planet from '../../types/Planet'; import { getUserById } from './users'; import type Building from '../../types/Building'; import type Ship from '../../types/Ship'; -import type DBResource from '../../types/DBResource'; +import type PlayerResource from '../../types/PlayerResource'; export const getAllPlanets = async (options?: { fetchUserPlanets: boolean }) => { const planets = await Planets(); @@ -32,7 +32,7 @@ export const createPlanet = async (options: { name: string, owner?: User, ownerI owner: user, name: options.name, fields: options.fields, - resources: new Array, + resources: new Array, buildings: new Array, ships: new Array } diff --git a/src/lib/db/resources.ts b/src/lib/db/resources.ts new file mode 100644 index 0000000..d487850 --- /dev/null +++ b/src/lib/db/resources.ts @@ -0,0 +1,12 @@ +import DBResource from '../../types/DBResource'; +import { Resources } from '../db/mongodb'; + +export const getAllResources = async () => { + return (await Resources()).find({}).toArray() as unknown as Array; +} + +export const getResourceById = async (id: string) => { + return (await Resources()).findOne({ + id + }) as unknown as DBResource; +} \ No newline at end of file diff --git a/src/lib/db/ships.ts b/src/lib/db/ships.ts new file mode 100644 index 0000000..8ead886 --- /dev/null +++ b/src/lib/db/ships.ts @@ -0,0 +1,12 @@ +import DBShip from '../../types/DBShip'; +import { Ships } from '../db/mongodb'; + +export const getAllShips = async () => { + return (await Ships()).find({}).toArray() as unknown as Array; +} + +export const getResourceById = async (id: string) => { + return (await Ships()).findOne({ + id + }) as unknown as DBShip; +} \ No newline at end of file diff --git a/src/lib/utils/resourceManager.ts b/src/lib/utils/resourceManager.ts index 8e2e002..ada3e3b 100644 --- a/src/lib/utils/resourceManager.ts +++ b/src/lib/utils/resourceManager.ts @@ -1,10 +1,10 @@ import { ObjectId } from "mongodb" import { Planets } from "../db/mongodb"; -import type DBResource from "../../types/DBResource"; +import type PlayerResource from "../../types/PlayerResource"; import { getPlanetById } from "../db/planets"; export const createInitialResources = async (planetId: ObjectId) => { - const resources: Array = [ + const resources: Array = [ { name: "coal", amount: 11, @@ -64,7 +64,7 @@ export const createInitialResources = async (planetId: ObjectId) => { updatePlanetResources(planetId, resources); } -export const getResourcesFromPlanet = async (planetId: ObjectId): Promise | null> => { +export const getResourcesFromPlanet = async (planetId: ObjectId): Promise | null> => { const planet = await getPlanetById(planetId); if(!planet) return null; @@ -72,7 +72,7 @@ export const getResourcesFromPlanet = async (planetId: ObjectId): Promise) => { +export const updatePlanetResources = async (planetId: ObjectId, resources: Array) => { const planets = await Planets(); await planets.updateOne({ _id: planetId }, { $set: { @@ -81,7 +81,7 @@ export const updatePlanetResources = async (planetId: ObjectId, resources: Array }); } -export const calculateCurrentAvailableResources = async (planetId: ObjectId): Promise> => { +export const calculateCurrentAvailableResources = async (planetId: ObjectId): Promise> => { const resources = await getResourcesFromPlanet(planetId); if(resources === null) return []; diff --git a/src/pages/api/build/createBuilding.ts b/src/pages/api/build/createBuilding.ts index 3e99696..fea9d55 100644 --- a/src/pages/api/build/createBuilding.ts +++ b/src/pages/api/build/createBuilding.ts @@ -5,7 +5,7 @@ import { getUserByAccessToken, getUserResearch } from "../../../lib/db/users"; import Planet from "../../../types/Planet"; import { createOrUpgradeBuilding, getPlanetById } from "../../../lib/db/planets"; import { ObjectId } from "mongodb"; -import DBResource from "../../../types/DBResource"; +import DBResource from "../../../types/PlayerResource"; import { getAllBuildings } from "../../../lib/db/buildings"; import DBBuilding from "../../../types/DBBuilding"; diff --git a/src/pages/api/research/performResearch.ts b/src/pages/api/research/performResearch.ts index b7fea54..be1b00d 100644 --- a/src/pages/api/research/performResearch.ts +++ b/src/pages/api/research/performResearch.ts @@ -1,13 +1,13 @@ import { type APIRoute } from "astro"; import validateAccessToken from "../../../lib/utils/validateAccessToken"; -import research from '../../../lib/data/research.json'; import { createOrUpgradeResearch, getUserByAccessToken, getUserResearch } from "../../../lib/db/users"; import { getPlanetById } from "../../../lib/db/planets"; import Planet from "../../../types/Planet"; import { ObjectId } from "mongodb"; import { calculateCurrentAvailableResources, getResourcesFromPlanet, updatePlanetResources } from "../../../lib/utils/resourceManager"; -import DBResource from "../../../types/DBResource"; +import PlayerResource from "../../../types/PlayerResource"; import calculateAvailableResources from "../../../lib/utils/calculateAvailableResources"; +import { getAllResearch, getResearchById } from "../../../lib/db/research"; export const POST: APIRoute = async({ request }) => { const response = await validateAccessToken(request); @@ -36,9 +36,11 @@ export const POST: APIRoute = async({ request }) => { ) } + const researchId = body.research; - const researchData = research.filter((element: any) => element.id === researchId)[0]; - if(!researchId || !researchData) { + const research = await getResearchById(researchId); + + if(!research) { return new Response( JSON.stringify({ code: 400, @@ -72,7 +74,7 @@ export const POST: APIRoute = async({ request }) => { // check requirements // buildings const buildings = userPlanet.buildings; - researchData.requirements.buildings.forEach((buildingReq) => { + research.requirements.buildings.forEach((buildingReq) => { if(buildings.filter((building) => building.id === buildingReq.id)[0]?.level ?? 0 < buildingReq.level) { return new Response( JSON.stringify({ @@ -86,7 +88,7 @@ export const POST: APIRoute = async({ request }) => { // research const playerResearch = await getUserResearch(user); - researchData.requirements.research.forEach((researchReq) => { + research.requirements.research.forEach((researchReq) => { if(playerResearch.filter((research) => research.id === researchReq.id)[0].level < researchReq.level) { return new Response( JSON.stringify({ @@ -113,19 +115,19 @@ export const POST: APIRoute = async({ request }) => { const level = playerCurrentResearch ? playerCurrentResearch.level : 0; const newResources = structuredClone(resources); const missingResources: Array<{}> = []; - Object.entries(researchData.requirements.resources).forEach(([key, value]) => { - const res = resources.filter((element: DBResource) => element.name === key)[0]; - const cost = playerCurrentResearch ? value * Math.pow(researchData.multiplier, level) : value; + research.requirements.resources.forEach(resource => { + const res = resources.filter((element: PlayerResource) => element.name === resource.name)[0]; + const cost = playerCurrentResearch ? resource.amount * Math.pow(research.multiplier, level) : resource.amount; if(res.amount < cost) { missingResources.push({ - name: key, + name: resource.name, required: cost, available: res.amount }); return; } - else newResources.filter((element: DBResource) => element.name === key)[0].amount -= cost; + else newResources.filter((element: PlayerResource) => element.name === resource.name)[0].amount -= cost; }); if(missingResources.length > 0) { diff --git a/src/pages/game/research.astro b/src/pages/game/research.astro index 5c867d4..5d96e87 100644 --- a/src/pages/game/research.astro +++ b/src/pages/game/research.astro @@ -8,7 +8,6 @@ import ItemCard from '../../components/ItemCard.astro'; import DBResearch from '../../types/DBResearch'; import { getAllResearch } from '../../lib/db/research'; -// const researchList = (await import('../../lib/data/research.json')).default; const researchList = await getAllResearch(); const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; diff --git a/src/pages/game/ships.astro b/src/pages/game/ships.astro index 5bc8108..2b43c7d 100644 --- a/src/pages/game/ships.astro +++ b/src/pages/game/ships.astro @@ -2,12 +2,10 @@ import Layout from '../../layouts/Layout.astro'; import NavBar from '../../components/NavBar.astro'; import { getUserByAccessToken } from '../../lib/db/users'; -import { getHighestWeightedLanguage, getLocales } from '../../lib/utils/langDriver'; +import { getHighestWeightedLanguage, getLocales, getObj } from '../../lib/utils/langDriver'; import ResourceBar from '../../components/ResourceBar.astro'; - -import ships from '../../lib/data/ships.json'; -import { getPlanetById } from '../../lib/db/planets'; -import { ObjectId } from 'mongodb'; +import { getAllShips } from '../../lib/db/ships'; +import ItemCard from '../../components/ItemCard.astro'; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const username = Astro.cookies.get('username')?.value ?? ""; @@ -16,24 +14,48 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout'); const checkUser = await getUserByAccessToken(loggedToken); if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); -const planet = await getPlanetById(new ObjectId(Astro.cookies.get('planetid')?.value)); - -if(!planet) return; +const ships = await getAllShips(); const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); + +const modalSet: { [key: string]: { resources: Array, research: Array, buildings: Array } } = {}; + +for(const ship of ships) { + modalSet[ship.id] = { + resources: ship.requirements.resources, + research: ship.requirements.research, + buildings: ship.requirements.buildings, + // energy: building.energy + }; +} --- - {ships.map(ship => ( -
-
- ({planet.ships.find((s => s.id === ship.id))?.amount ?? 0}) {ship.id} -
-
- ))} +
+
+

Required resources

+
None
+

Required buildings

+
None
+

Required research

+
None
+
+
+ +
+ {ships.map(ship => <> + + )} +
-