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,$dists,$tick) = $c->model->selectrow_array(q{
56 SELECT pid,distorters,tick
57 FROM current_planet_stats LEFT JOIN current_development_scans USING (pid)
58 WHERE x = $1 AND y = $2 AND z = $3
61 $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
65 my $query = $c->model->prepare(q{SELECT scan_id
67 WHERE pid = $1 AND type = $2 AND tick >= tick()});
68 $query->execute($planet,$type);
70 if (my $scan = $query->fetchrow_hashref){
71 $c->reply("scan already exist: "
72 . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
74 my $req = $c->model->prepare(q{
75 SELECT * FROM scan_requests
77 AND pid = $2 AND type = $3 AND NOT sent
79 $req->execute($c->uid,$planet,$type);
82 if(my $scan = $req->fetchrow_hashref){
83 $req = $c->model->prepare(q{
84 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
87 $req->execute($c->nick,$scan->{id});
90 $req = $c->model->prepare(q{
91 INSERT INTO scan_requests (uid,nick,pid,type)
92 VALUES($1,$2,$3,$4) RETURNING (id)
94 $req->execute($c->uid,$c->nick,$planet,$type);
101 $dists = "$dists dists PT$tick"
103 $dists = "DISTS UNKNOWN, ADD DEVSCAN"
105 $c->message(privmsg => $c->disp->targets->{scan}
106 ,"<b>$id</b> http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
107 . " ($x:$y:$z $type - $dists) | <".$c->nick."> $msg"
109 $c->reply("sent request ($x:$y:$z $type)");
111 $c->reply("something went wrong..");
119 %scantypes = map {$_ => $i++} @scantypes;
123 : 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.)
126 my ($self, $c, $msg) = @_;
129 if ($msg =~ /^-([pdunja]+)$/){
131 push @notype, $scantypes[$scanid{$_}-1];
137 my $reqs = $c->model->prepare(q{
138 SELECT min(sr.id) AS id, x,y,z,type
139 FROM scan_requests sr
140 JOIN current_planet_stats p USING (pid)
141 WHERE sr.time > NOW() - '30 min'::INTERVAL
142 AND NOT EXISTS (SELECT scan_id FROM scans
151 $reqs->execute(\@notype);
153 while (my $req = $reqs->fetchrow_hashref){
154 $text .= "<b>$req->{id}</b> http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
157 $c->reply($text || 'No unhandled requests.');
161 : 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.)
165 my ($self, $c,$msg) = @_;
167 my ($t, $x, $y, $z, $tick)
168 = $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/ or die 'ARGS';
169 my $type =$scantypes[$scanid{$t}-1];
171 my $scan = $c->model->selectrow_hashref(q{
172 SELECT id, scan_id, tick FROM scans
173 WHERE type = $1 AND pid = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
174 ORDER BY tick DESC LIMIT 1
175 },undef,$type,$x,$y,$z,$tick);
176 unless ($scan->{id}){
177 $c->reply("No $type scan for $x:$y:$z. Planet might not exist.");
183 my $fid = $c->model->selectrow_array(q{
184 SELECT fid FROM fleet_scans WHERE id = $1
185 },undef,$scan->{id});
186 my $ships = $c->model->prepare(q{
187 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
189 $ships->execute($fid);
190 while (my $ship = $ships->fetchrow_hashref){
191 $text .= " $ship->{ship} $ship->{amount} |"
195 $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
198 $c->reply("<b>$type</b> scan of $x:$y:$z "
199 . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
203 : Help(syntax: .anon nick message)
207 my ($self, $c, $msg) = @_;
208 my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
210 $c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
211 .$c->channel.")</c>");
212 $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
216 : Help(syntax: .addscan list of scans)
218 my ($self, $c, $msg) = @_;
221 my $oldscan = $dbh->prepare(q{
222 SELECT scan_id FROM scans
223 WHERE groupscan = $1 AND scan_id = LOWER($2) AND tick >= tick() - 168
225 my $addscan = $dbh->prepare(q{
226 INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER($1),tick(),$2,COALESCE($3,-1))
228 my $user = $dbh->selectrow_hashref(q{SELECT uid,username, scan_points, tick()
229 FROM users WHERE uid = ? },undef,$c->uid);
234 while ($msg =~ m{/.+?scan(_id|_grp)?=(\w+)}g){
236 my $group = ($1 ~~ /_grp/ || 0);
237 unless($dbh->selectrow_array($oldscan,undef,$group,$id)){
238 $addscan->execute($id,$group,$user->{uid});
246 if ($user->{uid} && ($scans || $groupscans)){
247 $dbh->do(q{UPDATE users SET scan_points = scan_points + $2 WHERE uid = $1}
248 ,undef,$user->{uid},$scans);
249 my $points = $user->{scan_points} + $scans;
250 $c->reply("Added $scans scans and $groupscans groupscans, $points in total.");