chiark / gitweb /
Query planner and dumpqueryresults doc strings; dumpqueryresults improved
[ypp-sc-tools.db-test.git] / yarrg / web / routetrade
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
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
12    download the source.
13
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.
18
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.
23
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/>.
26
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.
30
31
32  This Mason component is the core trade planner for a specific route.
33
34
35 </%doc>
36 <%args>
37 $dbh
38 @islandids
39 @archipelagoes
40 $qa
41 </%args>
42 <%perl>
43
44 my @flow_conds;
45 my @query_params;
46
47 my $sd_condition= sub {
48         my ($bs, $ix) = @_;
49         my $islandid= $islandids[$ix];
50         if (defined $islandid) {
51                 return "${bs}.islandid = $islandid";
52         } else {
53                 push @query_params, $archipelagoes[$ix];
54                 return "${bs}_islands.archipelago = ?";
55         }
56 };
57
58 foreach my $src_i (0..$#islandids) {
59         my $src_cond= $sd_condition->('sell',$src_i);
60         my @dst_conds;
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
66                         $dst_cond=
67                                 "($dst_cond AND sell.islandid = buy.islandid)";
68                 }
69                 push @dst_conds, $dst_cond;
70         }
71         push @flow_conds, "$src_cond AND (
72                         ".join("
73                      OR ",@dst_conds)."
74                 )";
75 }
76
77 my $stmt= "             
78         SELECT  commods.commodname              commodname,
79                 commods.commodid                commodid,
80                 commods.unitmass                mass,
81                 commods.unitvolume              volume,
82                 sell_islands.islandid           org_id,
83                 sell_islands.islandname         org_name,
84                 sell.price                      org_price,
85                 sum(sell.qty)                   org_qty,
86                 buy_islands.islandid            dst_id,
87                 buy_islands.islandname          dst_name,
88                 buy.price                       dst_price,
89                 sum(buy.qty)                    dst_qty
90         FROM commods
91         JOIN buy  on commods.commodid = buy.commodid
92         JOIN sell on commods.commodid = sell.commodid
93         JOIN islands as sell_islands on sell.islandid = sell_islands.islandid
94         JOIN islands as buy_islands  on buy.islandid  = buy_islands.islandid
95         WHERE   (
96                 ".join("
97            OR   ", @flow_conds)."
98         )
99           AND   buy.price > sell.price
100         GROUP BY commodname, commods.commodid,
101                 org_id, org_price, dst_id, dst_price
102      ";
103
104 my $sth= $dbh->prepare($stmt);
105 $sth->execute(@query_params);
106
107 </%perl>
108 % if ($qa->{'debug'}) {
109 <pre>
110 <% $stmt |h %>
111 <% join(' | ',@query_params) |h %>
112 </pre>
113 % }
114
115 <& dumpqueryresults, sth =>$sth &>
116
117 <%init>
118 use CommodsWeb;
119 </%init>