| 1 | package Exobrain::Agent::Action::SendmailXP; | ||
| 2 | use Moose; | ||
| 3 | use Method::Signatures; | ||
| 4 | |||
| 5 | with 'Exobrain::Agent::Run'; | ||
| 6 | with 'Exobrain::Agent::Depends'; | ||
| 7 | |||
| 8 | # ABSTRACT: Reward users on HabitRPG for sending email. | ||
| 9 | our $VERSION = '1.08'; # VERSION | ||
| 10 | |||
| 11 | use constant DEBUG => 0; | ||
| 12 | |||
| 13 | method depends() { qw(Intent::HabitRPG Measurement::Mailbox) } | ||
| 14 | |||
| 15 | # WTF exobrain, why do I need to use a (...) signature here? | ||
| 16 | # What on earth are you being passed? | ||
| 17 | |||
| 18 | method run(...) { | ||
| 19 | my $task = $self->config->{task} or die "No HabitRPG task"; | ||
| 20 | my $cache = $self->cache; | ||
| 21 | |||
| 22 | $self->exobrain->watch_loop( | ||
| 23 | class => 'Measurement::Mailbox', | ||
| 24 | filter => sub { $_->mailbox =~ /sent/i }, | ||
| 25 | then => sub { | ||
| 26 | my $event = shift; | ||
| 27 | |||
| 28 | my $key = [ $event->server, $event->mailbox ]; | ||
| 29 | my $count = $event->count; | ||
| 30 | |||
| 31 | my $old_count = $cache->get( $key ) || 0; | ||
| 32 | |||
| 33 | warn "Handling @$key at $count (from $old_count) msgs\n" if DEBUG; | ||
| 34 | |||
| 35 | if ($count > $old_count) { | ||
| 36 | |||
| 37 | # Sweet! They sent email | ||
| 38 | $cache->set( $key, $count ); | ||
| 39 | |||
| 40 | $event->exobrain->intent('HabitRPG', | ||
| 41 | task => $task, | ||
| 42 | direction => 'up', | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | 1; | ||
| 50 | |||
| 51 | __END__ |