]> ruin.nu Git - NDIRC.git/blob - Commands/Scans.pm
Converted Scans
[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 use strict;
21 use warnings;
22 use feature ':5.10';
23
24 use MooseX::Declare;
25 use NDIRC::Dispatcher;
26
27 command getscans => {
28         help => q(syntax: .getscans X:Y:Z),
29         type => q(pm),
30 }, class extends NDIRC::Command {
31         method execute($c,$args) {
32                 my ($x,$y,$z) = $args =~ /(\d+)\D+(\d+)\D+(\d+)/ or die 'ARGS';
33                 $c->reply("https://nd.ruin.nu/stats/find/$x:$y:$z");
34         }
35 };
36
37 my %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
38
39 my @scantypes = ('Planet','Landing', 'Development'
40         ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
41
42 command gs => {
43         help => q(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),
44         acl => q(irc_gs),
45         type => q(pm),
46 }, class extends NDIRC::Command {
47         method execute($c,$args) {
48                 my ($typeid, $x, $y, $z, $msg) = $args =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/
49                         or die 'ARGS';
50                 $typeid = $scanid{$typeid};
51                 my $type = $scantypes[$typeid-1];
52
53                 my ($planet,$dists,$tick) = $c->model->selectrow_array(q{
54 SELECT pid,distorters,tick
55 FROM current_planet_stats LEFT JOIN current_development_scans USING (pid)
56 WHERE x = $1 AND y = $2 AND z = $3
57                         },undef,$x,$y,$z);
58                 unless ($planet){
59                         $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
60                         return;
61                 }
62
63                 my $query = $c->model->prepare(q{
64 SELECT scan_id FROM scans
65 WHERE pid = $1 AND type = $2 AND tick >= tick()
66                         });
67                 $query->execute($planet,$type);
68
69                 if (my $scan = $query->fetchrow_hashref){
70                         $c->reply("scan already exist: "
71                                 . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
72                 }else{
73                         my $req = $c->model->prepare(q{
74 SELECT * FROM scan_requests
75 WHERE uid = $1
76         AND pid = $2 AND type = $3 AND NOT sent
77                                 });
78                         $req->execute($c->uid,$planet,$type);
79
80                         my $id;
81                         if(my $scan = $req->fetchrow_hashref){
82                                 $req = $c->model->prepare(q{
83 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
84 WHERE id = $2
85                                         });
86                                 $req->execute($c->nick,$scan->{id});
87                                 $id = $scan->{id};
88                         }else{
89                                 $req = $c->model->prepare(q{
90 INSERT INTO scan_requests (uid,nick,pid,type)
91 VALUES($1,$2,$3,$4) RETURNING (id)
92                                         });
93                                 $req->execute($c->uid,$c->nick,$planet,$type);
94                                 $id = $req->fetchrow;
95                         }
96
97                         if ($id){
98                                 if (defined $dists){
99                                         $dists = "$dists dists PT$tick"
100                                 }else{
101                                         $dists = "DISTS UNKNOWN, ADD DEVSCAN"
102                                 }
103                                 $c->message(privmsg => $c->disp->targets->{scan}
104                                         ,"<b>$id</b> http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
105                                         . " ($x:$y:$z $type - $dists) | <".$c->nick."> $msg"
106                                 );
107                                 $c->reply("sent request ($x:$y:$z $type)");
108                         }else{
109                                 $c->reply("something went wrong..");
110                         }
111                 }
112         }
113 };
114
115 my %scantypes;
116 {
117         my $i = 1;
118         %scantypes = map {$_ => $i++} @scantypes;
119 }
120
121 command scanreqs => {
122         help => q(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.),
123         acl => q(irc_scanreqs),
124 }, class extends NDIRC::Command {
125         method execute($c,$msg) {
126                 my @notype;
127                 if ($msg =~ /^-([pdunja]+)$/){
128                         for (split //, $1){
129                                 push @notype, $scantypes[$scanid{$_}-1];
130                         }
131                 }elsif($msg){
132                         die 'ARGS';
133                 }
134
135                 my $reqs = $c->model->prepare(q{
136 SELECT min(sr.id) AS id, x,y,z,type
137 FROM scan_requests sr
138         JOIN current_planet_stats p USING (pid)
139 WHERE sr.time > NOW() - '30 min'::INTERVAL
140         AND NOT EXISTS (SELECT scan_id FROM scans
141                 WHERE pid = sr.pid
142                         AND type = sr.type
143                         AND tick >= sr.tick
144         )
145         AND type <> ALL($1)
146 GROUP BY x,y,z,type
147 ORDER BY id
148                         });
149                 $reqs->execute(\@notype);
150                 my $text = '';
151                 while (my $req = $reqs->fetchrow_hashref){
152                         $text .= "<b>$req->{id}</b> http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
153                 }
154
155                 $c->reply($text || 'No unhandled requests.');
156         }
157 };
158
159 command scan => {
160         help => q(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.),
161         acl => q(irc_scan),
162         type => q(pm),
163 }, class extends NDIRC::Command {
164         method execute($c,$msg) {
165                 my ($t, $x, $y, $z, $tick)
166                 = $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/ or die 'ARGS';
167                 my $type =$scantypes[$scanid{$t}-1];
168
169                 my $scan = $c->model->selectrow_hashref(q{
170 SELECT id, scan_id, tick FROM scans
171 WHERE type = $1 AND pid = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
172 ORDER BY tick DESC LIMIT 1
173                         },undef,$type,$x,$y,$z,$tick);
174                 unless ($scan->{id}){
175                         $c->reply("No $type scan for $x:$y:$z. Planet might not exist.");
176                         return;
177                 }
178                 my $text = '';
179                 given ($t){
180                         when(/[ua]/){
181                                 my $fid = $c->model->selectrow_array(q{
182 SELECT fid FROM fleet_scans WHERE id = $1
183                                         },undef,$scan->{id});
184                                 my $ships = $c->model->prepare(q{
185 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
186                                         });
187                                 $ships->execute($fid);
188                                 while (my $ship = $ships->fetchrow_hashref){
189                                         $text .= " $ship->{ship} $ship->{amount} |"
190                                 }
191                         }
192                         default {
193                                 $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
194                         }
195                 }
196                 $c->reply("<b>$type</b> scan of $x:$y:$z "
197                         . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
198         }
199 };
200
201 command anon => {
202         help => q(syntax: .anon nick message),
203         type => q(scan),
204         acl => q(irc_anonscan),
205 }, class extends NDIRC::Command {
206         method execute($c,$msg) {
207                 my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
208
209                 $c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
210                         .$c->channel.")</c>");
211                 $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
212         }
213 };
214
215 command addscan => {
216         help => q(syntax: .addscan list of scans),
217 }, class extends NDIRC::Command {
218         method execute($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
261 1;