1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 #**************************************************************************/
20 package NDWeb::Pages::Intel;
22 use warnings FATAL => 'all';
26 use CGI qw/:standard/;
28 use base qw/NDWeb::XMLPage/;
30 $NDWeb::Page::PAGES{intel} = __PACKAGE__;
34 if ($self->{URI} =~ m{^/.*/((\d+)(?: |:)(\d+)(?: |:)(\d+))$}){
42 $self->{TITLE} = 'Intel';
43 my $DBH = $self->{DBH};
45 return $self->noAccess unless $self->isIntel || $self->isHC;
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;
56 my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
57 if (defined param('show')){
58 if (param('show') eq 'all'){
60 }elsif (param('show') =~ /^(\d+)$/){
61 $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
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);
71 if (defined param('cmd') && param('cmd') eq 'coords'){
72 my $coords = param('coords');
73 $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
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);
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});
88 while (my $planet = $planets->fetchrow_hashref){
89 push @planets,$planet;
91 $BODY->param(CoordList => \@planets);
93 if (defined $thread and defined param('cmd') and param('cmd') eq 'forumpost'){
94 addForumPost($DBH,$thread,$ND::UID,param('message'));
97 if ($planet && defined param('cmd')){
98 if (param('cmd') eq 'change'){
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;
107 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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;
117 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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;
127 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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');
136 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
139 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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);
149 while (my $user = $findchannel->fetchrow_hashref){
150 push @channelusers,$user;
152 $BODY->param(ChannelUsers => \@channelusers);
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});
161 for my $status ("","Friendly", "NAP", "Hostile"){
162 push @status,{Status => $status, Selected => defined $planet->{planet_status} && $status eq $planet->{planet_status}}
164 $BODY->param(PlanetStatus => \@status);
165 my @alliances = alliances($planet->{alliance_id});
166 $BODY->param(Alliances => \@alliances);
168 $BODY->param(Thread => viewForumThread $thread);
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;
175 while (my $intel = $query->fetchrow_hashref){
176 if ($intel->{ingal}){
177 $intel->{missionclass} = 'ingal';
179 $intel->{missionclass} = $intel->{mission};
182 $intel->{ODD} = $i % 2;
183 push @incomings,$intel;
185 push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
187 $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
188 $query->execute($planet->{id}) or $error .= $DBH->errstr;
191 while (my $intel = $query->fetchrow_hashref){
192 if ($intel->{ingal}){
193 $intel->{missionclass} = 'ingal';
195 $intel->{missionclass} = $intel->{mission};
198 $intel->{ODD} = $i % 2;
199 push @outgoings,$intel;
201 push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
203 $BODY->param(IntelLIsts => \@intellists);
205 }elsif(!param('coords')){
206 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
207 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' ))
208 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
209 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
211 $query->execute() or $error .= $DBH->errstr;
216 while (my $intel = $query->fetchrow_hashref){
217 if ($intel->{ingal}){
218 $intel->{missionclass} = 'ingal';
220 $intel->{missionclass} = $intel->{mission};
223 $intel->{ODD} = $i % 2;
226 push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
227 $BODY->param(IntelLIsts => \@intellists);
229 $BODY->param(Error => $error);