From 4204c21780e67d8b0444d8d730d75375653e9142 Mon Sep 17 00:00:00 2001 From: Vampire Kitten Date: Fri, 14 Jun 2024 01:49:18 +0200 Subject: [PATCH] Add Config Change To WebUI Adds a settings menu to change various options. Currently can only change config.json, changing postTutorialInventory.json for the purpose of standing caps planned. --- src/controllers/custom/getConfigData.ts | 10 +++ src/controllers/custom/updateConfigData.ts | 16 +++++ src/routes/custom.ts | 7 +- src/routes/webui.ts | 3 + static/webui/index.html | 80 ++++++++++++++++++++-- static/webui/script.js | 52 ++++++++++++-- 6 files changed, 157 insertions(+), 11 deletions(-) create mode 100644 src/controllers/custom/getConfigData.ts create mode 100644 src/controllers/custom/updateConfigData.ts diff --git a/src/controllers/custom/getConfigData.ts b/src/controllers/custom/getConfigData.ts new file mode 100644 index 00000000..a0a1df55 --- /dev/null +++ b/src/controllers/custom/getConfigData.ts @@ -0,0 +1,10 @@ +import { RequestHandler } from "express"; +import path from "path"; +const rootDir = path.join(__dirname, "../../.."); + +const getConfigData: RequestHandler = (_req, res) => { + const configFile = require(path.join(rootDir, "config.json")); + res.json(configFile); +}; + +export { getConfigData }; diff --git a/src/controllers/custom/updateConfigData.ts b/src/controllers/custom/updateConfigData.ts new file mode 100644 index 00000000..051d5d1c --- /dev/null +++ b/src/controllers/custom/updateConfigData.ts @@ -0,0 +1,16 @@ +import { RequestHandler } from "express"; +import path from "path"; +const rootDir = path.join(__dirname, "../../.."); + +const updateConfigData: RequestHandler = async (req) => { + const fs = require('fs'); + const updateSettingsData = req.body; + + fs.writeFile(path.join(rootDir, "config.json"), updateSettingsData, function(err:any) { + if(err) { + return console.log(err); + } + }); +}; + +export { updateConfigData }; diff --git a/src/routes/custom.ts b/src/routes/custom.ts index 8a4218d7..4b01f1ca 100644 --- a/src/routes/custom.ts +++ b/src/routes/custom.ts @@ -2,6 +2,8 @@ import express from "express"; import { getItemListsController } from "@/src/controllers/custom/getItemListsController"; import { createAccountController } from "@/src/controllers/custom/createAccountController"; import { addItemController } from "@/src/controllers/custom/addItemController"; +import { getConfigData } from "@/src/controllers/custom/getConfigData"; +import { updateConfigData } from "@/src/controllers/custom/updateConfigData"; const customRouter = express.Router(); @@ -10,4 +12,7 @@ customRouter.get("/getItemLists", getItemListsController); customRouter.post("/createAccount", createAccountController); customRouter.post("/addItem", addItemController); -export { customRouter }; +customRouter.get("/config", getConfigData); +customRouter.post("/config", updateConfigData); + +export { customRouter }; \ No newline at end of file diff --git a/src/routes/webui.ts b/src/routes/webui.ts index 677031c6..93005f34 100644 --- a/src/routes/webui.ts +++ b/src/routes/webui.ts @@ -19,6 +19,9 @@ webuiRouter.use("/webui", (req, res, next) => { }); // Serve virtual routes +webuiRouter.get("/webui/settings", (_req, res) => { + res.sendFile(path.join(rootDir, "static/webui/index.html")); +}); webuiRouter.get("/webui/inventory", (_req, res) => { res.sendFile(path.join(rootDir, "static/webui/index.html")); }); diff --git a/static/webui/index.html b/static/webui/index.html index a680fd33..61194238 100644 --- a/static/webui/index.html +++ b/static/webui/index.html @@ -6,7 +6,7 @@ - +