]> ruin.nu Git - ndwebbie.git/blob - intel.pl
fatal warnings
[ndwebbie.git] / intel.pl
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
20 use strict;
21 use warnings FATAL => 'all';
22 no warnings qw(uninitialized);
23 use POSIX;
24 our $BODY;
25 our $DBH;
26 our $LOG;
27 my $error;
28
29 $ND::TEMPLATE->param(TITLE => 'Intel');
30
31 die "You don't have access" unless isIntel() || isHC();
32
33 my $planet;
34 if (param('coords') =~ /^(\d+)(?: |:)(\d+)(?: |:)(\d+)$/){
35         my $query = $DBH->prepare(q{SELECT x,y,z,coords(x,y,z),id, nick, alliance,alliance_id, planet_status,channel FROM current_planet_stats
36 WHERE  x = ? AND y = ? AND z = ?});
37         $planet = $DBH->selectrow_hashref($query,undef,$1,$2,$3);
38 }
39
40 my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
41 if (param('show') eq 'all'){
42         $showticks = '';
43 }elsif (param('show') =~ /^(\d+)$/){
44         $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
45 }
46
47 if (param('cmd') eq 'coords'){
48         my $coords = param('coords');
49         $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
50         x integer NOT NULL,
51         y integer NOT NULL,
52         z integer NOT NULL,
53         PRIMARY KEY (x,y,z)
54                 )});
55         my $insert = $DBH->prepare(q{INSERT INTO coordlist (x,y,z) VALUES(?,?,?)});
56         while ($coords =~ m/(\d+):(\d+):(\d+)/g){
57                 $insert->execute($1,$2,$3);
58         }
59         my $planets = $DBH->prepare(q{SELECT (((p.x || ':') || p.y) || ':') || p.z AS coords, alliance FROM current_planet_stats p
60         JOIN coordlist c ON p.x = c.x AND p.y = c.y AND p.z = c.z
61 ORDER BY alliance, p.x, p.y, p.z});
62         $planets->execute;
63         my @planets;
64         while (my $planet = $planets->fetchrow_hashref){
65                 push @planets,$planet;
66         }
67         $BODY->param(CoordList => \@planets);
68 }
69
70 if ($planet){
71         if (param('cmd') eq 'change'){
72                 $DBH->begin_work;
73                 if (param('cnick')){
74                         my $value = escapeHTML(param('nick'));
75                         if ($DBH->do(q{UPDATE planets SET nick = ? WHERE id =?}
76                                         ,undef,$value,$planet->{id})){
77                                 $planet->{nick} = $value;
78                         }else{
79                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
80                         }
81                 }
82                 if (param('cchannel')){
83                         my $value = escapeHTML(param('channel'));
84                         if ($DBH->do(q{UPDATE planets SET channel = ? WHERE id =?}
85                                         ,undef,$value,$planet->{id})){
86                                 $planet->{channel} = $value;
87                         }else{
88                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
89                         }
90                 }
91                 if (param('cstatus')){
92                         my $value = escapeHTML(param('status'));
93                         if ($DBH->do(q{UPDATE planets SET planet_status = ? WHERE id =?}
94                                         ,undef,$value,$planet->{id})){
95                                 $planet->{planet_status} = $value;
96                         }else{
97                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
98                         }
99                 }
100                 if (param('calliance')){
101                         if ($DBH->do(q{UPDATE planets SET alliance_id = NULLIF(?,-1) WHERE id =?}
102                                         ,undef,param('alliance'),$planet->{id})){
103                                 $planet->{alliance_id} = param('alliance');
104                         }else{
105                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
106                         }
107                 }
108                 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
109         }
110 }
111
112 if (param('coords')){
113         my $channel = param('coords');
114         $channel = $planet->{channel} if ($planet);
115         my $findchannel = $DBH->prepare('SELECT coords(x,y,z),alliance,nick,channel FROM current_planet_stats WHERE channel ILIKE ? ');
116         $findchannel->execute($channel);
117         my @channelusers;
118         while (my $user = $findchannel->fetchrow_hashref){
119                 push @channelusers,$user;
120         }
121         $BODY->param(ChannelUsers => \@channelusers);
122 }
123
124 if ($planet){
125         $BODY->param(Coords => $planet->{coords});
126         $BODY->param(Planet => $planet->{id});
127         $BODY->param(Nick => $planet->{nick});
128         $BODY->param(Channel => $planet->{channel});
129         my @status;
130         for my $status ("&nbsp;","Friendly", "NAP", "Hostile"){
131                 push @status,{Status => $status, Selected => $status eq $planet->{planet_status}}
132         }
133         $BODY->param(PlanetStatus => \@status);
134         my @alliances = alliances($planet->{alliance_id});
135         $BODY->param(Alliances => \@alliances);
136
137         my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin',"t.id = ? $showticks"));
138         $query->execute($planet->{id}) or $error .= $DBH->errstr;
139         my @intellists;
140         my @incomings;
141         my $i = 0;
142         while (my $intel = $query->fetchrow_hashref){
143                 if ($intel->{ingal}){
144                         $intel->{missionclass} = 'ingal';
145                 }else{
146                         $intel->{missionclass} = $intel->{mission};
147                 }
148                 $i++;
149                 $intel->{ODD} = $i % 2;
150                 push @incomings,$intel;
151         }
152         push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
153
154         $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
155         $query->execute($planet->{id}) or $error .= $DBH->errstr;
156         my @outgoings;
157         $i = 0;
158         while (my $intel = $query->fetchrow_hashref){
159                 if ($intel->{ingal}){
160                         $intel->{missionclass} = 'ingal';
161                 }else{
162                         $intel->{missionclass} = $intel->{mission};
163                 }
164                 $i++;
165                 $intel->{ODD} = $i % 2;
166                 push @outgoings,$intel;
167         }
168         push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
169
170         $BODY->param(IntelLIsts => \@intellists);
171
172 }elsif(!param('coords')){
173         my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin, t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',qq{not ingal
174                         AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL) AND i.mission != 'Attack')
175                                 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
176                         AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
177                         $showticks}));
178         $query->execute() or $error .= $DBH->errstr;
179
180         my @intellists;
181         my @intel;
182         my $i = 0;
183         while (my $intel = $query->fetchrow_hashref){
184                 if ($intel->{ingal}){
185                         $intel->{missionclass} = 'ingal';
186                 }else{
187                         $intel->{missionclass} = $intel->{mission};
188                 }
189                 $i++;
190                 $intel->{ODD} = $i % 2;
191                 push @intel,$intel;
192         }
193         push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
194         $BODY->param(IntelLIsts => \@intellists);
195 }
196 my $query = $DBH->prepare(q{SELECT i.id, u.username, i.message, report_date FROM intel_messages i
197         JOIN users u ON u.uid = i.uid
198         WHERE NOT handled ORDER BY report_date});
199 $query->execute;
200 my @messages;
201 while (my $message = $query->fetchrow_hashref){
202         $message->{message} = parseMarkup($message->{message});
203         push @messages,$message;
204 }
205 $BODY->param(IntelMessages => \@messages);
206 $BODY->param(Error => $error);
207 1;