# HG changeset patch # User Dominic Cleal # Date 1230490660 0 # Node ID 74213ea642da70cb2dbee8229fa949a540594aeb # Parent 7d43407a92144c80a8d7fe979f4feecc3839cb60 Countdown text is now dynamic using the finish timestamp diff -r 7d43407a9214 -r 74213ea642da skillbot.pl --- 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(); }