]> ruin.nu Git - ndwebbie.git/blob - intel.pl
minor fixes
[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 ($planet){
45         if (param('cmd') eq 'change'){
46                 $DBH->begin_work;
47                 if (param('cnick')){
48                         my $value = escapeHTML(param('nick'));
49                         if ($DBH->do(q{UPDATE planets SET nick = ? WHERE id =?}
50                                         ,undef,$value,$planet->{id})){
51                                 $planet->{nick} = $value;
52                         }else{
53                                 print "<p> Something went wrong: ".$DBH->errstr."</p>";
54                         }
55                 }
56                 if (param('cchannel')){
57                         my $value = escapeHTML(param('channel'));
58                         if ($DBH->do(q{UPDATE planets SET channel = ? WHERE id =?}
59                                         ,undef,$value,$planet->{id})){
60                                 $planet->{channel} = $value;
61                         }else{
62                                 print "<p> Something went wrong: ".$DBH->errstr."</p>";
63                         }
64                 }
65                 if (param('cstatus')){
66                         my $value = escapeHTML(param('status'));
67                         if ($DBH->do(q{UPDATE planets SET planet_status = ? WHERE id =?}
68                                         ,undef,$value,$planet->{id})){
69                                 $planet->{planet_status} = $value;
70                         }else{
71                                 print "<p> Something went wrong: ".$DBH->errstr."</p>";
72                         }
73                 }
74                 if (param('calliance')){
75                         if ($DBH->do(q{UPDATE planets SET alliance_id = NULLIF(?,-1) WHERE id =?}
76                                         ,undef,param('alliance'),$planet->{id})){
77                                 $planet->{alliance_id} = param('alliance');
78                         }else{
79                                 print "<p> Something went wrong: ".$DBH->errstr."</p>";
80                         }
81                 }
82                 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
83         }
84 }
85
86 if (param('coords')){
87         my $channel = param('coords');
88         $channel = $planet->{channel} if ($planet);
89         my $findchannel = $DBH->prepare('SELECT coords(x,y,z),alliance,nick,channel FROM current_planet_stats WHERE channel ILIKE ? ');
90         $findchannel->execute($channel);
91         my @channelusers;
92         while (my $user = $findchannel->fetchrow_hashref){
93                 push @channelusers,$user;
94         }
95         $BODY->param(ChannelUsers => \@channelusers);
96 }
97
98 if ($planet){
99         $BODY->param(Coords => $planet->{coords});
100         $BODY->param(Planet => $planet->{id});
101         $BODY->param(Nick => $planet->{nick});
102         $BODY->param(Channel => $planet->{channel});
103         my @status;
104         for my $status ("&nbsp;","Friendly", "NAP", "Hostile"){
105                 push @status,{Status => $status, Selected => $status eq $planet->{planet_status}}
106         }
107         $BODY->param(PlanetStatus => \@status);
108         my @alliances = alliances($planet->{alliance_id});
109         $BODY->param(Alliances => \@alliances);
110
111         my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin',"t.id = ? $showticks"));
112         $query->execute($planet->{id}) or print $DBH->errstr;
113         my @intellists;
114         my @intel;
115         while (my $intel = $query->fetchrow_hashref){
116                 if ($intel->{ingal}){
117                         $intel->{missionclass} = 'ingal';
118                 }else{
119                         $intel->{missionclass} = $intel->{mission};
120                 }
121                 push @intel,$intel;
122         }
123         push @intellists,{Message => 'Incoming fleets', Intel => \@intel, Origin => 1};
124
125         my $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
126         $query->execute($planet->{id}) or print $DBH->errstr;
127         my @intel;
128         while (my $intel = $query->fetchrow_hashref){
129                 if ($intel->{ingal}){
130                         $intel->{missionclass} = 'ingal';
131                 }else{
132                         $intel->{missionclass} = $intel->{mission};
133                 }
134                 push @intel,$intel;
135         }
136         push @intellists,{Message => 'Outgoing Fleets', Intel => \@intel, Target => 1};
137
138         $BODY->param(IntelLIsts => \@intellists);
139
140 }elsif(!param('coords')){
141         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
142                         AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL) AND i.mission != 'Attack')
143                                 OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
144                         AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
145                         $showticks}));
146         $query->execute() or print $DBH->errstr;
147
148         my @intellists;
149         my @intel;
150         while (my $intel = $query->fetchrow_hashref){
151                 if ($intel->{ingal}){
152                         $intel->{missionclass} = 'ingal';
153                 }else{
154                         $intel->{missionclass} = $intel->{mission};
155                 }
156                 push @intel,$intel;
157         }
158         push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
159         $BODY->param(IntelLIsts => \@intellists);
160 }
161 my $query = $DBH->prepare(q{SELECT i.id, u.username, i.message, report_date FROM intel_messages i
162         JOIN users u ON u.uid = i.uid
163         WHERE NOT handled ORDER BY report_date});
164 $query->execute;
165 my @messages;
166 while (my $message = $query->fetchrow_hashref){
167         $message->{message} = parseMarkup($message->{message});
168         push @messages,$message;
169 }
170 $BODY->param(IntelMessages => \@messages);
171 1;