forked from OpenWF/SpaceNinjaServer
feat: implement startDojoRecipeController to place new rooms in Dojo (#273)
Co-authored-by: Sainan <Sainan@users.noreply.github.com>
This commit is contained in:
parent
6c64b5070e
commit
e35a7fd69f
@ -24,7 +24,8 @@
|
|||||||
"@typescript-eslint/no-unsafe-assignment": "warn",
|
"@typescript-eslint/no-unsafe-assignment": "warn",
|
||||||
"@typescript-eslint/no-explicit-any": "warn",
|
"@typescript-eslint/no-explicit-any": "warn",
|
||||||
"@typescript-eslint/no-loss-of-precision": "warn",
|
"@typescript-eslint/no-loss-of-precision": "warn",
|
||||||
"no-case-declarations": "warn"
|
"no-case-declarations": "warn",
|
||||||
|
"no-mixed-spaces-and-tabs": "warn"
|
||||||
},
|
},
|
||||||
"parser": "@typescript-eslint/parser",
|
"parser": "@typescript-eslint/parser",
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
import { Types } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
import { Guild } from "@/src/models/guildModel";
|
import { Guild } from "@/src/models/guildModel";
|
||||||
import { IDojoClient } from "@/src/types/guildTypes";
|
import { IDojoClient, IDojoComponentClient } from "@/src/types/guildTypes";
|
||||||
import { toOid, toMongoDate } from "@/src/helpers/inventoryHelpers";
|
import { toOid, toMongoDate } from "@/src/helpers/inventoryHelpers";
|
||||||
|
|
||||||
export const getGuildDojoController: RequestHandler = async (req, res) => {
|
export const getGuildDojoController: RequestHandler = async (req, res) => {
|
||||||
@ -39,13 +39,21 @@ export const getGuildDojoController: RequestHandler = async (req, res) => {
|
|||||||
DojoComponents: []
|
DojoComponents: []
|
||||||
};
|
};
|
||||||
guild.DojoComponents.forEach(dojoComponent => {
|
guild.DojoComponents.forEach(dojoComponent => {
|
||||||
dojo.DojoComponents.push({
|
const clientComponent: IDojoComponentClient = {
|
||||||
id: toOid(dojoComponent._id),
|
id: toOid(dojoComponent._id),
|
||||||
pf: dojoComponent.pf,
|
pf: dojoComponent.pf,
|
||||||
ppf: dojoComponent.ppf,
|
ppf: dojoComponent.ppf,
|
||||||
CompletionTime: toMongoDate(dojoComponent.CompletionTime),
|
|
||||||
DecoCapacity: 600
|
DecoCapacity: 600
|
||||||
});
|
};
|
||||||
|
if (dojoComponent.pi) {
|
||||||
|
clientComponent.pi = toOid(dojoComponent.pi);
|
||||||
|
clientComponent.op = dojoComponent.op!;
|
||||||
|
clientComponent.pp = dojoComponent.pp!;
|
||||||
|
}
|
||||||
|
if (dojoComponent.CompletionTime) {
|
||||||
|
clientComponent.CompletionTime = toMongoDate(dojoComponent.CompletionTime);
|
||||||
|
}
|
||||||
|
dojo.DojoComponents.push(clientComponent);
|
||||||
});
|
});
|
||||||
res.json(dojo);
|
res.json(dojo);
|
||||||
};
|
};
|
||||||
|
28
src/controllers/api/startDojoRecipeController.ts
Normal file
28
src/controllers/api/startDojoRecipeController.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { RequestHandler } from "express";
|
||||||
|
import { IDojoComponentClient } from "@/src/types/guildTypes";
|
||||||
|
import { getGuildForRequest } from "@/src/services/guildService";
|
||||||
|
import { Types } from "mongoose";
|
||||||
|
|
||||||
|
interface IStartDojoRecipeRequest {
|
||||||
|
PlacedComponent: IDojoComponentClient;
|
||||||
|
Revision: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const startDojoRecipeController: RequestHandler = async (req, res) => {
|
||||||
|
const guild = await getGuildForRequest(req);
|
||||||
|
// At this point, we know that a member of the guild is making this request. Assuming they are allowed to start a build.
|
||||||
|
const request = JSON.parse(req.body.toString()) as IStartDojoRecipeRequest;
|
||||||
|
guild.DojoComponents!.push({
|
||||||
|
_id: new Types.ObjectId(),
|
||||||
|
pf: request.PlacedComponent.pf,
|
||||||
|
ppf: request.PlacedComponent.ppf,
|
||||||
|
pi: new Types.ObjectId(request.PlacedComponent.pi!.$oid),
|
||||||
|
op: request.PlacedComponent.op,
|
||||||
|
pp: request.PlacedComponent.pp,
|
||||||
|
CompletionTime: new Date(Date.now()) // TOOD: Omit this field & handle the "Collecting Materials" state.
|
||||||
|
});
|
||||||
|
await guild.save();
|
||||||
|
res.json({
|
||||||
|
DojoRequestStatus: 0
|
||||||
|
});
|
||||||
|
};
|
@ -4,6 +4,9 @@ import { model, Schema } from "mongoose";
|
|||||||
const dojoComponentSchema = new Schema<IDojoComponentDatabase>({
|
const dojoComponentSchema = new Schema<IDojoComponentDatabase>({
|
||||||
pf: { type: String, required: true },
|
pf: { type: String, required: true },
|
||||||
ppf: String,
|
ppf: String,
|
||||||
|
pi: Schema.Types.ObjectId,
|
||||||
|
op: String,
|
||||||
|
pp: String,
|
||||||
CompletionTime: Date
|
CompletionTime: Date
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -53,6 +53,7 @@ import { guildTechController } from "../controllers/api/guildTechController";
|
|||||||
import { dojoController } from "@/src/controllers/api/dojoController";
|
import { dojoController } from "@/src/controllers/api/dojoController";
|
||||||
import { getGuildDojoController } from "@/src/controllers/api/getGuildDojoController";
|
import { getGuildDojoController } from "@/src/controllers/api/getGuildDojoController";
|
||||||
import { syndicateSacrificeController } from "../controllers/api/syndicateSacrificeController";
|
import { syndicateSacrificeController } from "../controllers/api/syndicateSacrificeController";
|
||||||
|
import { startDojoRecipeController } from "@/src/controllers/api/startDojoRecipeController";
|
||||||
|
|
||||||
const apiRouter = express.Router();
|
const apiRouter = express.Router();
|
||||||
|
|
||||||
@ -116,5 +117,6 @@ apiRouter.post("/sell.php", sellController);
|
|||||||
apiRouter.post("/upgrades.php", upgradesController);
|
apiRouter.post("/upgrades.php", upgradesController);
|
||||||
apiRouter.post("/guildTech.php", guildTechController);
|
apiRouter.post("/guildTech.php", guildTechController);
|
||||||
apiRouter.post("/syndicateSacrifice.php", syndicateSacrificeController);
|
apiRouter.post("/syndicateSacrifice.php", syndicateSacrificeController);
|
||||||
|
apiRouter.post("/startDojoRecipe.php", startDojoRecipeController);
|
||||||
|
|
||||||
export { apiRouter };
|
export { apiRouter };
|
||||||
|
18
src/services/guildService.ts
Normal file
18
src/services/guildService.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { Request } from "express";
|
||||||
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
|
import { getInventory } from "@/src/services/inventoryService";
|
||||||
|
import { Guild } from "@/src/models/guildModel";
|
||||||
|
|
||||||
|
export const getGuildForRequest = async (req: Request) => {
|
||||||
|
const accountId = await getAccountIdForRequest(req);
|
||||||
|
const inventory = await getInventory(accountId);
|
||||||
|
const guildId = req.query.guildId as string;
|
||||||
|
if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
|
||||||
|
throw new Error("Account is not in the guild that it has sent a request for");
|
||||||
|
}
|
||||||
|
const guild = await Guild.findOne({ _id: guildId });
|
||||||
|
if (!guild) {
|
||||||
|
throw new Error("Account thinks it is a in guild that doesn't exist");
|
||||||
|
}
|
||||||
|
return guild;
|
||||||
|
};
|
@ -1,5 +1,6 @@
|
|||||||
import { Types } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
import { IOid, IMongoDate } from "@/src/types/commonTypes";
|
import { IOid, IMongoDate } from "@/src/types/commonTypes";
|
||||||
|
import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
|
|
||||||
export interface IGuild {
|
export interface IGuild {
|
||||||
Name: string;
|
Name: string;
|
||||||
@ -31,13 +32,18 @@ export interface IDojoComponentClient {
|
|||||||
id: IOid;
|
id: IOid;
|
||||||
pf: string;
|
pf: string;
|
||||||
ppf: string;
|
ppf: string;
|
||||||
CompletionTime: IMongoDate;
|
pi?: IOid; // Parent ID. N/A to root.
|
||||||
DecoCapacity: number;
|
op?: string; // "Open Portal"? N/A to root.
|
||||||
|
pp?: string; // "Parent Portal"? N/A to root.
|
||||||
|
RegularCredits?: number; // "Collecting Materials" state: Number of credits that were donated.
|
||||||
|
MiscItems?: IMiscItem[]; // "Collecting Materials" state: Resources that were donated.
|
||||||
|
CompletionTime?: IMongoDate;
|
||||||
|
DecoCapacity?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IDojoComponentDatabase {
|
export interface IDojoComponentDatabase
|
||||||
|
extends Omit<IDojoComponentClient, "id" | "pi" | "CompletionTime" | "DecoCapacity"> {
|
||||||
_id: Types.ObjectId;
|
_id: Types.ObjectId;
|
||||||
pf: string;
|
pi?: Types.ObjectId;
|
||||||
ppf: string;
|
CompletionTime?: Date;
|
||||||
CompletionTime: Date;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user