Add requirements section to buildings and research
This commit is contained in:
parent
afca4bc0ea
commit
5bb3940088
|
@ -4,39 +4,47 @@
|
|||
"buildings": [
|
||||
{
|
||||
"id": "iron-mine",
|
||||
"cost": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
],
|
||||
"requirements": {
|
||||
"buildings": [],
|
||||
"research": [],
|
||||
"resources": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
"energy": 11,
|
||||
"multiplier": 2
|
||||
},
|
||||
{
|
||||
"id": "gold-mine",
|
||||
"cost": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
],
|
||||
"requirements": {
|
||||
"buildings": [],
|
||||
"research": [],
|
||||
"resources": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
"energy": 11,
|
||||
"multiplier": 2.5
|
||||
}
|
||||
|
@ -46,20 +54,24 @@
|
|||
"buildings": [
|
||||
{
|
||||
"id": "research-lab",
|
||||
"cost": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
],
|
||||
"requirements": {
|
||||
"buildings": [],
|
||||
"research": [],
|
||||
"resources": [
|
||||
{
|
||||
"name": "iron",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "gold",
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"name": "coal",
|
||||
"amount": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
"energy": 100,
|
||||
"multiplier": 3
|
||||
}
|
||||
|
|
|
@ -1,61 +1,108 @@
|
|||
[
|
||||
{
|
||||
"id": "basic-engine",
|
||||
"levelRequired": 1,
|
||||
"requiredResearch": [],
|
||||
"cost": {
|
||||
"iron": 1000,
|
||||
"gold": 500
|
||||
"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": {
|
||||
"iron": 2000,
|
||||
"gold": 1000
|
||||
"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": {
|
||||
"iron": 1000,
|
||||
"gold": 500
|
||||
"requirements": {
|
||||
"buildings": [
|
||||
{
|
||||
"id": "research-lab",
|
||||
"level": 1
|
||||
}
|
||||
],
|
||||
"research": [],
|
||||
"resources": {
|
||||
"iron": 1000,
|
||||
"gold": 500
|
||||
}
|
||||
},
|
||||
"time": 60,
|
||||
"multiplier": 2
|
||||
}, {
|
||||
"id": "defensive-utilities",
|
||||
"levelRequired": 1,
|
||||
"requiredResearch": [],
|
||||
"cost": {
|
||||
"iron": 1000,
|
||||
"gold": 500
|
||||
"requirements": {
|
||||
"buildings": [
|
||||
{
|
||||
"id": "research-lab",
|
||||
"level": 1
|
||||
}
|
||||
],
|
||||
"research": [],
|
||||
"resources": {
|
||||
"iron": 1000,
|
||||
"gold": 500
|
||||
}
|
||||
},
|
||||
"time": 60,
|
||||
"multiplier": 2
|
||||
}, {
|
||||
"id": "oxygen-production",
|
||||
"levelRequired": 1,
|
||||
"requiredResearch": [],
|
||||
"cost": {
|
||||
"iron": 1000,
|
||||
"gold": 500
|
||||
"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
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue