chore: turn guild DojoComponents into a DocumentArray #1070

Merged
Sainan merged 1 commits from dojo-id into main 2025-03-03 05:46:17 -08:00
8 changed files with 36 additions and 27 deletions

View File

@ -9,7 +9,7 @@ export const changeDojoRootController: RequestHandler = async (req, res) => {
// At this point, we know that a member of the guild is making this request. Assuming they are allowed to change the root.
const idToNode: Record<string, INode> = {};
guild.DojoComponents!.forEach(x => {
guild.DojoComponents.forEach(x => {
idToNode[x._id.toString()] = {
component: x,
parent: undefined,
@ -18,7 +18,7 @@ export const changeDojoRootController: RequestHandler = async (req, res) => {
});
let oldRoot: INode | undefined;
guild.DojoComponents!.forEach(x => {
guild.DojoComponents.forEach(x => {
const node = idToNode[x._id.toString()];
if (x.pi) {
idToNode[x.pi.toString()].children.push(node);
@ -47,7 +47,7 @@ export const changeDojoRootController: RequestHandler = async (req, res) => {
);
top.children.forEach(x => stack.push(x));
}
guild.DojoComponents!.forEach(x => {
guild.DojoComponents.forEach(x => {
x._id = idMap[x._id.toString()];
if (x.pi) {
x.pi = idMap[x.pi.toString()];

View File

@ -13,15 +13,15 @@ export const getGuildDojoController: RequestHandler = async (req, res) => {
}
// Populate dojo info if not present
if (!guild.DojoComponents || guild.DojoComponents.length == 0) {
guild.DojoComponents = [
if (guild.DojoComponents.length == 0) {
guild.DojoComponents.push([
{
_id: new Types.ObjectId(),
pf: "/Lotus/Levels/ClanDojo/DojoHall.level",
ppf: "",
CompletionTime: new Date(Date.now())
}
];
]);
await guild.save();
}

View File

@ -5,8 +5,8 @@ import { ExportDojoRecipes } from "warframe-public-export-plus";
export const queueDojoComponentDestructionController: RequestHandler = async (req, res) => {
const guild = await getGuildForRequest(req);
const componentId = req.query.componentId as string;
const component = guild.DojoComponents!.splice(
guild.DojoComponents!.findIndex(x => x._id.toString() === componentId),
const component = guild.DojoComponents.splice(
guild.DojoComponents.findIndex(x => x._id.toString() === componentId),
1
)[0];
const room = Object.values(ExportDojoRecipes.rooms).find(x => x.resultType == component.pf);

View File

@ -4,7 +4,7 @@ import { getDojoClient, getGuildForRequest } from "@/src/services/guildService";
export const setDojoComponentMessageController: 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 change the message.
const component = guild.DojoComponents!.find(x => x._id.equals(req.query.componentId as string))!;
const component = guild.DojoComponents.id(req.query.componentId as string)!;
const payload = JSON.parse(String(req.body)) as SetDojoComponentMessageRequest;
if ("Name" in payload) {
component.Name = payload.Name;

View File

@ -20,7 +20,7 @@ export const startDojoRecipeController: RequestHandler = async (req, res) => {
guild.DojoEnergy += room.energy;
}
guild.DojoComponents!.push({
guild.DojoComponents.push({
_id: new Types.ObjectId(),
pf: request.PlacedComponent.pf,
ppf: request.PlacedComponent.ppf,

View File

@ -4,7 +4,7 @@ import {
ITechProjectDatabase,
ITechProjectClient
} from "@/src/types/guildTypes";
import { model, Schema } from "mongoose";
import { Document, Model, model, Schema, Types } from "mongoose";
import { typeCountSchema } from "./inventoryModels/inventoryModel";
import { toMongoDate } from "../helpers/inventoryHelpers";
@ -44,7 +44,7 @@ techProjectSchema.set("toJSON", {
const guildSchema = new Schema<IGuildDatabase>(
{
Name: { type: String, required: true },
DojoComponents: [dojoComponentSchema],
DojoComponents: { type: [dojoComponentSchema], default: [] },
DojoCapacity: { type: Number, default: 100 },
DojoEnergy: { type: Number, default: 5 },
TechProjects: { type: [techProjectSchema], default: undefined }
@ -52,4 +52,23 @@ const guildSchema = new Schema<IGuildDatabase>(
{ id: false }
);
export const Guild = model<IGuildDatabase>("Guild", guildSchema);
type GuildDocumentProps = {
DojoComponents: Types.DocumentArray<IDojoComponentDatabase>;
};
// eslint-disable-next-line @typescript-eslint/ban-types
type GuildModel = Model<IGuildDatabase, {}, GuildDocumentProps>;
export const Guild = model<IGuildDatabase, GuildModel>("Guild", guildSchema);
// eslint-disable-next-line @typescript-eslint/ban-types
export type TGuildDatabaseDocument = Document<unknown, {}, IGuildDatabase> &
Omit<
IGuildDatabase & {
_id: Types.ObjectId;
} & {
__v: number;
},
keyof GuildDocumentProps
> &
GuildDocumentProps;

View File

@ -1,10 +1,9 @@
import { Request } from "express";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { getInventory } from "@/src/services/inventoryService";
import { Guild } from "@/src/models/guildModel";
import { Guild, TGuildDatabaseDocument } from "@/src/models/guildModel";
import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
import { IDojoClient, IDojoComponentClient, IGuildDatabase } from "@/src/types/guildTypes";
import { Document, Types } from "mongoose";
import { IDojoClient, IDojoComponentClient } from "@/src/types/guildTypes";
import { toMongoDate, toOid } from "@/src/helpers/inventoryHelpers";
export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
@ -41,7 +40,7 @@ export const getDojoClient = (guild: TGuildDatabaseDocument, status: number): ID
DojoRequestStatus: status,
DojoComponents: []
};
guild.DojoComponents!.forEach(dojoComponent => {
guild.DojoComponents.forEach(dojoComponent => {
const clientComponent: IDojoComponentClient = {
id: toOid(dojoComponent._id),
pf: dojoComponent.pf,
@ -62,12 +61,3 @@ export const getDojoClient = (guild: TGuildDatabaseDocument, status: number): ID
});
return dojo;
};
// eslint-disable-next-line @typescript-eslint/ban-types
export type TGuildDatabaseDocument = Document<unknown, {}, IGuildDatabase> &
IGuildDatabase &
Required<{
_id: Types.ObjectId;
}> & {
__v: number;
};

View File

@ -8,7 +8,7 @@ export interface IGuild {
export interface IGuildDatabase extends IGuild {
_id: Types.ObjectId;
DojoComponents?: IDojoComponentDatabase[];
DojoComponents: IDojoComponentDatabase[];
DojoCapacity: number;
DojoEnergy: number;
TechProjects?: ITechProjectDatabase[];