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 ND::Web::Pages::Intel;
22 use warnings FATAL => 'all';
26 use CGI qw/:standard/;
28 $ND::PAGES{intel} = {parse => \&parse, process => \&process, render=> \&render};
32 if ($uri =~ m{^/.*/(\w+)$}){
46 $ND::TEMPLATE->param(TITLE => 'Intel');
48 return $ND::NOACCESS unless isIntel() || isHC();
51 if (defined param('coords') && param('coords') =~ /^(\d+)(?: |:)(\d+)(?: |:)(\d+)$/){
52 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
53 WHERE x = ? AND y = ? AND z = ?});
54 $planet = $DBH->selectrow_hashref($query,undef,$1,$2,$3) or $ND::ERROR .= p $DBH->errstr;
57 my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
58 if (defined param('show')){
59 if (param('show') eq 'all'){
61 }elsif (param('show') =~ /^(\d+)$/){
62 $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
68 $thread = $DBH->selectrow_hashref(q{SELECT ftid AS id, subject FROM forum_threads
69 where ftid = $1},undef,$planet->{ftid}) or $ND::ERROR .= p($DBH->errstr);
72 if (defined param('cmd') && param('cmd') eq 'coords'){
73 my $coords = param('coords');
74 $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
80 my $insert = $DBH->prepare(q{INSERT INTO coordlist (x,y,z) VALUES(?,?,?)});
81 while ($coords =~ m/(\d+):(\d+):(\d+)/g){
82 $insert->execute($1,$2,$3);
84 my $planets = $DBH->prepare(q{SELECT (((p.x || ':') || p.y) || ':') || p.z AS coords, alliance FROM current_planet_stats p
85 JOIN coordlist c ON p.x = c.x AND p.y = c.y AND p.z = c.z
86 ORDER BY alliance, p.x, p.y, p.z});
89 while (my $planet = $planets->fetchrow_hashref){
90 push @planets,$planet;
92 $BODY->param(CoordList => \@planets);
94 if (defined $thread and defined param('cmd') and param('cmd') eq 'forumpost'){
95 addForumPost($DBH,$thread,$ND::UID,param('message'));
98 if ($planet && defined param('cmd')){
99 if (param('cmd') eq 'change'){
102 my $value = escapeHTML(param('nick'));
103 if ($DBH->do(q{UPDATE planets SET nick = ? WHERE id =?}
104 ,undef,$value,$planet->{id})){
105 intel_log $ND::UID,$planet->{id},"Set nick to: $value";
106 $planet->{nick} = $value;
108 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
111 if (param('cchannel')){
112 my $value = escapeHTML(param('channel'));
113 if ($DBH->do(q{UPDATE planets SET channel = ? WHERE id =?}
114 ,undef,$value,$planet->{id})){
115 intel_log $ND::UID,$planet->{id},"Set channel to: $value";
116 $planet->{channel} = $value;
118 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
121 if (param('cstatus')){
122 my $value = escapeHTML(param('status'));
123 if ($DBH->do(q{UPDATE planets SET planet_status = ? WHERE id =?}
124 ,undef,$value,$planet->{id})){
125 intel_log $ND::UID,$planet->{id},"Set planet_status to: $value";
126 $planet->{planet_status} = $value;
128 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
131 if (param('calliance')){
132 if ($DBH->do(q{UPDATE planets SET alliance_id = NULLIF(?,-1) WHERE id =?}
133 ,undef,param('alliance'),$planet->{id})){
134 intel_log $ND::UID,$planet->{id},"Set alliance_id to: ".param('alliance');
135 $planet->{alliance_id} = param('alliance');
137 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
140 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
144 if (param('coords')){
145 my $channel = param('coords');
146 $channel = $planet->{channel} if ($planet);
147 my $findchannel = $DBH->prepare('SELECT coords(x,y,z),alliance,nick,channel FROM current_planet_stats WHERE channel ILIKE ? ');
148 $findchannel->execute($channel);
150 while (my $user = $findchannel->fetchrow_hashref){
151 push @channelusers,$user;
153 $BODY->param(ChannelUsers => \@channelusers);
157 $BODY->param(Coords => $planet->{coords});
158 $BODY->param(Planet => $planet->{id});
159 $BODY->param(Nick => $planet->{nick});
160 $BODY->param(Channel => $planet->{channel});
162 for my $status (" ","Friendly", "NAP", "Hostile"){
163 push @status,{Status => $status, Selected => defined $planet->{planet_status} && $status eq $planet->{planet_status}}
165 $BODY->param(PlanetStatus => \@status);
166 my @alliances = alliances($planet->{alliance_id});
167 $BODY->param(Alliances => \@alliances);
169 $BODY->param(Thread => viewForumThread $thread);
171 my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin',"t.id = ? $showticks"));
172 $query->execute($planet->{id}) or $error .= $DBH->errstr;
176 while (my $intel = $query->fetchrow_hashref){
177 if ($intel->{ingal}){
178 $intel->{missionclass} = 'ingal';
180 $intel->{missionclass} = $intel->{mission};
183 $intel->{ODD} = $i % 2;
184 push @incomings,$intel;
186 push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
188 $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
189 $query->execute($planet->{id}) or $error .= $DBH->errstr;
192 while (my $intel = $query->fetchrow_hashref){
193 if ($intel->{ingal}){
194 $intel->{missionclass} = 'ingal';
196 $intel->{missionclass} = $intel->{mission};
199 $intel->{ODD} = $i % 2;
200 push @outgoings,$intel;
202 push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
204 $BODY->param(IntelLIsts => \@intellists);
206 }elsif(!param('coords')){
207 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
208 AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL) AND i.mission != 'Attack')
209 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
210 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
212 $query->execute() or $error .= $DBH->errstr;
217 while (my $intel = $query->fetchrow_hashref){
218 if ($intel->{ingal}){
219 $intel->{missionclass} = 'ingal';
221 $intel->{missionclass} = $intel->{mission};
224 $intel->{ODD} = $i % 2;
227 push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
228 $BODY->param(IntelLIsts => \@intellists);
230 my $query = $DBH->prepare(q{SELECT i.id, u.username, i.message, report_date FROM intel_messages i
231 JOIN users u ON u.uid = i.uid
232 WHERE NOT handled ORDER BY report_date});
235 while (my $message = $query->fetchrow_hashref){
236 $message->{message} = parseMarkup($message->{message});
237 push @messages,$message;
239 #$BODY->param(IntelMessages => \@messages);
240 $BODY->param(Error => $error);