From bd2ca40452e655158fa5300cedb21b9d8e3b69f2 Mon Sep 17 00:00:00 2001 From: BlipRanger <1860540+BlipRanger@users.noreply.github.com> Date: Mon, 10 Jul 2023 20:16:23 -0400 Subject: [PATCH] WIP way to edit a char --- server.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/server.js b/server.js index 9b543ecd9..f94bb7a66 100644 --- a/server.js +++ b/server.js @@ -1026,6 +1026,38 @@ app.post("/editcharacter", urlencodedParser, async function (request, response) } }); +// take the name of a character, the field to be chaged, and the new value +// and change the value of the field in the character's json file +// and save the file. +app.post("/editcharacterattribute", urlencodedParser, async function (request, response) { + if (!request.body) { + console.error('Error: no response body detected'); + response.status(400).send('Error: no response body detected'); + return; + } + + if (request.body.ch_name === '' || request.body.ch_name === undefined || request.body.ch_name === '.') { + console.error('Error: invalid name.'); + response.status(400).send('Error: invalid name.'); + return; + } + + try { + const avatarPath = path.join(charactersPath, request.body.avatar_url); + charaRead(avatarPath).then((char) => { + char = JSON.parse(char); + char[request.body.field] = request.body.value; + char = JSON.stringify(char); + + }).then(() => { + charaWrite(avatarPath, char, request.body.avatar_url, response, 'Character saved'); + }); + } + catch { + console.error('An error occured, character edit invalidated.'); + } +}); + app.post("/deletecharacter", urlencodedParser, function (request, response) { if (!request.body || !request.body.avatar_url) { return response.sendStatus(400);