3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
9 YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10 The YARRG website is covered by the GNU Affero GPL v3 or later, which
11 basically means that every installation of the website will let you
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU Affero General Public License as
16 published by the Free Software Foundation, either version 3 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Affero General Public License for more details.
24 You should have received a copy of the GNU Affero General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28 are used without permission. This program is not endorsed or
29 sponsored by Three Rings.
32 This Mason component is the core trade planner for a specific route.
47 my $sd_condition= sub {
49 my $islandid= $islandids[$ix];
50 if (defined $islandid) {
51 return "${bs}.islandid = $islandid";
53 push @query_params, $archipelagoes[$ix];
54 return "${bs}_islands.archipelago = ?";
58 foreach my $src_i (0..$#islandids) {
59 my $src_cond= $sd_condition->('sell',$src_i);
61 foreach my $dst_i ($src_i..$#islandids) {
62 my $dst_cond= $sd_condition->('buy',$dst_i);
63 if ($dst_i==$src_i and !defined $islandids[$src_i]) {
64 # we always want arbitrage, but mentioning an arch
65 # once shouldn't produce intra-arch trades
67 "($dst_cond AND sell.islandid = buy.islandid)";
69 push @dst_conds, $dst_cond;
71 push @flow_conds, "$src_cond AND (
78 SELECT sell_islands.islandname org_name,
79 sell_islands.islandid org_id,
81 sum(sell.qty) org_qty,
82 buy_islands.islandname dst_name,
83 buy_islands.islandid dst_id,
86 commods.commodname commodname,
87 commods.commodid commodid,
88 commods.unitmass mass,
89 commods.unitvolume volume,
90 buy.price - sell.price unitprofit,
91 min(sell.qty,buy.qty) tqty,
92 min(sell.qty,buy.qty) * (buy.price-sell.price) profit
94 JOIN buy on commods.commodid = buy.commodid
95 JOIN sell on commods.commodid = sell.commodid
96 JOIN islands as sell_islands on sell.islandid = sell_islands.islandid
97 JOIN islands as buy_islands on buy.islandid = buy_islands.islandid
102 AND buy.price > sell.price
103 GROUP BY commods.commodid, org_id, org_price, dst_id, dst_price
104 ORDER BY org_name, dst_name, profit DESC, commodname,
105 org_price, dst_price DESC
108 my $sth= $dbh->prepare($stmt);
109 $sth->execute(@query_params);
114 % if ($qa->{'debug'}) {
117 <% join(' | ',@query_params) |h %>
122 <& dumpqueryresults:start, sth => $sth &>
124 % while ($flow= $sth->fetchrow_hashref()) {
125 % $flow->{Ix}= @flows;
126 % $flow->{Var}= "f$flow->{Ix}";
127 % push @flows, $flow;
128 <& dumpqueryresults:row, sth => $sth, row => $flow &>
130 <& dumpqueryresults:end &>
140 ", map { "$_->{profit} $_->{Var}" } @flows)."
146 foreach my $flow (@flows) {
147 foreach my $od (qw(org dst)) {
148 my $cstname= join '_',
153 $flow->{"${od}_price"};
154 push @{ $avail_csts{$cstname}{Flows} }, $flow->{Var};
155 $avail_csts{$cstname}{Qty}= $flow->{"${od}_qty"};
158 foreach my $cstname (sort keys %avail_csts) {
159 my $c= $avail_csts{$cstname};
161 ". sprintf("%-30s","$cstname:")." ".
162 join("+", @{ $c->{Flows} }).
163 " <= ".$c->{Qty}."\n";
169 ", map { "$_->{Var} >= 0" } @flows)."
173 # glpsol --cpxlp /dev/stdin <t.cplex -o /dev/stdout