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