From e7da16c9b7d85abb236ecb761c1b6d9c62107b27 Mon Sep 17 00:00:00 2001 From: Govorunb Date: Thu, 20 Jun 2024 01:51:40 +1000 Subject: [PATCH] fix #49 --- common/types.ts | 2 +- ebs/src/modules/game/index.ts | 3 +-- frontend/www/src/modules/modal.ts | 30 ++++++++++++++++++------------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/common/types.ts b/common/types.ts index ad7e5d6..95c4a43 100644 --- a/common/types.ts +++ b/common/types.ts @@ -44,7 +44,7 @@ export type EnumParam = ParameterBase & { export type VectorParam = ParameterBase & { type: LiteralTypes.Vector; - defaultValues?: number[]; + defaultValue?: [number, number, number]; min?: number[]; max?: number[]; }; diff --git a/ebs/src/modules/game/index.ts b/ebs/src/modules/game/index.ts index 8ad2e9d..ccf898f 100644 --- a/ebs/src/modules/game/index.ts +++ b/ebs/src/modules/game/index.ts @@ -8,7 +8,7 @@ import { isStressTesting, startStressTest, StressTestRequest } from "./stresstes export let connection: GameConnection = new GameConnection(); -app.ws("/private/socket", (ws, req) => { +app.ws("/private/socket", (ws) => { connection.setSocket(ws); }); @@ -36,7 +36,6 @@ app.post( ); app.post("/private/setresult", (req, res) => { - //console.log(req.body); const msg = { ...connection.makeMessage(MessageType.Result), ...req.body, diff --git a/frontend/www/src/modules/modal.ts b/frontend/www/src/modules/modal.ts index bb1e642..e806e3d 100644 --- a/frontend/www/src/modules/modal.ts +++ b/frontend/www/src/modules/modal.ts @@ -26,7 +26,7 @@ const $modalConfirm = document.getElementById("modal-confirm")! as HTMLButtonEle const $modalCancel = document.getElementById("modal-cancel")! as HTMLButtonElement; /* Options */ -const $modalOptionsForm: HTMLFormElement = document.getElementById("modal-options-form")! as HTMLFormElement; +const $modalOptionsForm = document.getElementById("modal-options-form")! as HTMLFormElement; const $modalOptions = document.getElementById("modal-options")!; const $paramToggle = document.getElementById("modal-toggle")!; const $paramText = document.getElementById("modal-text")!; @@ -196,13 +196,16 @@ function checkForm() { function setCartArgsFromForm(form: HTMLFormElement) { const formData = new FormData(form); - formData.forEach((v, k) => { - if (k.endsWith("[]")) { - k = k.slice(0, -2); - - if (!cart!.args[k]) cart!.args[k] = []; - cart!.args[k].push(v); - } else cart!.args[k] = v; + formData.forEach((val, name) => { + const match = /(?\w+)\[(?\d{1,2})\]$/.exec(name); + if (!match?.length) { + cart!.args[name] = val; + } else { + const paramName = match.groups!["paramName"]; + cart!.args[paramName] ??= []; + const index = parseInt(match.groups!["index"]); + cart!.args[paramName][index] = val; + } }); } @@ -361,15 +364,18 @@ function addVector(modal: HTMLElement, param: VectorParam) { input.min = param.min?.toString() ?? ""; input.max = param.max?.toString() ?? ""; - setupField(field, input, param, true); + setupField(field, input, param, i); - if (Number.isFinite((param.defaultValues ?? [])[i])) input.value = (param.defaultValues ?? [])![i]!.toString(); + const defVal = param.defaultValue?.[i]; + input.value = Number.isFinite(defVal) + ? defVal!.toString() + : "0"; } modal.appendChild(field); } -function setupField(field: HTMLElement, inputElem: HTMLSelectElement | HTMLInputElement, param: Parameter, isArray?: boolean) { +function setupField(field: HTMLElement, inputElem: HTMLSelectElement | HTMLInputElement, param: Parameter, arrayIndex?: number) { const label = field.querySelector("label")!; field.id += "-" + param.name; @@ -379,7 +385,7 @@ function setupField(field: HTMLElement, inputElem: HTMLSelectElement | HTMLInput } inputElem.id += "-" + param.name; - inputElem.name = param.name.concat(isArray !== undefined ? "[]" : ""); + inputElem.name = param.name.concat(arrayIndex !== undefined ? `[${arrayIndex}]` : ""); label.id += "-" + param.name; label.htmlFor = inputElem.id;