Initial checkin of a karma module. r=kerz b=111757

This commit is contained in:
ian%hixie.ch 2001-11-25 01:01:35 +00:00
parent 3d3259f0ed
commit 9e4bc32d9a

View File

@ -0,0 +1,180 @@
# -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
################################
# Karma Module #
################################
package BotModules::Karma;
use vars qw(@ISA);
@ISA = qw(BotModules);
1;
# RegisterConfig - Called when initialised, should call registerVariables
sub RegisterConfig {
my $self = shift;
$self->SUPER::RegisterConfig(@_);
$self->registerVariables(
# [ name, save?, settable? ]
['scores', 1, 1, {}], # nick => total karma.
['privateScores', 1, 1, {}], # nick => nick karma nick karma...
['secondsDelayRequired', 1, 1, 20],
['_lastspoken', 0, 0, {}], # nick => nick => time
);
}
sub Help {
my $self = shift;
my ($event) = @_;
return {
'' => 'A karma tracker. If you have authenticated (using the \'auth\' command) then it will also keep track of your own setting of people\'s karma, as well as the total of everyone\'s settings.',
'++' => 'Increase someone\'s karma. Syntax: victim++',
'--' => 'Decrease someone\'s karma. Syntax: victim++',
'?' => 'Find someone\'s current karma level. Syntax: victim karma?',
'rank' => 'Rank people by karma. Syntax: karma rank.',
};
}
sub Told {
my $self = shift;
my ($event, $message) = @_;
if ($message =~ /^(\S+)\+\+$/os) {
$self->ChangeKarma($event, $1, 1);
} elsif ($message =~ /^(\S+)\-\-$/os) {
$self->ChangeKarma($event, $1, -1);
} elsif ($message =~ /^(?:karma\s+)?(?:ranks?|list)[?]*$/os) {
$self->ReportKarmaRanks($event, $1);
} elsif ($message =~ /^(\S+)(?:\s+karma)?\?$/os) {
$self->ReportKarma($event, $1);
} else {
return $self->SUPER::Told(@_);
}
return 0; # dealt with it...
}
sub Heard {
my $self = shift;
my ($event, $message) = @_;
if ($message =~ /^(\S+)\+\+$/os) {
$self->ChangeKarma($event, $1, 1);
} elsif ($message =~ /^(\S+)\-\-$/os) {
$self->ChangeKarma($event, $1, -1);
} else {
return $self->SUPER::Told(@_);
}
return 0; # we've dealt with it, no need to do anything else.
}
sub ChangeKarma {
my $self = shift;
my ($event, $who, $delta) = @_;
$self->debug("$who += $delta requested");
if ((defined($self->{'_lastSpoken'}->{$event->{'user'}})) and
(defined($self->{'_lastSpoken'}->{$event->{'user'}}->{lc $who})) and
((time() - $self->{'_lastSpoken'}->{$event->{'user'}}->{lc $who}) <= $self->{'secondsDelayRequired'})) {
$self->{'_lastSpoken'}->{$event->{'user'}}->{lc $who} = $self->{'_lastSpoken'}->{$event->{'user'}}->{lc $who}+5;
my $delay = $self->{'secondsDelayRequired'} - (time() - $self->{'_lastSpoken'}->{$event->{'user'}}->{lc $who});
$self->directSay($event, "You will have to wait another $delay seconds before being able to change $who's karma.");
} else {
if (not defined($self->{'_lastSpoken'}->{$event->{'user'}})) {
$self->{'_lastSpoken'}->{$event->{'user'}} = {};
}
$self->{'_lastSpoken'}->{$event->{'user'}}->{lc $who} = time();
if (lc $event->{'from'} eq lc $who) {
if ($delta > 0) {
$delta = -$delta;
}
}
if ($event->{'channel'} ne '') {
$self->{'scores'}->{lc $who} += $delta;
}
$nick = lc $event->{'userName'};
if ($nick) {
if (not defined($self->{"privateScores"}->{$nick})) {
$self->{"privateScores"}->{$nick} = (lc($who) . ' ' . $delta);
} else {
my %privateScores = split(' ', $self->{"privateScores"}->{$nick});
$privateScores{lc $who} += $delta;
my @privateScores = %privateScores;
local $" = ' ';
$self->{'privateScores'}->{$nick} .= "@privateScores";
}
} elsif ($event->{'channel'} eq '') {
$self->say($event, 'For private stats, you need to authenticate. Use the \'newuser\' and \'auth\' commands.');
}
$self->saveConfig();
}
}
sub ReportKarma {
my $self = shift;
my ($event, $who) = @_;
if (not defined($self->{'scores'}->{lc $who})) {
$self->say($event, "$who has no karma.");
} else {
my $karma = $self->{'scores'}->{lc $who};
$self->say($event, "$who has $karma points of karma.");
}
if ($event->{'channel'} eq '') {
$nick = lc $event->{'userName'};
if ($nick) {
if (not defined($self->{"privateScores"}->{$nick})) {
$self->say($event, "You have not given anyone any karma.");
} else {
my %privateScores = split(' ', $self->{"privateScores"}->{$nick});
my $karma = $privateScores{lc $who};
if (defined($karma)) {
$self->say($event, "You have not given $who any karma.");
} else {
$self->say($event, "You have given $who $karma points of karma.");
}
}
} else {
$self->say($event, 'For private stats, you need to authenticate. Use the \'newuser\' and \'auth\' commands.');
}
}
}
sub ReportKarmaRanks {
my $self = shift;
my ($event) = @_;
my @order = sort { $self->{'scores'}->{$b} <=> $self->{'scores'}->{$a} } keys(%{$self->{'scores'}});
if (scalar(@order)) {
if ($event->{'channel'} ne '') {
my $top = $order[0];
my $score = $self->{'scores'}->{$top};
$self->say($event, "The person with the most karma is $top with $score points.");
}
$self->directSay($event, "Global rankings:");
$self->ReportKarmaRanksList($event, \@order, $self->{'scores'});
}
if ($event->{'channel'} eq '') {
$nick = lc $event->{'userName'};
if ($nick) {
if (defined($self->{"privateScores"}->{$nick})) {
my %privateScores = split(' ', $self->{"privateScores"}->{$nick});
@order = sort { $privateScores{$b} <=> $privateScores{$a} } keys(%privateScores);
if (scalar(@order)) {
$self->directSay($event, "Personal rankings:");
$self->ReportKarmaRanksList($event, \@order, \%privateScores);
} else {
$self->say($event, "I seem to have lost track of the people to which you gave karma points.");
}
} else {
$self->say($event, "You have not given anyone karma.");
}
} else {
$self->say($event, 'For private stats, you need to authenticate. Use the \'newuser\' and \'auth\' commands.');
}
}
}
sub ReportKarmaRanksList {
my $self = shift;
my($event, $order, $scores) = @_;
my $rank = 1;
foreach my $entry (@$order) {
my $score = $scores->{$entry};
$self->directSay($event, "$rank. $entry ($score)");
$rank++;
}
}