From 0e55648336c783fc51b980a31dbe67b8502b214d Mon Sep 17 00:00:00 2001 From: LenAnderson Date: Fri, 15 Dec 2023 12:16:46 +0000 Subject: [PATCH] add rounding (round, ceil, floor) to /rand --- public/scripts/variables.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/public/scripts/variables.js b/public/scripts/variables.js index 1eecb3b77..8ec5abe4b 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -637,9 +637,19 @@ function lenValuesCallback(value) { return parsedValue.length; } -function randValuesCallback(from, to) { +function randValuesCallback(from, to, args) { const range = to - from; - return from + Math.random() * range; + const value = from + Math.random() * range; + if (args.round == 'round') { + return Math.round(value); + } + if (args.round == 'ceil') { + return Math.ceil(value); + } + if (args.round == 'floor') { + return Math.floor(value); + } + return value; } export function registerVariableCommands() { @@ -673,5 +683,5 @@ export function registerVariableCommands() { registerSlashCommand('sqrt', (_, value) => sqrtValuesCallback(value), [], '(a) – performs a square root operation of a value and passes the result down the pipe, can use variable names, e.g. /sqrt i', true, true); registerSlashCommand('round', (_, value) => roundValuesCallback(value), [], '(a) – rounds a value and passes the result down the pipe, can use variable names, e.g. /round i', true, true); registerSlashCommand('len', (_, value) => lenValuesCallback(value), [], '(a) – gets the length of a value and passes the result down the pipe, can use variable names, e.g. /len i', true, true); - registerSlashCommand('rand', (args, value) => randValuesCallback(Number(args.from ?? 0), Number(args.to ?? (value.length ? value : 1))), [], '(from=number=0 to=number=1) – returns a random number between from and to, e.g. /rand or /rand 10 or /rand from=5 to=10', true, true); + registerSlashCommand('rand', (args, value) => randValuesCallback(Number(args.from ?? 0), Number(args.to ?? (value.length ? value : 1)), args), [], '(from=number=0 to=number=1 round=round|ceil|floor) – returns a random number between from and to, e.g. /rand or /rand 10 or /rand from=5 to=10', true, true); }