Add API support for researching
This commit is contained in:
parent
44fdb88ab2
commit
6a7644f59f
|
@ -77,16 +77,16 @@ export const getUserResearch = async (user: User): Promise<Array<Research>> => {
|
||||||
return research;
|
return research;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createOrUpgradeResearch = async (username: string, research: Research) => {
|
export const createOrUpgradeResearch = async (userId: ObjectId, research: Research) => {
|
||||||
const users = await Users();
|
const users = await Users();
|
||||||
|
|
||||||
const result = await users.updateOne(
|
const result = await users.updateOne(
|
||||||
{ username, "research.id": research.id },
|
{ _id: userId, "research.id": research.id },
|
||||||
{ $set: { "research.$.level": research.level } }
|
{ $set: { "research.$.level": research.level } }
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result.modifiedCount === 0) {
|
if (result.modifiedCount === 0) {
|
||||||
await users.updateOne({ username },
|
await users.updateOne({ _id: userId },
|
||||||
{ $push: { research } }
|
{ $push: { research } }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
import { type APIRoute } from "astro";
|
||||||
|
import validateAccessToken from "../../../lib/utils/validateAccessToken";
|
||||||
|
import { getUserByAccessToken, getUserResearch } from "../../../lib/db/users";
|
||||||
|
|
||||||
|
export const GET: APIRoute = async({ request }) => {
|
||||||
|
const response = await validateAccessToken(request);
|
||||||
|
if(response instanceof Response) return response;
|
||||||
|
|
||||||
|
const user = await getUserByAccessToken(response);
|
||||||
|
if(user === null) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 401,
|
||||||
|
message: "Unauthorized"
|
||||||
|
}), { status: 401 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 200,
|
||||||
|
message: "OK",
|
||||||
|
data: await getUserResearch(user)
|
||||||
|
}), { status: 200 }
|
||||||
|
)
|
||||||
|
}
|
|
@ -1,12 +1,15 @@
|
||||||
import { type APIRoute } from "astro";
|
import { type APIRoute } from "astro";
|
||||||
import validateAccessToken from "../../../lib/utils/validateAccessToken";
|
import validateAccessToken from "../../../lib/utils/validateAccessToken";
|
||||||
import { getUserResources, updateUserResources } from "../../../lib/utils/resourceManager";
|
|
||||||
import research from '../../../lib/data/research.json';
|
import research from '../../../lib/data/research.json';
|
||||||
import { createOrUpgradeResearch, getUserByAccessToken, getUserResearch } from "../../../lib/db/users";
|
import { createOrUpgradeResearch, getUserByAccessToken, getUserResearch } from "../../../lib/db/users";
|
||||||
import type DBResource from "../../../types/DBResource";
|
import { getPlanetById } from "../../../lib/db/planets";
|
||||||
|
import Planet from "../../../types/Planet";
|
||||||
|
import { ObjectId } from "mongodb";
|
||||||
|
import { calculateCurrentAvailableResources, getResourcesFromPlanet, updatePlanetResources } from "../../../lib/utils/resourceManager";
|
||||||
|
import DBResource from "../../../types/DBResource";
|
||||||
|
import calculateAvailableResources from "../../../lib/utils/calculateAvailableResources";
|
||||||
|
|
||||||
export const POST: APIRoute = async({ request }) => {
|
export const POST: APIRoute = async({ request }) => {
|
||||||
// validate access token
|
|
||||||
const response = await validateAccessToken(request);
|
const response = await validateAccessToken(request);
|
||||||
if(response instanceof Response) return response;
|
if(response instanceof Response) return response;
|
||||||
|
|
||||||
|
@ -20,8 +23,20 @@ export const POST: APIRoute = async({ request }) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if research id is valid
|
let body;
|
||||||
const researchId = (await request.json()).research;
|
try {
|
||||||
|
body = await request.json()
|
||||||
|
} catch(e) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 400,
|
||||||
|
message: "Bad Request",
|
||||||
|
error: "Invalid JSON body"
|
||||||
|
}), { status: 400 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const researchId = body.research;
|
||||||
const researchData = research.filter((element: any) => element.id === researchId)[0];
|
const researchData = research.filter((element: any) => element.id === researchId)[0];
|
||||||
if(!researchId || !researchData) {
|
if(!researchId || !researchData) {
|
||||||
return new Response(
|
return new Response(
|
||||||
|
@ -32,17 +47,38 @@ export const POST: APIRoute = async({ request }) => {
|
||||||
}), { status: 400 }
|
}), { status: 400 }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
let userPlanet: Planet | null;
|
||||||
|
try {
|
||||||
|
userPlanet = await getPlanetById(new ObjectId(body.planetId));
|
||||||
|
} catch(e) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 400,
|
||||||
|
message: "Bad Request",
|
||||||
|
error: "Invalid planet id"
|
||||||
|
}), { status: 400 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if(!userPlanet) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 404,
|
||||||
|
message: "Not Found",
|
||||||
|
error: "Planet not found"
|
||||||
|
}), { status: 404 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// check requirements
|
// check requirements
|
||||||
// buildings
|
// buildings
|
||||||
const buildings = user.buildings;
|
const buildings = userPlanet.buildings;
|
||||||
researchData.requirements.buildings.forEach((buildingReq) => {
|
researchData.requirements.buildings.forEach((buildingReq) => {
|
||||||
if(buildings.filter((building) => building.id === buildingReq.id)[0].level < buildingReq.level) {
|
if(buildings.filter((building) => building.id === buildingReq.id)[0]?.level ?? 0 < buildingReq.level) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
code: 400,
|
code: 400,
|
||||||
message: "Bad Request",
|
message: "Bad Request",
|
||||||
error: `${buildingReq.id} level ${buildingReq.level} required`
|
error: `${buildingReq.id} level ${buildingReq.level} required, found ${buildings.filter((building) => building.id === buildingReq.id)[0]?.level ?? 0}`
|
||||||
}), { status: 400 }
|
}), { status: 400 }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -56,38 +92,54 @@ export const POST: APIRoute = async({ request }) => {
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
code: 400,
|
code: 400,
|
||||||
message: "Bad Request",
|
message: "Bad Request",
|
||||||
error: `${researchReq.id} level ${researchReq.level} required`
|
error: `${researchReq.id} level ${researchReq.level} required, found ${playerResearch.filter((research) => research.id === researchReq.id)[0].level}`
|
||||||
}), { status: 400 }
|
}), { status: 400 }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// resources
|
// resources
|
||||||
const resources = await getUserResources(user._id);
|
const resources = await calculateCurrentAvailableResources(userPlanet._id);
|
||||||
|
if(!resources) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 500,
|
||||||
|
message: "Internal Server Error",
|
||||||
|
error: "Failed to get resources"
|
||||||
|
}), { status: 500 }
|
||||||
|
)
|
||||||
|
}
|
||||||
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;
|
const missingResources: Array<{}> = [];
|
||||||
Object.entries(researchData.requirements.resources).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;
|
||||||
|
|
||||||
if(res.amount < cost) hasEnoughResources = false;
|
if(res.amount < cost) {
|
||||||
|
missingResources.push({
|
||||||
|
name: key,
|
||||||
|
required: cost,
|
||||||
|
available: res.amount
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
else newResources.filter((element: DBResource) => element.name === key)[0].amount -= cost;
|
else newResources.filter((element: DBResource) => element.name === key)[0].amount -= cost;
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!hasEnoughResources) {
|
if(missingResources.length > 0) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
code: 400,
|
code: 400,
|
||||||
message: "Bad Request",
|
message: "Bad Request",
|
||||||
error: "Not enough resources"
|
data: missingResources
|
||||||
}), { status: 400 }
|
}), { status: 400 }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
await updateUserResources(user._id, newResources);
|
await updatePlanetResources(userPlanet._id, newResources);
|
||||||
await createOrUpgradeResearch(user.username, { id: researchId, level: level + 1 });
|
await createOrUpgradeResearch(user._id, { id: researchId, level: level + 1 });
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|
Loading…
Reference in New Issue