From 9aa8213f7b98a8b5d6a1d76193116925038289d4 Mon Sep 17 00:00:00 2001 From: Sainan Date: Sat, 11 May 2024 18:06:06 +0200 Subject: [PATCH] Store dojo components in database as subdocument array --- .../api/createGuildDojoController.ts | 28 ----------- src/controllers/api/getGuildDojoController.ts | 50 ++++++++++++++++++- src/models/guildModel.ts | 15 ++++-- src/routes/api.ts | 2 - src/types/guildTypes.ts | 15 +++++- 5 files changed, 73 insertions(+), 37 deletions(-) delete mode 100644 src/controllers/api/createGuildDojoController.ts diff --git a/src/controllers/api/createGuildDojoController.ts b/src/controllers/api/createGuildDojoController.ts deleted file mode 100644 index 1f74fac1..00000000 --- a/src/controllers/api/createGuildDojoController.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { RequestHandler } from "express"; -import { IDojoClient } from "@/src/types/guildTypes"; - -export const createGuildDojoController: RequestHandler = (req, res) => { - // req.body.toString() -> {"SpawnComponent":{"id":{"$oid":"000000000000000000000000"},"pf":"/Lotus/Levels/ClanDojo/DojoHall.level","ppf":""}} - - // I'm guessing the response is same as getGuildDojo.php, which responds like this for a fresh dojo: - res.json({ - _id: { $oid: req.params.guildId }, - Name: "If you can read this in-game, tell me I need to fetch the clan name from the DB, lol.", - Tier: 1, - FixedContributions: true, - DojoRevision: 1, - RevisionTime: Math.round(Date.now() / 1000), - Energy: 5, - Capacity: 100, - DojoRequestStatus: 0, - DojoComponents: [ - { - pf: "/Lotus/Levels/ClanDojo/DojoHall.level", - ppf: "", - id: { $oid: "000000000000000000000000" }, - CompletionTime: { $date: { $numberLong: "" + Date.now() } }, - DecoCapacity: 600 - } - ] - } satisfies IDojoClient); -}; diff --git a/src/controllers/api/getGuildDojoController.ts b/src/controllers/api/getGuildDojoController.ts index 4c8b683b..b38b23a9 100644 --- a/src/controllers/api/getGuildDojoController.ts +++ b/src/controllers/api/getGuildDojoController.ts @@ -1,5 +1,51 @@ import { RequestHandler } from "express"; +import { Types } from "mongoose"; +import { Guild } from "@/src/models/guildModel"; +import { IDojoClient } from "@/src/types/guildTypes"; +import { toOid, toMongoDate } from "@/src/helpers/inventoryHelpers"; -export const getGuildDojoController: RequestHandler = (_req, res) => { - res.json({}); // This is what I got for a fresh clan. +export const getGuildDojoController: RequestHandler = async (req, res) => { + const guildId = req.query.guildId as string; + + const guild = await Guild.findOne({ _id: guildId }); + if (!guild) { + res.status(404).end(); + return; + } + + // Populate dojo info if not present + if (!guild.DojoComponents || guild.DojoComponents.length == 0) { + guild.DojoComponents = [ + { + _id: new Types.ObjectId(), + pf: "/Lotus/Levels/ClanDojo/DojoHall.level", + ppf: "", + CompletionTime: new Date(Date.now()) + } + ]; + guild.save(); + } + + const dojo: IDojoClient = { + _id: { $oid: guildId }, + Name: guild.Name, + Tier: 1, + FixedContributions: true, + DojoRevision: 1, + RevisionTime: Math.round(Date.now() / 1000), + Energy: 5, + Capacity: 100, + DojoRequestStatus: 0, + DojoComponents: [] + }; + guild.DojoComponents.forEach(dojoComponent => { + dojo.DojoComponents.push({ + id: toOid(dojoComponent._id), + pf: dojoComponent.pf, + ppf: dojoComponent.ppf, + CompletionTime: toMongoDate(dojoComponent.CompletionTime), + DecoCapacity: 600 + }); + }); + res.json(dojo); }; diff --git a/src/models/guildModel.ts b/src/models/guildModel.ts index 99023d2f..450bf06c 100644 --- a/src/models/guildModel.ts +++ b/src/models/guildModel.ts @@ -1,10 +1,17 @@ -import { IGuild } from "@/src/types/guildTypes"; +import { IGuildDatabase, IDojoComponentDatabase } from "@/src/types/guildTypes"; import { model, Schema } from "mongoose"; import { toOid } from "@/src/helpers/inventoryHelpers"; -const guildSchema = new Schema( +const dojoComponentSchema = new Schema({ + pf: { type: String, required: true }, + ppf: String, + CompletionTime: Date +}); + +const guildSchema = new Schema( { - Name: { type: String, required: true } + Name: { type: String, required: true }, + DojoComponents: [dojoComponentSchema] }, { id: false } ); @@ -17,4 +24,4 @@ guildSchema.set("toJSON", { } }); -export const Guild = model("Guild", guildSchema); +export const Guild = model("Guild", guildSchema); diff --git a/src/routes/api.ts b/src/routes/api.ts index 3056baf9..8ca15002 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -52,7 +52,6 @@ import { getGuildLogController } from "../controllers/api/getGuildLogController" import { guildTechController } from "../controllers/api/guildTechController"; import { dojoController } from "@/src/controllers/api/dojoController"; import { getGuildDojoController } from "@/src/controllers/api/getGuildDojoController"; -import { createGuildDojoController } from "@/src/controllers/api/createGuildDojoController"; const apiRouter = express.Router(); @@ -115,6 +114,5 @@ apiRouter.post("/createGuild.php", createGuildController); apiRouter.post("/sell.php", sellController); apiRouter.post("/upgrades.php", upgradesController); apiRouter.post("/guildTech.php", guildTechController); -apiRouter.post("/createGuildDojo.php", createGuildDojoController); export { apiRouter }; diff --git a/src/types/guildTypes.ts b/src/types/guildTypes.ts index 4cfa853b..c969db6a 100644 --- a/src/types/guildTypes.ts +++ b/src/types/guildTypes.ts @@ -1,9 +1,15 @@ +import { Types } from "mongoose"; import { IOid, IMongoDate } from "@/src/types/commonTypes"; export interface IGuild { Name: string; } +export interface IGuildDatabase extends IGuild { + _id: Types.ObjectId; + DojoComponents?: IDojoComponentDatabase[]; +} + export interface ICreateGuildRequest { guildName: string; } @@ -22,9 +28,16 @@ export interface IDojoClient { } export interface IDojoComponentClient { + id: IOid; pf: string; ppf: string; - id: IOid; CompletionTime: IMongoDate; DecoCapacity: number; } + +export interface IDojoComponentDatabase { + _id: Types.ObjectId; + pf: string; + ppf: string; + CompletionTime: Date; +}