diff --git a/src/index.ts b/src/index.ts index 7afd9387..f9d671a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,13 +21,14 @@ import mongoose from "mongoose"; import { JSONStringify } from "json-with-bigint"; import { startWebServer } from "./services/webService"; -import { validateConfig } from "@/src/services/configWatcherService"; +import { syncConfigWithDatabase, validateConfig } from "@/src/services/configWatcherService"; import { updateWorldStateCollections } from "./services/worldStateService"; // Patch JSON.stringify to work flawlessly with Bigints. JSON.stringify = JSONStringify; validateConfig(); +syncConfigWithDatabase(); mongoose .connect(config.mongodbUrl) diff --git a/src/models/inboxModel.ts b/src/models/inboxModel.ts index d339707e..139e8b44 100644 --- a/src/models/inboxModel.ts +++ b/src/models/inboxModel.ts @@ -27,11 +27,12 @@ export interface IMessage { icon?: string; highPriority?: boolean; lowPrioNewPlayers?: boolean; - startDate?: Date; - endDate?: Date; + transmission?: string; att?: string[]; countedAtt?: ITypeCount[]; - transmission?: string; + startDate?: Date; + endDate?: Date; + goalTag?: string; CrossPlatform?: boolean; arg?: Arg[]; gifts?: IGift[]; @@ -107,6 +108,7 @@ const messageSchema = new Schema( lowPrioNewPlayers: Boolean, startDate: Date, endDate: Date, + goalTag: String, date: { type: Date, required: true }, r: Boolean, CrossPlatform: Boolean, diff --git a/src/services/configWatcherService.ts b/src/services/configWatcherService.ts index bb64d5da..072b6b61 100644 --- a/src/services/configWatcherService.ts +++ b/src/services/configWatcherService.ts @@ -3,6 +3,7 @@ import fsPromises from "fs/promises"; import { logger } from "../utils/logger"; import { config, configPath, loadConfig } from "./configService"; import { getWebPorts, sendWsBroadcast, startWebServer, stopWebServer } from "./webService"; +import { Inbox } from "../models/inboxModel"; let amnesia = false; fs.watchFile(configPath, (now, then) => { @@ -22,6 +23,7 @@ fs.watchFile(configPath, (now, then) => { process.exit(1); } validateConfig(); + syncConfigWithDatabase(); const webPorts = getWebPorts(); if (config.httpPort != webPorts.http || config.httpsPort != webPorts.https) { @@ -51,6 +53,15 @@ export const validateConfig = (): void => { } } } + if ( + config.worldState?.galleonOfGhouls && + config.worldState.galleonOfGhouls != 1 && + config.worldState.galleonOfGhouls != 2 && + config.worldState.galleonOfGhouls != 3 + ) { + config.worldState.galleonOfGhouls = 0; + modified = true; + } if (modified) { logger.info(`Updating config file to fix some issues with it.`); void saveConfig(); @@ -61,3 +72,10 @@ export const saveConfig = async (): Promise => { amnesia = true; await fsPromises.writeFile(configPath, JSON.stringify(config, null, 2)); }; + +export const syncConfigWithDatabase = (): void => { + // Event messages are deleted after endDate. Since we don't use beginDate/endDate and instead have config toggles, we need to delete the messages once those bools are false. + if (!config.worldState?.galleonOfGhouls) { + void Inbox.deleteMany({ goalTag: "GalleonRobbery" }); + } +}; diff --git a/src/services/inboxService.ts b/src/services/inboxService.ts index cc5afc29..d623030d 100644 --- a/src/services/inboxService.ts +++ b/src/services/inboxService.ts @@ -54,6 +54,22 @@ export const createNewEventMessages = async (req: Request): Promise => { }); } + // BUG: Deleting the inbox message manually means it'll just be automatically re-created. This is because we don't use startDate/endDate for these config-toggled events. + if (config.worldState?.galleonOfGhouls) { + if (!(await Inbox.exists({ ownerId: account._id, goalTag: "GalleonRobbery" }))) { + newEventMessages.push({ + sndr: "/Lotus/Language/Bosses/BossCouncilorVayHek", + sub: "/Lotus/Language/Events/GalleonRobberyIntroMsgTitle", + msg: "/Lotus/Language/Events/GalleonRobberyIntroMsgDesc", + icon: "/Lotus/Interface/Icons/Npcs/VayHekPortrait.png", + transmission: "/Lotus/Sounds/Dialog/GalleonOfGhouls/DGhoulsWeekOneInbox0010VayHek", + att: ["/Lotus/Upgrades/Skins/Events/OgrisOldSchool"], + startDate: new Date(), + goalTag: "GalleonRobbery" + }); + } + } + if (newEventMessages.length === 0) { return; }