]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Intel.pm
5a4a47c91551263674aa09e01b99f68fa65c8a26
[ndwebbie.git] / ND / Web / Pages / Intel.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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 #**************************************************************************/
19
20 package ND::Web::Pages::Intel;
21 use strict;
22 use warnings FATAL => 'all';
23 use ND::Web::Forum;
24 use ND::Web::Include;
25 use ND::Include;
26 use CGI qw/:standard/;
27
28 $ND::PAGES{intel} = {parse => \&parse, process => \&process, render=> \&render};
29
30 sub parse {
31         my ($uri) = @_;
32         if ($uri =~ m{^/.*/(\w+)$}){
33                 param('list',$1);
34         }
35 }
36
37 sub process {
38
39 }
40
41 sub render {
42         my ($DBH,$BODY) = @_;
43
44         my $error;
45
46         $ND::TEMPLATE->param(TITLE => 'Intel');
47
48         return $ND::NOACCESS unless isIntel() || isHC();
49
50         my $planet;
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;
55         }
56
57         my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
58         if (defined param('show')){
59                 if (param('show') eq 'all'){
60                         $showticks = '';
61                 }elsif (param('show') =~ /^(\d+)$/){
62                         $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
63                 }
64         }
65
66         my $thread;
67         if (defined $planet){
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);
70         }
71
72         if (defined param('cmd') && param('cmd') eq 'coords'){
73                 my $coords = param('coords');
74                 $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
75                         x integer NOT NULL,
76                         y integer NOT NULL,
77                         z integer NOT NULL,
78                         PRIMARY KEY (x,y,z)
79                         )});
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);
83                 }
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});
87                 $planets->execute;
88                 my @planets;
89                 while (my $planet = $planets->fetchrow_hashref){
90                         push @planets,$planet;
91                 }
92                 $BODY->param(CoordList => \@planets);
93         }
94         if (defined $thread and defined param('cmd') and param('cmd') eq 'forumpost'){
95                 addForumPost($DBH,$thread,$ND::UID,param('message'));
96         }
97
98         if ($planet && defined param('cmd')){
99                 if (param('cmd') eq 'change'){
100                         $DBH->begin_work;
101                         if (param('cnick')){
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;
107                                 }else{
108                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
109                                 }
110                         }
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;
117                                 }else{
118                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
119                                 }
120                         }
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;
127                                 }else{
128                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
129                                 }
130                         }
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');
136                                 }else{
137                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
138                                 }
139                         }
140                         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
141                 }
142         }
143
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);
149                 my @channelusers;
150                 while (my $user = $findchannel->fetchrow_hashref){
151                         push @channelusers,$user;
152                 }
153                 $BODY->param(ChannelUsers => \@channelusers);
154         }
155
156         if ($planet){
157                 $BODY->param(Coords => $planet->{coords});
158                 $BODY->param(Planet => $planet->{id});
159                 $BODY->param(Nick => $planet->{nick});
160                 $BODY->param(Channel => $planet->{channel});
161                 my @status;
162                 for my $status ("&nbsp;","Friendly", "NAP", "Hostile"){
163                         push @status,{Status => $status, Selected => defined $planet->{planet_status} && $status eq $planet->{planet_status}}
164                 }
165                 $BODY->param(PlanetStatus => \@status);
166                 my @alliances = alliances($planet->{alliance_id});
167                 $BODY->param(Alliances => \@alliances);
168
169                 $BODY->param(Thread => viewForumThread $thread);
170
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;
173                 my @intellists;
174                 my @incomings;
175                 my $i = 0;
176                 while (my $intel = $query->fetchrow_hashref){
177                         if ($intel->{ingal}){
178                                 $intel->{missionclass} = 'ingal';
179                         }else{
180                                 $intel->{missionclass} = $intel->{mission};
181                         }
182                         $i++;
183                         $intel->{ODD} = $i % 2;
184                         push @incomings,$intel;
185                 }
186                 push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
187
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;
190                 my @outgoings;
191                 $i = 0;
192                 while (my $intel = $query->fetchrow_hashref){
193                         if ($intel->{ingal}){
194                                 $intel->{missionclass} = 'ingal';
195                         }else{
196                                 $intel->{missionclass} = $intel->{mission};
197                         }
198                         $i++;
199                         $intel->{ODD} = $i % 2;
200                         push @outgoings,$intel;
201                 }
202                 push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
203
204                 $BODY->param(IntelLIsts => \@intellists);
205
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)
211                                 $showticks}));
212                 $query->execute() or $error .= $DBH->errstr;
213
214                 my @intellists;
215                 my @intel;
216                 my $i = 0;
217                 while (my $intel = $query->fetchrow_hashref){
218                         if ($intel->{ingal}){
219                                 $intel->{missionclass} = 'ingal';
220                         }else{
221                                 $intel->{missionclass} = $intel->{mission};
222                         }
223                         $i++;
224                         $intel->{ODD} = $i % 2;
225                         push @intel,$intel;
226                 }
227                 push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
228                 $BODY->param(IntelLIsts => \@intellists);
229         }
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});
233         $query->execute;
234         my @messages;
235         while (my $message = $query->fetchrow_hashref){
236                 $message->{message} = parseMarkup($message->{message});
237                 push @messages,$message;
238         }
239         #$BODY->param(IntelMessages => \@messages);
240         $BODY->param(Error => $error);
241         return $BODY;
242 }
243 1;