]> ruin.nu Git - NDIRC.git/blobdiff - Commands/Usermgm.pm
Converted .getships and .shipshome
[NDIRC.git] / Commands / Usermgm.pm
index 3563b84fc12b7754b6dfb41330adff97ea6ed8cb..101ea06910d949b30a974c67a24c4ec2e62bf5e4 100644 (file)
@@ -247,4 +247,48 @@ WHERE username ILIKE $1 ORDER BY lower(username)
        $c->reply("<b>$i</b> Users(days): $text");
 }
 
+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>");
+       }
+}
+
 1;