]> ruin.nu Git - NDIRC.git/commitdiff
Converted Scans
authorMichael Andreen <harv@ruin.nu>
Sun, 6 Dec 2009 00:22:58 +0000 (01:22 +0100)
committerMichael Andreen <harv@ruin.nu>
Sun, 6 Dec 2009 00:22:58 +0000 (01:22 +0100)
Commands/Scans.pm

index 06ae96f960e93bfd352d1ee0cf142a9ef8353c03..bc0f4c07014eecf0bceb969dacfc7cc3d6c61ab4 100644 (file)
 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 #**************************************************************************/
 
 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 #**************************************************************************/
 
-package NDIRC::Commands::Scans;
-
 use strict;
 use warnings;
 use feature ':5.10';
 
 use strict;
 use warnings;
 use feature ':5.10';
 
-use Moose;
-use MooseX::MethodAttributes;
+use MooseX::Declare;
+use NDIRC::Dispatcher;
 
 
-sub getscans
-       : Help(syntax: .getscans X:Y:Z)
-       : Type(pm)
-{
-       my ($self,$c,$args) = @_;
-       my ($x,$y,$z) = $args =~ /(\d+)\D+(\d+)\D+(\d+)/ or die 'ARGS';
-       $c->reply("https://nd.ruin.nu/stats/find/$x:$y:$z");
-}
+command getscans => {
+       help => q(syntax: .getscans X:Y:Z),
+       type => q(pm),
+}, class extends NDIRC::Command {
+       method execute($c,$args) {
+               my ($x,$y,$z) = $args =~ /(\d+)\D+(\d+)\D+(\d+)/ or die 'ARGS';
+               $c->reply("https://nd.ruin.nu/stats/find/$x:$y:$z");
+       }
+};
 
 my %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
 
 my @scantypes = ('Planet','Landing', 'Development'
        ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
 
 
 my %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
 
 my @scantypes = ('Planet','Landing', 'Development'
        ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
 
-sub gs
-       : Help(syntax: gs type X:Y:Z message | type is the first character in the scan name, like p for planet scan, message is a message to scanners, like plz or thanks like plz or thanks)
-       : ACL(irc_gs)
-       : Type(pm)
-{
-       my ($self,$c,$args) = @_;
-
-       my ($typeid, $x, $y, $z, $msg) = $args =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/
-               or die 'ARGS';
-       $typeid = $scanid{$typeid};
-       my $type = $scantypes[$typeid-1];
+command gs => {
+       help => q(syntax: gs type X:Y:Z message | type is the first character in the scan name, like p for planet scan, message is a message to scanners, like plz or thanks like plz or thanks),
+       acl => q(irc_gs),
+       type => q(pm),
+}, class extends NDIRC::Command {
+       method execute($c,$args) {
+               my ($typeid, $x, $y, $z, $msg) = $args =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/
+                       or die 'ARGS';
+               $typeid = $scanid{$typeid};
+               my $type = $scantypes[$typeid-1];
 
 
-       my ($planet,$dists,$tick) = $c->model->selectrow_array(q{
+               my ($planet,$dists,$tick) = $c->model->selectrow_array(q{
 SELECT pid,distorters,tick
 FROM current_planet_stats LEFT JOIN current_development_scans USING (pid)
 WHERE x = $1 AND y = $2 AND z = $3
 SELECT pid,distorters,tick
 FROM current_planet_stats LEFT JOIN current_development_scans USING (pid)
 WHERE x = $1 AND y = $2 AND z = $3
-               },undef,$x,$y,$z);
-       unless ($planet){
-               $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
-               return;
-       }
+                       },undef,$x,$y,$z);
+               unless ($planet){
+                       $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
+                       return;
+               }
 
 
-       my $query = $c->model->prepare(q{SELECT scan_id
-               FROM scans
-               WHERE pid = $1 AND type = $2 AND tick >= tick()});
-       $query->execute($planet,$type);
+               my $query = $c->model->prepare(q{
+SELECT scan_id FROM scans
+WHERE pid = $1 AND type = $2 AND tick >= tick()
+                       });
+               $query->execute($planet,$type);
 
 
-       if (my $scan = $query->fetchrow_hashref){
-               $c->reply("scan already exist: "
-                       . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
-       }else{
-               my $req = $c->model->prepare(q{
+               if (my $scan = $query->fetchrow_hashref){
+                       $c->reply("scan already exist: "
+                               . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
+               }else{
+                       my $req = $c->model->prepare(q{
 SELECT * FROM scan_requests
 WHERE uid = $1
        AND pid = $2 AND type = $3 AND NOT sent
 SELECT * FROM scan_requests
 WHERE uid = $1
        AND pid = $2 AND type = $3 AND NOT sent
-               });
-               $req->execute($c->uid,$planet,$type);
+                               });
+                       $req->execute($c->uid,$planet,$type);
 
 
-               my $id;
-               if(my $scan = $req->fetchrow_hashref){
-                       $req = $c->model->prepare(q{
+                       my $id;
+                       if(my $scan = $req->fetchrow_hashref){
+                               $req = $c->model->prepare(q{
 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
 WHERE id = $2
 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
 WHERE id = $2
-                       });
-                       $req->execute($c->nick,$scan->{id});
-                       $id = $scan->{id};
-               }else{
-                       $req = $c->model->prepare(q{
+                                       });
+                               $req->execute($c->nick,$scan->{id});
+                               $id = $scan->{id};
+                       }else{
+                               $req = $c->model->prepare(q{
 INSERT INTO scan_requests (uid,nick,pid,type)
 VALUES($1,$2,$3,$4) RETURNING (id)
 INSERT INTO scan_requests (uid,nick,pid,type)
 VALUES($1,$2,$3,$4) RETURNING (id)
-                       });
-                       $req->execute($c->uid,$c->nick,$planet,$type);
-                       $id = $req->fetchrow;
-               }
-
-               if ($id){
+                                       });
+                               $req->execute($c->uid,$c->nick,$planet,$type);
+                               $id = $req->fetchrow;
+                       }
 
 
-                       if (defined $dists){
-                               $dists = "$dists dists PT$tick"
+                       if ($id){
+                               if (defined $dists){
+                                       $dists = "$dists dists PT$tick"
+                               }else{
+                                       $dists = "DISTS UNKNOWN, ADD DEVSCAN"
+                               }
+                               $c->message(privmsg => $c->disp->targets->{scan}
+                                       ,"<b>$id</b> http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
+                                       . " ($x:$y:$z $type - $dists) | <".$c->nick."> $msg"
+                               );
+                               $c->reply("sent request ($x:$y:$z $type)");
                        }else{
                        }else{
-                               $dists = "DISTS UNKNOWN, ADD DEVSCAN"
+                               $c->reply("something went wrong..");
                        }
                        }
-                       $c->message(privmsg => $c->disp->targets->{scan}
-                               ,"<b>$id</b> http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
-                               . " ($x:$y:$z $type - $dists) | <".$c->nick."> $msg"
-                       );
-                       $c->reply("sent request ($x:$y:$z $type)");
-               }else{
-                       $c->reply("something went wrong..");
                }
        }
                }
        }
-}
+};
 
 my %scantypes;
 {
 
 my %scantypes;
 {
@@ -119,22 +118,21 @@ my %scantypes;
        %scantypes = map {$_ => $i++} @scantypes;
 }
 
        %scantypes = map {$_ => $i++} @scantypes;
 }
 
-sub scanreqs
-       : Help(syntax: .scanreqs [-pdunja] | Lists scan requests that haven't been handled. The argument can be used to omit types you don't have, like .scanreqs -ja to list all requests except jumpgates and advanced unit scans.)
-       : ACL(irc_scanreqs)
-{
-       my ($self, $c, $msg) = @_;
-
-       my @notype;
-       if ($msg =~ /^-([pdunja]+)$/){
-               for (split //, $1){
-                       push @notype, $scantypes[$scanid{$_}-1];
+command scanreqs => {
+       help => q(syntax: .scanreqs [-pdunja] | Lists scan requests that haven't been handled. The argument can be used to omit types you don't have, like .scanreqs -ja to list all requests except jumpgates and advanced unit scans.),
+       acl => q(irc_scanreqs),
+}, class extends NDIRC::Command {
+       method execute($c,$msg) {
+               my @notype;
+               if ($msg =~ /^-([pdunja]+)$/){
+                       for (split //, $1){
+                               push @notype, $scantypes[$scanid{$_}-1];
+                       }
+               }elsif($msg){
+                       die 'ARGS';
                }
                }
-       }elsif($msg){
-               die 'ARGS';
-       }
 
 
-       my $reqs = $c->model->prepare(q{
+               my $reqs = $c->model->prepare(q{
 SELECT min(sr.id) AS id, x,y,z,type
 FROM scan_requests sr
        JOIN current_planet_stats p USING (pid)
 SELECT min(sr.id) AS id, x,y,z,type
 FROM scan_requests sr
        JOIN current_planet_stats p USING (pid)
@@ -147,114 +145,117 @@ WHERE sr.time > NOW() - '30 min'::INTERVAL
        AND type <> ALL($1)
 GROUP BY x,y,z,type
 ORDER BY id
        AND type <> ALL($1)
 GROUP BY x,y,z,type
 ORDER BY id
-               });
-       $reqs->execute(\@notype);
-       my $text = '';
-       while (my $req = $reqs->fetchrow_hashref){
-               $text .= "<b>$req->{id}</b> http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
-       }
-
-       $c->reply($text || 'No unhandled requests.');
-}
+                       });
+               $reqs->execute(\@notype);
+               my $text = '';
+               while (my $req = $reqs->fetchrow_hashref){
+                       $text .= "<b>$req->{id}</b> http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
+               }
 
 
-sub scan
-       : Help(syntax: scan type X:Y:Z [tick] | type is the first character in the scan name, like p for planet scan. Specify a tick if you want scans older than the specified tick.)
-       : ACL(irc_scan)
-       : Type(pm)
-{
-       my ($self, $c,$msg) = @_;
+               $c->reply($text || 'No unhandled requests.');
+       }
+};
 
 
-       my ($t, $x, $y, $z, $tick)
+command scan => {
+       help => q(syntax: scan type X:Y:Z [tick] | type is the first character in the scan name, like p for planet scan. Specify a tick if you want scans older than the specified tick.),
+       acl => q(irc_scan),
+       type => q(pm),
+}, class extends NDIRC::Command {
+       method execute($c,$msg) {
+               my ($t, $x, $y, $z, $tick)
                = $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/ or die 'ARGS';
                = $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/ or die 'ARGS';
-       my $type =$scantypes[$scanid{$t}-1];
+               my $type =$scantypes[$scanid{$t}-1];
 
 
-       my $scan = $c->model->selectrow_hashref(q{
+               my $scan = $c->model->selectrow_hashref(q{
 SELECT id, scan_id, tick FROM scans
 WHERE type = $1 AND pid = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
 ORDER BY tick DESC LIMIT 1
                        },undef,$type,$x,$y,$z,$tick);
 SELECT id, scan_id, tick FROM scans
 WHERE type = $1 AND pid = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
 ORDER BY tick DESC LIMIT 1
                        },undef,$type,$x,$y,$z,$tick);
-       unless ($scan->{id}){
-               $c->reply("No $type scan for $x:$y:$z. Planet might not exist.");
-               return;
-       }
-       my $text = '';
-       given ($t){
-               when(/[ua]/){
-                       my $fid = $c->model->selectrow_array(q{
+               unless ($scan->{id}){
+                       $c->reply("No $type scan for $x:$y:$z. Planet might not exist.");
+                       return;
+               }
+               my $text = '';
+               given ($t){
+                       when(/[ua]/){
+                               my $fid = $c->model->selectrow_array(q{
 SELECT fid FROM fleet_scans WHERE id = $1
 SELECT fid FROM fleet_scans WHERE id = $1
-                       },undef,$scan->{id});
-                       my $ships = $c->model->prepare(q{
+                                       },undef,$scan->{id});
+                               my $ships = $c->model->prepare(q{
 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
-                       });
-                       $ships->execute($fid);
-                       while (my $ship = $ships->fetchrow_hashref){
-                               $text .= " $ship->{ship} $ship->{amount} |"
+                                       });
+                               $ships->execute($fid);
+                               while (my $ship = $ships->fetchrow_hashref){
+                                       $text .= " $ship->{ship} $ship->{amount} |"
+                               }
+                       }
+                       default {
+                               $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
                        }
                }
                        }
                }
-               default {
-                       $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
-               }
+               $c->reply("<b>$type</b> scan of $x:$y:$z "
+                       . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
        }
        }
-       $c->reply("<b>$type</b> scan of $x:$y:$z "
-               . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
-}
+};
 
 
-sub anon
-       : Help(syntax: .anon nick message)
-       : Type(scan)
-       : ACL(irc_anonscan)
-{
-       my ($self, $c, $msg) = @_;
-       my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
+command anon => {
+       help => q(syntax: .anon nick message),
+       type => q(scan),
+       acl => q(irc_anonscan),
+}, class extends NDIRC::Command {
+       method execute($c,$msg) {
+               my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
 
 
-       $c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
-               .$c->channel.")</c>");
-       $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
-}
+               $c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
+                       .$c->channel.")</c>");
+               $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
+       }
+};
 
 
-sub addscan
-       : Help(syntax: .addscan list of scans)
-{
-       my ($self, $c, $msg) = @_;
-       my $dbh = $c->model;
+command addscan => {
+       help => q(syntax: .addscan list of scans),
+}, class extends NDIRC::Command {
+       method execute($c,$msg) {
+               my $dbh = $c->model;
 
 
-       my $oldscan = $dbh->prepare(q{
+               my $oldscan = $dbh->prepare(q{
 SELECT scan_id FROM scans
 WHERE groupscan = $1 AND scan_id = LOWER($2) AND tick >= tick() - 168
 SELECT scan_id FROM scans
 WHERE groupscan = $1 AND scan_id = LOWER($2) AND tick >= tick() - 168
-               });
-       my $addscan = $dbh->prepare(q{
+                       });
+               my $addscan = $dbh->prepare(q{
 INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER($1),tick(),$2,COALESCE($3,-1))
 INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER($1),tick(),$2,COALESCE($3,-1))
-               });
-       my $user = $dbh->selectrow_hashref(q{SELECT uid,username, scan_points, tick()
-               FROM users WHERE uid = ? },undef,$c->uid);
-       my $groupscans = 0;
-       my $scans = 0;
-       eval {
-               $dbh->begin_work;
-               while ($msg =~ m{/.+?scan(_id|_grp)?=(\w+)}g){
-                       my $id = $2;
-                       my $group = ($1 ~~ /_grp/ || 0);
-                       unless($dbh->selectrow_array($oldscan,undef,$group,$id)){
-                               $addscan->execute($id,$group,$user->{uid});
-                               if ($group){
-                                       $groupscans++;
-                               }else{
-                                       $scans++;
+                       });
+               my $user = $dbh->selectrow_hashref(q{SELECT uid,username, scan_points, tick()
+                       FROM users WHERE uid = ? },undef,$c->uid);
+               my $groupscans = 0;
+               my $scans = 0;
+               eval {
+                       $dbh->begin_work;
+                       while ($msg =~ m{/.+?scan(_id|_grp)?=(\w+)}g){
+                               my $id = $2;
+                               my $group = ($1 ~~ /_grp/ || 0);
+                               unless($dbh->selectrow_array($oldscan,undef,$group,$id)){
+                                       $addscan->execute($id,$group,$user->{uid});
+                                       if ($group){
+                                               $groupscans++;
+                                       }else{
+                                               $scans++;
+                                       }
                                }
                        }
                                }
                        }
+                       if ($user->{uid} && ($scans || $groupscans)){
+                               $dbh->do(q{UPDATE users SET scan_points = scan_points + $2 WHERE uid = $1}
+                                       ,undef,$user->{uid},$scans);
+                               my $points = $user->{scan_points} + $scans;
+                               $c->reply("Added $scans scans and $groupscans groupscans, $points in total.");
+                       }
+                       $dbh->commit;
+               };
+               if ($@){
+                       $dbh->rollback;
+                       die $@;
                }
                }
-               if ($user->{uid} && ($scans || $groupscans)){
-                       $dbh->do(q{UPDATE users SET scan_points = scan_points + $2 WHERE uid = $1}
-                               ,undef,$user->{uid},$scans);
-                       my $points = $user->{scan_points} + $scans;
-                       $c->reply("Added $scans scans and $groupscans groupscans, $points in total.");
-               }
-               $dbh->commit;
-       };
-       if ($@){
-               $dbh->rollback;
-               die $@;
        }
        }
-}
+};
 
 1;
 
 1;