1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 #**************************************************************************/
26 $ND::TEMPLATE->param(TITLE => 'Defense Calls');
28 die "You don't have access" unless isBC();
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
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
39 $call = $DBH->selectrow_hashref($query,undef,$1);
42 if (param('cmd') eq 'Submit'){
43 }elsif (param('cmd') eq 'Submit'){
46 if ($DBH->do(q{UPDATE calls SET landing_tick = ? WHERE id = ?}
47 ,undef,param('tick'),$call->{id})){
48 $call->{landing_tick} = param('tick');
50 print "<p> Something went wrong: ".$DBH->errstr."</p>";
54 if ($DBH->do(q{UPDATE calls SET info = ? WHERE id = ?}
55 ,undef,param('info'),$call->{id})){
56 $call->{info} = param('info');
58 print "<p> Something went wrong: ".$DBH->errstr."</p>";
61 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
62 }elsif(param('cmd') =~ /^(Cover|Uncover|Ignore|Open) call$/){
64 if (param('cmd') eq 'Cover call'){
65 $extra_vars = ", covered = TRUE, open = FALSE";
66 }elsif (param('cmd') eq 'Uncover call'){
67 $extra_vars = ", covered = FALSE, open = TRUE";
68 }elsif (param('cmd') eq 'Ignore call'){
69 $extra_vars = ", covered = FALSE, open = FALSE";
70 }elsif (param('cmd') eq 'Open call'){
71 $extra_vars = ", covered = FALSE, open = TRUE";
73 if ($DBH->do(qq{UPDATE calls SET dc = ? $extra_vars WHERE id = ?},
74 ,undef,$ND::UID,$call->{id})){
75 $call->{covered} = (param('cmd') eq 'Cover call');
76 $call->{open} = (param('cmd') =~ /^(Uncover|Open) call$/);
78 print "<p> Something went wrong: ".$DBH->errstr."</p>";
80 }elsif(param('cmd') eq 'Remove'){
82 my $query = $DBH->prepare(q{DELETE FROM incomings WHERE id = ? AND call = ?});
83 for my $param (param()){
84 if ($param =~ /^change:(\d+)$/){
85 if($query->execute($1,$call->{id})){
86 $LOG->execute($ND::UID,"DC deleted fleet: $1, call $call->{id}");
88 print "<p> Something went wrong: ".$DBH->errstr."</p>";
92 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
93 }elsif(param('cmd') eq 'Change'){
95 my $query = $DBH->prepare(q{UPDATE incomings SET shiptype = ? WHERE id = ? AND call = ?});
96 for my $param (param()){
97 if ($param =~ /^change:(\d+)$/){
98 my $shiptype = escapeHTML(param("shiptype:$1"));
99 if($query->execute($shiptype,$1,$call->{id})){
100 $LOG->execute($ND::UID,"DC set fleet: $1, call $call->{id} to: $shiptype");
102 print "<p> Something went wrong: ".$DBH->errstr."</p>";
106 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
111 $BODY->param(Call => $call->{id});
112 $BODY->param(Coords => $call->{coords});
113 $BODY->param(DefensePoints => $call->{defense_points});
114 $BODY->param(LandingTick => $call->{landing_tick});
115 $BODY->param(ETA => $call->{landing_tick}-$ND::TICK);
116 $BODY->param(Info => $call->{info});
117 if ($call->{covered}){
118 $BODY->param(Cover => 'Uncover');
120 $BODY->param(Cover => 'Cover');
122 if ($call->{open} && !$call->{covered}){
123 $BODY->param(Cover => 'Ignore');
125 $BODY->param(Cover => 'Open');
127 my $fleets = $DBH->prepare(q{
128 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 < ? ))
129 ORDER BY fleet ASC});
130 my $ships = $DBH->prepare('SELECT ship,amount FROM fleet_ships WHERE fleet = ?');
131 $fleets->execute($call->{member},$call->{landing_tick},$call->{landing_tick});
133 while (my $fleet = $fleets->fetchrow_hashref){
134 if ($fleet->{back} == $call->{landing_tick}){
135 $fleet->{Fleetcatch} = 1;
137 $ships->execute($fleet->{id});
139 while (my $ship = $ships->fetchrow_hashref){
142 $fleet->{Ships} = \@ships;
143 push @fleets, $fleet;
145 $BODY->param(Fleets => \@fleets);
147 my $attackers = $DBH->prepare(q{
148 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
150 JOIN current_planet_stats p ON i.sender = p.id
152 ORDER BY p.x,p.y,p.z});
153 $attackers->execute($call->{id});
155 while(my $attacker = $attackers->fetchrow_hashref){
156 push @attackers,$attacker;
158 $BODY->param(Attackers => \@attackers);
160 my $where = 'open AND c.landing_tick-6 > tick()';
161 if (param('show') eq 'covered'){
163 }elsif (param('show') eq 'all'){
165 }elsif (param('show') eq 'uncovered'){
166 $where = 'not covered';
168 my $query = $DBH->prepare(qq{
169 SELECT c.id, coords(p.x,p.y,p.z), u.defense_points, c.landing_tick,
170 TRIM('/' FROM concat(p2.race||'/')) AS race, TRIM('/' FROM concat(i.amount||'/')) AS amount,
171 TRIM('/' FROM concat(i.eta||'/')) AS eta, TRIM('/' FROM concat(i.shiptype||'/')) AS shiptype,
172 TRIM('/' FROM concat(c.landing_tick - tick() ||'/')) AS curreta,
173 TRIM('/' FROM concat(p2.alliance ||'/')) AS alliance,
174 TRIM('/' FROM concat(coords(p2.x,p2.y,p2.z) ||'/')) AS attackers
176 JOIN incomings i ON i.call = c.id
177 JOIN users u ON c.member = u.uid
178 JOIN current_planet_stats p ON u.planet = p.id
179 JOIN current_planet_stats p2 ON i.sender = p2.id
181 GROUP BY c.id, p.x,p.y,p.z, u.username, c.landing_tick, c.info,u.defense_points
182 ORDER BY c.landing_tick DESC
183 })or print $DBH->errstr;
184 $query->execute or print $DBH->errstr;
187 while (my $call = $query->fetchrow_hashref){
188 $call->{ODD} = $i % 2;
192 $BODY->param(Calls => \@calls);