App-RoboBot
view release on metacpan or search on metacpan
lib/App/RoboBot/Plugin/Social/Karma.pm view on Meta::CPAN
'++karma' => { method => 'add_karma',
description => "Explicitly adds to the given nick's karma rating.",
usage => '<nick>' },
'--karma' => { method => 'subtract_karma',
description => "Explicitly subtracts from the given nick's karma rating.",
usage => '<nick>' },
'karma-leaders' => { method => 'karma_leaders',
description => 'Displays the nicks on your current network with the highest current karma.', },
'karma-losers' => { method => 'karma_losers',
description => 'Displays the nicks on your current network with the lowest current karma.', },
}},
);
has 'nf' => (
is => 'ro',
isa => 'Number::Format',
default => sub { Number::Format->new() }
);
sub add_karma {
my ($self, $message, $command, $rpl, $nick) = @_;
$nick = App::RoboBot::Nick->new( config => $self->bot->config, name => "$nick" );
# Users can self-karma--, but they can't add karma to their own nick.
if (defined $nick && $nick->id != $message->sender->id) {
my $res = $self->bot->config->db->do(q{
insert into karma_karma ???
}, {
nick_id => $nick->id,
karma => 1,
from_nick_id => $message->sender->id,
});
}
return;
}
sub subtract_karma {
my ($self, $message, $command, $rpl, $nick) = @_;
$nick = App::RoboBot::Nick->new( config => $self->bot->config, name => "$nick" );
if (defined $nick) {
my $res = $self->bot->config->db->do(q{
insert into karma_karma ???
}, {
nick_id => $nick->id,
karma => -1,
from_nick_id => $message->sender->id,
});
}
return;
}
sub update_karma {
my ($self, $message) = @_;
my %nicks = ($message->raw =~ m{([A-Za-z0-9_]+)([+-]{2})}ogs);
return unless scalar(keys %nicks) > 0;
foreach my $nick (keys %nicks) {
my $karma_amount = $nicks{$nick} eq '++' ? 1 : -1;
my $res = $self->bot->config->db->do(q{
select id
from nicks
where lower(name) = lower(?)
}, $nick);
if ($res && $res->next) {
next if $karma_amount > 0 && $res->{'id'} == $message->sender->id;
my $nick_id = $res->{'id'};
$self->bot->config->db->do(q{
insert into karma_karma ???
}, {
nick_id => $nick_id,
karma => $karma_amount,
from_nick_id => $message->sender->id,
});
}
}
}
sub display_karma {
my ($self, $message, $command, $rpl, @nicks) = @_;
if (!@nicks || @nicks < 1) {
@nicks = ($message->sender->name);
}
foreach my $nick (@nicks) {
my $res = $self->bot->config->db->do(q{
select id, name
from nicks
where lower(name) = lower(?)
}, $nick);
next unless $res && $res->next;
my $nick_id = $res->{'id'};
my $nick_name = $res->{'name'};
$res = $self->bot->config->db->do(q{
with t as (select count(*) as nicks from nicks)
select n.name,
coalesce(sum(k.karma)::real * (count(distinct(k.from_nick_id))::real / t.nicks), 0) * 100 as karma
from karma_karma k
join nicks n on (n.id = k.nick_id),
t
where n.id = ?
group by n.name, t.nicks
}, $nick_id);
( run in 0.718 second using v1.01-cache-2.11-cpan-39bf76dae61 )