From 9f5a2d1eb3dbf4a5c4faf1906e794db4d10042ce Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Tue, 11 Jan 2022 02:01:31 -0500 Subject: [PATCH] [WebHost] weighted-settings: Validate settings before allowing game generation or export --- WebHostLib/static/assets/weighted-settings.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/WebHostLib/static/assets/weighted-settings.js b/WebHostLib/static/assets/weighted-settings.js index 9a298ce3cc..a89a003fbe 100644 --- a/WebHostLib/static/assets/weighted-settings.js +++ b/WebHostLib/static/assets/weighted-settings.js @@ -835,7 +835,7 @@ const updateGameSetting = (event) => { localStorage.setItem('weighted-settings', JSON.stringify(options)); }; -const exportSettings = () => { +const validateSettings = () => { const settings = JSON.parse(localStorage.getItem('weighted-settings')); const userMessage = document.getElementById('user-message'); let errorMessage = null; @@ -891,6 +891,12 @@ const exportSettings = () => { // If no error occurred, hide the user message if it is visible userMessage.classList.remove('visible'); + return settings; +}; + +const exportSettings = () => { + const settings = validateSettings(); + if (!settings) { return; } const yamlText = jsyaml.safeDump(settings, { noCompatMode: true }).replaceAll(/'(\d+)':/g, (x, y) => `${y}:`); download(`${document.getElementById('player-name').value}.yaml`, yamlText); @@ -908,9 +914,12 @@ const download = (filename, text) => { }; const generateGame = (raceMode = false) => { + const settings = validateSettings(); + if (!settings) { return; } + axios.post('/api/generate', { - weights: { player: localStorage.getItem('weighted-settings') }, - presetData: { player: localStorage.getItem('weighted-settings') }, + weights: { player: JSON.stringify(settings) }, + presetData: { player: JSON.stringify(settings) }, playerCount: 1, race: raceMode ? '1' : '0', }).then((response) => {