From 41623807e101a0a6f316fcb74330b8c41eeaa9f2 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Wed, 9 Apr 2025 15:41:21 +0200 Subject: [PATCH] chore: improve changeDojoRoot Using SortId instead of actually changing the component ids. What's strange is that providing/omitting SortId does seem to make a difference in regards to deco positioning, which is presumably what the POST body would be for. I've opted to simply always provide the SortId in hopes that this avoids the need for repositioning entirely. --- .../api/changeDojoRootController.ts | 19 +++++++------------ src/models/guildModel.ts | 1 + src/routes/api.ts | 1 + src/services/guildService.ts | 1 + src/types/guildTypes.ts | 1 + 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/controllers/api/changeDojoRootController.ts b/src/controllers/api/changeDojoRootController.ts index e28b92d98..fb334b88b 100644 --- a/src/controllers/api/changeDojoRootController.ts +++ b/src/controllers/api/changeDojoRootController.ts @@ -15,6 +15,11 @@ export const changeDojoRootController: RequestHandler = async (req, res) => { return; } + // Example POST body: {"pivot":[0, 0, -64],"components":"{\"670429301ca0a63848ccc467\":{\"R\":[0,0,0],\"P\":[0,3,32]},\"6704254a1ca0a63848ccb33c\":{\"R\":[0,0,0],\"P\":[0,9.25,-32]},\"670429461ca0a63848ccc731\":{\"R\":[-90,0,0],\"P\":[-47.999992370605,3,16]}}"} + if (req.body) { + logger.warn(`ignoring changeDojoRoot body, decos may end up beyond room bounds: ${String(req.body)}`); + } + const idToNode: Record = {}; guild.DojoComponents.forEach(x => { idToNode[x._id.toString()] = { @@ -43,23 +48,13 @@ export const changeDojoRootController: RequestHandler = async (req, res) => { newRoot.component.pp = undefined; newRoot.parent = undefined; - // Don't even ask me why this is needed because I don't know either + // Set/update SortId in top-to-bottom order const stack: INode[] = [newRoot]; - let i = 0; - const idMap: Record = {}; while (stack.length != 0) { const top = stack.shift()!; - idMap[top.component._id.toString()] = new Types.ObjectId( - (++i).toString(16).padStart(8, "0") + top.component._id.toString().substr(8) - ); + top.component.SortId = new Types.ObjectId(); top.children.forEach(x => stack.push(x)); } - guild.DojoComponents.forEach(x => { - x._id = idMap[x._id.toString()]; - if (x.pi) { - x.pi = idMap[x.pi.toString()]; - } - }); logger.debug("New tree:\n" + treeToString(newRoot)); diff --git a/src/models/guildModel.ts b/src/models/guildModel.ts index 2d7570392..7cd28c6cc 100644 --- a/src/models/guildModel.ts +++ b/src/models/guildModel.ts @@ -43,6 +43,7 @@ const dojoLeaderboardEntrySchema = new Schema( ); const dojoComponentSchema = new Schema({ + SortId: Schema.Types.ObjectId, pf: { type: String, required: true }, ppf: String, pi: Schema.Types.ObjectId, diff --git a/src/routes/api.ts b/src/routes/api.ts index d8be0cb33..7dfd562fa 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -145,6 +145,7 @@ const apiRouter = express.Router(); apiRouter.get("/abandonLibraryDailyTask.php", abandonLibraryDailyTaskController); apiRouter.get("/abortDojoComponentDestruction.php", abortDojoComponentDestructionController); apiRouter.get("/cancelGuildAdvertisement.php", cancelGuildAdvertisementController); +apiRouter.get("/changeDojoRoot.php", changeDojoRootController); apiRouter.get("/changeGuildRank.php", changeGuildRankController); apiRouter.get("/checkDailyMissionBonus.php", checkDailyMissionBonusController); apiRouter.get("/claimLibraryDailyTaskReward.php", claimLibraryDailyTaskRewardController); diff --git a/src/services/guildService.ts b/src/services/guildService.ts index f8e33a5e4..c92dde713 100644 --- a/src/services/guildService.ts +++ b/src/services/guildService.ts @@ -147,6 +147,7 @@ export const getDojoClient = async ( if (!componentId || dojoComponent._id.equals(componentId)) { const clientComponent: IDojoComponentClient = { id: toOid(dojoComponent._id), + SortId: toOid(dojoComponent.SortId ? dojoComponent.SortId : dojoComponent._id), // always providing a SortId so decos don't need repositioning to reparent pf: dojoComponent.pf, ppf: dojoComponent.ppf, Name: dojoComponent.Name, diff --git a/src/types/guildTypes.ts b/src/types/guildTypes.ts index 2502b57ed..518e6bcf1 100644 --- a/src/types/guildTypes.ts +++ b/src/types/guildTypes.ts @@ -190,6 +190,7 @@ export interface IDojoComponentDatabase "id" | "SortId" | "pi" | "CompletionTime" | "DestructionTime" | "Decos" | "PaintBot" > { _id: Types.ObjectId; + SortId?: Types.ObjectId; pi?: Types.ObjectId; CompletionTime?: Date; CompletionLogPending?: boolean;