+sub targetlist : Local {
+ my ($self, $c, $alliances, $order) = @_;
+ my $dbh = $c->model;
+
+ $c->stash(allies => $alliances);
+ $alliances ||= '';
+ my @alliances = split /,/, $alliances;
+
+ $c->forward('listAlliances');
+
+ if ($order && $order =~ /^(sizerank|valuerank|scorerank|xprank|nfvalue|nfvalue2)$/){
+ $order = "$1";
+ }else{
+ $order = "nfvalue";
+ }
+ $order = "p.$order" if $order =~ /rank$/;
+
+ my $query = $dbh->prepare(q{
+SELECT p.pid AS id, coords(p.x,p.y,p.z),p.x,p.y,p.alliance, p.score, p.value, p.size, p.xp,nfvalue, nfvalue - sum(pa.value) AS nfvalue2, p.race
+FROM current_planet_stats p
+ JOIN (SELECT g.x,g.y, sum(p.value) AS nfvalue
+ FROM galaxies g join current_planet_stats p on g.x = p.x AND g.y = p.y
+ WHERE g.tick = (SELECT max(tick) from galaxies)
+ AND (planet_status IN ('','Hostile')
+ AND (relationship IS NULL OR relationship IN ('','Hostile')))
+ GROUP BY g.x,g.y
+ ) g USING (x,y)
+ JOIN current_planet_stats pa USING (x,y,aid)
+WHERE p.x <> 200
+ AND aid = ANY ($1)
+ AND p.relationship IN ('','Hostile')
+GROUP BY p.pid, p.x,p.y,p.z,p.alliance, p.score, p.value, p.size, p.xp, nfvalue,p.race
+ ,p.scorerank,p.valuerank,p.sizerank,p.xprank
+ORDER BY
+ } . $order);
+ $query->execute(\@alliances);
+ $c->stash(planets => $query->fetchall_arrayref({}) );
+ $c->forward('create');
+}
+
+sub posttargetalliances : Local {
+ my ($self, $c) = @_;
+
+ $c->res->redirect($c->uri_for('targetlist',join ',',$c->req->param('alliances')));
+}
+
+sub targetcalc : Local {
+ my ($self, $c, $target) = @_;
+ my $dbh = $c->model;
+
+ $target = $dbh->selectrow_hashref(q{
+SELECT pid,p.value,p.score,metal_roids, crystal_roids, eonium_roids, ds.total, race
+FROM raids r
+ JOIN raid_targets rt ON r.id = rt.raid
+ JOIN current_planet_stats p USING (pid)
+ LEFT OUTER JOIN current_planet_scans ps USING (pid)
+ LEFT OUTER JOIN current_development_scans ds USING (pid)
+WHERE rt.id = ? AND r.open AND not r.removed
+ AND r.id IN (SELECT raid FROM raid_access NATURAL JOIN groupmembers WHERE uid = ?)
+ },undef,$target,$c->user->id);
+
+ my $planet = $dbh->selectrow_hashref(q{
+SELECT score,value FROM current_planet_stats WHERE pid = $1
+ },undef,$c->user->planet);
+
+ my %races = (Ter => 1, Cat => 2, Xan => 3, Zik => 4, Etd => 5);
+ my @query = (
+ "def_1_race=$races{$target->{race}}",
+ "def_structures=$target->{total}",
+ "def_metal_asteroids=$target->{metal_roids}",
+ "def_crystal_asteroids=$target->{crystal_roids}",
+ "def_eonium_asteroids=$target->{eonium_roids}",
+ #"def_planet_value_1=$target->{value}",
+ #"def_planet_score_1=$target->{score}",
+ "att_planet_value_1=$planet->{value}",
+ "att_planet_score_1=$planet->{score}",
+ );
+
+ my $fleets = $dbh->prepare(q{
+SELECT DISTINCT ON (name) name, tick, fid FROM fleets
+WHERE pid = $1 AND mission = 'Full fleet'
+ORDER BY name ASC, tick DESC
+ });
+ my $ships = $dbh->prepare(q{
+SELECT id, amount FROM fleet_ships fs JOIN ship_stats s ON s.name = fs.ship
+WHERE fid = $1
+ });
+
+ for ('def','att'){
+ my $planet = $c->user->planet;
+ $planet = $target->{pid} if /^def/;
+ $fleets->execute($planet);
+ my $nrfleets = 0;
+ my $tick = 0;
+ while (my $fleet = $fleets->fetchrow_hashref){
+ $ships->execute($fleet->{fid});
+ next unless $tick < $fleet->{tick};
+ $tick = $fleet->{tick};
+ ++$nrfleets;
+ while (my $ship = $ships->fetchrow_hashref){
+ push @query, "${_}_${nrfleets}_$ship->{id}=$ship->{amount}";
+ }
+ }
+ push @query, "${_}_fleets=$nrfleets";
+ }
+ my $query = join '&', @query;
+ #$c->res->body("http://game.planetarion.com/bcalc.pl?$query");
+ $c->res->redirect("http://game.planetarion.com/bcalc.pl?$query");
+}
+
+
+sub listAlliances : Private {
+ my ($self, $c) = @_;
+ my @alliances;
+ my $query = $c->model->prepare(q{SELECT aid AS id,alliance AS name FROM alliances
+ WHERE relationship IN ('','Hostile')
+ AND alliance IN (SELECT alliance FROM planets)
+ ORDER BY LOWER(alliance)
+ });
+ $query->execute;
+ push @alliances,{id => '', name => ''};
+ while (my $ally = $query->fetchrow_hashref){
+ push @alliances,$ally;
+ }
+ $c->stash(alliances => \@alliances);
+}
+