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'){
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}");
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');
57 $LOG->execute($ND::UID,"DC updated info for call $call->{id}");
59 print "<p> Something went wrong: ".$DBH->errstr."</p>";
62 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
63 }elsif(param('cmd') =~ /^(Cover|Uncover|Ignore|Open|Take) call$/){
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";
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;
81 print "<p> Something went wrong: ".$DBH->errstr."</p>";
83 }elsif(param('cmd') eq 'Remove'){
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}");
91 print "<p> Something went wrong: ".$DBH->errstr."</p>";
95 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
96 }elsif(param('cmd') eq 'Change'){
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");
105 print "<p> Something went wrong: ".$DBH->errstr."</p>";
109 $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
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');
124 $BODY->param(Cover => 'Cover');
126 if ($call->{open} && !$call->{covered}){
127 $BODY->param(Ignore => 'Ignore');
129 $BODY->param(Ignore => 'Open');
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});
137 while (my $fleet = $fleets->fetchrow_hashref){
138 if ($fleet->{back} == $call->{landing_tick}){
139 $fleet->{Fleetcatch} = 1;
141 $ships->execute($fleet->{id});
143 while (my $ship = $ships->fetchrow_hashref){
146 $fleet->{Ships} = \@ships;
147 push @fleets, $fleet;
149 $BODY->param(Fleets => \@fleets);
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
154 JOIN current_planet_stats p ON i.sender = p.id
156 ORDER BY p.x,p.y,p.z});
157 $attackers->execute($call->{id});
159 while(my $attacker = $attackers->fetchrow_hashref){
160 push @attackers,$attacker;
162 $BODY->param(Attackers => \@attackers);
164 my $where = 'open AND c.landing_tick-6 > tick()';
165 if (param('show') eq 'covered'){
167 }elsif (param('show') eq 'all'){
169 }elsif (param('show') eq 'uncovered'){
170 $where = 'not covered';
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
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
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;
191 while (my $call = $query->fetchrow_hashref){
192 $call->{ODD} = $i % 2;
196 $BODY->param(Calls => \@calls);