]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/Intel.pm
Converted check/stats page
[ndwebbie.git] / NDWeb / 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 NDWeb::Pages::Intel;
21 use strict;
22 use warnings FATAL => 'all';
23 use NDWeb::Forum;
24 use NDWeb::Include;
25 use ND::Include;
26 use CGI qw/:standard/;
27
28 use base qw/NDWeb::XMLPage/;
29
30 $NDWeb::Page::PAGES{intel} = __PACKAGE__;
31
32 sub parse {
33         my $self = shift;
34         if ($self->{URI} =~ m{^/.*/((\d+)(?: |:)(\d+)(?: |:)(\d+))$}){
35                 param('coords',$1);
36         }
37 }
38
39 sub render_body {
40         my $self = shift;
41         my ($BODY) = @_;
42         $self->{TITLE} = 'Intel';
43         my $DBH = $self->{DBH};
44
45         return $self->noAccess unless $self->isIntel || $self->isHC;
46
47         my $error;
48
49         my $planet;
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;
54         }
55
56         my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
57         if (defined param('show')){
58                 if (param('show') eq 'all'){
59                         $showticks = '';
60                 }elsif (param('show') =~ /^(\d+)$/){
61                         $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
62                 }
63         }
64
65         my $thread;
66         if (defined $planet){
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);
69         }
70
71         if (defined param('cmd') && param('cmd') eq 'coords'){
72                 my $coords = param('coords');
73                 $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
74                         x integer NOT NULL,
75                         y integer NOT NULL,
76                         z integer NOT NULL,
77                         PRIMARY KEY (x,y,z)
78                         )});
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);
82                 }
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});
86                 $planets->execute;
87                 my @planets;
88                 while (my $planet = $planets->fetchrow_hashref){
89                         push @planets,$planet;
90                 }
91                 $BODY->param(CoordList => \@planets);
92         }
93         if (defined $thread and defined param('cmd') and param('cmd') eq 'forumpost'){
94                 addForumPost($DBH,$thread,$ND::UID,param('message'));
95         }
96
97         if ($planet && defined param('cmd')){
98                 if (param('cmd') eq 'change'){
99                         $DBH->begin_work;
100                         if (param('cnick')){
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;
106                                 }else{
107                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
108                                 }
109                         }
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;
116                                 }else{
117                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
118                                 }
119                         }
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;
126                                 }else{
127                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
128                                 }
129                         }
130                         if (param('cgov')){
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;
136                                 }else{
137                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
138                                 }
139                         }
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');
145                                 }else{
146                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
147                                 }
148                         }
149                         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
150                 }
151         }
152
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);
158                 my @channelusers;
159                 while (my $user = $findchannel->fetchrow_hashref){
160                         push @channelusers,$user;
161                 }
162                 $BODY->param(ChannelUsers => \@channelusers);
163         }
164
165         if ($planet){
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});
170                 my @gov;
171                 for my $gov ("","Feu", "Dic", "Dem","Uni"){
172                         push @gov,{Gov => $gov, Selected => $gov eq $planet->{gov}}
173                 }
174                 $BODY->param(Gov => \@gov);
175                 my @status;
176                 for my $status ("","Friendly", "NAP", "Hostile"){
177                         push @status,{Status => $status, Selected => $status eq $planet->{planet_status}}
178                 }
179                 $BODY->param(PlanetStatus => \@status);
180                 my @alliances = alliances($planet->{alliance_id});
181                 $BODY->param(Alliances => \@alliances);
182
183                 $BODY->param(Thread => viewForumThread $thread);
184
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;
187                 my @intellists;
188                 my @incomings;
189                 while (my $intel = $query->fetchrow_hashref){
190                         if ($intel->{ingal}){
191                                 $intel->{missionclass} = 'ingal';
192                         }else{
193                                 $intel->{missionclass} = $intel->{mission};
194                         }
195                         push @incomings,$intel;
196                 }
197                 push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
198
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;
201                 my @outgoings;
202                 while (my $intel = $query->fetchrow_hashref){
203                         if ($intel->{ingal}){
204                                 $intel->{missionclass} = 'ingal';
205                         }else{
206                                 $intel->{missionclass} = $intel->{mission};
207                         }
208                         push @outgoings,$intel;
209                 }
210                 push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
211
212                 $BODY->param(IntelLIsts => \@intellists);
213
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)
219                                 AND NOT (i.back IS NOT NULL AND i.back = i.tick + 4)
220                                 $showticks
221                                 }));
222                 $query->execute() or $error .= $DBH->errstr;
223
224                 my @intellists;
225                 my @intel;
226                 while (my $intel = $query->fetchrow_hashref){
227                         if ($intel->{ingal}){
228                                 $intel->{missionclass} = 'ingal';
229                         }else{
230                                 $intel->{missionclass} = $intel->{mission};
231                         }
232                         push @intel,$intel;
233                 }
234                 push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
235                 $BODY->param(IntelLIsts => \@intellists);
236         }
237         $BODY->param(Error => $error);
238         return $BODY;
239 }
240 1;