]> ruin.nu Git - NDIRC.git/blob - Commands/Scans.pm
Converted the .scanreqs command
[NDIRC.git] / Commands / Scans.pm
1 #**************************************************************************
2 #   Copyright (C) 2008 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 my %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
30
31 my @scantypes = ('Planet','Landing', 'Development'
32         ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
33
34 sub gs
35         : 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)
36         : ACL(irc_gs)
37         : Type(pm)
38 {
39         my ($self,$c,$args) = @_;
40
41         my ($typeid, $x, $y, $z, $msg) = $args =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/
42                 or die 'ARGS';
43         $typeid = $scanid{$typeid};
44         my $type = $scantypes[$typeid-1];
45
46         my $planet = $c->model->selectrow_array(q{SELECT planetid($1,$2,$3,tick())}
47                 ,undef,$x,$y,$z);
48
49         my $query = $c->model->prepare(q{SELECT scan_id
50                 FROM scans
51                 WHERE planet = $1 AND type = $2 AND tick >= tick()});
52         $query->execute($planet,$type);
53
54         if (my $scan = $query->fetchrow_hashref){
55                 $c->reply("scan already exist: "
56                         . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
57         }else{
58                 my $req = $c->model->prepare(q{
59 SELECT * FROM scan_requests
60 WHERE uid = (SELECT uid FROM users WHERE hostmask ILIKE $1)
61         AND planet = $2 AND type = $3 AND NOT sent
62                 });
63                 $req->execute($c->host,$planet,$type);
64
65                 my $id;
66                 if(my $scan = $req->fetchrow_hashref){
67                         $req = $c->model->prepare(q{
68 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
69 WHERE id = $2
70                         });
71                         $req->execute($c->nick,$scan->{id});
72                         $id = $scan->{id};
73                 }else{
74                         $req = $c->model->prepare(q{
75 INSERT INTO scan_requests (uid,nick,planet,type)
76 VALUES((SELECT uid FROM users WHERE hostmask ILIKE $1),$2,$3,$4) RETURNING (id)
77                         });
78                         $req->execute($c->host,$c->nick,$planet,$type);
79                         $id = $req->fetchrow;
80                 }
81
82                 if ($id){
83                         $c->message("msg $ND::scanchan"
84                                 ,"<b>$id</b> http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
85                                 . " ($x:$y:$z $type) | <".$c->nick."> $msg"
86                         );
87                         $c->reply("sent request ($x:$y:$z $type)");
88                 }else{
89                         $c->reply("something went wrong..");
90                 }
91         }
92 }
93
94 my %scantypes;
95 {
96         my $i = 1;
97         %scantypes = map {$_ => $i++} @scantypes;
98 }
99
100 sub scanreqs
101         : 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.)
102         : ACL(irc_scanreqs)
103 {
104         my ($self, $c, $msg) = @_;
105
106         my @notype;
107         if ($msg =~ /^-([pdunja]+)$/){
108                 for (split //, $1){
109                         push @notype, $scantypes[$scanid{$_}-1];
110                 }
111         }elsif($msg){
112                 die 'ARGS';
113         }
114
115         my $reqs = $c->model->prepare(q{
116 SELECT min(sr.id) AS id, x,y,z,type
117 FROM scan_requests sr
118         JOIN current_planet_stats p ON p.id = sr.planet
119 WHERE sr.time > NOW() - '30 min'::INTERVAL
120         AND NOT EXISTS (SELECT scan_id FROM scans
121                 WHERE planet = sr.planet
122                         AND type = sr.type
123                         AND tick >= sr.tick
124         )
125         AND type <> ALL($1)
126 GROUP BY x,y,z,type
127 ORDER BY id
128                 });
129         $reqs->execute(\@notype);
130         my $text = '';
131         while (my $req = $reqs->fetchrow_hashref){
132                 $text .= "<b>$req->{id}</b> http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
133         }
134
135         $c->reply($text || 'No unhandled requests.');
136 }
137
138 1;