#!/usr/bin/perl -w
use strict;
use CGI qw/:standard *ul *table *Tr/;
$|=1;
print header,"\n",
start_html("Mineral Compression"),"\n";
print h1(a({href=>url,},"Mineral Compression")),"\n";
## TODO: config
my $opt_d = 'dbo';
my $opt_u = 'dbo';
my $opt_p = undef;
use DBI;
my $dbh = DBI->connect("DBI:mysql:database=$opt_d", $opt_u, $opt_p);
die "cant connect to db: ".DBI->errstr unless $dbh;
use EVE::Market;
my $market = EVE::Market->new(
where => "connectedhighsec",
when => 30*24*60*60,
) or die "cant get market";
my %prices = (
34 => 3.5,
35 => 2.8,
36 => 24,
37 => 110,
38 => 45,
39 => 2250,
40 => 3900,
);
for (keys %prices) {
my $p = eval { $market->get_price(typeID => $_)->{avg} } || 0;
if ($p) {
$prices{$_} = $p;
}
}
my %toobad = ();
my $sth_toocomplex = $dbh->prepare(qq(
select
distinct b.productTypeID
from
invBlueprintTypes b,
typeActivityMaterials m
where
b.blueprintTypeID = m.typeID
and m.activityID = 1
and (
m.requiredTypeID not in (34, 35, 36, 37, 38, 39, 40)
or
m.recycle = 1
)
)) or die $dbh->errstr;
$sth_toocomplex->execute() or die $dbh->errstr;
my $tc = $sth_toocomplex->fetchall_arrayref();
$sth_toocomplex->finish();
for (@$tc) {
my ($tcid,) = @$_;
$toobad{$tcid} = 1;
}
printf "got %i toocomplex
\n", scalar @$tc;
my $sth_lossy = $dbh->prepare(qq(
select
distinct r.typeID
from
typeActivityMaterials r,
invBlueprintTypes b,
typeActivityMaterials m
where
m.activityID = 1
and r.activityID = 6
and m.typeID = b.blueprintTypeID
and r.typeID = b.productTypeID
and r.requiredTypeID = m.requiredTypeID
and r.quantity < m.quantity
)) or die $dbh->errstr;
$sth_lossy->execute() or die $dbh->errstr;
my $tl = $sth_lossy->fetchall_arrayref();
$sth_lossy->finish();
for (@$tl) {
my ($tcid,) = @$_;
$toobad{$tcid} = 1;
}
printf "got %i lossy
\n", scalar @$tl;
my $sth_meta = $dbh->prepare(qq(
select
distinct typeID
from
dgmTypeAttributes
where
attributeID = 633
and coalesce(valueFloat, valueInt) > 5
)) or die $dbh->errstr;
$sth_meta->execute() or die $dbh->errstr;
my $ml = $sth_meta->fetchall_arrayref();
$sth_meta->finish();
for (@$ml) {
my ($tcid,) = @$_;
$toobad{$tcid} = 1;
}
printf "got %i meta
\n", scalar @$ml;
my $sth_cands = $dbh->prepare(qq(
select
r.typeID,
sum(r.quantity)
from
typeActivityMaterials r
where
r.activityID = 6
and r.requiredTypeID in (34, 35, 36, 37, 38, 39, 40)
group by r.typeID
)) or die $dbh->errstr;
$sth_cands->execute() or die $dbh->errstr;
$tc = $sth_cands->fetchall_arrayref();
$sth_cands->finish();
my %cands=();
my $skips=0;
for (@$tc) {
my ($id,$quant,) = @$_;
if ($toobad{$id}) {
$skips++;
next;
}
$cands{$id} = $quant;
}
printf "skipped %i candidates
\n", $skips;
printf "got %i candidates
\n", scalar keys %cands;
my $sth_params = $dbh->prepare(qq(
select
i.typeName,
i.volume,
i.portionSize,
i.typeID
from
invTypes i
where
i.typeID = ?
and i.published = 1
)) or die $dbh->errstr;
my $sth_bpo = $dbh->prepare(qq(
select
p.typeID,
p.basePrice
from
invTypes i,
invBlueprintTypes b,
invTypes p
where
i.typeID = ?
and b.productTypeID = i.typeID
and p.typeID = b.blueprintTypeID
)) or die $dbh->errstr;
for my $id (keys %cands) {
$sth_params->execute($id) or die $dbh->errstr;
my $cr = $sth_params->fetchrow_arrayref();
$sth_params->finish();
unless ($cr) {
delete $cands{$id};
next;
}
my ($name, $vol, $chunk, $tid, ) = @{$cr};
$sth_bpo->execute($id) or die $dbh->errstr;
my $br = $sth_bpo->fetchrow_arrayref();
$sth_bpo->finish();
my ($btid, $bprice,) = (undef, undef,);
if ($br) { ($btid, $bprice,) = @$br; }
my $mins = $cands{$id};
$cands{$id} = [$name, $mins/$vol/$chunk, $tid, $vol/$chunk, $bprice, $btid, $chunk,];
}
printf "qualified %i candidates
\n", scalar keys %cands;
my $mins = $dbh->selectall_arrayref(qq(
select
i.typeName,
i.typeID
from
invTypes i
where
i.typeID in (34, 35, 36, 37, 38, 39, 40)
order by
i.typeID
)) or die $dbh->errstr;
print start_table({border=>1,},),
Tr(th(["Name", "BPO Price", "Min/m3", "ISK",
map(a({href=>"showinfo:".$_->[1]},substr($_->[0],0,4)),@$mins),],),),"\n";
my $fact = 0.95;
my @prices = ("Factor: ".sprintf("x%.2f",$fact), "", "", "Price:",);
for (@{$mins}) {
my ($tn, $tid,) = @$_;
push @prices, sprintf("%.2f", ($prices{$tid}||0)*$fact);
}
print Tr(td([@prices,])),"\n";
my $sth_reqs;
my @best = sort { $cands{$b}[1] <=> $cands{$a}[1] } keys %cands;
for (0..100) {
my $id = shift @best;
my ($name, $comp, $tid, $vol, $bprice, $btid, $chunk,) = @{$cands{$id}};
my $sth = $sth_reqs ||= $dbh->prepare(qq{
select
requiredTypeID,
quantity
from
typeActivityMaterials
where
typeID = ?
and activityID = 6
}) or die $dbh->errstr;
$sth->execute($id) or die $sth->errstr;
my $reqs = $sth->fetchall_hashref('requiredTypeID');
$sth->finish();
my $price = 0;
for (keys %$reqs) {
$price += $reqs->{$_}->{quantity} * ($prices{$_}||0) * $fact;
}
$price /= $chunk;
my $pp = eval { $market->get_price(typeID => $id)->{avg} } || 0;
my ($pf, $pt, $pc,) = ("n/a","", "black",);
if ($pp) {
$pf = sprintf "%.3f", $pp/$price;
$pt = sprintf("%s market / ",&shortval($pp));
$pc = ($pf <= 1) ? "green" : "red";
}
$pt .= sprintf("%s mins",&shortval($price));
my $pr = span({ title => $pt, style => "color: ".$pc, }, $pf);
print Tr({align=>'right',},
td({align=>'left',},a({href=>"showinfo:$tid",},$name)),
td([ ( $btid
? a({href=>"showinfo:$btid",},&shortisk($bprice))
: "n/a" ),
int($comp),
$pr,
map(defined($reqs->{$_->[1]}) ? $reqs->{$_->[1]}->{quantity}: "",@$mins),
]
)), "\n";
# printf "cand: %5i for '%s' at %i ISK per BPO 1000*1000*1000) {
return sprintf "%.1fG", $price/(1000*1000*1000);
} elsif ($price > 1000*1000) {
return sprintf "%.1fM", $price/(1000*1000);
} elsif ($price > 1000) {
return sprintf "%.1fk", $price/(1000);
} else {
return sprintf "%.1f", $price;
}
}
sub shortisk {
my ($price,) = @_;
return sprintf "%s ISK", &shortval($price);
}