]> ruin.nu Git - ndwebbie.git/blob - calls.pl
minor fixes
[ndwebbie.git] / calls.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 => 'Defense Calls');
27
28 die "You don't have access" unless isBC();
29
30 my $call;
31 if (param('call') =~ /^(\d+)$/){
32         my $query = $DBH->prepare(q{
33 SELECT c.id, coords(p.x,p.y,p.z), c.landing_tick, c.info, covered, open, dc.username AS dc, u.defense_points,c.member
34 FROM calls c 
35         JOIN users u ON c.member = u.uid
36         LEFT OUTER JOIN users dc ON c.dc = dc.uid
37         JOIN current_planet_stats p ON u.planet = p.id
38 WHERE c.id = ?});
39         $call = $DBH->selectrow_hashref($query,undef,$1);
40 }
41 if ($call){
42         if (param('cmd') eq 'Submit'){
43                 $DBH->begin_work;
44                 if (param('ctick')){
45                         if ($DBH->do(q{UPDATE calls SET landing_tick = ? WHERE id = ?}
46                                         ,undef,param('tick'),$call->{id})){
47                                 $call->{landing_tick} = param('tick');
48                                 $LOG->execute($ND::UID,"DC updated landing tick for call $call->{id}");
49                         }else{
50                                 print "<p> Something went wrong: ".$DBH->errstr."</p>";
51                         }
52                 }
53                 if (param('cinfo')){
54                         if ($DBH->do(q{UPDATE calls SET info = ? WHERE id = ?}
55                                         ,undef,param('info'),$call->{id})){
56                                 $call->{info} = param('info');
57                                 $LOG->execute($ND::UID,"DC updated info for call $call->{id}");
58                         }else{
59                                 print "<p> Something went wrong: ".$DBH->errstr."</p>";
60                         }
61                 }
62                 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
63         }elsif(param('cmd') =~ /^(Cover|Uncover|Ignore|Open|Take) call$/){
64                 print "test";
65                 my $extra_vars = '';
66                 if (param('cmd') eq 'Cover call'){
67                         $extra_vars = ", covered = TRUE, open = FALSE";
68                 }elsif (param('cmd') eq 'Uncover call'){
69                         $extra_vars = ", covered = FALSE, open = TRUE";
70                 }elsif (param('cmd') eq 'Ignore call'){
71                         $extra_vars = ", covered = FALSE, open = FALSE";
72                 }elsif (param('cmd') eq 'Open call'){
73                         $extra_vars = ", covered = FALSE, open = TRUE";
74                 }
75                 if ($DBH->do(qq{UPDATE calls SET dc = ? $extra_vars WHERE id = ?},
76                         ,undef,$ND::UID,$call->{id})){
77                         $call->{covered} = (param('cmd') eq 'Cover call');
78                         $call->{open} = (param('cmd') =~ /^(Uncover|Open) call$/);
79                         $call->{DC} = $ND::USER;
80                 }else{
81                         print "<p> Something went wrong: ".$DBH->errstr."</p>";
82                 }
83         }elsif(param('cmd') eq 'Remove'){
84                 $DBH->begin_work;
85                 my $query = $DBH->prepare(q{DELETE FROM incomings WHERE id = ? AND call = ?});
86                 for my $param (param()){
87                         if ($param =~ /^change:(\d+)$/){
88                                 if($query->execute($1,$call->{id})){
89                                         $LOG->execute($ND::UID,"DC deleted fleet: $1, call $call->{id}");
90                                 }else{
91                                         print "<p> Something went wrong: ".$DBH->errstr."</p>";
92                                 }
93                         }
94                 }
95                 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
96         }elsif(param('cmd') eq 'Change'){
97                 $DBH->begin_work;
98                 my $query = $DBH->prepare(q{UPDATE incomings SET shiptype = ? WHERE id = ? AND call = ?});
99                 for my $param (param()){
100                         if ($param =~ /^change:(\d+)$/){
101                                 my $shiptype = escapeHTML(param("shiptype:$1"));
102                                 if($query->execute($shiptype,$1,$call->{id})){
103                                         $LOG->execute($ND::UID,"DC set fleet: $1, call $call->{id} to: $shiptype");
104                                 }else{
105                                         print "<p> Something went wrong: ".$DBH->errstr."</p>";
106                                 }
107                         }
108                 }
109                 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
110         }
111 }
112
113 if ($call){
114         $BODY->param(Call => $call->{id});
115         $BODY->param(Coords => $call->{coords});
116         $BODY->param(DefensePoints => $call->{defense_points});
117         $BODY->param(LandingTick => $call->{landing_tick});
118         $BODY->param(ETA => $call->{landing_tick}-$ND::TICK);
119         $BODY->param(Info => $call->{info});
120         $BODY->param(DC => $call->{dc});
121         if ($call->{covered}){
122                 $BODY->param(Cover => 'Uncover');
123         }else{
124                 $BODY->param(Cover => 'Cover');
125         }
126         if ($call->{open} && !$call->{covered}){
127                 $BODY->param(Ignore => 'Ignore');
128         }else{
129                 $BODY->param(Ignore => 'Open');
130         }
131         my $fleets = $DBH->prepare(q{
132 SELECT id,mission,landing_tick,eta, (landing_tick+eta-1) AS back FROM fleets WHERE uid = ? AND (fleet = 0 OR (landing_tick + eta > ? AND landing_tick - eta - 11 < ? ))
133 ORDER BY fleet ASC});
134         my $ships = $DBH->prepare('SELECT ship,amount FROM fleet_ships WHERE fleet = ?');
135         $fleets->execute($call->{member},$call->{landing_tick},$call->{landing_tick});
136         my @fleets;
137         while (my $fleet = $fleets->fetchrow_hashref){
138                 if ($fleet->{back} == $call->{landing_tick}){
139                         $fleet->{Fleetcatch} = 1;
140                 }
141                 $ships->execute($fleet->{id});
142                 my @ships;
143                 while (my $ship = $ships->fetchrow_hashref){
144                         push @ships,$ship;
145                 }
146                 $fleet->{Ships} = \@ships;
147                 push @fleets, $fleet;
148         }
149         $BODY->param(Fleets => \@fleets);
150         
151         my $attackers = $DBH->prepare(q{
152 SELECT coords(p.x,p.y,p.z), p.planet_status, p.race,i.eta,i.amount,i.fleet,i.shiptype,p.relationship,p.alliance,i.id
153 FROM incomings i
154         JOIN current_planet_stats p ON i.sender = p.id
155 WHERE i.call = ?
156 ORDER BY p.x,p.y,p.z});
157         $attackers->execute($call->{id});
158         my @attackers;
159         while(my $attacker = $attackers->fetchrow_hashref){
160                 push @attackers,$attacker;
161         }
162         $BODY->param(Attackers => \@attackers);
163 }else{
164         my $where = 'open AND c.landing_tick-6 > tick()';
165         if (param('show') eq 'covered'){
166                 $where = 'covered';
167         }elsif (param('show') eq 'all'){
168                 $where = 'true';
169         }elsif (param('show') eq 'uncovered'){
170                 $where = 'not covered';
171         }
172         my $query = $DBH->prepare(qq{
173 SELECT c.id, coords(p.x,p.y,p.z), u.defense_points, c.landing_tick, 
174         TRIM('/' FROM concat(p2.race||'/')) AS race, TRIM('/' FROM concat(i.amount||'/')) AS amount,
175         TRIM('/' FROM concat(i.eta||'/')) AS eta, TRIM('/' FROM concat(i.shiptype||'/')) AS shiptype,
176         TRIM('/' FROM concat(c.landing_tick - tick() ||'/')) AS curreta,
177         TRIM('/' FROM concat(p2.alliance ||'/')) AS alliance,
178         TRIM('/' FROM concat(coords(p2.x,p2.y,p2.z) ||'/')) AS attackers
179 FROM calls c 
180         JOIN incomings i ON i.call = c.id
181         JOIN users u ON c.member = u.uid
182         JOIN current_planet_stats p ON u.planet = p.id
183         JOIN current_planet_stats p2 ON i.sender = p2.id
184 WHERE $where
185 GROUP BY c.id, p.x,p.y,p.z, u.username, c.landing_tick, c.info,u.defense_points
186 ORDER BY c.landing_tick DESC
187                 })or print $DBH->errstr;
188         $query->execute or print $DBH->errstr;
189         my @calls;
190         my $i = 0;
191         while (my $call = $query->fetchrow_hashref){
192                 $call->{ODD} = $i % 2;
193                 push @calls, $call;
194                 $i++;
195         }
196         $BODY->param(Calls => \@calls);
197 }
198 1;