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,gov 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>";
131 my $value = escapeHTML(param('gov'));
132 if ($DBH->do(q{UPDATE planets SET gov = ? WHERE id =?}
133 ,undef,$value,$planet->{id})){
134 intel_log $ND::UID,$planet->{id},"Set government to: $value";
135 $planet->{gov} = $value;
137 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
140 if (param('calliance')){
141 if ($DBH->do(q{UPDATE planets SET alliance_id = NULLIF(?,-1) WHERE id =?}
142 ,undef,param('alliance'),$planet->{id})){
143 intel_log $ND::UID,$planet->{id},"Set alliance_id to: ".param('alliance');
144 $planet->{alliance_id} = param('alliance');
146 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
149 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
153 if (param('coords')){
154 my $channel = param('coords');
155 $channel = $planet->{channel} if ($planet);
156 my $findchannel = $DBH->prepare('SELECT coords(x,y,z),alliance,nick,channel FROM current_planet_stats WHERE channel ILIKE ? ');
157 $findchannel->execute($channel);
159 while (my $user = $findchannel->fetchrow_hashref){
160 push @channelusers,$user;
162 $BODY->param(ChannelUsers => \@channelusers);
166 $BODY->param(Coords => $planet->{coords});
167 $BODY->param(Planet => $planet->{id});
168 $BODY->param(Nick => escapeHTML($planet->{nick}));
169 $BODY->param(Channel => $planet->{channel});
171 for my $gov ("","Feu", "Dic", "Dem","Uni"){
172 push @gov,{Gov => $gov, Selected => $gov eq $planet->{gov}}
174 $BODY->param(Gov => \@gov);
176 for my $status ("","Friendly", "NAP", "Hostile"){
177 push @status,{Status => $status, Selected => $status eq $planet->{planet_status}}
179 $BODY->param(PlanetStatus => \@status);
180 my @alliances = alliances($planet->{alliance_id});
181 $BODY->param(Alliances => \@alliances);
183 $BODY->param(Thread => viewForumThread $thread);
185 my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin',"t.id = ? $showticks"));
186 $query->execute($planet->{id}) or $error .= $DBH->errstr;
189 while (my $intel = $query->fetchrow_hashref){
190 if ($intel->{ingal}){
191 $intel->{missionclass} = 'ingal';
193 $intel->{missionclass} = $intel->{mission};
195 push @incomings,$intel;
197 push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
199 $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
200 $query->execute($planet->{id}) or $error .= $DBH->errstr;
202 while (my $intel = $query->fetchrow_hashref){
203 if ($intel->{ingal}){
204 $intel->{missionclass} = 'ingal';
206 $intel->{missionclass} = $intel->{mission};
208 push @outgoings,$intel;
210 push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
212 $BODY->param(IntelLIsts => \@intellists);
214 }elsif(!param('coords')){
215 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
216 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' ))
217 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
218 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
220 $query->execute() or $error .= $DBH->errstr;
224 while (my $intel = $query->fetchrow_hashref){
225 if ($intel->{ingal}){
226 $intel->{missionclass} = 'ingal';
228 $intel->{missionclass} = $intel->{mission};
232 push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
233 $BODY->param(IntelLIsts => \@intellists);
235 $BODY->param(Error => $error);