From 00d4cc2fd216ce523c63c7318d40e1af701cec62 Mon Sep 17 00:00:00 2001 From: Sainan Date: Wed, 12 Mar 2025 13:53:59 +0100 Subject: [PATCH] feat: track TechChanges in clan log --- src/controllers/api/getGuildLogController.ts | 7 +++ src/controllers/api/guildTechController.ts | 57 ++++++++++++++++++-- src/models/guildModel.ts | 26 +++++---- src/types/guildTypes.ts | 7 +++ 4 files changed, 78 insertions(+), 19 deletions(-) diff --git a/src/controllers/api/getGuildLogController.ts b/src/controllers/api/getGuildLogController.ts index 245d857e..4d859a65 100644 --- a/src/controllers/api/getGuildLogController.ts +++ b/src/controllers/api/getGuildLogController.ts @@ -18,6 +18,13 @@ export const getGuildLogController: RequestHandler = async (req, res) => { StandingsUpdates: [], ClassChanges: [] }; + guild.TechChanges?.forEach(entry => { + log.TechChanges.push({ + dateTime: toMongoDate(entry.dateTime), + entryType: entry.entryType, + details: entry.details + }); + }); guild.ClassChanges?.forEach(entry => { log.ClassChanges.push({ dateTime: toMongoDate(entry.dateTime), diff --git a/src/controllers/api/guildTechController.ts b/src/controllers/api/guildTechController.ts index 90714492..ff92244f 100644 --- a/src/controllers/api/guildTechController.ts +++ b/src/controllers/api/guildTechController.ts @@ -13,8 +13,9 @@ import { import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes"; import { IInventoryChanges } from "@/src/types/purchaseTypes"; import { config } from "@/src/services/configService"; -import { ITechProjectDatabase } from "@/src/types/guildTypes"; +import { ITechProjectClient, ITechProjectDatabase } from "@/src/types/guildTypes"; import { TGuildDatabaseDocument } from "@/src/models/guildModel"; +import { toMongoDate } from "@/src/helpers/inventoryHelpers"; export const guildTechController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); @@ -23,9 +24,29 @@ export const guildTechController: RequestHandler = async (req, res) => { const data = JSON.parse(String(req.body)) as TGuildTechRequest; const action = data.Action.split(",")[0]; if (action == "Sync") { - res.json({ - TechProjects: guild.toJSON().TechProjects - }); + let needSave = false; + const techProjects: ITechProjectClient[] = []; + if (guild.TechProjects) { + for (const project of guild.TechProjects) { + const techProject: ITechProjectClient = { + ItemType: project.ItemType, + ReqCredits: project.ReqCredits, + ReqItems: project.ReqItems, + State: project.State + }; + if (project.CompletionDate) { + techProject.CompletionDate = toMongoDate(project.CompletionDate); + if (Date.now() >= project.CompletionDate.getTime()) { + needSave ||= setTechLogState(guild, project.ItemType, 4, project.CompletionDate); + } + } + techProjects.push(techProject); + } + } + if (needSave) { + await guild.save(); + } + res.json({ TechProjects: techProjects }); } else if (action == "Start") { const recipe = ExportDojoRecipes.research[data.RecipeType!]; guild.TechProjects ??= []; @@ -42,6 +63,7 @@ export const guildTechController: RequestHandler = async (req, res) => { State: 0 }) - 1 ]; + setTechLogState(guild, techProject.ItemType, 5); if (config.noDojoResearchCosts) { processFundedProject(guild, techProject, recipe); } @@ -159,10 +181,35 @@ const processFundedProject = ( recipe: IDojoResearch ): void => { techProject.State = 1; - techProject.CompletionDate = new Date(new Date().getTime() + (config.noDojoResearchTime ? 0 : recipe.time) * 1000); + techProject.CompletionDate = new Date(Date.now() + (config.noDojoResearchTime ? 0 : recipe.time) * 1000); if (recipe.guildXpValue) { guild.XP += recipe.guildXpValue; } + setTechLogState(guild, techProject.ItemType, config.noDojoResearchTime ? 4 : 3, techProject.CompletionDate); +}; + +const setTechLogState = ( + guild: TGuildDatabaseDocument, + type: string, + state: number, + dateTime: Date | undefined = undefined +): boolean => { + guild.TechChanges ??= []; + const entry = guild.TechChanges.find(x => x.details == type); + if (entry) { + if (entry.entryType == state) { + return false; + } + entry.dateTime = dateTime ?? new Date(); + entry.entryType = state; + } else { + guild.TechChanges.push({ + dateTime: dateTime ?? new Date(), + entryType: state, + details: type + }); + } + return true; }; type TGuildTechRequest = diff --git a/src/models/guildModel.ts b/src/models/guildModel.ts index f1d16068..3c786ca8 100644 --- a/src/models/guildModel.ts +++ b/src/models/guildModel.ts @@ -2,15 +2,14 @@ import { IGuildDatabase, IDojoComponentDatabase, ITechProjectDatabase, - ITechProjectClient, IDojoDecoDatabase, ILongMOTD, IGuildMemberDatabase, - IGuildLogClassChange + IGuildLogClassChange, + IGuildLogTechChange } from "@/src/types/guildTypes"; import { Document, Model, model, Schema, Types } from "mongoose"; import { fusionTreasuresSchema, typeCountSchema } from "./inventoryModels/inventoryModel"; -import { toMongoDate } from "../helpers/inventoryHelpers"; const dojoDecoSchema = new Schema({ Type: String, @@ -51,17 +50,6 @@ const techProjectSchema = new Schema( { _id: false } ); -techProjectSchema.set("toJSON", { - virtuals: true, - transform(_doc, obj) { - const db = obj as ITechProjectDatabase; - const client = obj as ITechProjectClient; - if (db.CompletionDate) { - client.CompletionDate = toMongoDate(db.CompletionDate); - } - } -}); - const longMOTDSchema = new Schema( { message: String, @@ -70,6 +58,15 @@ const longMOTDSchema = new Schema( { _id: false } ); +const guildLogTechChangeSchema = new Schema( + { + dateTime: Date, + entryType: Number, + details: String + }, + { _id: false } +); + const guildLogClassChangeSchema = new Schema( { dateTime: Date, @@ -100,6 +97,7 @@ const guildSchema = new Schema( CeremonyContributors: { type: [Types.ObjectId], default: undefined }, CeremonyResetDate: Date, CeremonyEndo: Number, + TechChanges: { type: [guildLogTechChangeSchema], default: undefined }, ClassChanges: { type: [guildLogClassChangeSchema], default: undefined } }, { id: false } diff --git a/src/types/guildTypes.ts b/src/types/guildTypes.ts index 01348693..de04588d 100644 --- a/src/types/guildTypes.ts +++ b/src/types/guildTypes.ts @@ -47,6 +47,7 @@ export interface IGuildDatabase { CeremonyContributors?: Types.ObjectId[]; CeremonyResetDate?: Date; + TechChanges?: IGuildLogTechChange[]; ClassChanges?: IGuildLogClassChange[]; } @@ -163,6 +164,12 @@ export interface ITechProjectDatabase extends Omit