X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Fweb%2Froutetrade;h=e154f2c6c2c683d434ae182fd33b390051f6aa87;hb=e05f0567ea23491da8e48f892a3337f37139628f;hp=1c5a3e49db2b184ac019b4a8177cb8838df59040;hpb=fd6bf359d9051fe0c6d269a44bcc08cc53aa2fea;p=ypp-sc-tools.db-live.git diff --git a/yarrg/web/routetrade b/yarrg/web/routetrade index 1c5a3e4..e154f2c 100644 --- a/yarrg/web/routetrade +++ b/yarrg/web/routetrade @@ -2,24 +2,82 @@ $dbh @islandids @archipelagoes +$qa +<%perl> -%# So, add code to do right thing here: +my @flow_conds; +my @query_params; +my $sd_condition= sub { + my ($bs, $ix) = @_; + my $islandid= $islandids[$ix]; + if (defined $islandid) { + return "${bs}.islandid = $islandid"; + } else { + push @query_params, $archipelagoes[$ix]; + return "${bs}_islands.archipelago = ?"; + } +}; + +foreach my $src_i (0..$#islandids) { + my $src_cond= $sd_condition->('sell',$src_i); + my @dst_conds; + foreach my $dst_i ($src_i..$#islandids) { + my $dst_cond= $sd_condition->('buy',$dst_i); + if ($dst_i==$src_i and !defined $islandids[$src_i]) { + # we always want arbitrage, but mentioning an arch + # once shouldn't produce intra-arch trades + $dst_cond= + "($dst_cond AND sell.islandid = buy.islandid)"; + } + push @dst_conds, $dst_cond; + } + push @flow_conds, "$src_cond AND ( + ".join(" + OR ",@dst_conds)." + )"; +} + +my $stmt= " + SELECT commods.commodname commodname, + commods.commodid commodid, + commods.unitmass mass, + commods.unitvolume volume, + sell_islands.islandid org_id, + sell_islands.islandname org_name, + sell.price org_price, + sum(sell.qty) org_qty, + buy_islands.islandid dst_id, + buy_islands.islandname dst_name, + buy.price dst_price, + sum(buy.qty) dst_qty + FROM commods + JOIN buy on commods.commodid = buy.commodid + JOIN sell on commods.commodid = sell.commodid + JOIN islands as sell_islands on sell.islandid = sell_islands.islandid + JOIN islands as buy_islands on buy.islandid = buy_islands.islandid + WHERE ( + ".join(" + OR ", @flow_conds)." + ) + AND buy.price > sell.price + GROUP BY commodname, commods.commodid, + org_id, org_price, dst_id, dst_price + "; + +my $sth= $dbh->prepare($stmt); +$sth->execute(@query_params); + + +% if ($qa->{'debug'}) {
-Route is as follows:
-
-% foreach my $i (0..$#islandids) {
-%   my $islandid= $islandids[$i];
-%   my $archipelago= $archipelagoes[$i];
-%   $islandid= '' unless defined $islandid;
-%   $archipelago= '' unless defined $archipelago;
-%
-  islandid=<% $islandid |h %>  archipelago=<% $archipelago |h %>
-%
+<% $stmt |h %>
+<% join(' | ',@query_params) |h %>
+
% } - +<& dumpqueryresults, sth =>$sth &> <%init> use CommodsWeb;