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 #**************************************************************************/
26 $ND::TEMPLATE->param(TITLE => 'Intel');
28 die "You don't have access" unless isIntel() || isHC();
31 if (param('coords') =~ /^(\d+)(?: |:)(\d+)(?: |:)(\d+)$/){
32 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
33 WHERE x = ? AND y = ? AND z = ?});
34 $planet = $DBH->selectrow_hashref($query,undef,$1,$2,$3);
37 my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
38 if (param('show') eq 'all'){
40 }elsif (param('show') =~ /^(\d+)$/){
41 $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
44 if (param('cmd') eq 'coords'){
45 my $coords = param('coords');
46 $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
52 my $insert = $DBH->prepare(q{INSERT INTO coordlist (x,y,z) VALUES(?,?,?)});
53 while ($coords =~ m/(\d+):(\d+):(\d+)/g){
54 $insert->execute($1,$2,$3);
56 my $planets = $DBH->prepare(q{SELECT (((p.x || ':') || p.y) || ':') || p.z AS coords, alliance FROM current_planet_stats p
57 JOIN coordlist c ON p.x = c.x AND p.y = c.y AND p.z = c.z
58 ORDER BY alliance, p.x, p.y, p.z});
61 while (my $planet = $planets->fetchrow_hashref){
62 push @planets,$planet;
64 $BODY->param(CoordList => \@planets);
68 if (param('cmd') eq 'change'){
71 my $value = escapeHTML(param('nick'));
72 if ($DBH->do(q{UPDATE planets SET nick = ? WHERE id =?}
73 ,undef,$value,$planet->{id})){
74 $planet->{nick} = $value;
76 print "<p> Something went wrong: ".$DBH->errstr."</p>";
79 if (param('cchannel')){
80 my $value = escapeHTML(param('channel'));
81 if ($DBH->do(q{UPDATE planets SET channel = ? WHERE id =?}
82 ,undef,$value,$planet->{id})){
83 $planet->{channel} = $value;
85 print "<p> Something went wrong: ".$DBH->errstr."</p>";
88 if (param('cstatus')){
89 my $value = escapeHTML(param('status'));
90 if ($DBH->do(q{UPDATE planets SET planet_status = ? WHERE id =?}
91 ,undef,$value,$planet->{id})){
92 $planet->{planet_status} = $value;
94 print "<p> Something went wrong: ".$DBH->errstr."</p>";
97 if (param('calliance')){
98 if ($DBH->do(q{UPDATE planets SET alliance_id = NULLIF(?,-1) WHERE id =?}
99 ,undef,param('alliance'),$planet->{id})){
100 $planet->{alliance_id} = param('alliance');
102 print "<p> Something went wrong: ".$DBH->errstr."</p>";
105 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
109 if (param('coords')){
110 my $channel = param('coords');
111 $channel = $planet->{channel} if ($planet);
112 my $findchannel = $DBH->prepare('SELECT coords(x,y,z),alliance,nick,channel FROM current_planet_stats WHERE channel ILIKE ? ');
113 $findchannel->execute($channel);
115 while (my $user = $findchannel->fetchrow_hashref){
116 push @channelusers,$user;
118 $BODY->param(ChannelUsers => \@channelusers);
122 $BODY->param(Coords => $planet->{coords});
123 $BODY->param(Planet => $planet->{id});
124 $BODY->param(Nick => $planet->{nick});
125 $BODY->param(Channel => $planet->{channel});
127 for my $status (" ","Friendly", "NAP", "Hostile"){
128 push @status,{Status => $status, Selected => $status eq $planet->{planet_status}}
130 $BODY->param(PlanetStatus => \@status);
131 my @alliances = alliances($planet->{alliance_id});
132 $BODY->param(Alliances => \@alliances);
134 my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin',"t.id = ? $showticks"));
135 $query->execute($planet->{id}) or print $DBH->errstr;
138 while (my $intel = $query->fetchrow_hashref){
139 if ($intel->{ingal}){
140 $intel->{missionclass} = 'ingal';
142 $intel->{missionclass} = $intel->{mission};
146 push @intellists,{Message => 'Incoming fleets', Intel => \@intel, Origin => 1};
148 my $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
149 $query->execute($planet->{id}) or print $DBH->errstr;
151 while (my $intel = $query->fetchrow_hashref){
152 if ($intel->{ingal}){
153 $intel->{missionclass} = 'ingal';
155 $intel->{missionclass} = $intel->{mission};
159 push @intellists,{Message => 'Outgoing Fleets', Intel => \@intel, Target => 1};
161 $BODY->param(IntelLIsts => \@intellists);
163 }elsif(!param('coords')){
164 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
165 AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL) AND i.mission != 'Attack')
166 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
167 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
169 $query->execute() or print $DBH->errstr;
173 while (my $intel = $query->fetchrow_hashref){
174 if ($intel->{ingal}){
175 $intel->{missionclass} = 'ingal';
177 $intel->{missionclass} = $intel->{mission};
181 push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
182 $BODY->param(IntelLIsts => \@intellists);
184 my $query = $DBH->prepare(q{SELECT i.id, u.username, i.message, report_date FROM intel_messages i
185 JOIN users u ON u.uid = i.uid
186 WHERE NOT handled ORDER BY report_date});
189 while (my $message = $query->fetchrow_hashref){
190 $message->{message} = parseMarkup($message->{message});
191 push @messages,$message;
193 $BODY->param(IntelMessages => \@messages);