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