Temporary fix for buildings system

This commit is contained in:
Aelita4 2024-03-30 23:36:41 +01:00
parent 3025178129
commit afca4bc0ea
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
5 changed files with 75 additions and 30 deletions

View File

@ -4,21 +4,39 @@
"buildings": [ "buildings": [
{ {
"id": "iron-mine", "id": "iron-mine",
"cost": { "cost": [
"iron": 1, {
"gold": 1, "name": "iron",
"coal": 1 "amount": 1
}, },
{
"name": "gold",
"amount": 1
},
{
"name": "coal",
"amount": 1
}
],
"energy": 11, "energy": 11,
"multiplier": 2 "multiplier": 2
}, },
{ {
"id": "gold-mine", "id": "gold-mine",
"cost": { "cost": [
"iron": 2, {
"gold": 2, "name": "iron",
"coal": 2 "amount": 1
}, },
{
"name": "gold",
"amount": 1
},
{
"name": "coal",
"amount": 1
}
],
"energy": 11, "energy": 11,
"multiplier": 2.5 "multiplier": 2.5
} }
@ -28,11 +46,20 @@
"buildings": [ "buildings": [
{ {
"id": "research-lab", "id": "research-lab",
"cost": { "cost": [
"iron": 10, {
"gold": 11, "name": "iron",
"coal": 12 "amount": 1
}, },
{
"name": "gold",
"amount": 1
},
{
"name": "coal",
"amount": 1
}
],
"energy": 100, "energy": 100,
"multiplier": 3 "multiplier": 3
} }

View File

@ -1,16 +1,20 @@
import type Resource from "../../types/Resource";
import type Resources from "../../types/Resources"; import type Resources from "../../types/Resources";
export default function calculateAvailableResources(userResources: Resources, buildingCost: Resources) { export default function calculateAvailableResources(userResources: Array<Resource>, buildingCost: Array<Resource>) {
let canBuild = true; let canBuild = true;
const resources = {} as Resources; const resources = {} as Array<Resource>;
let resource: keyof Resources; userResources.forEach((userResource) => {
for (resource in buildingCost) { const resource = buildingCost.find((buildingResource) => buildingResource.name === userResource.name);
resources[resource] = userResources[resource] - buildingCost[resource]; if (resource) {
if (userResources[resource] < buildingCost[resource]) { resources.push({
canBuild = false; name: userResource.name,
} amount: userResource.amount - resource.amount
});
if (userResource.amount < resource.amount) canBuild = false;
} }
});
return { return {
canBuild, canBuild,

View File

@ -1,16 +1,26 @@
import { type APIRoute } from "astro"; import { type APIRoute } from "astro";
import validateAccessToken from "../../../lib/utils/validateAccessToken"; import validateAccessToken from "../../../lib/utils/validateAccessToken";
import { getAccessToken } from "../../../lib/db/accessTokens"; import { getAccessToken } from "../../../lib/db/accessTokens";
import { getUserResources } from "../../../lib/db/users"; // import { getUserResources } from "../../../lib/db/users";
import { getUserResources } from "../../../lib/utils/resourceManager";
import buildings from '../../../lib/data/buildings.json'; import buildings from '../../../lib/data/buildings.json';
import calculateAvailableResources from "../../../lib/utils/calculateAvailableResources"; import calculateAvailableResources from "../../../lib/utils/calculateAvailableResources";
import { getUserByAccessToken } from "../../../lib/db/users";
export const POST: APIRoute = async({ request }) => { export const POST: APIRoute = async({ request }) => {
const response = await validateAccessToken(request); const response = await validateAccessToken(request);
if(response instanceof Response) return response; if(response instanceof Response) return response;
const user = response.username; const user = await getUserByAccessToken(response);
const resources = await getUserResources(user); if(user === null) {
return new Response(
JSON.stringify({
code: 401,
message: "Unauthorized"
}), { status: 401 }
)
}
const resources = await getUserResources(user._id);
const buildingId = (await request.json()).building; const buildingId = (await request.json()).building;
const building = buildings.map(cat => cat.buildings.filter(b => b.id === buildingId))[0][0]; const building = buildings.map(cat => cat.buildings.filter(b => b.id === buildingId))[0][0];
const balance = calculateAvailableResources(resources, building.cost); const balance = calculateAvailableResources(resources, building.cost);

View File

@ -1,9 +1,11 @@
--- ---
import Layout from '../../layouts/Layout.astro'; import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro'; import NavBar from '../../components/NavBar.astro';
import { getUserByAccessToken, getUserResources } from '../../lib/db/users'; import { getUserByAccessToken } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver'; import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import ResourceBar from '../../components/ResourceBar.astro'; import ResourceBar from '../../components/ResourceBar.astro';
// import { calculateCurrentAvailableResources } from '../../lib/utils/resourceManager';
// import { getUserResources } from '../../lib/utils/resourceManager';
const buildingsList = (await import('../../lib/data/buildings.json')).default; const buildingsList = (await import('../../lib/data/buildings.json')).default;
@ -14,13 +16,14 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout');
const checkUser = await getUserByAccessToken(loggedToken); const checkUser = await getUserByAccessToken(loggedToken);
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout');
const resources = await getUserResources(checkUser._id); // const resources = await getUserResources(checkUser._id);
const locale = getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); const locale = getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const langResources = await getLocales(locale, 'resources'); const langResources = await getLocales(locale, 'resources');
const langGame = await getLocales(locale, 'game'); const langGame = await getLocales(locale, 'game');
const langBuildings = await getLocales(locale, 'buildings'); const langBuildings = await getLocales(locale, 'buildings');
// console.log(await calculateCurrentAvailableResources(checkUser._id));
--- ---
<Layout title="Buildings"> <Layout title="Buildings">

View File

@ -3,6 +3,7 @@ import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro'; import NavBar from '../../components/NavBar.astro';
import ResourceBar from '../../components/ResourceBar.astro'; import ResourceBar from '../../components/ResourceBar.astro';
import { getUserByAccessToken } from '../../lib/db/users'; import { getUserByAccessToken } from '../../lib/db/users';
// import { createInitialResources } from '../../lib/utils/resourceManager';
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
const username = Astro.cookies.get('username')?.value ?? ""; const username = Astro.cookies.get('username')?.value ?? "";
@ -10,7 +11,7 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout');
const checkUser = await getUserByAccessToken(loggedToken); const checkUser = await getUserByAccessToken(loggedToken);
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout');
// createInitialResources(checkUser._id);
--- ---
<Layout title="chujów sto"> <Layout title="chujów sto">