Add requirements section to buildings and research
This commit is contained in:
parent
afca4bc0ea
commit
5bb3940088
|
@ -4,7 +4,10 @@
|
||||||
"buildings": [
|
"buildings": [
|
||||||
{
|
{
|
||||||
"id": "iron-mine",
|
"id": "iron-mine",
|
||||||
"cost": [
|
"requirements": {
|
||||||
|
"buildings": [],
|
||||||
|
"research": [],
|
||||||
|
"resources": [
|
||||||
{
|
{
|
||||||
"name": "iron",
|
"name": "iron",
|
||||||
"amount": 1
|
"amount": 1
|
||||||
|
@ -17,13 +20,17 @@
|
||||||
"name": "coal",
|
"name": "coal",
|
||||||
"amount": 1
|
"amount": 1
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
|
},
|
||||||
"energy": 11,
|
"energy": 11,
|
||||||
"multiplier": 2
|
"multiplier": 2
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "gold-mine",
|
"id": "gold-mine",
|
||||||
"cost": [
|
"requirements": {
|
||||||
|
"buildings": [],
|
||||||
|
"research": [],
|
||||||
|
"resources": [
|
||||||
{
|
{
|
||||||
"name": "iron",
|
"name": "iron",
|
||||||
"amount": 1
|
"amount": 1
|
||||||
|
@ -36,7 +43,8 @@
|
||||||
"name": "coal",
|
"name": "coal",
|
||||||
"amount": 1
|
"amount": 1
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
|
},
|
||||||
"energy": 11,
|
"energy": 11,
|
||||||
"multiplier": 2.5
|
"multiplier": 2.5
|
||||||
}
|
}
|
||||||
|
@ -46,7 +54,10 @@
|
||||||
"buildings": [
|
"buildings": [
|
||||||
{
|
{
|
||||||
"id": "research-lab",
|
"id": "research-lab",
|
||||||
"cost": [
|
"requirements": {
|
||||||
|
"buildings": [],
|
||||||
|
"research": [],
|
||||||
|
"resources": [
|
||||||
{
|
{
|
||||||
"name": "iron",
|
"name": "iron",
|
||||||
"amount": 1
|
"amount": 1
|
||||||
|
@ -59,7 +70,8 @@
|
||||||
"name": "coal",
|
"name": "coal",
|
||||||
"amount": 1
|
"amount": 1
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
|
},
|
||||||
"energy": 100,
|
"energy": 100,
|
||||||
"multiplier": 3
|
"multiplier": 3
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,61 +1,108 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"id": "basic-engine",
|
"id": "basic-engine",
|
||||||
"levelRequired": 1,
|
"requirements": {
|
||||||
"requiredResearch": [],
|
"buildings": [
|
||||||
"cost": {
|
{
|
||||||
|
"id": "research-lab",
|
||||||
|
"level": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"research": [],
|
||||||
|
"resources": {
|
||||||
"iron": 1000,
|
"iron": 1000,
|
||||||
"gold": 500
|
"gold": 500
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"time": 60,
|
"time": 60,
|
||||||
"multiplier": 2
|
"multiplier": 2
|
||||||
}, {
|
}, {
|
||||||
"id": "advanced-engine",
|
"id": "advanced-engine",
|
||||||
"levelRequired": 5,
|
"requirements": {
|
||||||
"requiredResearch": ["basic-engine"],
|
"buildings": [
|
||||||
"cost": {
|
{
|
||||||
|
"id": "research-lab",
|
||||||
|
"level": 5
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"research": [
|
||||||
|
{
|
||||||
|
"id": "basic-engine",
|
||||||
|
"level": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"resources": {
|
||||||
"iron": 2000,
|
"iron": 2000,
|
||||||
"gold": 1000
|
"gold": 1000
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"time": 120,
|
"time": 120,
|
||||||
"multiplier": 3
|
"multiplier": 3
|
||||||
}, {
|
}, {
|
||||||
"id": "combat-utilities",
|
"id": "combat-utilities",
|
||||||
"levelRequired": 1,
|
"requirements": {
|
||||||
"requiredResearch": [],
|
"buildings": [
|
||||||
"cost": {
|
{
|
||||||
|
"id": "research-lab",
|
||||||
|
"level": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"research": [],
|
||||||
|
"resources": {
|
||||||
"iron": 1000,
|
"iron": 1000,
|
||||||
"gold": 500
|
"gold": 500
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"time": 60,
|
"time": 60,
|
||||||
"multiplier": 2
|
"multiplier": 2
|
||||||
}, {
|
}, {
|
||||||
"id": "defensive-utilities",
|
"id": "defensive-utilities",
|
||||||
"levelRequired": 1,
|
"requirements": {
|
||||||
"requiredResearch": [],
|
"buildings": [
|
||||||
"cost": {
|
{
|
||||||
|
"id": "research-lab",
|
||||||
|
"level": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"research": [],
|
||||||
|
"resources": {
|
||||||
"iron": 1000,
|
"iron": 1000,
|
||||||
"gold": 500
|
"gold": 500
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"time": 60,
|
"time": 60,
|
||||||
"multiplier": 2
|
"multiplier": 2
|
||||||
}, {
|
}, {
|
||||||
"id": "oxygen-production",
|
"id": "oxygen-production",
|
||||||
"levelRequired": 1,
|
"requirements": {
|
||||||
"requiredResearch": [],
|
"buildings": [
|
||||||
"cost": {
|
{
|
||||||
|
"id": "research-lab",
|
||||||
|
"level": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"research": [],
|
||||||
|
"resources": {
|
||||||
"iron": 1000,
|
"iron": 1000,
|
||||||
"gold": 500
|
"gold": 500
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"time": 200,
|
"time": 200,
|
||||||
"multiplier": 2
|
"multiplier": 2
|
||||||
}, {
|
}, {
|
||||||
"id": "terraforming",
|
"id": "terraforming",
|
||||||
"levelRequired": 7,
|
"requirements": {
|
||||||
"requiredResearch": ["oxygen-production"],
|
"buildings": [
|
||||||
"cost": {
|
{
|
||||||
"iron": 2000,
|
"id": "research-lab",
|
||||||
"gold": 1000
|
"level": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"research": [],
|
||||||
|
"resources": {
|
||||||
|
"iron": 1000,
|
||||||
|
"gold": 500
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"time": 500,
|
"time": 500,
|
||||||
"multiplier": 3
|
"multiplier": 3
|
||||||
|
|
|
@ -23,7 +23,7 @@ export const POST: APIRoute = async({ request }) => {
|
||||||
const resources = await getUserResources(user._id);
|
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.requirements.resources);
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|
|
@ -33,14 +33,42 @@ export const POST: APIRoute = async({ request }) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if user has enough resources
|
// check requirements
|
||||||
const resources = await getUserResources(user._id);
|
// 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);
|
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 playerCurrentResearch = playerResearch.filter((element: any) => element.id === researchId)[0];
|
||||||
const level = playerCurrentResearch ? playerCurrentResearch.level : 0;
|
const level = playerCurrentResearch ? playerCurrentResearch.level : 0;
|
||||||
const newResources = structuredClone(resources);
|
const newResources = structuredClone(resources);
|
||||||
let hasEnoughResources = true;
|
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 res = resources.filter((element: DBResource) => element.name === key)[0];
|
||||||
const cost = playerCurrentResearch ? value * Math.pow(researchData.multiplier, level) : value;
|
const cost = playerCurrentResearch ? value * Math.pow(researchData.multiplier, level) : value;
|
||||||
|
|
||||||
|
|
|
@ -33,9 +33,13 @@ const langBuildings = await getLocales(locale, 'buildings');
|
||||||
{buildingsList.map(cat => (
|
{buildingsList.map(cat => (
|
||||||
<div class="building-card">
|
<div class="building-card">
|
||||||
<h3>{langBuildings[`Label_${cat.category}`]}</h3>
|
<h3>{langBuildings[`Label_${cat.category}`]}</h3>
|
||||||
{cat.buildings.map(building => (
|
{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>
|
<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>
|
</div>
|
||||||
))}
|
))}
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
|
@ -7,7 +7,7 @@ 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';
|
||||||
|
|
||||||
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;
|
const researchList = (await import('../../lib/data/research.json')).default;
|
||||||
|
|
||||||
|
@ -31,11 +31,11 @@ researchList.forEach(element => {
|
||||||
const tempResDetails: ResearchDetail = {
|
const tempResDetails: ResearchDetail = {
|
||||||
id: element.id,
|
id: element.id,
|
||||||
level: userLevel,
|
level: userLevel,
|
||||||
requiredResearch: element.requiredResearch,
|
requiredResearch: element.requirements.research,
|
||||||
cost: {}
|
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);
|
tempResDetails.cost[key] = value * Math.pow(element.multiplier, userLevel);
|
||||||
});
|
});
|
||||||
researchDetails.push(tempResDetails);
|
researchDetails.push(tempResDetails);
|
||||||
|
@ -60,7 +60,7 @@ researchList.forEach(element => {
|
||||||
<div>{langResearch['Label_required']}:</div>
|
<div>{langResearch['Label_required']}:</div>
|
||||||
{research.requiredResearch.length !== 0 ? research.requiredResearch.map(req => (
|
{research.requiredResearch.length !== 0 ? research.requiredResearch.map(req => (
|
||||||
<div>
|
<div>
|
||||||
{langResearch[`Label_${req}`].name}
|
{langResearch[`Label_${req.id}`].name} - {req.level}
|
||||||
</div>
|
</div>
|
||||||
)) : langResearch['Label_none']}
|
)) : langResearch['Label_none']}
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue