1 #**************************************************************************
2 # Copyright (C) 2006 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 #**************************************************************************/
26 our @ISA = qw/Exporter/;
28 our @EXPORT = qw/addScan addScanGroup sendScan reqScan scanReqs getScan/;
31 my ($id,$verbose) = @_;
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){
39 $ND::DBH->do('UPDATE users SET scan_points = scan_points + 1 WHERE uid = ? ',undef,$user[0]);
41 $ND::server->command("notice $ND::nick Added scan, points now $user[2]");
43 $ND::server->command("msg $ND::target Added scan, but unknown user, no points");
47 $ND::server->command("msg $ND::target a scan with that id has already been added within the last 48 ticks");
52 my ($id,$verbose) = @_;
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){
60 $ND::server->command("notice $ND::nick Added scan group. Points will be added after parsing.");
62 $ND::server->command("msg $ND::target Added scan, but unknown user, no points");
66 $ND::server->command("msg $ND::target a scan with that id has already been added within the last 48 ticks");
72 my ($msg, $command) = @_;
74 if (defined $msg && $msg =~ /^(\S+) (.*)$/){
78 $ND::server->command("notice $ND::nick syntax: $command nick message");
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");
91 %scantypes = map {$_ => $i++} @scantypes;
95 my ($msg, $command) = @_;
100 if ($msg && $msg =~ /-([pdunja]+)/){
102 push @notype, $scantypes[$scanid{$_}-1];
106 my $reqs = $ND::DBH->prepare(q{SELECT min(sr.id) AS id, x,y,z,type
107 FROM scan_requests sr
108 JOIN current_planet_stats p ON p.id = sr.planet
109 WHERE sr.time > NOW() - '30 min'::INTERVAL
110 AND NOT EXISTS (SELECT scan_id FROM scans
111 WHERE planet = sr.planet
119 $reqs->execute(\@notype);
121 while (my $req = $reqs->fetchrow_hashref){
122 $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} "
125 $text = 'No unhandled requests.' unless $text;
126 $ND::server->command("notice $ND::nick $text");
131 my ($msg, $command) = @_;
133 my ($t, $x, $y, $z, $type, $tick);
134 if (defined $msg && $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/){
136 $type =$scantypes[$scanid{$1}-1];
137 ($x,$y,$z) = ($2,$3,$4);
140 $ND::server->command("notice $ND::nick syntax: $command type X:Y:Z [tick]"
141 . " | type is the first character in the scan name, like p for planet scan."
142 . " Specify a tick if you want scans older than the specified tick."
147 if (my $user = member){
148 my $scan = $ND::DBH->selectrow_hashref(q{
149 SELECT id, scan_id, tick FROM scans
150 WHERE type = $1 AND planet = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
151 ORDER BY tick DESC LIMIT 1
152 },undef,$type,$x,$y,$z,$tick);
153 unless ($scan->{id}){
154 $ND::server->command("notice $ND::nick No such scan");
158 my $fid = $ND::DBH->selectrow_array(q{
159 SELECT fid FROM fleet_scans WHERE id = $1
160 },undef,$scan->{id});
161 my $ships = $ND::DBH->prepare(q{
162 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
164 $ships->execute($fid);
165 while (my $ship = $ships->fetchrow_hashref){
166 $text .= " $ship->{ship} $ship->{amount} |"
169 $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
171 $ND::server->command("notice $ND::nick $ND::B$type$ND::O scan of $x:$y:$z "
172 . "($scan->{scan_id} pt: $ND::B$scan->{tick}$ND::O) $text");