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 #**************************************************************************/
21 use warnings FATAL => 'all';
27 $ND::TEMPLATE->param(TITLE => 'Intel');
29 die "You don't have access" unless isIntel() || isHC();
32 if (defined param('coords') && param('coords') =~ /^(\d+)(?: |:)(\d+)(?: |:)(\d+)$/){
33 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
34 WHERE x = ? AND y = ? AND z = ?});
35 $planet = $DBH->selectrow_hashref($query,undef,$1,$2,$3);
38 my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
39 if (defined param('show')){
40 if (param('show') eq 'all'){
42 }elsif (param('show') =~ /^(\d+)$/){
43 $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
47 if (defined param('cmd') && param('cmd') eq 'coords'){
48 my $coords = param('coords');
49 $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
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);
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});
64 while (my $planet = $planets->fetchrow_hashref){
65 push @planets,$planet;
67 $BODY->param(CoordList => \@planets);
70 if ($planet && defined param('cmd')){
71 if (param('cmd') eq 'change'){
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;
79 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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;
88 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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;
97 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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');
105 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
108 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
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);
118 while (my $user = $findchannel->fetchrow_hashref){
119 push @channelusers,$user;
121 $BODY->param(ChannelUsers => \@channelusers);
125 $BODY->param(Coords => $planet->{coords});
126 $BODY->param(Planet => $planet->{id});
127 $BODY->param(Nick => $planet->{nick});
128 $BODY->param(Channel => $planet->{channel});
130 for my $status (" ","Friendly", "NAP", "Hostile"){
131 push @status,{Status => $status, Selected => defined $planet->{planet_status} && $status eq $planet->{planet_status}}
133 $BODY->param(PlanetStatus => \@status);
134 my @alliances = alliances($planet->{alliance_id});
135 $BODY->param(Alliances => \@alliances);
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;
142 while (my $intel = $query->fetchrow_hashref){
143 if ($intel->{ingal}){
144 $intel->{missionclass} = 'ingal';
146 $intel->{missionclass} = $intel->{mission};
149 $intel->{ODD} = $i % 2;
150 push @incomings,$intel;
152 push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
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;
158 while (my $intel = $query->fetchrow_hashref){
159 if ($intel->{ingal}){
160 $intel->{missionclass} = 'ingal';
162 $intel->{missionclass} = $intel->{mission};
165 $intel->{ODD} = $i % 2;
166 push @outgoings,$intel;
168 push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
170 $BODY->param(IntelLIsts => \@intellists);
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)
178 $query->execute() or $error .= $DBH->errstr;
183 while (my $intel = $query->fetchrow_hashref){
184 if ($intel->{ingal}){
185 $intel->{missionclass} = 'ingal';
187 $intel->{missionclass} = $intel->{mission};
190 $intel->{ODD} = $i % 2;
193 push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
194 $BODY->param(IntelLIsts => \@intellists);
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});
201 while (my $message = $query->fetchrow_hashref){
202 $message->{message} = parseMarkup($message->{message});
203 push @messages,$message;
205 $BODY->param(IntelMessages => \@messages);
206 $BODY->param(Error => $error);