X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=lib%2FNDWeb%2FController%2FCalls.pm;h=4033e5c06f6111a78ec04bde4b8847afef7edfc5;hb=2a703f7250ea25f2f556eb25b644dcb7268f92ce;hp=cf468a88aaaa4701ed3730d5f883879e32997277;hpb=11149303e8c0c031f5e08c5792930b490ba4a30b;p=ndwebbie.git diff --git a/lib/NDWeb/Controller/Calls.pm b/lib/NDWeb/Controller/Calls.pm index cf468a8..4033e5c 100644 --- a/lib/NDWeb/Controller/Calls.pm +++ b/lib/NDWeb/Controller/Calls.pm @@ -54,28 +54,29 @@ sub list : Local { my $query = $dbh->prepare(qq{ SELECT id,coords(x,y,z),planet,landing_tick,dc,curreta,fleets - ,(0.2*(attack_points/a.attack)+ 0.4*(defense_points/a.defense) + ,(0.2*(attack_points/GREATEST(a.attack,1)) + + 0.4*(defense_points/GREATEST(a.defense,1)) + 0.2*(c.size/a.size) + 0.05*(c.score/a.score) + 0.15*(c.value/a.value))::Numeric(3,2) AS defprio - ,array_accum(race::text) AS race - ,array_accum(amount) AS amount - ,array_accum(eta) AS eta - ,array_accum(shiptype) AS shiptype + ,array_accum(COALESCE(race::text,'')) AS race + ,array_accum(COALESCE(amount,0)) AS amount + ,array_accum(COALESCE(eta,0)) AS eta + ,array_accum(COALESCE(shiptype,'')) AS shiptype ,array_accum(COALESCE(alliance,'?')) AS alliance ,array_accum(coords) AS attackers FROM (SELECT c.id, p.x,p.y,p.z,p.id AS planet, p.size, p.value, p.score ,u.defense_points, u.attack_points, c.landing_tick ,dc.username AS dc, (c.landing_tick - tick()) AS curreta ,p2.race, i.amount, i.eta, i.shiptype, p2.alliance - ,coords(p2.x,p2.y,p2.z), COUNT(DISTINCT f.id) AS fleets + ,coords(p2.x,p2.y,p2.z), COUNT(DISTINCT f.fid) AS fleets FROM calls c JOIN users u ON c.member = u.uid JOIN current_planet_stats p ON u.planet = p.id LEFT OUTER JOIN incomings i ON i.call = c.id LEFT OUTER JOIN current_planet_stats p2 ON i.sender = p2.id LEFT OUTER JOIN users dc ON c.dc = dc.uid - LEFT OUTER JOIN fleets f ON f.target = u.planet - AND f.tick = c.landing_tick AND f.back = f.tick + f.eta - 1 + LEFT OUTER JOIN launch_confirmations f ON f.target = u.planet + AND f.landing_tick = c.landing_tick AND f.back = f.landing_tick + f.eta - 1 WHERE $where GROUP BY c.id, p.x,p.y,p.z,p.id, p.size, p.score, p.value, c.landing_tick ,u.defense_points,u.attack_points,dc.username @@ -112,18 +113,38 @@ sub edit : Local { my $outgoings = $dbh->prepare(q{ - SELECT i.id,i.mission, i.name, i.tick,eta - , i.amount, coords(x,y,z) AS coords, t.id AS planet - FROM fleets i - LEFT OUTER JOIN (planets - NATURAL JOIN planet_stats) t ON i.target = t.id - AND t.tick = ( SELECT MAX(tick) FROM planet_stats) - WHERE i.sender = $1 - AND (i.tick > $2 - 14 OR i.mission = 'Full fleet') - ORDER BY i.tick,x,y,z +( + SELECT DISTINCT ON (mission,name) fid,mission,name,tick, NULL AS eta + ,amount, NULL AS coords, planet, NULL AS back, NULL AS recalled + FROM fleets f + WHERE planet = $1 AND tick <= $2 AND ( + mission = 'Full fleet' + OR fid IN (SELECT fid FROM fleet_scans) + ) AND ( + mission = 'Full fleet' + OR tick >= $2 - 12 + ) + ORDER BY mission,name,tick DESC +) UNION ( + SELECT fid,mission,name,landing_tick AS tick, eta, amount + , coords(x,y,z), t.id AS planet, back + , (back <> landing_tick + eta - 1) AS recalled + FROM fleets f + JOIN launch_confirmations USING (fid) + LEFT OUTER JOIN current_planet_stats t ON target = t.id + WHERE f.planet = $1 AND back >= $2 AND landing_tick - eta - 12 < $2 +) UNION ( + SELECT DISTINCT ON (tick,x,y,z,mission,name,amount) + NULL as fid, i.mission, i.name, i.tick,eta + , i.amount, coords(x,y,z), t.id AS planet, back, NULL AS recalled + FROM intel i + LEFT OUTER JOIN current_planet_stats t ON i.target = t.id + WHERE uid = -1 AND i.sender = $1 AND i.tick > $2 - 14 AND i.tick < $2 + 14 + ORDER BY i.tick,x,y,z,mission,name,amount,back +) }); my $ships = $dbh->prepare(q{SELECT ship,amount FROM fleet_ships - WHERE id = ? ORDER BY num + WHERE fid = ? ORDER BY num }); $outgoings->execute($call->{planet},$call->{landing_tick}); my @fleets; @@ -132,34 +153,44 @@ sub edit : Local { $fleet->{back} == $call->{landing_tick}){ $fleet->{fleetcatch} = 1; } - $ships->execute($fleet->{id}); - if ($ships->rows != 0){ + if ($fleet->{fid}){ + $ships->execute($fleet->{fid}); my @ships; while (my $ship = $ships->fetchrow_hashref){ push @ships,$ship; } + push @ships, {ship => 'No', amount => 'ships'} if @ships == 0; $fleet->{ships} = \@ships; } push @fleets, $fleet; } + + my $available = $dbh->prepare(q{ +SELECT ship,amount from ships_home WHERE planet = $1 AND tick = $2 + }); + $available->execute($call->{planet}, $call->{landing_tick}); + my $fleet = {fid => $call->{member}, mission => 'Available' + , name => 'At home', ships => $available->fetchall_arrayref({}) + }; + push @fleets, $fleet; + $c->stash(fleets => \@fleets); my $defenders = $dbh->prepare(q{ - SELECT DISTINCT ON (i.tick,x,y,z,s.id,i.name,i.amount) i.id,i.mission, i.name, i.tick,eta - , i.amount, coords(x,y,z) AS coords, s.id AS planet - FROM fleets i - LEFT OUTER JOIN (planets - NATURAL JOIN planet_stats) s ON i.sender = s.id - AND s.tick = ( SELECT MAX(tick) FROM planet_stats) - WHERE i.target = ? - AND i.tick = ? AND i.mission = 'Defend' - ORDER BY i.tick,x,y,z +SELECT DISTINCT ON (x,y,z,s.id,name,amount,back) fid,mission, name, eta + , amount, coords(x,y,z) AS coords, landing_tick AS tick, f.planet + ,back, (back <> landing_tick + eta - 1) AS recalled +FROM launch_confirmations lc + JOIN fleets f USING (fid) + LEFT OUTER JOIN current_planet_stats s ON f.planet = s.id +WHERE target = ? AND landing_tick = ? AND mission = 'Defend' +ORDER BY x,y,z }); $defenders->execute($call->{planet},$call->{landing_tick}); my @defenders; while (my $fleet = $defenders->fetchrow_hashref){ - $ships->execute($fleet->{id}); + $ships->execute($fleet->{fid}); if ($ships->rows != 0){ my @ships; while (my $ship = $ships->fetchrow_hashref){ @@ -185,8 +216,8 @@ sub edit : Local { $outgoings->execute($attacker->{planet},$call->{landing_tick}); my @missions; while (my $mission = $outgoings->fetchrow_hashref){ - $ships->execute($mission->{id}); - if ($ships->rows != 0){ + if ($mission->{fid}){ + $ships->execute($mission->{fid}); my @ships; while (my $ship = $ships->fetchrow_hashref){ push @ships,$ship; @@ -196,7 +227,6 @@ sub edit : Local { } push @missions,$mission; } - delete $attacker->{planet}; $attacker->{missions} = \@missions; push @attackers,$attacker; } @@ -205,6 +235,29 @@ sub edit : Local { $c->forward('/forum/findPosts',[$call->{ftid}]); } +sub defleeches : Local { + my ( $self, $c, $type ) = @_; + my $dbh = $c->model; + + my $query = $dbh->prepare(q{SELECT username,defense_points,count(id) AS calls + , SUM(fleets) AS fleets, SUM(recalled) AS recalled + ,count(NULLIF(fleets,0)) AS defended_calls + FROM (SELECT username,defense_points,c.id,count(f.target) AS fleets + , count(NULLIF(f.landing_tick + f.eta -1 = f.back,TRUE)) AS recalled + FROM users u JOIN calls c ON c.member = u.uid + LEFT OUTER JOIN ( + SELECT * FROM launch_confirmations JOIN fleets USING (fid) + WHERE mission = 'Defend' + ) f ON u.planet = f.target AND c.landing_tick = f.landing_tick + GROUP BY username,defense_points,c.id + ) d + GROUP BY username,defense_points ORDER BY fleets DESC, defense_points + }); + $query->execute; + + $c->stash(members => $query->fetchall_arrayref({}) ); +} + sub postcallcomment : Local { my ($self, $c, $call) = @_; @@ -239,6 +292,12 @@ sub postcallupdate : Local { ,undef,$c->req->param('info'),$call->{id}); $log->execute($c->user->id,$call->{ftid},"Updated info"); } + if ($c->req->param('ccalc')){ + my $calc = $c->req->param('calc'); + $dbh->do(q{UPDATE calls SET calc = ? WHERE id = ?} + ,undef,$calc,$call->{id}); + $log->execute($c->user->id,$call->{ftid},html_escape('Updated calc to: [URL]'.$calc.'[/URL]')); + } }elsif($c->req->param('cmd') =~ /^(Cover|Uncover|Ignore|Open|Take) call$/){ my $extra_vars = ''; if ($1 eq 'Cover'){ @@ -308,8 +367,8 @@ sub findCall : Private { my $query = $dbh->prepare(q{ 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 - ,u.planet, u.username AS member, u.sms,c.ftid + ,open, dc.username AS dc, u.defense_points,c.member AS uid + ,u.planet, u.username AS member, u.sms,c.ftid,calc FROM calls c JOIN users u ON c.member = u.uid LEFT OUTER JOIN users dc ON c.dc = dc.uid @@ -318,7 +377,7 @@ sub findCall : Private { }); $call = $dbh->selectrow_hashref($query,undef,$call); - $c->assert_user_roles(qw/calls_edit/) unless $c->user->id == $call->{member}; + $c->assert_user_roles(qw/calls_edit/) unless $c->user->id == $call->{uid}; $c->stash(call => $call); }