
=head1 NAME

Bot::BasicBot::Pluggable::Module::GoodBad

=head1 SYNOPSIS

Tracks Good/Bad for people

=head1 IRC USAGE

Commands:

=over 4

=item <thing>++ # <comment>

Increases the kerma for <thing>

=item <thing>-- # <comment>

Decreases the karma for <thing>

=back

=cut

package Bot::BasicBot::Pluggable::Module::GoodBad;
our $VERSION = '0.01_01';

use warnings;
use strict;
use base qw(Bot::BasicBot::Pluggable::Module);

sub said {
    my ( $self, $mess, $pri ) = @_;
    my $body = $mess->{body};

    my ( $command, $param ) = split( /\s+/, $body, 2 );
    $command = lc($command);
    $param =~ s/\?*\s*$// if $param;

    if ( $pri == 0 ) {
        if (   ( $body =~ /(\w+)\+\+\s*/ )
            or ( $body =~ /\(([\w\s]+)\)\+\+\s*/ ) )
        {

            #print STDERR "$1++\n";
            $self->good( $1, 1, $mess->{who} );

            return $self->reply_with_goodbad( $mess, $1 );
        }
        elsif (( $body =~ /(\w+)\-\-/ )
            or ( $body =~ /\(([\w\s]+)\)\-\-/ ) )
        {

            #print STDERR "$1--\n";
            $self->bad( $1, 0, $', $mess->{who} );
            return $self->reply_with_goodbad( $mess, $1 );
        }
    }
}

sub good {
    my ( $self, $object, $good, $who ) = @_;
    $object = lc($object);
    $object =~ s/-/ /g;
    my $row = { who => $who, timestamp => time, positive => $good };

    my @changes = @{ $self->get("goodbad_$object") || [] };
    push @changes, $row;
    $self->set( "goodbad_$object" => \@changes );
    return;
}

sub bad {
    my ( $self, $object, $bad, $who ) = @_;
    $object = lc($object);
    $object =~ s/-/ /g;
    my $row = { who => $who, timestamp => time, negative => $bad };

    my @changes = @{ $self->get("goodbad_$object") || [] };
    push @changes, $row;
    $self->set( "goodbad_$object" => \@changes );
    return;
}

sub get_goodbad {
    my ( $self, $object ) = @_;
    $object = lc($object);
    $object =~ s/-/ /g;
    my @changes = @{ $self->get("goodbad_$object") || [] };

    my $good = 0;
    my $bad  = 0;

    for my $row (@changes) {
        if ( $row->{positive} ) {
            $good++;
        }
        else {
            $bad++;
        }
    }

    if ( wantarray() ) {
        return ( $good, $bad );
    }
}

sub reply_with_goodbad {
    my ( $self, $message, $target ) = @_;
    my ( $good, $bad ) = $self->get_goodbad($target);
    my $reply .= $target . "(" . $good . "++, " . $bad . "--" . ")";
    $self->reply( $message, $reply ) if $reply;
}

1;
