]> ruin.nu Git - ndwebbie.git/blob - intel.pl
e5ce1e071fe24b7eb51ae1fde9987bce1c4ebef4
[ndwebbie.git] / intel.pl
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 use strict;
21 use warnings FATAL => 'all';
22 use ND::Web::Forum;
23 use ND::Include;
24
25 our $BODY;
26 our $DBH;
27 our $ERROR;
28 my $error;
29
30 $ND::TEMPLATE->param(TITLE => 'Intel');
31
32 die "You don't have access" unless isIntel() || isHC();
33
34 my $planet;
35 if (defined param('coords') && param('coords') =~ /^(\d+)(?: |:)(\d+)(?: |:)(\d+)$/){
36         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
37 WHERE  x = ? AND y = ? AND z = ?});
38         $planet = $DBH->selectrow_hashref($query,undef,$1,$2,$3) or $ND::ERROR .= p $DBH->errstr;
39 }
40
41 my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
42 if (defined param('show')){
43         if (param('show') eq 'all'){
44                 $showticks = '';
45         }elsif (param('show') =~ /^(\d+)$/){
46                 $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
47         }
48 }
49
50 my $thread;
51 if (defined $planet){
52         $thread = $DBH->selectrow_hashref(q{SELECT ftid AS id, subject FROM forum_threads
53                 where ftid = $1},undef,$planet->{ftid}) or $ERROR .= p($DBH->errstr);
54 }
55
56 if (defined param('cmd') && param('cmd') eq 'coords'){
57         my $coords = param('coords');
58         $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
59         x integer NOT NULL,
60         y integer NOT NULL,
61         z integer NOT NULL,
62         PRIMARY KEY (x,y,z)
63                 )});
64         my $insert = $DBH->prepare(q{INSERT INTO coordlist (x,y,z) VALUES(?,?,?)});
65         while ($coords =~ m/(\d+):(\d+):(\d+)/g){
66                 $insert->execute($1,$2,$3);
67         }
68         my $planets = $DBH->prepare(q{SELECT (((p.x || ':') || p.y) || ':') || p.z AS coords, alliance FROM current_planet_stats p
69         JOIN coordlist c ON p.x = c.x AND p.y = c.y AND p.z = c.z
70 ORDER BY alliance, p.x, p.y, p.z});
71         $planets->execute;
72         my @planets;
73         while (my $planet = $planets->fetchrow_hashref){
74                 push @planets,$planet;
75         }
76         $BODY->param(CoordList => \@planets);
77 }
78 if (defined $thread and defined param('cmd') and param('cmd') eq 'forumpost'){
79         addForumPost($DBH,$thread,$ND::UID,param('message'));
80 }
81
82 if ($planet && defined param('cmd')){
83         if (param('cmd') eq 'change'){
84                 $DBH->begin_work;
85                 if (param('cnick')){
86                         my $value = escapeHTML(param('nick'));
87                         if ($DBH->do(q{UPDATE planets SET nick = ? WHERE id =?}
88                                         ,undef,$value,$planet->{id})){
89                                 intel_log $ND::UID,$planet->{id},"Set nick to: $value";
90                                 $planet->{nick} = $value;
91                         }else{
92                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
93                         }
94                 }
95                 if (param('cchannel')){
96                         my $value = escapeHTML(param('channel'));
97                         if ($DBH->do(q{UPDATE planets SET channel = ? WHERE id =?}
98                                         ,undef,$value,$planet->{id})){
99                                 intel_log $ND::UID,$planet->{id},"Set channel to: $value";
100                                 $planet->{channel} = $value;
101                         }else{
102                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
103                         }
104                 }
105                 if (param('cstatus')){
106                         my $value = escapeHTML(param('status'));
107                         if ($DBH->do(q{UPDATE planets SET planet_status = ? WHERE id =?}
108                                         ,undef,$value,$planet->{id})){
109                                 intel_log $ND::UID,$planet->{id},"Set planet_status to: $value";
110                                 $planet->{planet_status} = $value;
111                         }else{
112                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
113                         }
114                 }
115                 if (param('calliance')){
116                         if ($DBH->do(q{UPDATE planets SET alliance_id = NULLIF(?,-1) WHERE id =?}
117                                         ,undef,param('alliance'),$planet->{id})){
118                                 intel_log $ND::UID,$planet->{id},"Set alliance_id to: ".param('alliance');
119                                 $planet->{alliance_id} = param('alliance');
120                         }else{
121                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
122                         }
123                 }
124                 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
125         }
126 }
127
128 if (param('coords')){
129         my $channel = param('coords');
130         $channel = $planet->{channel} if ($planet);
131         my $findchannel = $DBH->prepare('SELECT coords(x,y,z),alliance,nick,channel FROM current_planet_stats WHERE channel ILIKE ? ');
132         $findchannel->execute($channel);
133         my @channelusers;
134         while (my $user = $findchannel->fetchrow_hashref){
135                 push @channelusers,$user;
136         }
137         $BODY->param(ChannelUsers => \@channelusers);
138 }
139
140 if ($planet){
141         $BODY->param(Coords => $planet->{coords});
142         $BODY->param(Planet => $planet->{id});
143         $BODY->param(Nick => $planet->{nick});
144         $BODY->param(Channel => $planet->{channel});
145         my @status;
146         for my $status ("&nbsp;","Friendly", "NAP", "Hostile"){
147                 push @status,{Status => $status, Selected => defined $planet->{planet_status} && $status eq $planet->{planet_status}}
148         }
149         $BODY->param(PlanetStatus => \@status);
150         my @alliances = alliances($planet->{alliance_id});
151         $BODY->param(Alliances => \@alliances);
152
153         $BODY->param(Thread => viewForumThread $thread);
154
155         my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin',"t.id = ? $showticks"));
156         $query->execute($planet->{id}) or $error .= $DBH->errstr;
157         my @intellists;
158         my @incomings;
159         my $i = 0;
160         while (my $intel = $query->fetchrow_hashref){
161                 if ($intel->{ingal}){
162                         $intel->{missionclass} = 'ingal';
163                 }else{
164                         $intel->{missionclass} = $intel->{mission};
165                 }
166                 $i++;
167                 $intel->{ODD} = $i % 2;
168                 push @incomings,$intel;
169         }
170         push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
171
172         $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
173         $query->execute($planet->{id}) or $error .= $DBH->errstr;
174         my @outgoings;
175         $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 @outgoings,$intel;
185         }
186         push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
187
188         $BODY->param(IntelLIsts => \@intellists);
189
190 }elsif(!param('coords')){
191         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
192                         AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL) AND i.mission != 'Attack')
193                                 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
194                         AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
195                         $showticks}));
196         $query->execute() or $error .= $DBH->errstr;
197
198         my @intellists;
199         my @intel;
200         my $i = 0;
201         while (my $intel = $query->fetchrow_hashref){
202                 if ($intel->{ingal}){
203                         $intel->{missionclass} = 'ingal';
204                 }else{
205                         $intel->{missionclass} = $intel->{mission};
206                 }
207                 $i++;
208                 $intel->{ODD} = $i % 2;
209                 push @intel,$intel;
210         }
211         push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
212         $BODY->param(IntelLIsts => \@intellists);
213 }
214 my $query = $DBH->prepare(q{SELECT i.id, u.username, i.message, report_date FROM intel_messages i
215         JOIN users u ON u.uid = i.uid
216         WHERE NOT handled ORDER BY report_date});
217 $query->execute;
218 my @messages;
219 while (my $message = $query->fetchrow_hashref){
220         $message->{message} = parseMarkup($message->{message});
221         push @messages,$message;
222 }
223 #$BODY->param(IntelMessages => \@messages);
224 $BODY->param(Error => $error);
225 1;