#!/usr/bin/perl -w use strict; $|=1; use EVEcommon; use POSIX qw(strftime); use Time::HiRes qw ( time ); my $begin = time; use CGI qw/ :standard /; my $debug = param("debug") || url_param("debug") || 0; my $mode = param("mode"); my $show = param("show"); my $group = param("group"); $mode = (defined $mode && $mode =~ /^(view|edit|form)$/) ? $1 : 'view'; $show = (defined $show && $show =~ /^(all|have|want)$/) ? $1 : 'have'; $group = (defined $group && $group =~ /^(\d+)$/) ? int($1) : undef; my $self = self_url; if ($mode eq 'form') { param('mode', 'edit'); } unless ($ENV{REMOTE_ADDR} =~ /^(192|10)\./) { $mode = "view"; # $show = "have"; # print header,start_html("yeah, right..."),"\n", # h1("year, right..."),"\n", # end_html; # exit 0; } my $opt_d = 'dbo'; my $opt_u = 'eve'; my ($opt_p, $opt_h,); use Getopt::Long qw(:config no_ignore_case bundling); GetOptions( 'd=s' => \$opt_d, # db name 'u=s' => \$opt_u, # db user 'p=s' => \$opt_p, # db pass 'h=s' => \$opt_h, # html output file ); use DBI; #my $dbh = DBI->connect("DBI:mysql:database=$opt_d", $opt_u, $opt_p); use EVE::Db; my $dbh = EVE::Db::dbh(); die "cant connect to db: ".DBI->errstr unless $dbh; # edit? if ($mode eq 'edit') { my $id = param("id"); $id = (defined $id && $id =~ /^(\d+)$/) ? $1 : undef; my $type = param("type"); $type = (defined $type && $type =~ /^(\d+)$/) ? $1 : undef; my $cnt = param("count"); $cnt = (defined $cnt && $cnt =~ /^\D*(\d+)\D*$/) ? $1 : undef; my $bat = param("batch"); $bat = (defined $bat && $bat =~ /^\D*(\d+)\D*$/) ? $1 : undef; my $me = param("me"); $me = (defined $me && $me =~ /^\D*(\d+)\D*$/) ? $1 : undef; my $pe = param("pe"); $pe = (defined $pe && $pe =~ /^\D*(\d+)\D*$/) ? $1 : undef; die "need either id or type" unless $id || $type; my $qry = ""; if ($id) { # update $qry = qq{ update eve.bpo set batch = ${\(defined $bat ? $bat : "batch")}, count = ${\(defined $cnt ? $cnt : "count")}, wantME = ${\(defined $me ? $me : "wantME")}, wantPE = ${\(defined $pe ? $pe : "wantPE")} where id = $id ${\(defined $type ? "and typeID = $type" : "")} }; } else { # insert $qry = qq{ insert into eve.bpo set typeID = $type, batch = ${\(defined $bat ? $bat : 1)}, count = ${\(defined $cnt ? $cnt : 1)}, wantME = ${\(defined $me ? $me : 0)}, wantPE = ${\(defined $pe ? $pe : 0)} }; } # warn $qry; my $esth = $dbh->prepare($qry) or die $dbh->errstr; $esth->execute or die $esth->errstr; $esth->finish() or die $esth->errstr; my $targ = param("reflect") || "mybpo"; print redirect($targ); exit; } # view? my @qpar = (); my $restrict = ""; if ($group) { $restrict .= "and i.groupID = ?"; push @qpar, $group; } my $sth = $dbh->prepare(qq{ select b.*, i.*, s.sellers, g.groupID, g.groupName from ( select count(*) as sellers, typeID from eve.invSeeds -- eve.crpNPCCorporationTrades -- where -- supplyDemand > 0 group by typeID ) s, dbo.invGroups g, dbo.invCategories c, dbo.invTypes i left join eve.bpo b on i.typeID = b.typeID where c.categoryName = 'Blueprint' and c.categoryID = g.categoryID and g.groupid = i.groupID and i.published = 1 and s.typeID = i.typeID $restrict }) or die $dbh->errstr; $sth->execute(@qpar) or die $sth->errstr; my %groups = (); my %groupheaders = (); while (my $row = $sth->fetchrow_hashref()) { next if $show eq 'have' && !defined $row->{id}; next if $show eq 'want' && defined $row->{id}; #my $grp = $row->{purpose}; my $grp = $row->{groupName}; $groupheaders{$grp} ||= a({href=>"mybpo?show=all&group=".$row->{groupID},}, $row->{groupName}); #my $grp = $row->{categoryName}; $groups{$grp} ||= []; push @{$groups{$grp}}, $row; } $sth->finish(); use CGI qw/:standard *table /; print header,"\n", start_html("My BPOs"), "\n", h1("My BPOs"),"\n"; my ($invhave, $invneed,) = (0,0,); for (sort keys %groups) { my $grp = $_; print h3($groupheaders{$grp}),"\n", start_table({border=>2,})."\n". Tr(th(['ID', 'Name', 'TypeID', 'Price', '#', 'ME', 'PE', 'act',]))."\n"; #Tr(th(['ID', 'Name', 'TypeID', 'Price', '#', 'ME', 'PE', 'Bat', 'act',]))."\n"; for (@{$groups{$grp}}) { $_->{typeName} = &name_by_typeid($_->{typeID}); } #for (sort {$a->{typeID} <=> $b->{typeID}} @{$groups{$grp}}) { for (sort {$a->{typeName} cmp $b->{typeName}} @{$groups{$grp}}) { my $b = $_; my $tid = $b->{typeID}; my $name = $b->{typeName}; if ($mode eq "form") { print start_form(-method=>'get', -action=> "mybpo"), hidden('type', $tid), hidden('mode', 'edit'), hidden('reflect', $self); } my ($id, $me, $pe, $bat, $cnt, $lnk, $opt, $but,); if ($b->{id}) { die "baaad show: $show" if $show eq "want"; $me = $b->{wantME} || 0; $pe = $b->{wantPE} || 0; $bat = $b->{batch} || 1; $cnt = $b->{count} || 1; $lnk = sprintf "bpo?type=%i&me=%i&pe=%i&patt=%s&batch=%i", $tid, $me, $pe, &urlify($name), $bat; #$id = a({href=>"mybpo?mode=edit&id=".$b->{id},}, $b->{id}); if ($b->{itemID}) { my $lnk = sprintf "showinfo:%s//%s", $b->{typeID}, $b->{itemID}; $id = a({href=>$lnk,}, $b->{id}); } else { $id = span({style=>"color: red",}, $b->{id}); } $opt = {}; print hidden("id", $id) if $mode eq "form"; $but = "upd"; $invhave += $b->{basePrice}||0; } else { die "baaad show: $show" if $show eq "have"; $me = " "; $pe = " "; $bat = " "; $cnt = " "; $lnk = sprintf "bpo?type=%i&patt=%s", $tid, &urlify($name); $id = " "; $opt = {bgcolor=>'yellow',}; $but = "add"; $invneed += $b->{basePrice}||0 } my ($tcnt, $tme, $tpe, $tbut, $tbat,) = ($cnt, $me, $pe, "", $bat,); if ($mode eq "form") { $tcnt = textfield(-size => 2, -name => 'count', -default => $cnt,); $tbat = textfield(-size => 4, -name => 'batch', -default => $bat,); $tme = textfield(-size => 4, -name => 'me', -default => $me,); $tpe = textfield(-size => 3, -name => 'pe', -default => $pe,); $tpe = textfield(-size => 3, -name => 'pe', -default => $pe,); $tbut = submit(-name => $but); } print Tr( td($opt, [ $id, a({href=>$lnk,}, $name), ],), td({align=>'right',}, [ a({href=>"showinfo:$tid",}, $tid), &shortprettyisk($b->{basePrice}), $tcnt, $tme, $tpe, $tbut,],), #$tcnt, $tme, $tpe, $tbat, $tbut,],), ), ($mode eq "form" ? end_form : ""), "\n"; } print end_table,"\n"; } if ($invhave || $invneed) { print hr, "\n", "Investment: ", $invneed ? &shortprettyisk($invneed)." need" : "", ($invhave && $invneed) ? ", " : "", $invhave ? &shortprettyisk($invhave)." have" : "", br, "\n"; } if ($debug) { print hr,"\n"; for (sort keys %ENV) { printf "%s: %s%s\n", $_, $ENV{$_}, br; } } my $runtime = time - $begin; print hr, "\n", strftime("Updated at %Y-%m-%d %H:%M:%S", gmtime),br, "\n", $runtime ? sprintf "Completed in %.3f sec.", $runtime : "",br,"\n", end_html,"\n"; exit 0; my $sth_name_by_typeid; sub name_by_typeid ($) { my ($tid,) = @_; my $sth = $sth_name_by_typeid ||= $dbh->prepare(qq{ select typeName from dbo.invTypes where typeID = ? }) or die $dbh->errstr; $sth->execute($tid) or die $sth->errstr; my $r = $sth->fetchrow_arrayref(); return "UNKNOWN:$tid" unless $r; return $r->[0]; } sub urlify ($){ my ($s,) = @_; $s =~ s/(\W)/sprintf("%%%02x", ord($1))/ige; return $s; }