Add requirements section to buildings and research

This commit is contained in:
Aelita4 2024-04-03 23:22:20 +02:00
parent afca4bc0ea
commit 5bb3940088
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
6 changed files with 174 additions and 83 deletions

View File

@ -4,7 +4,10 @@
"buildings": [
{
"id": "iron-mine",
"cost": [
"requirements": {
"buildings": [],
"research": [],
"resources": [
{
"name": "iron",
"amount": 1
@ -17,13 +20,17 @@
"name": "coal",
"amount": 1
}
],
]
},
"energy": 11,
"multiplier": 2
},
{
"id": "gold-mine",
"cost": [
"requirements": {
"buildings": [],
"research": [],
"resources": [
{
"name": "iron",
"amount": 1
@ -36,7 +43,8 @@
"name": "coal",
"amount": 1
}
],
]
},
"energy": 11,
"multiplier": 2.5
}
@ -46,7 +54,10 @@
"buildings": [
{
"id": "research-lab",
"cost": [
"requirements": {
"buildings": [],
"research": [],
"resources": [
{
"name": "iron",
"amount": 1
@ -59,7 +70,8 @@
"name": "coal",
"amount": 1
}
],
]
},
"energy": 100,
"multiplier": 3
}

View File

@ -1,61 +1,108 @@
[
{
"id": "basic-engine",
"levelRequired": 1,
"requiredResearch": [],
"cost": {
"requirements": {
"buildings": [
{
"id": "research-lab",
"level": 1
}
],
"research": [],
"resources": {
"iron": 1000,
"gold": 500
}
},
"time": 60,
"multiplier": 2
}, {
"id": "advanced-engine",
"levelRequired": 5,
"requiredResearch": ["basic-engine"],
"cost": {
"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",
"levelRequired": 1,
"requiredResearch": [],
"cost": {
"requirements": {
"buildings": [
{
"id": "research-lab",
"level": 1
}
],
"research": [],
"resources": {
"iron": 1000,
"gold": 500
}
},
"time": 60,
"multiplier": 2
}, {
"id": "defensive-utilities",
"levelRequired": 1,
"requiredResearch": [],
"cost": {
"requirements": {
"buildings": [
{
"id": "research-lab",
"level": 1
}
],
"research": [],
"resources": {
"iron": 1000,
"gold": 500
}
},
"time": 60,
"multiplier": 2
}, {
"id": "oxygen-production",
"levelRequired": 1,
"requiredResearch": [],
"cost": {
"requirements": {
"buildings": [
{
"id": "research-lab",
"level": 1
}
],
"research": [],
"resources": {
"iron": 1000,
"gold": 500
}
},
"time": 200,
"multiplier": 2
}, {
"id": "terraforming",
"levelRequired": 7,
"requiredResearch": ["oxygen-production"],
"cost": {
"iron": 2000,
"gold": 1000
"requirements": {
"buildings": [
{
"id": "research-lab",
"level": 1
}
],
"research": [],
"resources": {
"iron": 1000,
"gold": 500
}
},
"time": 500,
"multiplier": 3

View File

@ -23,7 +23,7 @@ export const POST: APIRoute = async({ request }) => {
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);
const balance = calculateAvailableResources(resources, building.requirements.resources);
return new Response(
JSON.stringify({

View File

@ -33,14 +33,42 @@ export const POST: APIRoute = async({ request }) => {
)
}
// check if user has enough resources
const resources = await getUserResources(user._id);
// check requirements
// buildings
const buildings = user.buildings;
researchData.requirements.buildings.forEach((buildingReq) => {
if(buildings.filter((building) => building.id === buildingReq.id)[0].level < buildingReq.level) {
return new Response(
JSON.stringify({
code: 400,
message: "Bad Request",
error: `${buildingReq.id} level ${buildingReq.level} required`
}), { status: 400 }
)
}
});
// research
const playerResearch = await getUserResearch(user);
researchData.requirements.research.forEach((researchReq) => {
if(playerResearch.filter((research) => research.id === researchReq.id)[0].level < researchReq.level) {
return new Response(
JSON.stringify({
code: 400,
message: "Bad Request",
error: `${researchReq.id} level ${researchReq.level} required`
}), { status: 400 }
)
}
});
// resources
const resources = await getUserResources(user._id);
const playerCurrentResearch = playerResearch.filter((element: any) => element.id === researchId)[0];
const level = playerCurrentResearch ? playerCurrentResearch.level : 0;
const newResources = structuredClone(resources);
let hasEnoughResources = true;
Object.entries(researchData.cost).forEach(([key, value]) => {
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;

View File

@ -33,9 +33,13 @@ const langBuildings = await getLocales(locale, 'buildings');
{buildingsList.map(cat => (
<div class="building-card">
<h3>{langBuildings[`Label_${cat.category}`]}</h3>
{cat.buildings.map(building => (
<div>{langBuildings[`Label_${building.id}`]} - {langResources['Label_iron']}: {building.cost['iron']}, {langResources['Label_gold']}: {building.cost['gold']}, {langResources['Label_coal']}: {building.cost['coal']} | <a id={`build_${building.id}`} href="#" class="a-button">{langGame['Link_build']}</a></div>
{cat.buildings.map(building => ( <>
<h4>{langBuildings[`Label_${building.id}`]}</h4>
{building.requirements.resources.map(res => (
<div>{langResources[`Label_${res.name}`]}: {res.amount}</div>
))}
<a id={`build_${building.id}`} href="#" class="a-button">{langGame['Link_build']}</a>
</>))}
</div>
))}
</Layout>

View File

@ -7,7 +7,7 @@ import { getUserByAccessToken } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import ResourceBar from '../../components/ResourceBar.astro';
type ResearchDetail = { id: string, level: number, requiredResearch: string[], cost: { [key: string]: number } };
type ResearchDetail = { id: string, level: number, requiredResearch: { id: string, level: number}[], cost: { [key: string]: number } };
const researchList = (await import('../../lib/data/research.json')).default;
@ -31,11 +31,11 @@ researchList.forEach(element => {
const tempResDetails: ResearchDetail = {
id: element.id,
level: userLevel,
requiredResearch: element.requiredResearch,
requiredResearch: element.requirements.research,
cost: {}
}
Object.entries(element.cost).forEach(([key, value]) => {
Object.entries(element.requirements.resources).forEach(([key, value]) => {
tempResDetails.cost[key] = value * Math.pow(element.multiplier, userLevel);
});
researchDetails.push(tempResDetails);
@ -60,7 +60,7 @@ researchList.forEach(element => {
<div>{langResearch['Label_required']}:</div>
{research.requiredResearch.length !== 0 ? research.requiredResearch.map(req => (
<div>
{langResearch[`Label_${req}`].name}
{langResearch[`Label_${req.id}`].name} - {req.level}
</div>
)) : langResearch['Label_none']}
</div>