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