changeset 53:d5872370102a

Sorting !eta output for multiple characters, tidying filtering
author Dominic Cleal <dominic@computerkb.co.uk>
date Thu, 25 Dec 2008 19:14:23 +0000
parents 0f8275723d81
children 9ed1e53a8d3e
files skillbot.pl
diffstat 1 files changed, 35 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/skillbot.pl	Tue Dec 23 16:41:59 2008 +0000
+++ b/skillbot.pl	Thu Dec 25 19:14:23 2008 +0000
@@ -228,38 +228,46 @@
 	# lists the current chars in training of the caller
 	if ($msg =~ /^\!eta(\s+(.+))?/i) {
 		my $found = 0;
-		for my $f (@friends) {
-			if ((defined $2 && (lc($f->{char}->name) eq lc($2)
-							|| lc($2) eq 'all' || lc($2) eq '*'))
-					|| (!defined $2 && $f->{nick} eq $their_nick)) {
-				check_friend($self, $f);
-				my $text;
-				if (defined $f->{skill}) {
-					my $downtime = '';
-					if (skill_finish_in_downtime($f->{skill})) {
-						$downtime = ", coincides with downtime";
-					}
 
-					my $finish = DateTime->from_epoch(
-							epoch => $f->{skill}->finish_time,
-							time_zone => $f->{tz});
+		my @filtered =
+			sort {
+				$a->{skill}->finish_time <=> $b->{skill}->finish_time
+			}
+			grep {
+				((defined $2 && (lc($_->{char}->name) eq lc($2)
+						|| lc($2) eq 'all' || lc($2) eq '*'))
+					|| (!defined $2 && $_->{nick} eq $their_nick))
+			} @friends;
 
-					$text = sprintf("Currently training %s %s ".
-									"(finish in %s, %s %s%s)",
-									$f->{skill}->name, $f->{skill}->level,
-									$f->{skill}->time_remaining,
-									$finish->strftime("%A %R"),
-									$finish->time_zone_short_name(),
-									$downtime);
-				} else {
-					$text = "No skill currently training";
+		for my $f (@filtered)
+		{
+			check_friend($self, $f);
+			my $text;
+			if (defined $f->{skill}) {
+				my $downtime = '';
+				if (skill_finish_in_downtime($f->{skill})) {
+					$downtime = ", coincides with downtime";
 				}
 
-				$self->privmsg('#' . $config{channel}, sprintf(
-							   "\002%s:\002 %s", $f->{char}->name,
-							   encode("utf8", $text)));
-				$found++;
+				my $finish = DateTime->from_epoch(
+						epoch => $f->{skill}->finish_time,
+						time_zone => $f->{tz});
+
+				$text = sprintf("Currently training %s %s ".
+								"(finish in %s, %s %s%s)",
+								$f->{skill}->name, $f->{skill}->level,
+								$f->{skill}->time_remaining,
+								$finish->strftime("%A %R"),
+								$finish->time_zone_short_name(),
+								$downtime);
+			} else {
+				$text = "No skill currently training";
 			}
+
+			$self->privmsg('#' . $config{channel}, sprintf(
+						   "\002%s:\002 %s", $f->{char}->name,
+						   encode("utf8", $text)));
+			$found++;
 		}
 
 		if ($found == 0) {