From 4c94bd0aa87350e071dfd41df08073e6f636efbc Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Mon, 27 Nov 2023 21:10:50 +0200
Subject: [PATCH] Add math operations
---
public/scripts/variables.js | 102 ++++++++++++++++++++++++++++++++++++
1 file changed, 102 insertions(+)
diff --git a/public/scripts/variables.js b/public/scripts/variables.js
index b2b6ed0fd..3b4fa26d8 100644
--- a/public/scripts/variables.js
+++ b/public/scripts/variables.js
@@ -340,6 +340,97 @@ function deleteGlobalVariable(name) {
return '';
}
+function parseNumericSeries(value) {
+ if (typeof value === 'number') {
+ return [value];
+ }
+
+ const array = value
+ .split(' ')
+ .map(i => i.trim())
+ .filter(i => i !== '')
+ .map(i => isNaN(Number(i)) ? resolveVariable(i) : Number(i))
+ .filter(i => !isNaN(i));
+
+ return array;
+}
+
+function performOperation(value, operation, singleOperand = false) {
+ if (!value) {
+ return 0;
+ }
+
+ const array = parseNumericSeries(value);
+
+ if (array.length === 0) {
+ return 0;
+ }
+
+ const result = singleOperand ? operation(array[0]) : operation(array);
+
+ if (isNaN(result) || !isFinite(result)) {
+ return 0;
+ }
+
+ return result;
+}
+
+function addValuesCallback(value) {
+ return performOperation(value, (array) => array.reduce((a, b) => a + b, 0));
+}
+
+function mulValuesCallback(value) {
+ return performOperation(value, (array) => array.reduce((a, b) => a * b, 1));
+}
+
+function subValuesCallback(value) {
+ return performOperation(value, (array) => array[0] - array[1]);
+}
+
+function divValuesCallback(value) {
+ return performOperation(value, (array) => {
+ if (array[1] === 0) {
+ console.warn('Division by zero.');
+ return 0;
+ }
+ return array[0] / array[1];
+ });
+}
+
+function modValuesCallback(value) {
+ return performOperation(value, (array) => {
+ if (array[1] === 0) {
+ console.warn('Division by zero.');
+ return 0;
+ }
+ return array[0] % array[1];
+ });
+}
+
+function powValuesCallback(value) {
+ return performOperation(value, (array) => Math.pow(array[0], array[1]));
+}
+
+function sinValuesCallback(value) {
+ return performOperation(value, Math.sin, true);
+}
+
+function cosValuesCallback(value) {
+ return performOperation(value, Math.cos, true);
+}
+
+function logValuesCallback(value) {
+ return performOperation(value, Math.log, true);
+}
+
+function roundValuesCallback(value) {
+ return performOperation(value, Math.round, true);
+}
+
+function absValuesCallback(value) {
+ return performOperation(value, Math.abs, true);
+}
+
export function registerVariableCommands() {
registerSlashCommand('listvar', listVariablesCallback, [], ' – list registered chat variables', true, true);
registerSlashCommand('setvar', (args, value) => setLocalVariable(args.key || args.name, value), [], 'key=varname (value) – set a local variable value and pass it down the pipe, e.g. /setvar key=color green', true, true);
@@ -352,4 +443,15 @@ export function registerVariableCommands() {
registerSlashCommand('while', whileCallback, [], 'left=varname1 right=varname2 rule=comparison "(command)" – compare the value of the left operand "a" with the value of the right operand "b", and if the condition yields true, then execute any valid slash command enclosed in quotes. Numeric values and string literals for left and right operands supported. Available rules: gt => a > b, gte => a >= b, lt => a < b, lte => a <= b, eq => a == b, neq => a != b, not => !a, in (strings) => a includes b, nin (strings) => a not includes b, e.g. /setvar key=i 0 | /while left=i right=10 rule=let "/addvar key=i 1" adds 1 to the value of "i" until it reaches 10. Loops are limited to 100 iterations by default, pass guard=off to disable.', true, true);
registerSlashCommand('flushvar', (_, value) => deleteLocalVariable(value), [], '(key) – delete a local variable, e.g. /flushvar score', true, true);
registerSlashCommand('flushglobalvar', (_, value) => deleteGlobalVariable(value), [], '(key) – delete a global variable, e.g. /flushglobalvar score', true, true);
+ registerSlashCommand('add', (_, value) => addValuesCallback(value), [], '(a b c d) – performs an addition of the set of values and passes the result down the pipe, can use variable names, e.g. /add 10 i 30 j', true, true);
+ registerSlashCommand('mul', (_, value) => mulValuesCallback(value), [], '(a b c d) – performs a multiplication of the set of values and passes the result down the pipe, can use variable names, e.g. /mul 10 i 30 j', true, true);
+ registerSlashCommand('sub', (_, value) => subValuesCallback(value), [], '(a b) – performs a subtraction of two values and passes the result down the pipe, can use variable names, e.g. /sub i 5', true, true);
+ registerSlashCommand('div', (_, value) => divValuesCallback(value), [], '(a b) – performs a division of two values and passes the result down the pipe, can use variable names, e.g. /div 10 i', true, true);
+ registerSlashCommand('mod', (_, value) => modValuesCallback(value), [], '(a b) – performs a modulo operation of two values and passes the result down the pipe, can use variable names, e.g. /mod i 2', true, true);
+ registerSlashCommand('pow', (_, value) => powValuesCallback(value), [], '(a b) – performs a power operation of two values and passes the result down the pipe, can use variable names, e.g. /pow i 2', true, true);
+ registerSlashCommand('sin', (_, value) => sinValuesCallback(value), [], '(a) – performs a sine operation of a value and passes the result down the pipe, can use variable names, e.g. /sin i', true, true);
+ registerSlashCommand('cos', (_, value) => cosValuesCallback(value), [], '(a) – performs a cosine operation of a value and passes the result down the pipe, can use variable names, e.g. /cos i', true, true);
+ registerSlashCommand('log', (_, value) => logValuesCallback(value), [], '(a) – performs a logarithm operation of a value and passes the result down the pipe, can use variable names, e.g. /log i', true, true);
+ registerSlashCommand('abs', (_, value) => absValuesCallback(value), [], '(a) – performs an absolute value operation of a value and passes the result down the pipe, can use variable names, e.g. /abs 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);
}