]> ruin.nu Git - NDIRC.git/blob - Commands/Scans.pm
Removed the global variables for special chans, using targets hashref in Dispatcher...
[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 = $c->model->selectrow_array(q{SELECT planetid($1,$2,$3,tick())}
56                 ,undef,$x,$y,$z);
57         unless ($planet){
58                 $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
59                 return;
60         }
61
62         my $query = $c->model->prepare(q{SELECT scan_id
63                 FROM scans
64                 WHERE pid = $1 AND type = $2 AND tick >= tick()});
65         $query->execute($planet,$type);
66
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}");
70         }else{
71                 my $req = $c->model->prepare(q{
72 SELECT * FROM scan_requests
73 WHERE uid = (SELECT uid FROM users WHERE hostmask ILIKE $1)
74         AND pid = $2 AND type = $3 AND NOT sent
75                 });
76                 $req->execute($c->host,$planet,$type);
77
78                 my $id;
79                 if(my $scan = $req->fetchrow_hashref){
80                         $req = $c->model->prepare(q{
81 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
82 WHERE id = $2
83                         });
84                         $req->execute($c->nick,$scan->{id});
85                         $id = $scan->{id};
86                 }else{
87                         $req = $c->model->prepare(q{
88 INSERT INTO scan_requests (uid,nick,pid,type)
89 VALUES((SELECT uid FROM users WHERE hostmask ILIKE $1),$2,$3,$4) RETURNING (id)
90                         });
91                         $req->execute($c->host,$c->nick,$planet,$type);
92                         $id = $req->fetchrow;
93                 }
94
95                 if ($id){
96                         $c->message(privmsg =>$c->disp->targets->{scans}
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"
99                         );
100                         $c->reply("sent request ($x:$y:$z $type)");
101                 }else{
102                         $c->reply("something went wrong..");
103                 }
104         }
105 }
106
107 my %scantypes;
108 {
109         my $i = 1;
110         %scantypes = map {$_ => $i++} @scantypes;
111 }
112
113 sub scanreqs
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.)
115         : ACL(irc_scanreqs)
116 {
117         my ($self, $c, $msg) = @_;
118
119         my @notype;
120         if ($msg =~ /^-([pdunja]+)$/){
121                 for (split //, $1){
122                         push @notype, $scantypes[$scanid{$_}-1];
123                 }
124         }elsif($msg){
125                 die 'ARGS';
126         }
127
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
134                 WHERE pid = sr.pid
135                         AND type = sr.type
136                         AND tick >= sr.tick
137         )
138         AND type <> ALL($1)
139 GROUP BY x,y,z,type
140 ORDER BY id
141                 });
142         $reqs->execute(\@notype);
143         my $text = '';
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} "
146         }
147
148         $c->reply($text || 'No unhandled requests.');
149 }
150
151 sub scan
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.)
153         : ACL(irc_scan)
154         : Type(pm)
155 {
156         my ($self, $c,$msg) = @_;
157
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];
161
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.");
169                 return;
170         }
171         my $text = '';
172         given ($t){
173                 when(/[ua]/){
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
179                         });
180                         $ships->execute($fid);
181                         while (my $ship = $ships->fetchrow_hashref){
182                                 $text .= " $ship->{ship} $ship->{amount} |"
183                         }
184                 }
185                 default {
186                         $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
187                 }
188         }
189         $c->reply("<b>$type</b> scan of $x:$y:$z "
190                 . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
191 }
192
193 sub anon
194         : Help(syntax: .anon nick message)
195         : Type(scan)
196         : ACL(irc_anonscan)
197 {
198         my ($self, $c, $msg) = @_;
199         my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
200
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>");
204 }
205
206 sub addscan
207         : Help(syntax: .addscan list of scans)
208 {
209         my ($self, $c, $msg) = @_;
210         my $dbh = $c->model;
211
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
215                 });
216         my $addscan = $dbh->prepare(q{
217 INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER($1),tick(),$2,COALESCE($3,-1))
218                 });
219         my $user = $dbh->selectrow_hashref(q{SELECT uid,username, scan_points, tick()
220                 FROM users WHERE hostmask ILIKE ? },undef,$c->host);
221         my $groupscans = 0;
222         my $scans = 0;
223         eval {
224                 $dbh->begin_work;
225                 while ($msg =~ m{/.+?scan(_id|_grp)?=(\w+)}g){
226                         my $id = $2;
227                         my $group = ($1 ~~ /_grp/ || 0);
228                         unless($dbh->selectrow_array($oldscan,undef,$group,$id)){
229                                 $addscan->execute($id,$group,$user->{uid});
230                                 if ($group){
231                                         $groupscans++;
232                                 }else{
233                                         $scans++;
234                                 }
235                         }
236                 }
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.");
242                 }
243                 $dbh->commit;
244         };
245         if ($@){
246                 $dbh->rollback;
247                 die $@;
248         }
249 }
250
251 1;