[msgformat] Add more mathematical functions

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-02-10 01:12:57 +01:00
parent 0d9536ee3f
commit 8b8c7601f1
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
2 changed files with 70 additions and 6 deletions

View File

@ -1,10 +1,22 @@
package numeric package numeric
import "github.com/Luzifer/twitch-bot/plugins" import (
"math"
"github.com/Luzifer/twitch-bot/plugins"
)
func Register(args plugins.RegistrationArguments) error { 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 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 }

View File

@ -25,6 +25,19 @@ Examples below are using this syntax in the code block:
< Output from the template < Output from the template
``` ```
#### `add`
Returns float from calculation: `float1 + float2`
Syntax: `add <float1> <float2>`
Example:
```
# {{ printf "%.0f" (add 1 2) }}%
< 3
```
#### `arg` #### `arg`
Takes the message sent to the channel, splits by space and returns the Nth element Takes the message sent to the channel, splits by space and returns the Nth element
@ -104,6 +117,19 @@ Example:
< Luziferus - foobar < Luziferus - foobar
``` ```
#### `div`
Returns float from calculation: `float1 / float2`
Syntax: `div <float1> <float2>`
Example:
```
# {{ printf "%.0f" (div 27 9) }}%
< 3
```
#### `fixUsername` #### `fixUsername`
Ensures the username no longer contains the `@` or `#` prefix Ensures the username no longer contains the `@` or `#` prefix
@ -171,19 +197,32 @@ Example:
< Last Quote: #32 < Last Quote: #32
``` ```
#### `multiply` #### `mul` (deprecated: `multiply`)
Returns float from calculation: `float1 * float2` Returns float from calculation: `float1 * float2`
Syntax: `multiply <float1> <float2>` Syntax: `mul <float1> <float2>`
Example: Example:
``` ```
# {{ printf "%.0f" (multiply 100 (seededRandom "test")) }}% # {{ printf "%.0f" (mul 100 (seededRandom "test")) }}%
< 35% < 35%
``` ```
#### `pow`
Returns float from calculation: `float1 ** float2`
Syntax: `pow <float1> <float2>`
Example:
```
# {{ printf "%.0f" (pow 10 4) }}%
< 10000
```
#### `recentGame` #### `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. 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% < Your int this hour: 17%
``` ```
#### `sub`
Returns float from calculation: `float1 - float2`
Syntax: `sub <float1> <float2>`
Example:
```
# {{ printf "%.0f" (sub 10 4) }}%
< 6
```
#### `tag` #### `tag`
Takes the message sent to the channel, returns the value of the tag specified Takes the message sent to the channel, returns the value of the tag specified