]> ruin.nu Git - NDIRC.git/blob - Commands/Scans.pm
Converted the .gs 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 1;