]> ruin.nu Git - NDIRC.git/commitdiff
Converted .settype and .settypeall
authorMichael Andreen <harv@ruin.nu>
Tue, 19 May 2009 20:24:04 +0000 (22:24 +0200)
committerMichael Andreen <harv@ruin.nu>
Wed, 20 May 2009 16:43:40 +0000 (18:43 +0200)
Commands/Def.pm
Context.pm
Def.pm
database/roles.sql

index 74d0aa8dd85de111414620e2510cbb2d2483e983..e8067dd7a50b27ad147526a8bdf66acf51688914 100644 (file)
@@ -23,6 +23,8 @@ use strict;
 use warnings;
 use feature ':5.10';
 
+use CGI;
+
 use Moose;
 use MooseX::MethodAttributes;
 
@@ -79,5 +81,74 @@ ORDER BY c.landing_tick;
                .$c->nick." TO RESPOND");
 }
 
+sub settype
+       : Help(Usage: .settype incId type | or: .settyp callId X:Y:Z type | or: .settypeall callId type)
+       : Type(def)
+       : ACL(irc_settype)
+       : Alias(settypeall)
+{
+       my ($self,$c,$msg) = @_;
+       my $dbh = $c->model;
+
+       my $query = q{
+SELECT i.id,call,shiptype, coords(x,y,z),c.landing_tick - tick() AS eta
+FROM incomings i
+       JOIN current_planet_stats p ON i.sender = p.id
+       JOIN calls c ON i.call = c.id
+       };
+       my $fleets;
+       my $type;
+       my $call;
+
+       if ($self->name eq 'settypeall'){
+               ($call,$type) = $msg =~ /^(\d+) (.*)$/ or die 'ARGS';
+
+               $fleets = $dbh->prepare($query . q{
+WHERE i.call = ?
+               });
+               $fleets->execute($call);
+       }else{
+               my ($id,$x,$y,$z,$t) = $msg =~ /^(\d+) (\d+):(\d+):(\d+) (.*)$/;
+               if (defined $id){
+                       $fleets = $dbh->prepare($query . q{
+WHERE i.call = ? AND p.id = planetid(?,?,?,tick())
+                       });
+                       $fleets->execute($id,$x,$y,$z);
+               }else{
+                       ($id,$t) = $msg =~ /^(\d+) (.*)$/ or die 'ARGS';
+                       $fleets = $dbh->prepare($query . q{
+WHERE i.id = ?
+                       });
+                       $fleets->execute($id);
+               }
+               $type = $t;
+       }
+
+       $type = CGI::escapeHTML($type);
+       $dbh->begin_work;
+       my $deflog = '';
+       my $settype = $dbh->prepare(q{UPDATE incomings SET shiptype = ? WHERE id = ?});
+       while (my $inc = $fleets->fetchrow_hashref){
+               $call //= $inc->{call};
+               if ($inc->{eta} < 0){
+                       $c->reply("This call is old. Did you use the call id instead of inc id by"
+                               ." accident? You can use .settypeall callid to set the type on all incs"
+                               ." in a call. Or use webbie if you really want to update this old call.");
+                       $dbh->rollback;
+                       return;
+               }
+               $settype->execute($type,$inc->{id});
+               $deflog .= "Set fleet: [B]$inc->{id} [/B] to: [B]$type [/B]\n";
+               $c->reply("Set fleet $inc->{id} from $inc->{coords} on call $call to $type (previously $inc->{shiptype})");
+       }
+       if ($fleets->rows == 0){
+               $c->reply("No matching fleets");
+               $dbh->rollback;
+       }else{
+               $c->def_log($call,$deflog);
+               $dbh->commit;
+       }
+}
+
 1;
 
index 6831bdef9c0ddae934e21413d4467a4284088aa3..124da62e5ca301a33da1a18534b24ac6737c51c6 100644 (file)
@@ -120,6 +120,16 @@ INSERT INTO forum_posts (ftid,uid,message) VALUES(
        $log->execute($c->host,$message,$planet);
 }
 
+sub def_log {
+       my ($c,$call, $message) = @_;
+       my $log = $c->model->prepare(q{
+INSERT INTO forum_posts (ftid,uid,message) VALUES(
+       (SELECT ftid FROM calls WHERE id = $3)
+       ,(SELECT uid FROM users WHERE hostmask ILIKE $1),$2)
+               });
+       $log->execute($c->host,$message,$call);
+}
+
 sub _build_roles {
        my ($self) = @_;
 
diff --git a/Def.pm b/Def.pm
index d8115012c5c179f2975935e5ac46dc455ea62cc7..f2285d1572930b887585f9ec31bfbfe726ae8514 100644 (file)
--- a/Def.pm
+++ b/Def.pm
@@ -28,7 +28,7 @@ require Exporter;
 
 our @ISA = qw/Exporter/;
 
-our @EXPORT = qw/showCall setType takeCall covCall ignoreCall
+our @EXPORT = qw/showCall takeCall covCall ignoreCall
        setDefPrio setCalc getCalc/;
 
 sub showCall {
@@ -58,70 +58,6 @@ SQL
        }
 }
 
-sub setType {
-       my ($msg,$command) = @_;
-       my ($type,$id,$x,$y,$z) = @_;
-       if ($command eq 'settypeall'){
-               if (defined $msg && $msg =~ /^(\d+) (.*)$/){
-                       $type = $2;
-                       $id = $1
-               }else{
-                       $ND::server->command("notice $ND::nick Usage: $command callId type");
-                       return;
-               }
-       }else{
-               if (defined $msg && $msg =~ /^(\d+) (\d+):(\d+):(\d+) (.*)$/){
-                       $type = $5;
-                       $id = $1;
-                       $x = $2;
-                       $y = $3;
-                       $z = $4;
-               }elsif (defined $msg && $msg =~ /^(\d+) (.*)$/){
-                       $id = $1;
-                       $type = $2;
-               }else{
-                       $ND::server->command("notice $ND::nick Usage: $command incId type | or: $command callId X:Y:Z type");
-                       return;
-               }
-       }
-       if (my $user = dc()){
-               my $fleet;
-               my $query = qq{
-                       SELECT i.id,call,shiptype, coords(x,y,z),c.landing_tick - tick() FROM incomings i 
-                               JOIN current_planet_stats p ON i.sender = p.id
-                               JOIN calls c ON i.call = c.id
-                       };
-               if ($command eq 'settypeall'){
-                       $fleet = $ND::DBH->prepare(qq{
-                                       $query
-                                       WHERE i.call = ?
-                               });
-                       $fleet->execute($id);
-               }elsif (defined $x){
-                       $fleet = $ND::DBH->prepare(qq{
-                                       $query
-                                       WHERE i.call = ? AND p.id = planetid(?,?,?,tick()) 
-                               });
-                       $fleet->execute($id,$x,$y,$z);
-               }else{
-                       $fleet = $ND::DBH->prepare(qq{
-                                       $query
-                                       WHERE i.id = ?
-                               });
-                       $fleet->execute($id);
-               }       
-               $type = escapeHTML($type);
-               while (my ($id,$call,$oldtype,$coords,$tick) = $fleet->fetchrow()){
-                       if($ND::DBH->do(q{UPDATE incomings SET shiptype = ? WHERE id = ?},undef,$type,$id) == 1){
-                               def_log $user->{uid}, $call , "Set fleet: [B] $id [/B] to: [B] $type [/B]";
-                               $ND::server->command("msg $ND::target Set fleet from $coords on call $call to $type (previously $oldtype)");
-                               if ($tick < 0 && not (defined $x && $x eq 'call')){
-                                       $ND::server->command("msg $ND::target This call is old, did you use the call id, instead of inc id by accident? You can use .settypeall callid to set the type on all incs in a call.");
-                               }
-                       }
-               }
-       }
-}
 sub takeCall {
        my ($msg,$command) = @_;
        my ($id);
index 09cc700b137a78c6fc9879b7fd8653ad8e862b8c..e2381603ebc348a7a33b10a4cfb1a2c7ee5132c2 100644 (file)
@@ -30,6 +30,7 @@ INSERT INTO roles VALUES('irc_setally');
 INSERT INTO roles VALUES('irc_setchannel');
 INSERT INTO roles VALUES('irc_anondef');
 INSERT INTO roles VALUES('irc_defcall');
+INSERT INTO roles VALUES('irc_settype');
 
 INSERT INTO group_roles (gid,role) VALUES(1,'irc_p_nick');
 INSERT INTO group_roles (gid,role) VALUES(1,'irc_p_intel');
@@ -61,6 +62,7 @@ INSERT INTO group_roles (gid,role) VALUES(1,'irc_setally');
 INSERT INTO group_roles (gid,role) VALUES(1,'irc_setchannel');
 INSERT INTO group_roles (gid,role) VALUES(1,'irc_anondef');
 INSERT INTO group_roles (gid,role) VALUES(1,'irc_defcall');
+INSERT INTO group_roles (gid,role) VALUES(1,'irc_settype');
 
 INSERT INTO group_roles (gid,role) VALUES(2,'irc_gs');
 INSERT INTO group_roles (gid,role) VALUES(2,'irc_scan');
@@ -94,6 +96,7 @@ INSERT INTO group_roles (gid,role) VALUES(3,'irc_setally');
 INSERT INTO group_roles (gid,role) VALUES(3,'irc_setchannel');
 INSERT INTO group_roles (gid,role) VALUES(3,'irc_anondef');
 INSERT INTO group_roles (gid,role) VALUES(3,'irc_defcall');
+INSERT INTO group_roles (gid,role) VALUES(3,'irc_settype');
 
 INSERT INTO group_roles (gid,role) VALUES(4,'irc_points_others');
 INSERT INTO group_roles (gid,role) VALUES(4,'irc_a');
@@ -114,6 +117,7 @@ INSERT INTO group_roles (gid,role) VALUES(6,'irc_d');
 INSERT INTO group_roles (gid,role) VALUES(6,'irc_sethostile');
 INSERT INTO group_roles (gid,role) VALUES(6,'irc_anondef');
 INSERT INTO group_roles (gid,role) VALUES(6,'irc_defcall');
+INSERT INTO group_roles (gid,role) VALUES(6,'irc_settype');
 
 INSERT INTO group_roles (gid,role) VALUES(8,'irc_scanreqs');
 INSERT INTO group_roles (gid,role) VALUES(8,'irc_anonscan');