summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--boha.raku57
-rw-r--r--lib/Boha/Karma.rakumod79
2 files changed, 90 insertions, 46 deletions
diff --git a/boha.raku b/boha.raku
index 20172c0..40ceb6b 100644
--- a/boha.raku
+++ b/boha.raku
@@ -2,65 +2,30 @@
use v6.d;
use Config::TOML;
use lib $?FILE.IO.sibling('lib').Str;
+use lib $?FILE.IO.parent(2).child('Red/lib').Str;
use Boha::TrackOps;
+use Boha::Karma;
use IRC::Client;
+use Red:api<2>;
my $config = from-toml(
file => (%*ENV<BOHA_CONFIG_FILE> // $?FILE.IO.sibling('boha.toml').Str)
);
-class TrackOps does IRC::Client::Plugin {
- has %!is-op;
+my $*RED-DEBUG=True;
- multi method is-op(
- Str() $server, Str() $channel, Str() $nick --> Bool
- ) {
- return %!is-op{$server}{$channel}{$nick}.so;
- }
- multi method is-op(IRC::Client::Message $e --> Bool) {
- fail 'not a channel message' unless $e.?channel;
- return self.is-op($e.server,$e.channel,$e.nick);
- }
-
- method !set-user-mode(
- Str() $server, Str() $channel, Str() $nick, Bool $mode
- ) {
- %!is-op{$server}{$channel}{$nick} = $mode;
- }
+red-defaults :boha['SQLite', :default];
- # response to /NAMES
- method irc-n353(IRC::Client::Message $e) {
- my $server = $e.server;
- my ($my-nick,$equal,$channel,$names) = $e.args();
+# if-not-exists?
+schema(Boha::KarmaEvent).create;
- for $names.split(/\s+/) -> $name-str {
- my $user = $name-str ~~ / ^ $<sigil> = [ '@' | '+' ]? $<nick> = [ .+ ] $ /;
+Boha::KarmaEvent.new-karma('dakkar',$_) for (1,-1,1,1);
+Boha::KarmaEvent.new-karma('boha',$_) for (-1,-1,-1,-1);
- self!set-user-mode(
- $server, $channel,
- $user<nick>,
- $user<sigil> eq '@'
- );
- }
+say Boha::KarmaEvent.current-karma();
- return $.NEXT;
- }
- method irc-mode-channel($e) {
- my ($server, $channel) = $e.server, $e.channel;
- my $new-mode = (
- $e.mode eq '+o' ?? True !!
- $e.mode eq '-o' ?? False !!
- return $.NEXT
- );
-
- for $e.nicks() -> $nick {
- self!set-user-mode($server,$channel,$nick,$new-mode);
- }
-
- return $.NEXT;
- }
-}
+=finish
class Boha1 does IRC::Client::Plugin {
has Boha::TrackOps $.ops handles <is-op>;
diff --git a/lib/Boha/Karma.rakumod b/lib/Boha/Karma.rakumod
new file mode 100644
index 0000000..2240743
--- /dev/null
+++ b/lib/Boha/Karma.rakumod
@@ -0,0 +1,79 @@
+use v6.d;
+use IRC::Client;
+use Red:api<2>;
+use Red::AST::Function;
+
+model Boha::KarmaEvent is table<karma_event> {
+ has Int $.id is serial;
+ has DateTime $.created is column .= now;
+ has Str $.nick is column;
+ has Int $.delta is column;
+
+ method current-karma-for(::?CLASS:U: Str $nick) {
+ #`{
+ this runs:
+
+ SELECT
+ SUM("karma_event".delta) as "data_1"
+ FROM
+ "karma_event"
+ WHERE
+ "karma_event".nick = ?
+
+ }
+ self.^all.grep({ .nick eq $nick }).map(
+ {Red::AST::Function.new(:func<SUM>, :args(.delta), :returns(Int)) }
+ );
+ }
+
+ method current-karma(::?CLASS:U: Int :$top) {
+ #`{
+ this runs:
+
+ SELECT
+ "karma_event".nick as "data_1", SUM("karma_event".delta) as "data_2"
+ FROM
+ "karma_event"
+ GROUP BY
+ "karma_event".nick
+ ORDER BY
+ data_2 DESC
+
+ }
+ my $rs = self.^all
+ .map(
+ {
+ .nick,
+ Red::AST::Function.new(:func<SUM>, :args(.delta), :returns(Int))
+ }
+ )
+ #`{
+ two notes:
+
+ * $_ is a class generated my the `map`, we want its second
+ column, so that's what we say (it's usually called `data_2`,
+ but we won't depend on that)
+
+ * the `sort` works by accident! only because
+ `translate(Red::AST::Mul $_ where .left.?value == -1,
+ "order")` does not prepend `.model.^as`
+
+ }
+ .sort({ -1 * .^columns[1].column})
+ ;
+ #`{
+ * there's no direct way to do a "group by"
+
+ * also, this will generate invalid SQL unless we swap the
+ `order by` and `group by` blocks in Red::Driver::CommonSQL
+
+ }
+ $rs.group = self.nick;
+ return $top ?? $rs.head($top) !! $rs;
+ }
+
+ method new-karma(::?CLASS:U: Str $nick, Int $delta) {
+ self.^create(:$nick,:$delta);
+ return;
+ }
+}