]> ruin.nu Git - NDIRC.git/blob - Commands/Scans.pm
06ae96f960e93bfd352d1ee0cf142a9ef8353c03
[NDIRC.git] / Commands / Scans.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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 #**************************************************************************/
19
20 package NDIRC::Commands::Scans;
21
22 use strict;
23 use warnings;
24 use feature ':5.10';
25
26 use Moose;
27 use MooseX::MethodAttributes;
28
29 sub getscans
30         : Help(syntax: .getscans X:Y:Z)
31         : Type(pm)
32 {
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");
36 }
37
38 my %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
39
40 my @scantypes = ('Planet','Landing', 'Development'
41         ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
42
43 sub gs
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)
45         : ACL(irc_gs)
46         : Type(pm)
47 {
48         my ($self,$c,$args) = @_;
49
50         my ($typeid, $x, $y, $z, $msg) = $args =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/
51                 or die 'ARGS';
52         $typeid = $scanid{$typeid};
53         my $type = $scantypes[$typeid-1];
54
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
59                 },undef,$x,$y,$z);
60         unless ($planet){
61                 $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
62                 return;
63         }
64
65         my $query = $c->model->prepare(q{SELECT scan_id
66                 FROM scans
67                 WHERE pid = $1 AND type = $2 AND tick >= tick()});
68         $query->execute($planet,$type);
69
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}");
73         }else{
74                 my $req = $c->model->prepare(q{
75 SELECT * FROM scan_requests
76 WHERE uid = $1
77         AND pid = $2 AND type = $3 AND NOT sent
78                 });
79                 $req->execute($c->uid,$planet,$type);
80
81                 my $id;
82                 if(my $scan = $req->fetchrow_hashref){
83                         $req = $c->model->prepare(q{
84 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
85 WHERE id = $2
86                         });
87                         $req->execute($c->nick,$scan->{id});
88                         $id = $scan->{id};
89                 }else{
90                         $req = $c->model->prepare(q{
91 INSERT INTO scan_requests (uid,nick,pid,type)
92 VALUES($1,$2,$3,$4) RETURNING (id)
93                         });
94                         $req->execute($c->uid,$c->nick,$planet,$type);
95                         $id = $req->fetchrow;
96                 }
97
98                 if ($id){
99
100                         if (defined $dists){
101                                 $dists = "$dists dists PT$tick"
102                         }else{
103                                 $dists = "DISTS UNKNOWN, ADD DEVSCAN"
104                         }
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"
108                         );
109                         $c->reply("sent request ($x:$y:$z $type)");
110                 }else{
111                         $c->reply("something went wrong..");
112                 }
113         }
114 }
115
116 my %scantypes;
117 {
118         my $i = 1;
119         %scantypes = map {$_ => $i++} @scantypes;
120 }
121
122 sub scanreqs
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.)
124         : ACL(irc_scanreqs)
125 {
126         my ($self, $c, $msg) = @_;
127
128         my @notype;
129         if ($msg =~ /^-([pdunja]+)$/){
130                 for (split //, $1){
131                         push @notype, $scantypes[$scanid{$_}-1];
132                 }
133         }elsif($msg){
134                 die 'ARGS';
135         }
136
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
143                 WHERE pid = sr.pid
144                         AND type = sr.type
145                         AND tick >= sr.tick
146         )
147         AND type <> ALL($1)
148 GROUP BY x,y,z,type
149 ORDER BY id
150                 });
151         $reqs->execute(\@notype);
152         my $text = '';
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} "
155         }
156
157         $c->reply($text || 'No unhandled requests.');
158 }
159
160 sub scan
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.)
162         : ACL(irc_scan)
163         : Type(pm)
164 {
165         my ($self, $c,$msg) = @_;
166
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];
170
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.");
178                 return;
179         }
180         my $text = '';
181         given ($t){
182                 when(/[ua]/){
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
188                         });
189                         $ships->execute($fid);
190                         while (my $ship = $ships->fetchrow_hashref){
191                                 $text .= " $ship->{ship} $ship->{amount} |"
192                         }
193                 }
194                 default {
195                         $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
196                 }
197         }
198         $c->reply("<b>$type</b> scan of $x:$y:$z "
199                 . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
200 }
201
202 sub anon
203         : Help(syntax: .anon nick message)
204         : Type(scan)
205         : ACL(irc_anonscan)
206 {
207         my ($self, $c, $msg) = @_;
208         my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
209
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>");
213 }
214
215 sub addscan
216         : Help(syntax: .addscan list of scans)
217 {
218         my ($self, $c, $msg) = @_;
219         my $dbh = $c->model;
220
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
224                 });
225         my $addscan = $dbh->prepare(q{
226 INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER($1),tick(),$2,COALESCE($3,-1))
227                 });
228         my $user = $dbh->selectrow_hashref(q{SELECT uid,username, scan_points, tick()
229                 FROM users WHERE uid = ? },undef,$c->uid);
230         my $groupscans = 0;
231         my $scans = 0;
232         eval {
233                 $dbh->begin_work;
234                 while ($msg =~ m{/.+?scan(_id|_grp)?=(\w+)}g){
235                         my $id = $2;
236                         my $group = ($1 ~~ /_grp/ || 0);
237                         unless($dbh->selectrow_array($oldscan,undef,$group,$id)){
238                                 $addscan->execute($id,$group,$user->{uid});
239                                 if ($group){
240                                         $groupscans++;
241                                 }else{
242                                         $scans++;
243                                 }
244                         }
245                 }
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.");
251                 }
252                 $dbh->commit;
253         };
254         if ($@){
255                 $dbh->rollback;
256                 die $@;
257         }
258 }
259
260 1;