]> ruin.nu Git - NDIRC.git/blob - Scans.pm
Allow min claim and forum dates for laston
[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/;
29
30 sub addScan {
31         my ($id,$verbose, $quiet) = @_;
32         if (1){
33                 unless ($ND::DBH->selectrow_array(q{SELECT scan_id FROM scans WHERE NOT groupscan AND scan_id = ? 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 (?,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                                         if ($quiet){
42                                                 $ND::server->command("msg $ND::target Added scan, points now $user[2]");
43                                         }
44                                         else{
45                                                 $ND::server->command("msg $ND::target Added scan, at tick $user[3]. $user[1] points now $user[2]");
46                                         }
47                                 }elsif ($verbose){
48                                         $ND::server->command("msg $ND::target Added scan, but unknown user, no points");
49                                 }
50                         }
51                 }elsif ($verbose){
52                         $ND::server->command("msg $ND::target a scan with that id has already been added within the last 48 ticks");
53                 }
54         }
55 }
56 sub addScanGroup {
57         my ($id,$verbose) = @_;
58         if (1){
59                 unless ($ND::DBH->selectrow_array("SELECT scan_id FROM scans WHERE groupscan AND  scan_id = ? AND tick >= tick() - 168",undef,$id)){
60                         my @user = $ND::DBH->selectrow_array(q{SELECT uid,username, scan_points, tick() 
61                                 FROM users WHERE hostmask ILIKE ? },undef,$ND::address);
62                         if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (?,tick(),true,COALESCE(?,-1))},
63                                         undef,$id,$user[0]) == 1){
64                                 if (@user){
65                                         $ND::server->command("msg $ND::target Added scan group, at tick $user[3]. Points will be added after parsing");
66                                 }elsif ($verbose){
67                                         $ND::server->command("msg $ND::target Added scan, but unknown user, no points");
68                                 }
69                         }
70                 }elsif ($verbose){
71                         $ND::server->command("msg $ND::target a scan with that id has already been added within the last 48 ticks");
72                 }
73         }
74 }
75
76 sub sendScan {
77         my ($msg, $command) = @_;
78         my ($target,$mess);
79         if (defined $msg && $msg =~ /^(\S+) (.*)$/){
80                 $target = $1;
81                 $mess = $2;
82         }else{
83                 $ND::server->command("notice $ND::nick syntax: $command nick message");
84                 return;
85         }
86         if (scanner()){
87                 $ND::server->command("msg $target ".chr(2).$mess.chr(3)."4 (reply with /msg $ND::scanchan)");
88                 $ND::server->command("msg $ND::target ${ND::C}3$target << $mess");
89         }
90 }
91
92 our %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
93
94 our @scantypes = ('Planet','Landing', 'Development'
95         ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
96
97 sub reqScan {
98         my ($msg, $command) = @_;
99
100         my ($x, $y, $z, $type, $typeid);
101         if (defined $msg && $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/){
102                 $typeid = $scanid{$1};
103                 $type = $scantypes[$typeid-1];
104                 ($x,$y,$z) = ($2,$3,$4);
105                 $msg = $5;
106         }else{
107                 $ND::server->command("notice $ND::nick syntax: $command type X:Y:Z message"
108                         . " | 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");
109                 return;
110         }
111
112         if (my $user = member){
113
114                 my $planet = $ND::DBH->selectrow_array(q{SELECT planetid($1,$2,$3,$4)}
115                         ,undef,$x,$y,$z,$ND::tick);
116
117                 my $query = $ND::DBH->prepare(q{SELECT scan_id
118                         FROM scans
119                         WHERE planet = $1 AND type = $2 AND tick >= $3});
120                 $query->execute($planet,$type,$ND::tick);
121
122                 if (my $scan = $query->fetchrow_hashref){
123                         $ND::server->command("notice $ND::nick scan already exist: "
124                                 . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
125                 }else{
126                         my $req = $ND::DBH->prepare(q{SELECT * FROM scan_requests
127                                 WHERE uid = $1 AND planet = $2
128                                         AND type = $3 AND NOT sent
129                         });
130                         $req->execute($user->{uid},$planet,$type);
131
132                         my $id;
133                         if(my $scan = $req->fetchrow_hashref){
134                                 $req = $ND::DBH->prepare(q{UPDATE scan_requests
135                                         SET nick = $1, tick = tick(), time = NOW()
136                                         WHERE id = $2});
137                                 $req->execute($ND::nick,$scan->{id});
138                                 $id = $scan->{id};
139                         }else{
140                                 $req = $ND::DBH->prepare(q{INSERT INTO scan_requests
141                                         (uid,nick,planet,type) VALUES($1,$2,$3,$4)
142                                         RETURNING (id)});
143                                 $req->execute($user->{uid},$ND::nick,$planet,$type);
144                                 $id = $req->fetchrow;
145                         }
146
147                         if ($id){
148                                 $ND::server->command("msg $ND::scanchan "
149                                         . "$ND::B$id$ND::O http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
150                                         . " ($x:$y:$z $type) | <$ND::nick> $msg"
151                                 );
152                                 $ND::server->command("notice $ND::nick sent request ($x:$y:$z $type)");
153                         }else{
154                                 $ND::server->command("notice $ND::nick something went wrong..");
155                         }
156
157                 }
158         }
159 }
160
161 our %scantypes;
162 {
163         my $i = 1;
164         %scantypes = map {$_ => $i++} @scantypes;
165 }
166
167 sub scanReqs {
168         my ($msg, $command) = @_;
169
170         if (scanner()){
171
172                 my $reqs = $ND::DBH->prepare(q{SELECT min(sr.id) AS id, x,y,z,type
173                         FROM scan_requests sr
174                                 JOIN current_planet_stats p ON p.id = sr.planet
175                         WHERE sr.time > NOW() - '30 min'::INTERVAL
176                                 AND NOT EXISTS (SELECT scan_id FROM scans
177                                         WHERE planet = sr.planet
178                                                 AND type = sr.type
179                                                 AND tick >= sr.tick
180                                 )
181                         GROUP BY x,y,z,type
182                         ORDER BY id
183                         });
184                 $reqs->execute;
185                 my $text = '';
186                 while (my $req = $reqs->fetchrow_hashref){
187                         $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} "
188                 }
189
190                 $text = 'No unhandled reqests.' unless $text;
191                 $ND::server->command("notice $ND::nick $text");
192         }
193 }
194
195 1;