From 8b8c7601f101fb220cfe6f36a3ea6ee5cbebf1f7 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 10 Feb 2022 01:12:57 +0100 Subject: [PATCH] [msgformat] Add more mathematical functions Signed-off-by: Knut Ahlers --- internal/template/numeric/numeric.go | 18 +++++++-- wiki/Templating.md | 58 ++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/internal/template/numeric/numeric.go b/internal/template/numeric/numeric.go index e0d6cc7..b4eadaa 100644 --- a/internal/template/numeric/numeric.go +++ b/internal/template/numeric/numeric.go @@ -1,10 +1,22 @@ package numeric -import "github.com/Luzifer/twitch-bot/plugins" +import ( + "math" + + "github.com/Luzifer/twitch-bot/plugins" +) func Register(args plugins.RegistrationArguments) error { - args.RegisterTemplateFunction("multiply", plugins.GenericTemplateFunctionGetter(multiply)) + args.RegisterTemplateFunction("add", plugins.GenericTemplateFunctionGetter(add)) + args.RegisterTemplateFunction("div", plugins.GenericTemplateFunctionGetter(div)) + args.RegisterTemplateFunction("mul", plugins.GenericTemplateFunctionGetter(mul)) + args.RegisterTemplateFunction("multiply", plugins.GenericTemplateFunctionGetter(mul)) // DEPRECATED + args.RegisterTemplateFunction("pow", plugins.GenericTemplateFunctionGetter(math.Pow)) + args.RegisterTemplateFunction("sub", plugins.GenericTemplateFunctionGetter(sub)) return nil } -func multiply(m1, m2 float64) float64 { return m1 * m2 } +func add(m1, m2 float64) float64 { return m1 + m2 } +func div(m1, m2 float64) float64 { return m1 / m2 } +func mul(m1, m2 float64) float64 { return m1 * m2 } +func sub(m1, m2 float64) float64 { return m1 - m2 } diff --git a/wiki/Templating.md b/wiki/Templating.md index 31e784c..1766922 100644 --- a/wiki/Templating.md +++ b/wiki/Templating.md @@ -25,6 +25,19 @@ Examples below are using this syntax in the code block: < Output from the template ``` +#### `add` + +Returns float from calculation: `float1 + float2` + +Syntax: `add ` + +Example: + +``` +# {{ printf "%.0f" (add 1 2) }}% +< 3 +``` + #### `arg` Takes the message sent to the channel, splits by space and returns the Nth element @@ -104,6 +117,19 @@ Example: < Luziferus - foobar ``` +#### `div` + +Returns float from calculation: `float1 / float2` + +Syntax: `div ` + +Example: + +``` +# {{ printf "%.0f" (div 27 9) }}% +< 3 +``` + #### `fixUsername` Ensures the username no longer contains the `@` or `#` prefix @@ -171,19 +197,32 @@ Example: < Last Quote: #32 ``` -#### `multiply` +#### `mul` (deprecated: `multiply`) Returns float from calculation: `float1 * float2` -Syntax: `multiply ` +Syntax: `mul ` Example: ``` -# {{ printf "%.0f" (multiply 100 (seededRandom "test")) }}% +# {{ printf "%.0f" (mul 100 (seededRandom "test")) }}% < 35% ``` +#### `pow` + +Returns float from calculation: `float1 ** float2` + +Syntax: `pow ` + +Example: + +``` +# {{ printf "%.0f" (pow 10 4) }}% +< 10000 +``` + #### `recentGame` Returns the last played game name of the specified user (see shoutout example) or the `fallback` if the game could not be fetched. If no fallback was supplied the message will fail and not be sent. @@ -223,6 +262,19 @@ Example: < Your int this hour: 17% ``` +#### `sub` + +Returns float from calculation: `float1 - float2` + +Syntax: `sub ` + +Example: + +``` +# {{ printf "%.0f" (sub 10 4) }}% +< 6 +``` + #### `tag` Takes the message sent to the channel, returns the value of the tag specified