+sub getships
+ : Help(Usage: .getships ship | % can be used as wildcard, e.g. beet%, shipshome shows the number of ships currently home)
+ : Alias(shipshome)
+ : ACL(irc_getships)
+{
+ my ($self,$c,$msg) = @_;
+ my ($ship) = $msg =~ /^(\S+)$/ or die 'ARGS';
+ my $dbh = $c->model;
+
+ my $f = $dbh->prepare(q{
+SELECT username,SUM(fs.amount) AS amount
+FROM users u
+ JOIN (SELECT DISTINCT ON (planet) planet,fid FROM fleets
+ WHERE mission = 'Full fleet' AND name <> 'Unit'
+ ORDER BY planet,tick DESC,fid DESC
+ ) f USING (planet)
+ JOIN fleet_ships fs USING (fid)
+WHERE ship ILIKE $1 AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
+GROUP BY username ORDER BY amount DESC
+ });
+ if ($self->name eq 'shipshome'){
+ $f = $dbh->prepare(q{
+SELECT username,SUM(amount) AS amount
+FROM available_ships
+WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
+GROUP BY username ORDER BY amount DESC
+ });
+ }
+ $f->execute($ship);
+ my $text;
+ my $i = 0;
+ my $total = 0;
+ while (my $user = $f->fetchrow_hashref){
+ $text .= "$user->{username}: $user->{amount} ";
+ $i++;
+ $total += $user->{amount};
+ }
+ if ($text){
+ $c->reply("<b>$i</b> Users with <b>$total $ship</b>: $text");
+ }else{
+ $c->reply("Couldn't find any user with <b>$ship</b>");
+ }
+}
+