1 #**************************************************************************
2 # Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu> *
4 # This program is free software; you can redistribute it and/or modify *
5 # it under the terms of the GNU General Public License as published by *
6 # the Free Software Foundation; either version 2 of the License, or *
7 # (at your option) any later version. *
9 # This program is distributed in the hope that it will be useful, *
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 # GNU General Public License for more details. *
14 # You should have received a copy of the GNU General Public License *
15 # along with this program; if not, write to the *
16 # Free Software Foundation, Inc., *
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 #**************************************************************************/
20 package NDIRC::Commands::Scans;
27 use MooseX::MethodAttributes;
30 : Help(syntax: .getscans X:Y:Z)
33 my ($self,$c,$args) = @_;
34 my ($x,$y,$z) = $args =~ /(\d+)\D+(\d+)\D+(\d+)/ or die 'ARGS';
35 $c->reply("https://nd.ruin.nu/stats/find/$x:$y:$z");
38 my %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
40 my @scantypes = ('Planet','Landing', 'Development'
41 ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
44 : 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)
48 my ($self,$c,$args) = @_;
50 my ($typeid, $x, $y, $z, $msg) = $args =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/
52 $typeid = $scanid{$typeid};
53 my $type = $scantypes[$typeid-1];
55 my $planet = $c->model->selectrow_array(q{SELECT planetid($1,$2,$3,tick())}
58 $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
62 my $query = $c->model->prepare(q{SELECT scan_id
64 WHERE pid = $1 AND type = $2 AND tick >= tick()});
65 $query->execute($planet,$type);
67 if (my $scan = $query->fetchrow_hashref){
68 $c->reply("scan already exist: "
69 . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
71 my $req = $c->model->prepare(q{
72 SELECT * FROM scan_requests
74 AND pid = $2 AND type = $3 AND NOT sent
76 $req->execute($c->uid,$planet,$type);
79 if(my $scan = $req->fetchrow_hashref){
80 $req = $c->model->prepare(q{
81 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
84 $req->execute($c->nick,$scan->{id});
87 $req = $c->model->prepare(q{
88 INSERT INTO scan_requests (uid,nick,pid,type)
89 VALUES($1,$2,$3,$4) RETURNING (id)
91 $req->execute($c->uid,$c->nick,$planet,$type);
96 $c->message(privmsg => $c->disp->targets->{scan}
97 ,"<b>$id</b> http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
98 . " ($x:$y:$z $type) | <".$c->nick."> $msg"
100 $c->reply("sent request ($x:$y:$z $type)");
102 $c->reply("something went wrong..");
110 %scantypes = map {$_ => $i++} @scantypes;
114 : 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.)
117 my ($self, $c, $msg) = @_;
120 if ($msg =~ /^-([pdunja]+)$/){
122 push @notype, $scantypes[$scanid{$_}-1];
128 my $reqs = $c->model->prepare(q{
129 SELECT min(sr.id) AS id, x,y,z,type
130 FROM scan_requests sr
131 JOIN current_planet_stats p USING (pid)
132 WHERE sr.time > NOW() - '30 min'::INTERVAL
133 AND NOT EXISTS (SELECT scan_id FROM scans
142 $reqs->execute(\@notype);
144 while (my $req = $reqs->fetchrow_hashref){
145 $text .= "<b>$req->{id}</b> http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
148 $c->reply($text || 'No unhandled requests.');
152 : 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.)
156 my ($self, $c,$msg) = @_;
158 my ($t, $x, $y, $z, $tick)
159 = $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/ or die 'ARGS';
160 my $type =$scantypes[$scanid{$t}-1];
162 my $scan = $c->model->selectrow_hashref(q{
163 SELECT id, scan_id, tick FROM scans
164 WHERE type = $1 AND pid = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
165 ORDER BY tick DESC LIMIT 1
166 },undef,$type,$x,$y,$z,$tick);
167 unless ($scan->{id}){
168 $c->reply("No $type scan for $x:$y:$z. Planet might not exist.");
174 my $fid = $c->model->selectrow_array(q{
175 SELECT fid FROM fleet_scans WHERE id = $1
176 },undef,$scan->{id});
177 my $ships = $c->model->prepare(q{
178 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
180 $ships->execute($fid);
181 while (my $ship = $ships->fetchrow_hashref){
182 $text .= " $ship->{ship} $ship->{amount} |"
186 $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
189 $c->reply("<b>$type</b> scan of $x:$y:$z "
190 . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
194 : Help(syntax: .anon nick message)
198 my ($self, $c, $msg) = @_;
199 my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
201 $c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
202 .$c->channel.")</c>");
203 $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
207 : Help(syntax: .addscan list of scans)
209 my ($self, $c, $msg) = @_;
212 my $oldscan = $dbh->prepare(q{
213 SELECT scan_id FROM scans
214 WHERE groupscan = $1 AND scan_id = LOWER($2) AND tick >= tick() - 168
216 my $addscan = $dbh->prepare(q{
217 INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER($1),tick(),$2,COALESCE($3,-1))
219 my $user = $dbh->selectrow_hashref(q{SELECT uid,username, scan_points, tick()
220 FROM users WHERE uid = ? },undef,$c->uid);
225 while ($msg =~ m{/.+?scan(_id|_grp)?=(\w+)}g){
227 my $group = ($1 ~~ /_grp/ || 0);
228 unless($dbh->selectrow_array($oldscan,undef,$group,$id)){
229 $addscan->execute($id,$group,$user->{uid});
237 if ($user->{uid} && ($scans || $groupscans)){
238 $dbh->do(q{UPDATE users SET scan_points = scan_points + $2 WHERE uid = $1}
239 ,undef,$user->{uid},$scans);
240 my $points = $user->{scan_points} + $scans;
241 $c->reply("Added $scans scans and $groupscans groupscans, $points in total.");