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