From 9e077e0aeb51cfd1eeeb12f2362fc11ac6d6b38a Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 19 May 2009 22:24:04 +0200 Subject: [PATCH] Converted .settype and .settypeall --- Commands/Def.pm | 71 ++++++++++++++++++++++++++++++++++++++++++++++ Context.pm | 10 +++++++ Def.pm | 66 +----------------------------------------- database/roles.sql | 4 +++ 4 files changed, 86 insertions(+), 65 deletions(-) diff --git a/Commands/Def.pm b/Commands/Def.pm index 74d0aa8..e8067dd 100644 --- a/Commands/Def.pm +++ b/Commands/Def.pm @@ -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; diff --git a/Context.pm b/Context.pm index 6831bde..124da62 100644 --- a/Context.pm +++ b/Context.pm @@ -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 d811501..f2285d1 100644 --- 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); diff --git a/database/roles.sql b/database/roles.sql index 09cc700..e238160 100644 --- a/database/roles.sql +++ b/database/roles.sql @@ -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'); -- 2.39.2