| 1 | package Bot::Backbone::Service::OFun::Karma; | ||
| 2 | $Bot::Backbone::Service::OFun::Karma::VERSION = '0.142230'; | ||
| 3 | use Bot::Backbone::Service; | ||
| 4 | |||
| 5 | with qw( | ||
| 6 | Bot::Backbone::Service::Role::Service | ||
| 7 | Bot::Backbone::Service::Role::Responder | ||
| 8 | Bot::Backbone::Service::Role::Storage | ||
| 9 | ); | ||
| 10 | |||
| 11 | # ABSTRACT: Keep track of your channel's favorite things | ||
| 12 | |||
| 13 | |||
| 14 | service_dispatcher as { | ||
| 15 | command '!score' => given_parameters { | ||
| 16 | parameter 'thing' => ( match_original => qr/.+/ ); | ||
| 17 | } respond_by_method 'score_of_thing'; | ||
| 18 | |||
| 19 | command '!best' => respond_by_method 'best_scores'; | ||
| 20 | command '!score' => respond_by_method 'best_scores'; | ||
| 21 | command '!worst' => respond_by_method 'worst_scores'; | ||
| 22 | |||
| 23 | command '!score_alias' => given_parameters { | ||
| 24 | parameter 'this' => ( match => qr/.+/ ); | ||
| 25 | parameter 'that' => ( match => qr/.+/ ); | ||
| 26 | } respond_by_method 'alias_this_to_that'; | ||
| 27 | command '!score_alias' => given_parameters { | ||
| 28 | parameter 'this' => ( match => qr/.+/ ); | ||
| 29 | } respond_by_method 'show_alias_of_this'; | ||
| 30 | command '!score_unalias' => given_parameters { | ||
| 31 | parameter 'this' => ( match => qr/.+/ ); | ||
| 32 | } respond_by_method 'unalias_this'; | ||
| 33 | |||
| 34 | also not_command spoken run_this_method 'update_scores'; | ||
| 35 | }; | ||
| 36 | |||
| 37 | |||
| 38 | sub load_schema { | ||
| 39 | my ($self, $conn) = @_; | ||
| 40 | |||
| 41 | $conn->run(fixup => sub { | ||
| 42 | $_->do(q[ | ||
| 43 | CREATE TABLE IF NOT EXISTS karma_score( | ||
| 44 | name varchar(255), | ||
| 45 | score int, | ||
| 46 | PRIMARY KEY (name) | ||
| 47 | ) | ||
| 48 | ]); | ||
| 49 | |||
| 50 | $_->do(q[ | ||
| 51 | CREATE TABLE IF NOT EXISTS karma_alias( | ||
| 52 | name varchar(255), | ||
| 53 | score_as varchar(255), | ||
| 54 | PRIMARY KEY (name) | ||
| 55 | ); | ||
| 56 | ]); | ||
| 57 | }); | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub ok_name { | ||
| 62 | my ($self, $name) = @_; | ||
| 63 | |||
| 64 | # No empty string votes | ||
| 65 | return unless $name; | ||
| 66 | |||
| 67 | # No file permissions | ||
| 68 | return if $name =~ /^[d-][r-][w-][x-][r-][w-][sx-][r-][w-]?[tx-]?/; | ||
| 69 | |||
| 70 | # And there should be at least a couple word chars | ||
| 71 | return unless $name =~ /\w.*?\w/; | ||
| 72 | |||
| 73 | # OK! | ||
| 74 | return 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub update_scores { | ||
| 79 | my ($self, $message) = @_; | ||
| 80 | |||
| 81 | my @args = $message->all_args; | ||
| 82 | THING: for my $i (0 .. $#args) { | ||
| 83 | my $arg = $args[$i]; | ||
| 84 | my $name = $arg->text; | ||
| 85 | |||
| 86 | # word by itself, join it to the previous maybe? | ||
| 87 | if ($name eq '++' or $name eq '--') { | ||
| 88 | |||
| 89 | # Can't be postfix ++/-- if it's the first thing | ||
| 90 | next THING unless $i > 0; | ||
| 91 | |||
| 92 | # Ignore if there's space between ++/-- and the previous thing | ||
| 93 | next THING unless $args[$i-1]->original =~ /\S$/; | ||
| 94 | |||
| 95 | # Looks legit, join the last word to this for the vote | ||
| 96 | $name = $args[$i-1]->text . $name; | ||
| 97 | } | ||
| 98 | |||
| 99 | if ($name =~ s/(\+\+|--)$//) { | ||
| 100 | my $direction = $1 eq '++' ? +1 : -1; | ||
| 101 | |||
| 102 | next THING unless $self->ok_name($name); | ||
| 103 | |||
| 104 | $self->db_conn->txn(fixup => sub { | ||
| 105 | $_->do(q[ | ||
| 106 | INSERT OR IGNORE INTO karma_score(name, score) | ||
| 107 | VALUES (?, ?) | ||
| 108 | ], undef, $name, 0); | ||
| 109 | |||
| 110 | $_->do(q[ | ||
| 111 | UPDATE karma_score | ||
| 112 | SET score = score + ? | ||
| 113 | WHERE name = ? | ||
| 114 | ], undef, $direction, $name); | ||
| 115 | }); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | sub score_of_thing { | ||
| 122 | my ($self, $message) = @_; | ||
| 123 | |||
| 124 | my $thing = $message->parameters->{thing}; | ||
| 125 | |||
| 126 | my ($score) = $self->db_conn->txn(fixup => sub { | ||
| 127 | my ($score_as) = $_->selectrow_array(q[ | ||
| 128 | SELECT score_as | ||
| 129 | FROM karma_alias | ||
| 130 | WHERE name = ? | ||
| 131 | ], undef, $thing); | ||
| 132 | |||
| 133 | $thing = $score_as if defined $score_as; | ||
| 134 | my $sth = $_->prepare(q[ | ||
| 135 | SELECT ks.score + COALESCE(SUM(kas.score), 0) | ||
| 136 | FROM karma_score ks | ||
| 137 | LEFT JOIN karma_alias ka ON ks.name = ka.score_as | ||
| 138 | LEFT JOIN karma_score kas ON ka.name = kas.name | ||
| 139 | WHERE ks.name = ? | ||
| 140 | ]); | ||
| 141 | |||
| 142 | $sth->execute($thing); | ||
| 143 | |||
| 144 | $sth->fetchrow_array; | ||
| 145 | }); | ||
| 146 | |||
| 147 | $score //= 0; | ||
| 148 | |||
| 149 | return "$thing: $score"; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub show_alias_of_this { | ||
| 154 | my ($self, $message) = @_; | ||
| 155 | |||
| 156 | my $this = $message->parameters->{this}; | ||
| 157 | |||
| 158 | my $aliases = $self->db_conn->run(fixup => sub { | ||
| 159 | $_->selectall_arrayref(q[ | ||
| 160 | SELECT name, score_as | ||
| 161 | FROM karma_alias | ||
| 162 | WHERE name = ? OR score_as = ? | ||
| 163 | ], undef, $this, $this); | ||
| 164 | }); | ||
| 165 | |||
| 166 | return qq[Nothing aliases to or from "$this".] unless @$aliases; | ||
| 167 | |||
| 168 | my ($scored_as, @included_scores); | ||
| 169 | for my $alias (@$aliases) { | ||
| 170 | my ($name, $score_as) = @$alias; | ||
| 171 | if ($name eq $this) { | ||
| 172 | $scored_as = $score_as; | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | push @included_scores, qq["$name"]; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | my @messages; | ||
| 180 | push @messages, qq[Warning: "$this" has aliases to and from for scoring, which is not supposed to happen.] | ||
| 181 | if $scored_as and @included_scores; | ||
| 182 | |||
| 183 | push @messages, qq[Scores for "$this" are counted for "$scored_as" instead.] | ||
| 184 | if $scored_as; | ||
| 185 | |||
| 186 | my $comma = ''; | ||
| 187 | if (@included_scores == 2) { | ||
| 188 | $comma = ' and '; | ||
| 189 | } | ||
| 190 | elsif (@included_scores > 2) { | ||
| 191 | $comma = ', '; | ||
| 192 | $included_scores[-1] = 'and ' . $included_scores[-1]; | ||
| 193 | } | ||
| 194 | |||
| 195 | push @messages, qq[Scores for "$this" also include ].join($comma, @included_scores)."." | ||
| 196 | if @included_scores; | ||
| 197 | |||
| 198 | return @messages; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | sub alias_this_to_that { | ||
| 203 | my ($self, $message) = @_; | ||
| 204 | |||
| 205 | my $this = $message->parameters->{this}; | ||
| 206 | my $that = $message->parameters->{that}; | ||
| 207 | |||
| 208 | return "Those are both the same thing." if $this eq $that; | ||
| 209 | |||
| 210 | for ($this, $that) { | ||
| 211 | return qq[Sorry, but "$_" cannot be scored.] unless $self->ok_name($_); | ||
| 212 | } | ||
| 213 | |||
| 214 | $self->db_conn->txn(fixup => sub { | ||
| 215 | my $dbh = $_; | ||
| 216 | |||
| 217 | $dbh->do(q[ | ||
| 218 | DELETE FROM karma_alias | ||
| 219 | WHERE name = ? OR score_as = ? OR name = ? | ||
| 220 | ], undef, $this, $this, $that); | ||
| 221 | |||
| 222 | # Make sure the name exists for JOINing too | ||
| 223 | $dbh->do(q[ | ||
| 224 | INSERT OR IGNORE INTO karma_score(name, score) | ||
| 225 | VALUES (?, ?) | ||
| 226 | ], undef, $_, 0) for ($this, $that); | ||
| 227 | |||
| 228 | $dbh->do(q[ | ||
| 229 | INSERT INTO karma_alias(name, score_as) | ||
| 230 | VALUES (?, ?) | ||
| 231 | ], undef, $this, $that); | ||
| 232 | }); | ||
| 233 | |||
| 234 | return qq[Scores for "$this" will count for "$that" instead.]; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | sub unalias_this { | ||
| 239 | my ($self, $message) = @_; | ||
| 240 | |||
| 241 | my $this = $message->parameters->{this}; | ||
| 242 | |||
| 243 | $self->db_conn->run(fixup => sub { | ||
| 244 | $_->do(q[ | ||
| 245 | DELETE FROM karma_alias | ||
| 246 | WHERE name = ? | ||
| 247 | ], undef, $this); | ||
| 248 | }); | ||
| 249 | |||
| 250 | return qq[Scores for "$this" will count for "$this" now.]; | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | sub best_scores { | ||
| 255 | my ($self, $message) = @_; | ||
| 256 | return $self->_n_scores(best => 10); | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | sub worst_scores { | ||
| 261 | my ($self, $message) = @_; | ||
| 262 | return $self->_n_scores(worst => 10); | ||
| 263 | } | ||
| 264 | |||
| 265 | sub _n_scores { | ||
| 266 | my ($self, $which, $n) = @_; | ||
| 267 | |||
| 268 | my $direction = $which eq 'best' ? 'DESC' : 'ASC'; | ||
| 269 | my ($scores) = $self->db_conn->run(fixup => sub { | ||
| 270 | $_->selectall_arrayref(qq[ | ||
| 271 | SELECT ks.name, ks.score + COALESCE(SUM(kas.score), 0) | ||
| 272 | FROM karma_score ks | ||
| 273 | LEFT JOIN karma_alias kb ON ks.name = kb.name | ||
| 274 | LEFT JOIN karma_alias ka ON ks.name = ka.score_as | ||
| 275 | LEFT JOIN karma_score kas ON ka.name = kas.name | ||
| 276 | WHERE kb.name IS NULL | ||
| 277 | GROUP BY ks.name | ||
| 278 | HAVING ks.score + COALESCE(SUM(kas.score), 0) != 0 | ||
| 279 | ORDER BY SUM(ks.score) $direction | ||
| 280 | LIMIT $n | ||
| 281 | ]); | ||
| 282 | }); | ||
| 283 | |||
| 284 | return "No scores." unless @$scores; | ||
| 285 | |||
| 286 | return map { "$_->[0]: $_->[1]" } @$scores; | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | sub initialize { } | ||
| 291 | |||
| 292 | __PACKAGE__->meta->make_immutable; | ||
| 293 | |||
| 294 | __END__ |