chiark / gitweb /
Merge branch 'stable-5.x'
[ypp-sc-tools.db-live.git] / yarrg / web / routetrade
index b198b9b3867ab7cff6ca4fb2150f2b33d037c921..9660094532aa1544fb5db23092546991c098ee75 100644 (file)
@@ -140,6 +140,9 @@ my $stmt= "
                commods.commodid                                commodid,
                commods.unitmass                                unitmass,
                commods.unitvolume                              unitvolume,
+               commods.ordval                                  ordval,
+               commods.posinclass                              posinclass,
+               commods.commodclassid                           commodclassid,
                dist                                            dist,
                buy.price - sell.price                          unitprofit
        FROM commods
@@ -407,11 +410,17 @@ Maximize
   totalprofit:
 ";
 
+my %stall_poe_limits;
+
 foreach my $sf (@subflows) {
        my $eup= $sf->{Flow}{ExpectedUnitProfit};
        $eup *= (1.0-$loss_per_delay_slot) ** $sf->{Org};
        $cplex .= sprintf "
                %+.20f %s", $eup, $sf->{Var};
+       if ($qa->{ShowStalls}>=2) {
+               my $stall= $sf->{Flow}{'dst_stallid'};
+               push @{ $stall_poe_limits{$stall} }, $sf;
+       }
 }
 $cplex .= "
 
@@ -485,6 +494,24 @@ foreach my $ci (0..($#islandids-1)) {
        $cplex.= "\n";
 }
 
+if ($qa->{ShowStalls}>=2) {
+       my $stallpoe= $dbh->prepare(<<END);
+SELECT max(qty*price) FROM buy WHERE stallid=?
+END
+       foreach my $stallid (sort { $a <=> $b } keys %stall_poe_limits) {
+               $stallpoe->execute($stallid);
+               my ($lim)= $stallpoe->fetchrow_array();
+               $stallpoe->finish();
+               $cplex.= "
+    ". sprintf("%-15s","poe_$stallid:")." ".
+               join(" + ", map {
+                       sprintf "%d %s", $_->{Flow}{'dst_price'}, $_->{Var};
+               } @{ $stall_poe_limits{$stallid} }).
+               " <= $lim";
+       }
+       $cplex.= "\n";
+}
+
 $cplex.= "
 Bounds
         ".(join "
@@ -512,7 +539,7 @@ if ($qa->{'debug'}) {
        my $input= pipethrough_prep();
        print $input $cplex or die $!;
        my $output= pipethrough_run_along($input, undef, 'glpsol',
-               qw(glpsol --tmlim 2 --memlim 5 --intopt --cuts --bfs
+               qw(glpsol --tmlim 5 --memlim 5 --intopt --cuts --bfs
                          --cpxlp /dev/stdin -o /dev/stdout));
        print "<pre>\n" if $qa->{'debug'};
        my $found_section= 0;
@@ -635,14 +662,18 @@ Generated by YARRG at <strong><%
 %
 % foreach my $i (0..$#islandids) {
 <% $tbody->(1) %>
-<tr><td colspan=4>
+<tr>
 %      $iquery->execute($islandids[$i]);
 %      my ($islandname) = $iquery->fetchrow_array();
 %      if (!$i) {
+<td colspan=2>
 <strong>Start at <% $islandname |h %></strong>
+<td colspan=2><a href="docs#posinclass">[what are these codes?]</a>
+<td>
 %      } else {
 %              my $this_dist= $distance->($islandids[$i-1],$islandids[$i]);
 %              $total_dist += $this_dist;
+<td colspan=5>
 <%perl>
                my $total_value= 0;
                foreach my $sf (@subflows) {
@@ -659,7 +690,7 @@ Generated by YARRG at <strong><%
 <%perl>
      my $age_reported= 0;
      my %flowlists;
-     #print "<tr><td colspan=6>" if $qa->{'debug'};
+     #print "<tr><td colspan=7>" if $qa->{'debug'};
      foreach my $od (qw(org dst)) {
        #print " [[ i $i od $od " if $qa->{'debug'};
        foreach my $sf (@subflows) {
@@ -673,6 +704,7 @@ Generated by YARRG at <strong><%
                my $price= $f->{"${od}_price"};
                my $stallname= $f->{"${od}_stallname"};
                my $todo= \$flowlists{$od}{
+                               (sprintf "%010d", $f->{'ordval'}),
                                $f->{'commodname'},
                                (sprintf "%07d", ($od eq 'dst' ?
                                                9999999-$price : $price)),
@@ -684,6 +716,33 @@ Generated by YARRG at <strong><%
                        dstArbitrage => 0,
                } unless $$todo;
                $$todo->{'commodname'}= $f->{'commodname'};
+               $$todo->{'posinclass'}= '';
+               my $incl= $f->{'posinclass'};
+
+               my $findclass= $dbh->prepare(<<END);
+SELECT commodclass, maxposinclass FROM commodclasses WHERE commodclassid = ?
+END
+               $findclass->execute($f->{'commodclassid'});
+               my $classinfo= $findclass->fetchrow_hashref();
+               if ($classinfo) {
+                       my $clname= $classinfo->{'commodclass'};
+                       my $desc= encode_entities(sprintf "%s is under %s",
+                                       $f->{'commodname'}, $clname);
+                       my $abbrev= substr($clname,0,1);
+                       if ($incl) {
+                               my $maxpic= $classinfo->{'maxposinclass'};
+                               $desc.= (sprintf ", commodity %d of %d",
+                                       $incl, $maxpic);
+                               if ($classinfo->{'maxposinclass'} >= 8) {
+                                       my @tmbs= qw(0 1 2 3 4 5 6 7 8 9);
+                                       my $tmbi= ($incl+0.5)*$#tmbs/$maxpic;
+                                       $abbrev.= " ".$tmbs[$tmbi]."&nbsp;";
+                               }
+                       }
+                       $$todo->{'posinclass'}=
+                               "<div class=mouseover title=\"$desc\">"
+                               .$abbrev."</div>";
+               }
                $$todo->{'stallname'}= $stallname;
                $$todo->{Price}= $price;
                $$todo->{Timestamp}= $f->{"${od}_timestamp"};
@@ -715,7 +774,7 @@ Generated by YARRG at <strong><%
 </%perl>
 <tr>
 <td colspan=1>
-<td colspan=2><% $xinfo %>
+<td colspan=3><% $xinfo %>
 <td colspan=2 align=right><% $totaldesc %>
 <td align=right><% $totalwas |h %> total
 <%perl>
@@ -746,6 +805,7 @@ Generated by YARRG at <strong><%
 % tr_datarow($m,$dline);
 <<% $td %>><% $collectdeliver %>
 <<% $td %>><% $t->{'commodname'} |h %>
+<<% $td %>><% $t->{'posinclass'} %>
 %
 %              my @stalls= sort keys %{ $t->{Stalls} };
 %              my $pstall= sub {
@@ -792,7 +852,7 @@ Generated by YARRG at <strong><%
 }
 </%perl><a name="summary"></a>
 <% $tbody->(1) %><tr>
-<td colspan=2>Total distance: <% $total_dist %> leagues.
+<td colspan=3>Total distance: <% $total_dist %> leagues.
 <td colspan=3 align=right>Overall net cash flow
 <td align=right><strong><%
   $total_total < 0 ? -$total_total." loss" : $total_total." gain"