# HG changeset patch # User Dominic Cleal # Date 1228581970 0 # Node ID 2ab5ccc01036dbcf227c891f07e07c35c190a083 # Parent 5e35c408cc3cf20fe9a1ca332e69281b768234cd Adding a check in shortly before an announcement to double-check the current skill diff -r 5e35c408cc3c -r 2ab5ccc01036 skillbot.pl --- a/skillbot.pl Sat Dec 06 16:30:55 2008 +0000 +++ b/skillbot.pl Sat Dec 06 16:46:10 2008 +0000 @@ -394,44 +394,61 @@ { my $self = shift; - foreach my $f (@friends) { - my $skill = $f->{char}->skill->in_training; - - # Nothing training - unless (defined $skill) - { - # If the char was training before and now has stopped then cancel - # the announcement - del_one_shot_timer($f->{timer}) if defined $f->{timer}; - next; - } + check_friend($self, $_) foreach (@friends); +} + +sub check_friend +{ + my $self = shift; + my $f = shift; - # Check for changes in the skill, skip or cancel announcement - if (defined $f->{skill}) { - next if $skill->id == $f->{skill}->id; - del_one_shot_timer($f->{timer}); - } + my $skill = $f->{char}->skill->in_training; + + # Nothing training + unless (defined $skill) + { + # If the char was training before and now has stopped then cancel + # the announcement + del_one_shot_timer($f->{timer}) if defined $f->{timer}; + next; + } - irc_debug("Character %s is training %s %lu (%s == %lu sec)", - $f->{char}->name, $skill->name, $skill->level, - $skill->time_remaining, ($skill->finish_time - time())); + # Check for changes in the skill, skip or cancel announcement + if (defined $f->{skill}) { + next if $skill->id == $f->{skill}->id; + del_one_shot_timer($f->{timer}); + } - $f->{skill} = $skill; + irc_debug("Character %s is training %s %lu (%s == %lu sec)", + $f->{char}->name, $skill->name, $skill->level, + $skill->time_remaining, ($skill->finish_time - time())); + + $f->{skill} = $skill; - my $text = sprintf("%s has completed training skill %s %s", - $f->{char}->name, $skill->name, $skill->level); - - if ($text =~ /[\n\r]/) { - $text =~ s/[\n\r]/ /g; - } + my $text = sprintf("%s has completed training skill %s %s", + $f->{char}->name, $skill->name, $skill->level); + + if ($text =~ /[\n\r]/) { + $text =~ s/[\n\r]/ /g; + } - $f->{timer} = add_one_shot_timer($skill->finish_time - time(), sub { - $self->privmsg('#' . $config{channel}, sprintf( - chr(2) . "%s:" . chr(15) . " %s", - $f->{nick}, encode("utf8", $text))); - delete $f->{skill}; + my $finish = $skill->finish_time - time(); + my $prefinish = $finish - 60; + + # Add a timer shortly before the end to ensure the user's still training + if ($prefinish > 0) + { + $f->{timer} = add_one_shot_timer($prefinish, sub { + check_friend($self, $f); }); } + + $f->{timer} = add_one_shot_timer($skill->finish_time - time(), sub { + $self->privmsg('#' . $config{channel}, sprintf( + chr(2) . "%s:" . chr(15) . " %s", + $f->{nick}, encode("utf8", $text))); + delete $f->{skill}; + }); } END {