From 2c62fb3c3cf47b62976272a9729990f5037dcce2 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Thu, 17 Jul 2025 05:04:30 -0700 Subject: [PATCH] chore: move syncConfigWithDatabase to configService (#2505) This avoids a cyclic dependency due to configController using this Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2505 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- src/controllers/custom/configController.ts | 3 +-- src/index.ts | 4 ++-- src/services/configService.ts | 18 ++++++++++++++++++ src/services/configWatcherService.ts | 20 +------------------- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/controllers/custom/configController.ts b/src/controllers/custom/configController.ts index 9f3c17e3..2a1aca25 100644 --- a/src/controllers/custom/configController.ts +++ b/src/controllers/custom/configController.ts @@ -1,9 +1,8 @@ import { RequestHandler } from "express"; -import { config } from "@/src/services/configService"; +import { config, syncConfigWithDatabase } from "@/src/services/configService"; import { getAccountForRequest, isAdministrator } from "@/src/services/loginService"; import { saveConfig } from "@/src/services/configWriterService"; import { sendWsBroadcastExcept } from "@/src/services/wsService"; -import { syncConfigWithDatabase } from "@/src/services/configWatcherService"; export const getConfigController: RequestHandler = async (req, res) => { const account = await getAccountForRequest(req); diff --git a/src/index.ts b/src/index.ts index 383e9123..6bd70f7c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ // First, init config. -import { config, configPath, loadConfig } from "@/src/services/configService"; +import { config, configPath, loadConfig, syncConfigWithDatabase } from "@/src/services/configService"; import fs from "fs"; try { loadConfig(); @@ -21,7 +21,7 @@ import mongoose from "mongoose"; import { JSONStringify } from "json-with-bigint"; import { startWebServer } from "@/src/services/webService"; -import { syncConfigWithDatabase, validateConfig } from "@/src/services/configWatcherService"; +import { validateConfig } from "@/src/services/configWatcherService"; import { updateWorldStateCollections } from "@/src/services/worldStateService"; // Patch JSON.stringify to work flawlessly with Bigints. diff --git a/src/services/configService.ts b/src/services/configService.ts index 36efe7e2..95a2bd4e 100644 --- a/src/services/configService.ts +++ b/src/services/configService.ts @@ -2,6 +2,7 @@ import fs from "fs"; import path from "path"; import { repoDir } from "@/src/helpers/pathHelper"; import { args } from "@/src/helpers/commandLineArguments"; +import { Inbox } from "@/src/models/inboxModel"; export interface IConfig { mongodbUrl: string; @@ -119,3 +120,20 @@ export const loadConfig = (): void => { Object.assign(config, newConfig); }; + +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. + // Also, for some reason, I can't just do `Inbox.deleteMany(...)`; - it needs this whole circus. + if (!config.worldState?.creditBoost) { + void Inbox.deleteMany({ globaUpgradeId: "5b23106f283a555109666672" }).then(() => {}); + } + if (!config.worldState?.affinityBoost) { + void Inbox.deleteMany({ globaUpgradeId: "5b23106f283a555109666673" }).then(() => {}); + } + if (!config.worldState?.resourceBoost) { + void Inbox.deleteMany({ globaUpgradeId: "5b23106f283a555109666674" }).then(() => {}); + } + if (!config.worldState?.galleonOfGhouls) { + void Inbox.deleteMany({ goalTag: "GalleonRobbery" }).then(() => {}); + } +}; diff --git a/src/services/configWatcherService.ts b/src/services/configWatcherService.ts index 599552f8..d028b8ee 100644 --- a/src/services/configWatcherService.ts +++ b/src/services/configWatcherService.ts @@ -1,10 +1,9 @@ import chokidar from "chokidar"; import { logger } from "@/src/utils/logger"; -import { config, configPath, loadConfig } from "@/src/services/configService"; +import { config, configPath, loadConfig, syncConfigWithDatabase } from "@/src/services/configService"; import { saveConfig, shouldReloadConfig } from "@/src/services/configWriterService"; import { getWebPorts, startWebServer, stopWebServer } from "@/src/services/webService"; import { sendWsBroadcast } from "@/src/services/wsService"; -import { Inbox } from "@/src/models/inboxModel"; import varzia from "@/static/fixed_responses/worldState/varzia.json"; chokidar.watch(configPath).on("change", () => { @@ -68,20 +67,3 @@ export const validateConfig = (): void => { void saveConfig(); } }; - -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. - // Also, for some reason, I can't just do `Inbox.deleteMany(...)`; - it needs this whole circus. - if (!config.worldState?.creditBoost) { - void Inbox.deleteMany({ globaUpgradeId: "5b23106f283a555109666672" }).then(() => {}); - } - if (!config.worldState?.affinityBoost) { - void Inbox.deleteMany({ globaUpgradeId: "5b23106f283a555109666673" }).then(() => {}); - } - if (!config.worldState?.resourceBoost) { - void Inbox.deleteMany({ globaUpgradeId: "5b23106f283a555109666674" }).then(() => {}); - } - if (!config.worldState?.galleonOfGhouls) { - void Inbox.deleteMany({ goalTag: "GalleonRobbery" }).then(() => {}); - } -};