WIP way to edit a char

This commit is contained in:
BlipRanger
2023-07-10 20:16:23 -04:00
parent d5c38077a4
commit bd2ca40452

View File

@@ -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);