]> ruin.nu Git - ndwebbie.git/blob - intel.pl
better error handling
[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         while (my $intel = $query->fetchrow_hashref){
140                 if ($intel->{ingal}){
141                         $intel->{missionclass} = 'ingal';
142                 }else{
143                         $intel->{missionclass} = $intel->{mission};
144                 }
145                 push @intel,$intel;
146         }
147         push @intellists,{Message => 'Incoming fleets', Intel => \@intel, Origin => 1};
148
149         my $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
150         $query->execute($planet->{id}) or $error .= $DBH->errstr;
151         my @intel;
152         while (my $intel = $query->fetchrow_hashref){
153                 if ($intel->{ingal}){
154                         $intel->{missionclass} = 'ingal';
155                 }else{
156                         $intel->{missionclass} = $intel->{mission};
157                 }
158                 push @intel,$intel;
159         }
160         push @intellists,{Message => 'Outgoing Fleets', Intel => \@intel, Target => 1};
161
162         $BODY->param(IntelLIsts => \@intellists);
163
164 }elsif(!param('coords')){
165         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
166                         AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL) AND i.mission != 'Attack')
167                                 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
168                         AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
169                         $showticks}));
170         $query->execute() or $error .= $DBH->errstr;
171
172         my @intellists;
173         my @intel;
174         while (my $intel = $query->fetchrow_hashref){
175                 if ($intel->{ingal}){
176                         $intel->{missionclass} = 'ingal';
177                 }else{
178                         $intel->{missionclass} = $intel->{mission};
179                 }
180                 push @intel,$intel;
181         }
182         push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
183         $BODY->param(IntelLIsts => \@intellists);
184 }
185 my $query = $DBH->prepare(q{SELECT i.id, u.username, i.message, report_date FROM intel_messages i
186         JOIN users u ON u.uid = i.uid
187         WHERE NOT handled ORDER BY report_date});
188 $query->execute;
189 my @messages;
190 while (my $message = $query->fetchrow_hashref){
191         $message->{message} = parseMarkup($message->{message});
192         push @messages,$message;
193 }
194 $BODY->param(IntelMessages => \@messages);
195 $BODY->param(Error => $error);
196 1;