The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use autodie;

use Exobrain;
use WebService::HabitRPG;

# PODNAME: habitrpg
# ABSTRACT: Send HabitRPG intents to HabitRPG
our $VERSION = '1.07'; # VERSION

use constant DEBUG => 0;

my $exobrain = Exobrain->new;

my $hrpg = WebService::HabitRPG->new(
    api_token => $exobrain->config->{HabitRPG}{api_token},
    user_id   => $exobrain->config->{HabitRPG}{user_id},
);

$exobrain->watch_loop(
    class => 'Intent::HabitRPG',
    then => sub {

        my $event = shift;

        my $task      = $event->data->{task};
        my $direction = $event->data->{direction};

        my $stats = $hrpg->user->{stats};

        my $result;

        if ($task and $direction) {
            debug("Moving $task $direction");
            $result = $hrpg->updown($task, $direction);
            debug("Completed move");
        }

        my $name = $hrpg->get_task($task)->{text};

        my $msg;

        if ($direction eq "up") {
            $msg = sprintf(
                "Congrats! You gained %+.2f XP and %+.2f GP for completing: $name",
                $result->{exp} - $stats->{exp},
                $result->{gp}  - $stats->{gp},
            );
        }
        else {
            # Must be down
            $msg = sprintf(
                "Oh no! You lost %+.2f HP for: $name",
                $result->{hp} - $stats->{hp},
            )
        }

        debug("Sending: $msg");
        $event->exobrain->notify($msg);
    }
);

sub debug {
    say "@_" if DEBUG;
    return;
}

__END__

=pod

=head1 NAME

habitrpg - Send HabitRPG intents to HabitRPG

=head1 VERSION

version 1.07

=head1 AUTHOR

Paul Fenwick <pjf@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Paul Fenwick.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut