aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-06-03 19:53:35 +0200
committerPatrick Spek <p.spek@tyil.nl>2020-06-03 19:53:35 +0200
commit6a4450a45e2604ebbeabc2d7fb83259d27882184 (patch)
treeccda494a45e5d9a0d33447633dd09e5a6f7507f9
parent6b77c775a60b94fe4ecb2b5a8d9cd64ec57f5782 (diff)
downloadIRC::Client::Plugin::DiceRolls-6a4450a45e2604ebbeabc2d7fb83259d27882184.tar.gz
IRC::Client::Plugin::DiceRolls-6a4450a45e2604ebbeabc2d7fb83259d27882184.tar.bz2
Implement ListActions from Grammar::DiceRolls
-rw-r--r--lib/IRC/Client/Plugin/DiceRolls.rakumod46
1 files changed, 37 insertions, 9 deletions
diff --git a/lib/IRC/Client/Plugin/DiceRolls.rakumod b/lib/IRC/Client/Plugin/DiceRolls.rakumod
index 799008d..cae0b23 100644
--- a/lib/IRC/Client/Plugin/DiceRolls.rakumod
+++ b/lib/IRC/Client/Plugin/DiceRolls.rakumod
@@ -5,6 +5,7 @@ use v6.d;
use Config;
use Grammar::DiceRolls;
use Grammar::DiceRolls::CountActions;
+use Grammar::DiceRolls::ListActions;
use IRC::Client;
unit class IRC::Client::Plugin::DiceRolls does IRC::Client::Plugin;
@@ -23,18 +24,45 @@ submethod TWEAK
;
}
-multi method irc-privmsg (
- $event where { $event.text.starts-with($!prefix) }
+method reload-config (
+ Config $config,
) {
- my $roll = $event.text.substr($!prefix.chars).trim;
- my $outcome = Grammar::DiceRolls.parse(
- $roll,
- :actions(Grammar::DiceRolls::CountActions)
- ).made;
+ $!config = $config;
- return "Invalid roll ($roll)" unless $outcome;
+ self.TWEAK;
+}
- "$roll = $outcome";
+multi method irc-privmsg (
+ $event where { $event.text.starts-with($!prefix) }
+) {
+ my $roll = $event.text.substr($!prefix.chars).trim || '1d20';
+
+ given ($!config.get('irc.plugins.dicerolls.style', 'list').fc) {
+ when 'list' {
+ my @outcomes = Grammar::DiceRolls.parse(
+ $roll,
+ :actions(Grammar::DiceRolls::ListActions)
+ ).made;
+
+ return "Invalid roll ($roll)" unless @outcomes;
+ return "$roll = {@outcomes.first}" if @outcomes.elems < 2;
+
+ "$roll = {@outcomes.join(' + ')} = {@outcomes.sum}";
+ }
+ when 'count' {
+ my $outcome = Grammar::DiceRolls.parse(
+ $roll,
+ :actions(Grammar::DiceRolls::CountActions)
+ ).made;
+
+ return "Invalid roll ($roll)" unless $outcome;
+
+ "$roll = $outcome";
+ }
+ default {
+ .alert("Invalid rolling style for irc.plugins.dicerolls.style ($_)") with $*LOG;
+ }
+ }
}
=begin pod