Temporary fix for buildings system
This commit is contained in:
parent
3025178129
commit
afca4bc0ea
|
@ -4,21 +4,39 @@
|
|||
"buildings": [
|
||||
{
|
||||
"id": "iron-mine",
|
||||
"cost": {
|
||||
"iron": 1,
|
||||
"gold": 1,
|
||||
"coal": 1
|
||||
"cost": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
],
|
||||
"energy": 11,
|
||||
"multiplier": 2
|
||||
},
|
||||
{
|
||||
"id": "gold-mine",
|
||||
"cost": {
|
||||
"iron": 2,
|
||||
"gold": 2,
|
||||
"coal": 2
|
||||
"cost": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
],
|
||||
"energy": 11,
|
||||
"multiplier": 2.5
|
||||
}
|
||||
|
@ -28,11 +46,20 @@
|
|||
"buildings": [
|
||||
{
|
||||
"id": "research-lab",
|
||||
"cost": {
|
||||
"iron": 10,
|
||||
"gold": 11,
|
||||
"coal": 12
|
||||
"cost": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
],
|
||||
"energy": 100,
|
||||
"multiplier": 3
|
||||
}
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
import type Resource from "../../types/Resource";
|
||||
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;
|
||||
const resources = {} as Resources;
|
||||
const resources = {} as Array<Resource>;
|
||||
|
||||
let resource: keyof Resources;
|
||||
for (resource in buildingCost) {
|
||||
resources[resource] = userResources[resource] - buildingCost[resource];
|
||||
if (userResources[resource] < buildingCost[resource]) {
|
||||
canBuild = false;
|
||||
}
|
||||
userResources.forEach((userResource) => {
|
||||
const resource = buildingCost.find((buildingResource) => buildingResource.name === userResource.name);
|
||||
if (resource) {
|
||||
resources.push({
|
||||
name: userResource.name,
|
||||
amount: userResource.amount - resource.amount
|
||||
});
|
||||
if (userResource.amount < resource.amount) canBuild = false;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
canBuild,
|
||||
|
|
|
@ -1,16 +1,26 @@
|
|||
import { type APIRoute } from "astro";
|
||||
import validateAccessToken from "../../../lib/utils/validateAccessToken";
|
||||
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 calculateAvailableResources from "../../../lib/utils/calculateAvailableResources";
|
||||
import { getUserByAccessToken } from "../../../lib/db/users";
|
||||
|
||||
export const POST: APIRoute = async({ request }) => {
|
||||
const response = await validateAccessToken(request);
|
||||
if(response instanceof Response) return response;
|
||||
|
||||
const user = response.username;
|
||||
const resources = await getUserResources(user);
|
||||
const user = await getUserByAccessToken(response);
|
||||
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 building = buildings.map(cat => cat.buildings.filter(b => b.id === buildingId))[0][0];
|
||||
const balance = calculateAvailableResources(resources, building.cost);
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
---
|
||||
import Layout from '../../layouts/Layout.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 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;
|
||||
|
||||
|
@ -14,13 +16,14 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout');
|
|||
const checkUser = await getUserByAccessToken(loggedToken);
|
||||
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 langResources = await getLocales(locale, 'resources');
|
||||
const langGame = await getLocales(locale, 'game');
|
||||
const langBuildings = await getLocales(locale, 'buildings');
|
||||
// console.log(await calculateCurrentAvailableResources(checkUser._id));
|
||||
---
|
||||
|
||||
<Layout title="Buildings">
|
||||
|
|
|
@ -3,6 +3,7 @@ import Layout from '../../layouts/Layout.astro';
|
|||
import NavBar from '../../components/NavBar.astro';
|
||||
import ResourceBar from '../../components/ResourceBar.astro';
|
||||
import { getUserByAccessToken } from '../../lib/db/users';
|
||||
// import { createInitialResources } from '../../lib/utils/resourceManager';
|
||||
|
||||
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
|
||||
const username = Astro.cookies.get('username')?.value ?? "";
|
||||
|
@ -10,7 +11,7 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout');
|
|||
|
||||
const checkUser = await getUserByAccessToken(loggedToken);
|
||||
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout');
|
||||
|
||||
// createInitialResources(checkUser._id);
|
||||
---
|
||||
|
||||
<Layout title="chujów sto">
|
||||
|
|
Loading…
Reference in New Issue