blib/lib/Bot/BasicBot/Pluggable/Module/Karma.pm

filter: Statementstmt 97.2% 106 / 109 Branchbran 87.5% 49 / 56 Conditioncond 61.1% 11 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 14 / 14 Podpod 55.5% 5 / 9 totaltotal 87.1% 189 / 217 SCAR 36.4
CC
37
Cov
91%
told
22.4
get_karma
7.0
add_karma
4.0
CRAP 38.1
« blib/lib/Bot/BasicBot/Pluggable/Module/Loader.pm ↑ Summary lib/Bot/BasicBot/Pluggable/Store.pm »
1 package Bot::BasicBot::Pluggable::Module::Karma;
2 3 $Bot::BasicBot::Pluggable::Module::Karma::VERSION = '1.20';
3 3 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub init {
8 3 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
9 3 $self->config(
10 {
11 user_ignore_selfkarma => 1,
12 user_num_comments => 3,
13 user_show_givers => 1,
14 user_randomize_reasons => 1,
15 user_karma_change_response => 1,
16 }
17 );
18 }
19  
20 sub help {
21 return
22 1 ▶"Gives karma for or against a particular thing. Usage: <thing>++ # comment, <thing>-- # comment, karma <thing>, explain <thing>.";
Subroutine help: called CC=1 SCAR=0.0
Pod documented
23 }
24  
25 sub told {
26 136 ▶ my ( $self, $mess ) = @_;
Subroutine told: called CC=19 SCAR=31.1
Pod documented
27 136 my $body = $mess->{body};
28 136 ▶ return 0 unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 136 136
29  
30 # If someone is trying to change the bot's karma, we'll have our bot nick in
31 # {addressed}, and '++' or '-' in the body ('-' rather than '--' because
32 # Bot::BasicBot removes one of the dashes as it considers it part of the
33 # address)
34 136 ▶ if ( $mess->{address} && ($body eq '++' or $body eq '-') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->{"address"} and $body eq "++" || $body eq "-") 0 136 136
Condition16%1/6
$mess->{"address"} and $body eq "++" || $body eq "-"
!ll&&!rl&&r
13600
$body eq "++" or $body eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/3
$mess->{"address"} and $body eq "++" || $body eq "-"
$mess->{"address"} $body eq "++" $body eq "-"
Truth table25%1/4
A and B or C
A= $mess->{"address"}B= $body eq "++"C= $body eq "-"
ABCresult
0XX0
1000
11X1
1011
35 0 ▶ $body = '--' if $body eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $body eq "-" 0 0 0
36 0 $body = $mess->{address} . $body;
37 }
38  
39 136 my $op_re = qr{ ( \-\- | \+\+ ) }x;
40 136 my $comment_re = qr{ (?: \s* \# \s* (.+) )? }x;
41 136 for my $regex (
42 qr{^ (\S+) $op_re $comment_re }x, # singleword++
43 qr{^ \( (.+) \) $op_re $comment_re }x # (more words)++
44 ) {
45 258 ▶ if (my($thing, $op, $comment) = $body =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $body =~ /$regex/x) 16 242 258
46 16 ▶ my $add = $op eq '++' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '++' ? : 8 8 16
47 16 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq $mess->{"who"} and $self->get("user_ignore_selfkarma")) 2 14 16
Condition100%3/3
$1 eq $mess->{"who"} and $self->get("user_ignore_selfkarma")
!ll&&!rl&&r
1222
MC/DC100%2/2
$1 eq $mess->{"who"} and $self->get("user_ignore_selfkarma")
$1 eq $mess->{"who"} $self->get("user_ignore_selfkarma")
Truth table100%3/3
A and B
A= $1 eq $mess->{"who"}B= $self->get("user_ignore_selfkarma")
ABresult
0X0
100
111
48 ( $1 eq $mess->{who} ) and $self->get("user_ignore_selfkarma")
49 ){
50 2 return;
51 }
52 14 my $reply = $self->add_karma( $thing, $add, $comment, $mess->{who} );
53 14 ▶ if (lc $thing eq lc $self->bot->nick) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $thing eq lc $self->bot->nick) 2 12 14
54 2 ▶ $reply .= ' ' . ($add ? '(thanks!)' : '(pffft)');
Branch: 1 branch
BranchTrueFalseTotal
$add ? : 1 1 2
55 }
56 14 return $reply;
57 }
58 }
59  
60 # OK, handle "karma" / "explain" commands
61 120 my ( $command, $param ) = split( /\s+/, $body, 2 );
62 120 $command = lc($command);
63  
64 120 ▶ if ( $command eq "karma" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq 'karma') { } 8 112 120
elsif ($command eq 'explain' and $param) { } 107 9 116
65 8 $param =~ s/\?+$//; # handle interrogatives - lop off trailing question marks
66 8 ▶ if ($param && $param eq 'chameleon') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param and $param eq "chameleon") 0 8 8
Condition66%2/3
$param and $param eq "chameleon"
!ll&&!rl&&r
170
MC/DC0%0/2
$param and $param eq "chameleon"
$param $param eq "chameleon"
Truth table66%2/3
A and B
A= $paramB= $param eq "chameleon"
ABresult
0X0
100
111
67 0 return "Karma karma karma karma karma chameleon, "
68 . "you come and go, you come and go...";
69 }
70 8 ▶ $param ||= $mess->{who};
Condition66%2/3
$param ||= $mess->{"who"}
l!l&&r!l&&!r
710
MC/DC0%0/2
$param ||= $mess->{"who"}
$param $mess->{"who"}
Truth table66%2/3
A ||= B
A= $paramB= $mess->{"who"}
ABresult
1X1
011
000
71 8 return "$param has karma of " . $self->get_karma($param) . ".";
72
73 ▶ } elsif ( $command eq "explain" and $param ) {
Condition100%3/3
$command eq "explain" and $param
!ll&&!rl&&r
41107
MC/DC100%2/2
$command eq "explain" and $param
$command eq "explain" $param
Truth table100%3/3
A and B
A= $command eq "explain"B= $param
ABresult
0X0
100
111
74 107 $param =~ s/^karma\s+//i;
75 107 my ( $karma, $good, $bad ) = $self->get_karma($param);
76 107 my $reply = "positive: " . $self->format_reasons($good) . "; ";
77 107 $reply .= "negative: " . $self->format_reasons($bad) . "; ";
78 107 $reply .= "overall: $karma.";
79  
80 107 return $reply;
81 }
82 }
83  
84  
85 sub format_reasons {
86 214 ▶ my ( $self, $reason ) = @_;
Subroutine format_reasons: called CC=4 SCAR=13.9
Pod undocumented
87 214 my $num_comments = $self->get('user_num_comments');
88  
89 214 ▶ if ( $num_comments == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_comments == 0) 4 210 214
90 4 return scalar( $reason->() );
91 }
92  
93 210 my @reasons = $reason->();
94 210 my $num_reasons = @reasons;
95  
96 210 ▶ if ( $num_reasons == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_reasons == 0) 1 209 210
97 1 return 'nothing';
98 }
99  
100 209 ▶ if ( $num_reasons == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_reasons == 1) 106 103 209
101 106 return ( $self->maybe_add_giver(@reasons) )[0];
102 }
103  
104 103 $self->trim_list( \@reasons, $num_comments );
105 103 return join( ', ', $self->maybe_add_giver(@reasons) );
106 }
107  
108 sub maybe_add_giver {
109 209 ▶ my ( $self, @reasons ) = @_;
Subroutine maybe_add_giver: called CC=2 SCAR=6.9
Pod undocumented
110 209 ▶ if ( $self->get('user_show_givers') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get('user_show_givers')) { } 202 7 209
111  
112 # adding a (user) string to the all reasons
113 202 return map { $_->{reason} . ' (' . $_->{who} . ')' } @reasons;
114 }
115 else {
116  
117 # just returning the reason string of the reason hash referenes
118 7 return map { $_->{reason} } @reasons;
119 }
120 }
121  
122 sub get_karma {
123 133 ▶ my ( $self, $thing ) = @_;
Subroutine get_karma: called CC=7 SCAR=19.5
Pod documented
124 133 $thing = lc($thing);
125 133 $thing =~ s/-/ /g;
126  
127 133 ▶ my @changes = grep { ref } @{ $self->get("karma_$thing") || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->get("karma_$thing") or [] 4 129 133
128  
129 133 my ( @good, @bad );
130 133 my $karma = 0;
131 133 my $positive = 0;
132 133 my $negative = 0;
133  
134 133 for my $row (@changes) {
135  
136 # just push non empty reasons on the array
137 903 my $reason = $row->{reason};
138 903 ▶ if ( $row->{positive} ) { $positive++; push( @good, +{ %$row } ) if $reason }
Branch: 2 branches
BranchTrueFalseTotal
if ($row->{'positive'}) { } 462 441 903
if $reason 109 353 462
139 441 ▶ else { $negative++; push( @bad, +{ %$row } ) if $reason }
Branch: 1 branch
BranchTrueFalseTotal
if $reason 315 126 441
140 }
141 133 $karma = $positive - $negative;
142  
143 # The subroutine references return differant values when called.
144 # If they are called in scalar context, they return the overall
145 # positive or negative karma, but when called in list context you
146 # get an array of hash references with all non empty reasons back.
147  
148 return wantarray()
149 ? (
150 $karma,
151 107 ▶ sub { return wantarray ? @good : $positive },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 105 2 107
152 107 ▶ sub { return wantarray ? @bad : $negative }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 105 2 107
153 )
154 133 ▶ : $karma;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 107 26 133
155 }
156  
157 sub add_karma {
158 14 ▶ my ( $self, $thing, $good, $reason, $who ) = @_;
Subroutine add_karma: called CC=4 SCAR=13.9
Pod documented
159 14 $thing = lc($thing);
160 14 $thing =~ s/-/ /g;
161 14 my $row =
162 { reason => $reason, who => $who, timestamp => time, positive => $good };
163 14 ▶ my @changes = map { +{ %$_ } } grep { ref } @{ $self->get("karma_$thing") || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->get("karma_$thing") or [] 4 10 14
164 14 push @changes, $row;
165 14 $self->set( "karma_$thing" => \@changes );
166 14 my $respond = $self->get('user_karma_change_response');
167 14 ▶ $respond = 1 if !defined $respond;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $respond 0 14 14
168 14 ▶ return $respond ?
Branch: 1 branch
BranchTrueFalseTotal
$respond ? : 14 0 14
169 "Karma for $thing is now " . scalar $self->get_karma($thing) : 1;
170 }
171  
172 sub trim_list {
173 103 ▶ my ( $self, $list, $count ) = @_;
Subroutine trim_list: called CC=3 SCAR=11.0
Pod undocumented
174  
175 # If randomization isn't requested we just return the reasons
176 # in reversed chronological order
177  
178 103 ▶ if ( $self->get('user_randomize_reasons') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get('user_randomize_reasons')) { } 100 3 103
179 100 fisher_yates_shuffle($list);
180 }
181 else {
182 3 @$list = reverse sort { $b->{timestamp} cmp $a->{timestamp} } @$list;
183 }
184  
185 103 ▶ if ( scalar(@$list) > $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$list > $count) 102 1 103
186 102 @$list = splice( @$list, 0, $count );
187 }
188 }
189  
190 sub fisher_yates_shuffle {
191 100 ▶ my $array = shift;
Subroutine fisher_yates_shuffle: called CC=2 SCAR=6.9
Pod undocumented
192 100 my $i = @$array;
193 100 ▶ while ( $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 300 100 400
194 300 my $j = int rand( $i + 1 );
195 300 @$array[ $i, $j ] = @$array[ $j, $i ];
196 }
197 }
198  
199 3 1;
200  
201 __END__
202  
203 =head1 NAME
204  
205 Bot::BasicBot::Pluggable::Module::Karma - tracks karma for various concepts
206  
207 =head1 VERSION
208  
209 version 1.20
210  
211 =head1 IRC USAGE
212  
213 =over 4
214  
215 =item <thing>++ # <comment>
216  
217 Increases the karma for <thing>.
218  
219 Responds with the new karma for <thing> unless C<karma_change_response> is set
220 to a false value.
221  
222 =item <thing>-- # <comment>
223  
224 Decreases the karma for <thing>.
225  
226 Responds with the new karma for <thing> unless C<karma_change_response> is set
227 to a false value.
228  
229 =item karma <thing>
230  
231 Replies with the karma rating for <thing>.
232  
233 =item explain <thing>
234  
235 Lists three each good and bad things said about <thing>:
236  
237 <user> explain Morbus
238 <bot> positive: committing lots of bot documentation; fixing the
239 fisher_yates; negative: filling the dev list. overall: 5
240  
241 =back
242  
243 =head1 METHODS
244  
245 =over 4
246  
247 =item get_karma($username)
248  
249 Returns either a string representing the total number of karma
250 points for the passed C<$username> or the total number of karma
251 points and subroutine reference for good and bad karma comments.
252 These references return the according karma levels when called in
253 scalar context or a array of hash reference. Every hash reference
254 has entries for the timestamp (timestamp), the giver (who) and the
255 explanation string (reason) for its karma action.
256  
257 =item add_karma($thing, $good, $reason, $who)
258  
259 Adds or subtracts from the passed C<$thing>'s karma. C<$good> is either 1 (to
260 add a karma point to the C<$thing> or 0 (to subtract). C<$reason> is an
261 optional string commenting on the reason for the change, and C<$who> is the
262 person modifying the karma of C<$thing>. Nothing is returned.
263  
264 =back
265  
266 =head1 VARS
267  
268 =over 4
269  
270 =item ignore_selfkarma
271  
272 Defaults to 1; determines whether to respect selfkarmaing or not.
273  
274 =item num_comments
275  
276 Defaults to 3; number of good and bad comments to display on
277 explanations. Set this variable to 0 if you do not want to list
278 reasons at all.
279  
280 =item show_givers
281  
282 Defaults to 1; whether to show who gave good or bad comments on
283 explanations.
284  
285 =item randomize_reasons
286  
287 Defaults to 1; whether to randomize the order of reasons. If set
288 to 0, the reasons are sorted in reversed chronological order.
289  
290 =item karma_change_response
291  
292 Defaults to 1; whether to show a response when the karma of a
293 thing is changed. If true, the bot will reply with the new karma.
294 If set to 0, the bot will silently update the karma, without
295 a response.
296  
297 =back
298  
299 =head1 AUTHOR
300  
301 Mario Domgoergen <mdom@cpan.org>
302  
303 This program is free software; you can redistribute it
304 and/or modify it under the same terms as Perl itself.
« blib/lib/Bot/BasicBot/Pluggable/Module/Loader.pm ↑ Summary lib/Bot/BasicBot/Pluggable/Store.pm »