From c832ba7888f83ec602f4ff7075bfd2197c5086b6 Mon Sep 17 00:00:00 2001 From: "Vincent Yanzee J. Tan" Date: Tue, 18 Jun 2024 10:50:53 +0800 Subject: [PATCH] added ranges in int parser and fixed xyz parser --- src/catalyst/core/cmdparser.ts | 41 +++++++++++++++++++++++++--------- src/server/commands/clear.ts | 1 + src/server/commands/help.ts | 1 + 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/catalyst/core/cmdparser.ts b/src/catalyst/core/cmdparser.ts index e9605f1..42aba07 100644 --- a/src/catalyst/core/cmdparser.ts +++ b/src/catalyst/core/cmdparser.ts @@ -451,7 +451,28 @@ registerCommandTypeParser('int', (argv, argDef) => { err.token = argv[0]; throw err; } - return { value: +argv[0]?.text }; + + // range + const val = +argv[0]?.text; + if (argDef.range) { + const range: [number | null, number | null] = argDef.range; + + // min range + if (typeof range[0] == 'number' && val < range[0]) { + const err = new CommandError(`${val} is too low, it must be atleast ${range[0]}`); + err.token = argv[0]; + throw err; + } + + // max range + if (typeof range[1] == 'number' && val > range[1]) { + const err = new CommandError(`${val} is too high, it must be atmost ${range[1]}`); + err.token = argv[0]; + throw err; + } + } + + return { value: val }; }); registerCommandTypeParser('boolean', (argv, argDef) => { @@ -549,21 +570,21 @@ registerCommandTypeParser('xyz', (argv, argDef) => { } function handleFn(org: vec3, rot: vec2): vec3 { - let finalX = org.x; - let finalY = org.y; - let finalZ = org.z; + let finalX = x; + let finalY = y; + let finalZ = z; // radians of thr rot const pitch = degToRad(rot.x); const yaw = degToRad(rot.y); // compute modifiers - if (xRel) finalX += x; - if (xRot) finalX += x * Math.cos(pitch) * -Math.sin(yaw); - if (yRel) finalY += y; - if (yRot) finalY += y * -Math.sin(pitch); - if (zRel) finalZ += z; - if (zRot) finalZ += z * Math.cos(pitch) * Math.cos(yaw); + if (xRel) finalX = org.x + x; + if (xRot) finalX = org.x + x * Math.cos(pitch) * -Math.sin(yaw); + if (yRel) finalY = org.y + y; + if (yRot) finalY = org.y + y * -Math.sin(pitch); + if (zRel) finalZ = org.z + z; + if (zRot) finalZ = org.z + z * Math.cos(pitch) * Math.cos(yaw); return { x: finalX, diff --git a/src/server/commands/clear.ts b/src/server/commands/clear.ts index 82077b1..79ee0c0 100644 --- a/src/server/commands/clear.ts +++ b/src/server/commands/clear.ts @@ -11,6 +11,7 @@ const info: commandSub = { name: "lineCount", dest: "lines", type: "int", + range: [ 1, null ], default: 100, } ] diff --git a/src/server/commands/help.ts b/src/server/commands/help.ts index 3ab9343..759f5b0 100644 --- a/src/server/commands/help.ts +++ b/src/server/commands/help.ts @@ -17,6 +17,7 @@ const info: commandSub = { type: "int", name: "page", dest: "page", + range: [ 0, null ], required: false, default: 1, }