changeset 15:2ab5ccc01036

Adding a check in shortly before an announcement to double-check the current skill
author Dominic Cleal <dominic@computerkb.co.uk>
date Sat, 06 Dec 2008 16:46:10 +0000
parents 5e35c408cc3c
children e238f28f774a
files skillbot.pl
diffstat 1 files changed, 48 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- 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 {