forked from OpenWF/SpaceNinjaServer
		
	chore: move syncConfigWithDatabase to configService (#2505)
This avoids a cyclic dependency due to configController using this Reviewed-on: OpenWF/SpaceNinjaServer#2505 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									5b215733aa
								
							
						
					
					
						commit
						2c62fb3c3c
					
				@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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.
 | 
			
		||||
 | 
			
		||||
@ -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(() => {});
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -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(() => {});
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user