changeset 59:74213ea642da

Countdown text is now dynamic using the finish timestamp
author Dominic Cleal <dominic@computerkb.co.uk>
date Sun, 28 Dec 2008 18:57:40 +0000
parents 7d43407a9214
children 69d7a89d3adc
files skillbot.pl
diffstat 1 files changed, 30 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/skillbot.pl	Sun Dec 28 18:36:29 2008 +0000
+++ b/skillbot.pl	Sun Dec 28 18:57:40 2008 +0000
@@ -250,7 +250,8 @@
 
 				$text = sprintf("Currently training %s ".
 								"(finish in %s, %s %s%s)",
-								$f->{skill_name}, $f->{skill_countdown},
+								$f->{skill_name},
+								countdown($f->{skill_finish}),
 								$finish->strftime("%A %R"),
 								$finish->time_zone_short_name(),
 								$downtime);
@@ -280,7 +281,7 @@
 		
 		for my $f (@training) {
 			$nreply .= sprintf("\002%s\002 (%s) | ",
-							   $f->{char}, $f->{skill_countdown});
+							   $f->{char}, countdown($f->{skill_finish}));
 		}
 		
 		$self->privmsg('#' . $config{channel}, substr($nreply, 0, -3));
@@ -504,7 +505,6 @@
 		if ($skill->id eq $f->{skill})
 		{
 			$f->{skill_finish} = $skill->finish_time;
-			$f->{skill_countdown} = $skill->time_remaining;
 			return;
 		}
 
@@ -530,7 +530,6 @@
 
 	$f->{skill} = $skill->id;
 	$f->{skill_finish} = $skill->finish_time;
-	$f->{skill_countdown} = $skill->time_remaining;
 	$f->{skill_name} = sprintf("%s %d", $skill->name, $skill->level);
 
 	# Check if the finish time is within scheduled EVE downtime
@@ -564,7 +563,6 @@
 					   encode("utf8", $text)));
 		delete $f->{skill};
 		delete $f->{skill_finish};
-		delete $f->{skill_countdown};
 		delete $f->{skill_name};
 	});
 }
@@ -583,6 +581,33 @@
 	return ([gmtime($ts)]->[2] == 11);
 }
 
+=pod
+
+Translate a timestamp to a countdown in terms of days, hours, minutes and
+seconds, EVE style.
+
+=cut
+sub countdown
+{
+	my $ts = shift;
+
+	my $tshift = sub {
+		my ($a, $b, $max) = @_;
+		if ($$a >= $max) {
+			$$b = floor($$a / $max);
+			$$a = $$a % $max;
+		}
+	};
+
+	my ($s, $m, $h, $d) = ($ts - time, 0, 0, 0);
+	&$tshift(\$s, \$m, 60);
+	&$tshift(\$m, \$h, 60);
+	&$tshift(\$h, \$d, 24);
+	
+	return sprintf("%dd %02dh %02dm %02ds", $d, $h, $m, $s);
+}
+
+
 END {
 	cleanup_and_die();
 }