chiark / gitweb /
Organise TODO list
[ypp-sc-tools.db-test.git] / yarrg / web / routetrade
index 91ff421625fd7d4757a6def0a6e471341ac073e1..b394a3527843e0cdeddd0ca7e7d1376506a45f3b 100644 (file)
  This Mason component is the core trade planner for a specific route.
 
 
-========== TODO ==========
-16:36 <ceb> alpha,byrne,papaya,turtle,jorvik,luthien is my example
-
-use POST for update.  Hrrm.
-
-LATER OR NOT AT ALL
-
-adjustable potential cost of losses (rather than fixed 1e-BIG per league)
-
-max volume/mass
-
-========== TODO ==========
-
 </%doc>
 <%args>
 $dbh
@@ -259,6 +246,11 @@ foreach my $f (@flows) {
        $f->{MaxProfit}= $f->{MaxQty} * $f->{'unitprofit'};
        $f->{MaxCapital}= $f->{MaxQty} * $f->{'org_price'};
 
+       $f->{MarginSortKey}= sprintf "%d",
+               $f->{'dst_price'} * 10000 / $f->{'org_price'};
+       $f->{Margin}= sprintf "%3.1f%%",
+               $f->{'dst_price'} * 100.0 / $f->{'org_price'} - 100.0;
+
        $f->{ExpectedUnitProfit}=
                $f->{'dst_price'} * (1.0 - $loss_per_league) ** $f->{'dist'}
                - $f->{'org_price'};
@@ -558,8 +550,9 @@ $addcols->({ Total => 0, DoReverse => 1 }, qw(
 % my %da_ages;
 %
 <h1>Voyage trading plan</h1>
-<table>
+<table rules=groups>
 % foreach my $i (0..$#islandids) {
+<tbody>
 <tr><td colspan=3><strong>
 %      $iquery->execute($islandids[$i]);
 %      my ($islandname) = $iquery->fetchrow_array();
@@ -569,56 +562,76 @@ Start at <% $islandname |h %>
 Sail to <% $islandname |h %>
 %      }
 </strong>
-%    my $age_reported= 0;
-%    foreach my $od (qw(dst org)) {
-%      my $sign= $od eq 'dst' ? -1 : +1;
-%      my %todo;
-%      foreach my $f (@flows) {
-%              next if $f->{Suppress};
-%              next unless $f->{"${od}_id"} == $islandids[$i];
-%              next unless $f->{OptQty};
-%              my $price= $f->{"${od}_price"};
-%              my $stallname= $f->{"${od}_stallname"};
-%              my $todo= \$todo{ $f->{'commodname'},
-%                                (sprintf "%07d", $price),
-%                                $stallname };
-%              $$todo= { Qty => 0 } unless $$todo;
-%              $$todo->{'commodname'}= $f->{'commodname'};
-%              $$todo->{'stallname'}= $stallname;
-%              $$todo->{Price}= $price;
-%              $$todo->{Timestamp}= $f->{"${od}_timestamp"};
-%              $$todo->{Qty} += $f->{OptQty};
-%              $$todo->{Total}= $$todo->{Price} * $$todo->{Qty};
-%              $$todo->{Stalls}= $f->{"${od}Stalls"};
-%      }
-%      if (%todo && !$age_reported++) {
-%              my $age= $now - (values %todo)[0]->{Timestamp};
-%              my $cellid= "da_${i}";
-%              $da_ages{$cellid}= $age;
-<td colspan=2>\
+<%perl>
+     my $age_reported= 0;
+     my %flowlists;
+     foreach my $od (qw(org dst)) {
+       foreach my $f (@flows) {
+               next if $f->{Suppress};
+               next unless $f->{"${od}_id"} == $islandids[$i];
+               next unless $f->{OptQty};
+               my $price= $f->{"${od}_price"};
+               my $stallname= $f->{"${od}_stallname"};
+               my $todo= \$flowlists{$od}{
+                               $f->{'commodname'},
+                               (sprintf "%07d", ($od eq 'dst' ?
+                                               9999999-$price : $price)),
+                               $stallname
+                       };
+               $$todo= {
+                       Qty => 0,
+                       orgArbitrage => 0,
+                       dstArbitrage => 0,
+               } unless $$todo;
+               $$todo->{'commodname'}= $f->{'commodname'};
+               $$todo->{'stallname'}= $stallname;
+               $$todo->{Price}= $price;
+               $$todo->{Timestamp}= $f->{"${od}_timestamp"};
+               $$todo->{Qty} += $f->{OptQty};
+               $$todo->{Total}= $$todo->{Price} * $$todo->{Qty};
+               $$todo->{Stalls}= $f->{"${od}Stalls"};
+               if ($f->{'org_id'} == $f->{'dst_id'}) {
+                       $$todo->{"${od}Arbitrage"}= 1;
+               }
+       }
+     }
+
+     my $total;
+     my $dline= 0;
+     my $show_flows= sub {
+       my ($od,$arbitrage,$collectdeliver) = @_;
+</%perl>
+%
+%      my $todo= $flowlists{$od};
+%      return unless $todo;
+%      foreach my $tkey (sort keys %$todo) {
+%              my $t= $todo->{$tkey};
+%              next if $t->{"${od}Arbitrage"} != $arbitrage;
+%              if (!$age_reported++) {
+%                      my $age= $now - $t->{Timestamp};
+%                      my $cellid= "da_${i}";
+%                      $da_ages{$cellid}= $age;
+<td colspan=3>\
 (Data age: <span id="<% $cellid %>"><% prettyprint_age($age) %></span>)
-%      }
-%      my $total= 0;
-%      my $dline= 0;
-%      foreach my $tkey (sort keys %todo) {
-%              my $t= $todo{$tkey};
+%              } elsif (!defined $total) {
+%                      $total= 0;
+<tbody>
+%              }
 %              $total += $t->{Total};
 %              my $span= 0 + keys %{ $t->{Stalls} };
 %              my $td= "td rowspan=$span";
 <tr class="datarow<% $dline %>">
-<<% $td %>><% $od eq 'org' ? 'Collect' : 'Deliver' %>
+<<% $td %>><% $collectdeliver %>
 <<% $td %>><% $t->{'commodname'} |h %>
 %
 %              my @stalls= sort keys %{ $t->{Stalls} };
 %              my $pstall= sub {
 %                      my $name= $stalls[$_[0]];
-%                      my $avail= $t->{Stalls}{$name};
-<td align=right><% $avail |h %> <% $od eq 'org' ? 'avail.' : 'wanted' %>
 <td><% $name |h %>
 %              };
 %
 %              $pstall->(0);
-<<% $td %> align=right><% $t->{Price} |h %> each
+<<% $td %> align=right><% $t->{Price} |h %> poe ea.
 <<% $td %> align=right><% $t->{Qty} |h %> unit(s)
 <<% $td %> align=right><% $t->{Total} |h %> total
 %
@@ -629,13 +642,27 @@ Sail to <% $islandname |h %>
 %
 %              $dline ^= 1;
 %      }
-%      if (%todo) {
+%    };
+%    my $show_total= sub {
+%      my ($totaldesc)= @_;
+%      if (defined $total) {
 <tr>
-<td colspan=5><td align=right><% $od eq 'org' ? 'Outlay' : 'Proceeds' %>
+<td colspan=3>
+<td colspan=2 align=right><% $totaldesc %>
 <td align=right><% $total |h %> total
 %      }
-%    }
-% }
+%      $total= undef;
+%      $dline= 0;
+<%perl>
+     };
+
+     $show_flows->('dst',0,'Deliver');  $show_total->('Proceeds');
+     $show_flows->('org',1,'Collect');  $show_total->('(Arbitrage) outlay');
+     $show_flows->('dst',1,'Deliver');  $show_total->('(Arbitrage) proceeds');
+     $show_flows->('org',0,'Collect');  $show_total->('Outlay');
+
+}
+</%perl>
 </table>
 <& query_age:dataages, id2age => \%da_ages &>
 %