]> ruin.nu Git - ndwebbie.git/blobdiff - lib/NDWeb/Controller/Raids.pm
Add a calc link on raids
[ndwebbie.git] / lib / NDWeb / Controller / Raids.pm
index 205e1056fb8485ec35e3cc4f67af3877a04edd71..d13b6704895b533fddf03e6ae9e6298c7dc6e2ae 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use parent 'Catalyst::Controller';
 
-use POSIX;
+use POSIX qw/floor pow/;
 use NDWeb::Include;
 use ND::Include;
 
@@ -96,124 +96,104 @@ sub view : Local {
        $c->forward('findRaid');
        $raid = $c->stash->{raid};
 
+       $c->stash(comma => \&comma_value);
 
        $c->stash(raid => $raid->{id});
-       my $noingal = '';
        my $planet;
        if ($c->user->planet){
                my $query = $dbh->prepare("SELECT value, score,x,y FROM current_planet_stats WHERE id = ?");
                $planet = $dbh->selectrow_hashref($query,undef,$c->user->planet);
-               $noingal = "AND NOT (x = $planet->{x} AND y = $planet->{y})";
        }
        $c->stash(message => parseMarkup($raid->{message}));
        $c->stash(landingtick => $raid->{tick});
-       my $targetquery = $dbh->prepare(qq{SELECT r.id, r.planet, size, score, value
+       my $targetquery = $dbh->prepare(q{SELECT r.id, r.planet, size, score, value
                , p.x,p.y,p.z, race
-               , p.value - p.size*200 - 
-                       COALESCE(ps.metal+ps.crystal+ps.eonium,0)/150 - 
-                       COALESCE(ss.total ,(SELECT
+               , p.value - p.size*200 -
+                       COALESCE(ps.metal+ps.crystal+ps.eonium,0)/150 -
+                       COALESCE(ds.total ,(SELECT
                                COALESCE(avg(total),0) FROM
-                               structure_scans)::int)*1500 AS fleetvalue
+                               current_development_scans)::int)*1500 AS fleetvalue
                ,(metal+crystal+eonium)/100 AS resvalue, comment
-               , hidden, light, medium, heavy
-               FROM current_planet_stats p 
-               JOIN raid_targets r ON p.id = r.planet 
-               LEFT OUTER JOIN planet_scans ps ON p.id = ps.planet
-               LEFT OUTER JOIN structure_scans ss ON p.id = ss.planet
-               WHERE r.raid = ?
-               $noingal
+               , hidden, light, medium, heavy, metal, crystal, eonium
+               ,metal_roids, crystal_roids, eonium_roids
+               ,amps, distorters, light_fac, medium_fac, heavy_fac
+               ,hulls, waves
+               FROM current_planet_stats p
+                       JOIN raid_targets r ON p.id = r.planet
+                       LEFT OUTER JOIN current_planet_scans ps ON p.id = ps.planet
+                       LEFT OUTER JOIN current_development_scans ds ON p.id = ds.planet
+               WHERE r.raid = $1
+                       AND NOT COALESCE(p.x = $2 AND p.y = $3,False)
                ORDER BY size});
-       $targetquery->execute($raid->{id});
+       $targetquery->execute($raid->{id},$planet->{x},$planet->{y});
        my @targets;
-       my %production = (0 => 'None', 35 => 'Light', 65 => 'Medium', 100 => 'High');
        while (my $target = $targetquery->fetchrow_hashref){
-               my %target;
-               if ($planet){
+               if ($planet && $planet->{x}){
                        if ($planet->{x} == $target->{x}){
-                               $target{style} = 'incluster';
+                               $target->{style} = 'incluster';
                        }
-                       $target{scorebash} = 'bash' if ($target->{score}/$planet->{score} < 0.4);
-                       $target{valuebash} = 'bash' if ($target->{value}/$planet->{value} < 0.4);
+                       $target->{scorebash} = 'bash' if ($target->{score}/$planet->{score} < 0.4);
+                       $target->{valuebash} = 'bash' if ($target->{value}/$planet->{value} < 0.4);
                        #next if ($target->{score}/$planet->{score} < 0.4) && ($target->{value}/$planet->{value} < 0.4);
                }
-               $target{id} = $target->{id};
-               $target{race} = $target->{race};
-               my $num = pow(10,length($target->{score})-2);
-               $target{score} = "Hidden"; #ceil($target->{score}/$num)*$num;
-               $num = pow(10,length($target->{value})-2);
-               $target{value} = "Hidden"; #ceil($target->{value}/$num)*$num;
-               $num = pow(10,length($target->{size})-2);
-               $target{size} = floor($target->{size}/$num)*$num;
-               $num = pow(10,length($target->{fleetvalue})-2);
-               $target{fleetvalue} = floor($target->{fleetvalue}/$num)*$num;
-               if (defined $target->{resvalue}){
-                       $num = pow(10,length($target->{resvalue})-2);
-                       $target{resvalue} = floor($target->{resvalue}/$num)*$num;
-               }
-               $target{comment} = parseMarkup($target->{comment}) if ($target->{comment});
-               
-               $target{hidden} = int($target->{hidden} / 100);
-               $target{light} = $production{$target->{light}};
-               $target{medium} = $production{$target->{medium}};
-               $target{heavy} = $production{$target->{heavy}};
-
-               my $unitscans = $dbh->prepare(q{ 
-                       SELECT DISTINCT ON (name) i.id,i.name, i.tick, i.amount 
-                       FROM fleets i
-                       WHERE  i.uid = -1
-                               AND i.sender = ?
-                               AND i.mission = 'Full fleet'
-                       GROUP BY i.id,i.tick,i.name,i.amount
-                       ORDER BY name,i.tick DESC
+
+               my $unitscans = $dbh->prepare(q{
+SELECT DISTINCT ON (name) fid, name, tick, amount
+FROM fleets
+WHERE planet = ?
+       AND mission = 'Full fleet'
+GROUP BY fid,tick,name,amount
+ORDER BY name,tick DESC
                });
                $unitscans->execute($target->{planet});
                my $ships = $dbh->prepare(q{SELECT ship,amount FROM fleet_ships
-                       WHERE id = ? ORDER BY num
+                       WHERE fid = ? ORDER BY num
                });
                my @missions;
                while (my $mission = $unitscans->fetchrow_hashref){
                        my @ships;
-                       $ships->execute($mission->{id});
+                       $ships->execute($mission->{fid});
                        while (my $ship = $ships->fetchrow_hashref){
                                push @ships,$ship;
                        }
                        push @ships, {ship => 'No', amount => 'ships'} if @ships == 0;
                        $mission->{ships} = \@ships;
-                       $mission->{amount} =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g; #Add comma for ever 3 digits, i.e. 1000 => 1,000
-                       delete $mission->{id};
                        push @missions,$mission;
                }
-               $target{missions} = \@missions;
-
-               my $query = $dbh->prepare(q{SELECT DISTINCT ON(rid) tick,category,name,amount
-                       FROM planet_data pd JOIN planet_data_types pdt ON pd.rid = pdt.id
-                       WHERE pd.id = $1 AND rid in (1,2,3,4,5,6,9,10,14,15,16,17,18)
-                       ORDER BY rid,tick DESC
-               });
-               $query->execute($target->{planet});
-               while (my $data = $query->fetchrow_hashref){
-                       $data->{amount} =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g; #Add comma for ever 3 digits, i.e. 1000 => 1,000
-                       $data->{name} =~ s/ /_/g;
-                       $target{$data->{category}.$data->{name}} = $data->{amount};
-               }
+               $target->{missions} = \@missions;
 
                my @roids;
                my @claims;
-               my $size = $target{size};
+               my $size = $target->{size};
                for (my $i = 1; $i <= $raid->{waves}; $i++){
                        my $roids = floor(0.25*$size);
                        $size -= $roids;
                        my $xp = 0;
-                       if ($planet){
+                       if ($planet && $planet->{score}){
                                $xp = pa_xp($roids,$planet->{score},$planet->{value},$target->{score},$target->{value});
                        }
                        push @roids,{wave => $i, roids => $roids, xp => $xp};
                        push @claims,{wave => $i}
                }
-               $target{roids} = \@roids;
-               $target{claims} = \@claims;
+               $target->{roids} = \@roids;
+               $target->{claims} = \@claims;
 
-               push @targets,\%target;
+               my $num = pow(10,length($target->{score})-2);
+               $target->{score} = "Hidden"; #ceil($target->{score}/$num)*$num;
+               $num = pow(10,length($target->{value})-2);
+               $target->{value} = "Hidden"; #ceil($target->{value}/$num)*$num;
+               $num = pow(10,length($target->{size})-2);
+               $target->{size} = floor($target->{size}/$num)*$num;
+               $num = pow(10,length($target->{fleetvalue})-2);
+               $target->{fleetvalue} = floor($target->{fleetvalue}/$num)*$num;
+               if (defined $target->{resvalue}){
+                       $num = pow(10,length($target->{resvalue})-2);
+                       $target->{resvalue} = floor($target->{resvalue}/$num)*$num;
+               }
+               $target->{comment} = parseMarkup($target->{comment}) if ($target->{comment});
+               $target->{hidden} = int($target->{hidden} / 100) if $target->{hidden};
+
+               push @targets,$target;
        }
        @targets = sort {$b->{roids}[0]{xp} <=> $a->{roids}[0]{xp} or $b->{size} <=> $a->{size}} @targets;
 
@@ -248,15 +228,16 @@ sub edit : Local {
        $c->stash(removegroups => \@remgroups);
        $c->stash(addgroups => \@addgroups);
 
-       if ($order =~ /^(score|size|value|xp)rank$/){
+       if ($order && $order =~ /^(score|size|value|xp)rank$/){
                $order .= " ASC";
-       }elsif ($order eq 'race'){
+       }elsif ($order && $order eq 'race'){
                $order .= ' ASC';
        }else {
-               $order .= 'p.x,p.y,p.z';
+               $order = 'p.x,p.y,p.z';
        }
 
-       my $targetquery = $dbh->prepare(qq{SELECT r.id,coords(x,y,z),comment,size,score,value,race,planet_status AS planetstatus,relationship,comment,r.planet, s.scans
+       my $targetquery = $dbh->prepare(qq{SELECT r.id,coords(x,y,z),comment,size
+               ,score,value,race,planet_status,relationship,comment,r.planet, s.scans
                FROM raid_targets r
                        JOIN current_planet_stats p ON p.id = r.planet
                        LEFT OUTER JOIN ( SELECT planet, array_accum(s::text) AS scans
@@ -403,6 +384,7 @@ sub posttargetupdates : Local {
                WHERE target = ? AND wave = ? AND uid = -2
                });
        my $remove = $dbh->prepare(q{DELETE FROM raid_targets WHERE raid = ? AND id = ?});
+
        for $_ ($c->req->param()){
                if (/^comment:(\d+)$/){
                        $comment->execute(html_escape $c->req->param($_),$1);
@@ -445,51 +427,61 @@ sub posttargetupdates : Local {
 sub open : Local {
        my ($self, $c, $raid) = @_;
 
+       $c->model->begin_work;
        $c->model->do(q{UPDATE raids SET open = TRUE, removed = FALSE WHERE id = ?}
                ,undef,$raid);
        $c->forward('log',[$raid, "BC opened raid"]);
+       $c->model->commit;
 
-       $c->res->redirect($c->req->referer);
+       $c->forward('/redirect');
 }
 
 sub close : Local {
        my ($self, $c, $raid) = @_;
 
+       $c->model->begin_work;
        $c->model->do(q{UPDATE raids SET open = FALSE WHERE id = ?}
                ,undef,$raid);
        $c->forward('log',[$raid, "BC closed raid"]);
+       $c->model->commit;
 
-       $c->res->redirect($c->req->referer);
+       $c->forward('/redirect');
 }
 
 sub remove : Local {
        my ($self, $c, $raid) = @_;
 
+       $c->model->begin_work;
        $c->model->do(q{UPDATE raids SET open = FALSE, removed = TRUE WHERE id = ?}
                ,undef,$raid);
        $c->forward('log',[$raid, "BC removed raid"]);
+       $c->model->commit;
 
-       $c->res->redirect($c->req->referer);
+       $c->forward('/redirect');
 }
 
 sub showcoords : Local {
        my ($self, $c, $raid) = @_;
 
+       $c->model->begin_work;
        $c->model->do(q{UPDATE raids SET released_coords = TRUE WHERE id = ?}
                ,undef,$raid);
        $c->forward('log',[$raid, "BC released coords"]);
+       $c->model->commit;
 
-       $c->res->redirect($c->req->referer);
+       $c->forward('/redirect');
 }
 
 sub hidecoords : Local {
        my ($self, $c, $raid) = @_;
 
+       $c->model->begin_work;
        $c->model->do(q{UPDATE raids SET released_coords = FALSE WHERE id = ?}
                ,undef,$raid);
        $c->forward('log',[$raid, "BC hid coords"]);
+       $c->model->commit;
 
-       $c->res->redirect($c->req->referer);
+       $c->forward('/redirect');
 }
 
 sub create : Local {
@@ -503,6 +495,7 @@ sub postcreate : Local {
        my ($self, $c) = @_;
        my $dbh = $c->model;
 
+       $dbh->begin_work;
        my $query = $dbh->prepare(q{INSERT INTO raids (tick,waves,message) VALUES(?,?,?) RETURNING (id)});
        $query->execute($c->req->param('tick'),$c->req->param('waves')
                ,html_escape $c->req->param('message'));
@@ -524,6 +517,9 @@ sub postcreate : Local {
                $addtarget->execute($raid,\@targets,\@gals,$c->req->param('sizelimit'));
                $c->forward('log',[$raid,"BC added planets (@targets) and the gals for (@gals)"]);
        }
+       $dbh->do(q{INSERT INTO raid_access (raid,gid) VALUES(?,2)}
+               ,undef,$raid);
+       $dbh->commit;
 
        $c->res->redirect($c->uri_for('edit',$raid));
 }
@@ -534,11 +530,12 @@ sub targetlist : Local {
 
        $c->stash(comma => \&comma_value);
        $c->stash(allies => $alliances);
+       $alliances ||= '';
        my @alliances = split /,/, $alliances;
 
        $c->forward('listAlliances');
 
-       if ($order =~ /^(sizerank|valuerank|scorerank|xprank|nfvalue|nfvalue2)$/){
+       if ($order && $order =~ /^(sizerank|valuerank|scorerank|xprank|nfvalue|nfvalue2)$/){
                $order = "$1";
        }else{
                $order = "nfvalue";
@@ -575,6 +572,71 @@ sub posttargetalliances : Local {
        $c->res->redirect($c->uri_for('targetlist',join ',',$c->req->param('alliances')));
 }
 
+sub targetcalc : Local {
+       my ($self, $c, $target) = @_;
+       my $dbh = $c->model;
+
+       my $target = $dbh->selectrow_hashref(q{
+SELECT p.id,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 ON rt.planet = p.id
+       LEFT OUTER JOIN current_planet_scans ps ON p.id = ps.planet
+       LEFT OUTER JOIN current_development_scans ds ON p.id = ds.planet
+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 id = $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 planet = $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->{id} 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;