From dabca46e884b605ee06cdd7cc04a0d08aa2559c1 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Wed, 18 Jun 2025 11:05:07 -0700 Subject: [PATCH] feat(webui): automatically commit toggle changes (#2198) Closes #2197 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2198 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- .../custom/updateConfigDataController.ts | 14 ++++-- src/services/configService.ts | 2 +- src/services/configWatcherService.ts | 6 --- static/webui/index.html | 23 +++++---- static/webui/script.js | 47 +++++++++---------- static/webui/translations/de.js | 2 +- static/webui/translations/en.js | 2 +- static/webui/translations/es.js | 2 +- static/webui/translations/fr.js | 2 +- static/webui/translations/ru.js | 2 +- static/webui/translations/zh.js | 2 +- 11 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/controllers/custom/updateConfigDataController.ts b/src/controllers/custom/updateConfigDataController.ts index 534dfe0f..7c87c372 100644 --- a/src/controllers/custom/updateConfigDataController.ts +++ b/src/controllers/custom/updateConfigDataController.ts @@ -1,15 +1,21 @@ import { RequestHandler } from "express"; -import { updateConfig } from "@/src/services/configWatcherService"; +import { saveConfig } from "@/src/services/configWatcherService"; import { getAccountForRequest, isAdministrator } from "@/src/services/loginService"; +import { config, IConfig } from "@/src/services/configService"; -const updateConfigDataController: RequestHandler = async (req, res) => { +export const updateConfigDataController: RequestHandler = async (req, res) => { const account = await getAccountForRequest(req); if (isAdministrator(account)) { - await updateConfig(String(req.body)); + const data = req.body as IUpdateConfigDataRequest; + config[data.key] = data.value; + await saveConfig(); res.end(); } else { res.status(401).end(); } }; -export { updateConfigDataController }; +interface IUpdateConfigDataRequest { + key: keyof IConfig; + value: never; +} diff --git a/src/services/configService.ts b/src/services/configService.ts index fbcda8af..aeabb306 100644 --- a/src/services/configService.ts +++ b/src/services/configService.ts @@ -2,7 +2,7 @@ import fs from "fs"; import path from "path"; import { repoDir } from "@/src/helpers/pathHelper"; -interface IConfig { +export interface IConfig { mongodbUrl: string; logger: { files: boolean; diff --git a/src/services/configWatcherService.ts b/src/services/configWatcherService.ts index 88a5acdc..544bd96c 100644 --- a/src/services/configWatcherService.ts +++ b/src/services/configWatcherService.ts @@ -46,12 +46,6 @@ export const validateConfig = (): void => { } }; -export const updateConfig = async (data: string): Promise => { - amnesia = true; - await fsPromises.writeFile(configPath, data); - Object.assign(config, JSON.parse(data)); -}; - export const saveConfig = async (): Promise => { amnesia = true; await fsPromises.writeFile(configPath, JSON.stringify(config, null, 2)); diff --git a/static/webui/index.html b/static/webui/index.html index 398eb647..9b9015a2 100644 --- a/static/webui/index.html +++ b/static/webui/index.html @@ -571,7 +571,7 @@

-
+
@@ -732,16 +732,21 @@
-
+ - -
-
+
+ + +
+ +
- -
- - +
+ + +
+ +
diff --git a/static/webui/script.js b/static/webui/script.js index b51648e1..bb7ec52a 100644 --- a/static/webui/script.js +++ b/static/webui/script.js @@ -1761,34 +1761,29 @@ function doAcquireMod() { const uiConfigs = [...$("#server-settings input[id]")].map(x => x.id); -function doChangeSettings() { - revalidateAuthz(() => { - fetch("/custom/config?" + window.authz) - .then(response => response.json()) - .then(json => { - for (const i of uiConfigs) { - var x = document.getElementById(i); - if (x != null) { - if (x.type == "checkbox") { - if (x.checked === true) { - json[i] = true; - } else { - json[i] = false; - } - } else if (x.type == "number") { - json[i] = parseInt(x.value); - } - } - } - $.post({ - url: "/custom/config?" + window.authz, - contentType: "text/plain", - data: JSON.stringify(json, null, 2) - }).then(() => { - // A few cheats affect the inventory response which in turn may change what values we need to show +for (const id of uiConfigs) { + const elm = document.getElementById(id); + if (elm.type == "checkbox") { + elm.onchange = function () { + $.post({ + url: "/custom/config?" + window.authz, + contentType: "application/json", + data: JSON.stringify({ key: id, value: this.checked }) + }).then(() => { + if (["infiniteCredits", "infinitePlatinum", "infiniteEndo", "infiniteRegalAya"].indexOf(id) != -1) { updateInventory(); - }); + } }); + }; + } +} + +function doSaveConfig(id) { + const elm = document.getElementById(id); + $.post({ + url: "/custom/config?" + window.authz, + contentType: "application/json", + data: JSON.stringify({ key: id, value: parseInt(elm.value) }) }); } diff --git a/static/webui/translations/de.js b/static/webui/translations/de.js index 8b88151b..a4771cf3 100644 --- a/static/webui/translations/de.js +++ b/static/webui/translations/de.js @@ -167,7 +167,7 @@ dict = { cheats_fastClanAscension: `Schneller Clan-Aufstieg`, cheats_spoofMasteryRank: `Gefälschter Meisterschaftsrang (-1 zum deaktivieren)`, cheats_nightwaveStandingMultiplier: `[UNTRANSLATED] Nightwave Standing Multiplier`, - cheats_saveSettings: `Einstellungen speichern`, + cheats_save: `[UNTRANSLATED] Save`, cheats_account: `Account`, cheats_unlockAllFocusSchools: `Alle Fokus-Schulen freischalten`, cheats_helminthUnlockAll: `Helminth vollständig aufleveln`, diff --git a/static/webui/translations/en.js b/static/webui/translations/en.js index 437d5057..f17c2e0a 100644 --- a/static/webui/translations/en.js +++ b/static/webui/translations/en.js @@ -166,7 +166,7 @@ dict = { cheats_fastClanAscension: `Fast Clan Ascension`, cheats_spoofMasteryRank: `Spoofed Mastery Rank (-1 to disable)`, cheats_nightwaveStandingMultiplier: `Nightwave Standing Multiplier`, - cheats_saveSettings: `Save Settings`, + cheats_save: `Save`, cheats_account: `Account`, cheats_unlockAllFocusSchools: `Unlock All Focus Schools`, cheats_helminthUnlockAll: `Fully Level Up Helminth`, diff --git a/static/webui/translations/es.js b/static/webui/translations/es.js index e07199ca..4638debe 100644 --- a/static/webui/translations/es.js +++ b/static/webui/translations/es.js @@ -167,7 +167,7 @@ dict = { cheats_fastClanAscension: `Ascenso rápido del clan`, cheats_spoofMasteryRank: `Rango de maestría simulado (-1 para desactivar)`, cheats_nightwaveStandingMultiplier: `Multiplicador de Reputación de Onda Nocturna`, - cheats_saveSettings: `Guardar configuración`, + cheats_save: `[UNTRANSLATED] Save`, cheats_account: `Cuenta`, cheats_unlockAllFocusSchools: `Desbloquear todas las escuelas de enfoque`, cheats_helminthUnlockAll: `Subir al máximo el Helminto`, diff --git a/static/webui/translations/fr.js b/static/webui/translations/fr.js index 8d4eceb2..e4c99dd6 100644 --- a/static/webui/translations/fr.js +++ b/static/webui/translations/fr.js @@ -167,7 +167,7 @@ dict = { cheats_fastClanAscension: `Ascension de clan rapide`, cheats_spoofMasteryRank: `Rang de maîtrise personnalisé (-1 pour désactiver)`, cheats_nightwaveStandingMultiplier: `[UNTRANSLATED] Nightwave Standing Multiplier`, - cheats_saveSettings: `Sauvegarder les paramètres`, + cheats_save: `[UNTRANSLATED] Save`, cheats_account: `Compte`, cheats_unlockAllFocusSchools: `Débloquer toutes les écoles de focus`, cheats_helminthUnlockAll: `Helminth niveau max`, diff --git a/static/webui/translations/ru.js b/static/webui/translations/ru.js index 7493099d..e0247b7e 100644 --- a/static/webui/translations/ru.js +++ b/static/webui/translations/ru.js @@ -167,7 +167,7 @@ dict = { cheats_fastClanAscension: `Мгновенное Вознесение Клана`, cheats_spoofMasteryRank: `Подделанный ранг мастерства (-1 для отключения)`, cheats_nightwaveStandingMultiplier: `[UNTRANSLATED] Nightwave Standing Multiplier`, - cheats_saveSettings: `Сохранить настройки`, + cheats_save: `[UNTRANSLATED] Save`, cheats_account: `Аккаунт`, cheats_unlockAllFocusSchools: `Разблокировать все школы фокуса`, cheats_helminthUnlockAll: `Полностью улучшить Гельминта`, diff --git a/static/webui/translations/zh.js b/static/webui/translations/zh.js index 722b709a..22bc33f9 100644 --- a/static/webui/translations/zh.js +++ b/static/webui/translations/zh.js @@ -167,7 +167,7 @@ dict = { cheats_fastClanAscension: `快速升级氏族`, cheats_spoofMasteryRank: `伪造精通段位(-1为禁用)`, cheats_nightwaveStandingMultiplier: `午夜电波声望倍率`, - cheats_saveSettings: `保存设置`, + cheats_save: `[UNTRANSLATED] Save`, cheats_account: `账户`, cheats_unlockAllFocusSchools: `解锁所有专精学派`, cheats_helminthUnlockAll: `完全升级Helminth`,