]> ruin.nu Git - NDIRC.git/blob - Scans.pm
Converted the .p command
[NDIRC.git] / Scans.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 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 package NDIRC::Scans;
20 use strict;
21 use warnings;
22 use ND::DB;
23 use NDIRC::Access;
24 require Exporter;
25
26 our @ISA = qw/Exporter/;
27
28 our @EXPORT = qw/addScan addScanGroup sendScan reqScan scanReqs getScan/;
29
30 sub addScan {
31         my ($id,$verbose) = @_;
32         if (1){
33                 unless ($ND::DBH->selectrow_array(q{SELECT scan_id FROM scans WHERE NOT groupscan AND scan_id = LOWER(?) AND tick >= tick() - 168},undef,$id)){
34                         my @user = $ND::DBH->selectrow_array(q{SELECT uid,username, scan_points, tick() 
35                                 FROM users WHERE hostmask ILIKE ? },undef,$ND::address);
36                         if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,uid) VALUES (LOWER(?),tick(),COALESCE(?,-1))},
37                                         undef,$id,$user[0]) == 1){
38                                 if (@user){
39                                         $ND::DBH->do('UPDATE users SET scan_points = scan_points + 1 WHERE uid = ? ',undef,$user[0]);
40                                         $user[2] += 1;
41                                         $ND::server->command("notice $ND::nick Added scan, points now $user[2]");
42                                 }elsif ($verbose){
43                                         $ND::server->command("msg $ND::target Added scan, but unknown user, no points");
44                                 }
45                         }
46                 }elsif ($verbose){
47                         $ND::server->command("msg $ND::target a scan with that id has already been added within the last 48 ticks");
48                 }
49         }
50 }
51 sub addScanGroup {
52         my ($id,$verbose) = @_;
53         if (1){
54                 unless ($ND::DBH->selectrow_array(q{SELECT scan_id FROM scans WHERE groupscan AND  scan_id = LOWER(?) AND tick >= tick() - 168},undef,$id)){
55                         my @user = $ND::DBH->selectrow_array(q{SELECT uid,username, scan_points, tick() 
56                                 FROM users WHERE hostmask ILIKE ? },undef,$ND::address);
57                         if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER(?),tick(),true,COALESCE(?,-1))},
58                                         undef,$id,$user[0]) == 1){
59                                 if (@user){
60                                         $ND::server->command("notice $ND::nick Added scan group. Points will be added after parsing.");
61                                 }elsif ($verbose){
62                                         $ND::server->command("msg $ND::target Added scan, but unknown user, no points");
63                                 }
64                         }
65                 }elsif ($verbose){
66                         $ND::server->command("msg $ND::target a scan with that id has already been added within the last 48 ticks");
67                 }
68         }
69 }
70
71 sub sendScan {
72         my ($msg, $command) = @_;
73         my ($target,$mess);
74         if (defined $msg && $msg =~ /^(\S+) (.*)$/){
75                 $target = $1;
76                 $mess = $2;
77         }else{
78                 $ND::server->command("notice $ND::nick syntax: $command nick message");
79                 return;
80         }
81         if (scanner()){
82                 $ND::server->command("msg $target ".chr(2).$mess.chr(3)."4 (reply with /msg $ND::scanchan)");
83                 $ND::server->command("msg $ND::target ${ND::C}3$target << $mess");
84         }
85 }
86
87 our %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
88
89 our @scantypes = ('Planet','Landing', 'Development'
90         ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
91
92 sub reqScan {
93         my ($msg, $command) = @_;
94
95         my ($x, $y, $z, $type, $typeid);
96         if (defined $msg && $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/){
97                 $typeid = $scanid{$1};
98                 $type = $scantypes[$typeid-1];
99                 ($x,$y,$z) = ($2,$3,$4);
100                 $msg = $5;
101         }else{
102                 $ND::server->command("notice $ND::nick syntax: $command type X:Y:Z message"
103                         . " | 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");
104                 return;
105         }
106
107         if (my $user = member){
108
109                 my $planet = $ND::DBH->selectrow_array(q{SELECT planetid($1,$2,$3,$4)}
110                         ,undef,$x,$y,$z,$ND::tick);
111
112                 my $query = $ND::DBH->prepare(q{SELECT scan_id
113                         FROM scans
114                         WHERE planet = $1 AND type = $2 AND tick >= $3});
115                 $query->execute($planet,$type,$ND::tick);
116
117                 if (my $scan = $query->fetchrow_hashref){
118                         $ND::server->command("notice $ND::nick scan already exist: "
119                                 . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
120                 }else{
121                         my $req = $ND::DBH->prepare(q{SELECT * FROM scan_requests
122                                 WHERE uid = $1 AND planet = $2
123                                         AND type = $3 AND NOT sent
124                         });
125                         $req->execute($user->{uid},$planet,$type);
126
127                         my $id;
128                         if(my $scan = $req->fetchrow_hashref){
129                                 $req = $ND::DBH->prepare(q{UPDATE scan_requests
130                                         SET nick = $1, tick = tick(), time = NOW()
131                                         WHERE id = $2});
132                                 $req->execute($ND::nick,$scan->{id});
133                                 $id = $scan->{id};
134                         }else{
135                                 $req = $ND::DBH->prepare(q{INSERT INTO scan_requests
136                                         (uid,nick,planet,type) VALUES($1,$2,$3,$4)
137                                         RETURNING (id)});
138                                 $req->execute($user->{uid},$ND::nick,$planet,$type);
139                                 $id = $req->fetchrow;
140                         }
141
142                         if ($id){
143                                 $ND::server->command("msg $ND::scanchan "
144                                         . "$ND::B$id$ND::O http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
145                                         . " ($x:$y:$z $type) | <$ND::nick> $msg"
146                                 );
147                                 $ND::server->command("notice $ND::nick sent request ($x:$y:$z $type)");
148                         }else{
149                                 $ND::server->command("notice $ND::nick something went wrong..");
150                         }
151
152                 }
153         }
154 }
155
156 our %scantypes;
157 {
158         my $i = 1;
159         %scantypes = map {$_ => $i++} @scantypes;
160 }
161
162 sub scanReqs {
163         my ($msg, $command) = @_;
164
165         if (scanner()){
166
167                 my @notype;
168                 if ($msg && $msg =~ /-([pdunja]+)/){
169                         for (split //, $1){
170                                 push @notype, $scantypes[$scanid{$_}-1];
171                         }
172                 }
173
174                 my $reqs = $ND::DBH->prepare(q{SELECT min(sr.id) AS id, x,y,z,type
175                         FROM scan_requests sr
176                                 JOIN current_planet_stats p ON p.id = sr.planet
177                         WHERE sr.time > NOW() - '30 min'::INTERVAL
178                                 AND NOT EXISTS (SELECT scan_id FROM scans
179                                         WHERE planet = sr.planet
180                                                 AND type = sr.type
181                                                 AND tick >= sr.tick
182                                 )
183                                 AND type <> ALL($1)
184                         GROUP BY x,y,z,type
185                         ORDER BY id
186                         });
187                 $reqs->execute(\@notype);
188                 my $text = '';
189                 while (my $req = $reqs->fetchrow_hashref){
190                         $text .= "$ND::B$req->{id}$ND::O http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
191                 }
192
193                 $text = 'No unhandled requests.' unless $text;
194                 $ND::server->command("notice $ND::nick $text");
195         }
196 }
197
198 sub getScan {
199         my ($msg, $command) = @_;
200
201         my ($t, $x, $y, $z, $type, $tick);
202         if (defined $msg && $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/){
203                 $t = $1;
204                 $type =$scantypes[$scanid{$1}-1];
205                 ($x,$y,$z) = ($2,$3,$4);
206                 $tick = $5;
207         }else{
208                 $ND::server->command("notice $ND::nick syntax: $command type X:Y:Z [tick]"
209                         . " | type is the first character in the scan name, like p for planet scan."
210                         . " Specify a tick if you want scans older than the specified tick."
211                 );
212                 return;
213         }
214
215         if (my $user = member){
216                 my $scan = $ND::DBH->selectrow_hashref(q{
217 SELECT id, scan_id, tick FROM scans
218 WHERE type = $1 AND planet = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
219 ORDER BY tick DESC LIMIT 1
220                         },undef,$type,$x,$y,$z,$tick);
221                 unless ($scan->{id}){
222                         $ND::server->command("notice $ND::nick No such scan");
223                 }
224                 my $text = '|';
225                 if ($t =~ /[uja]/){
226                         my $fid = $ND::DBH->selectrow_array(q{
227 SELECT fid FROM fleet_scans WHERE id = $1
228                                 },undef,$scan->{id});
229                         my $ships = $ND::DBH->prepare(q{
230 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
231                                 });
232                         $ships->execute($fid);
233                         while (my $ship = $ships->fetchrow_hashref){
234                                 $text .= " $ship->{ship} $ship->{amount} |"
235                         }
236                 }else{
237                         $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
238                 }
239                 $ND::server->command("notice $ND::nick $ND::B$type$ND::O scan of $x:$y:$z "
240                         . "($scan->{scan_id} pt: $ND::B$scan->{tick}$ND::O) $text");
241         }
242 }
243
244
245 1;