chiark / gitweb /
printable routetrades: 2-up versions; fix up tbody
[ypp-sc-tools.db-live.git] / yarrg / web / query_commod
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 generates the core of the `commodity' query.
33
34
35 </%doc>
36 <%args>
37 $quri
38 $dbh
39 $commodid => undef;
40 $commodstring => '';
41 $prselector
42 $someresults
43 $emsgokorprint
44 </%args>
45
46 <%perl>
47 my $emsg;
48 my ($commodname,$cmid);
49
50 my $qa= \%ARGS;
51 </%perl>
52
53 <h1>Commodity enquiry</h1>
54
55 % $prselector->('BuySell');
56 % $prselector->('ShowBlank');
57
58 <form action="<% $quri->() |h %>" method="get">
59
60 <& enter_commod, qa => $qa, dbh => $dbh, emsg_r => \$emsg,
61         commodname_r => \$commodname,
62         cmid_r => \$cmid
63  &>
64
65 <input type=submit name=submit value="Go">
66 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
67 <& "lookup:formhidden", ours => $ours &>
68
69 </form>
70
71 %#========== results ==========
72 <%perl>
73
74 $emsgokorprint->($emsg) or $cmid=undef;
75 return unless defined $cmid;
76 $someresults->();
77
78 #---------- actually compute the results and print them ----------
79
80 foreach my $bs (split /_/, $ARGS{BuySell}) {
81         $bs =~ m/^(buy|sell)$/ or die;
82         $bs= $1;
83         my ($ascdesc) = ($bs eq 'buy')
84                 ? ('DESC')
85                 : ('ASC');
86         my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
87         my $islands= $dbh->prepare(
88         "SELECT islands.islandid AS islandid, archipelago, islandname,
89                         sum(qty) as tqty
90                 FROM islands $joinkind $bs offers
91                 ON islands.islandid == offers.islandid AND commodid == ?
92                 GROUP BY islands.islandid
93                 ORDER BY archipelago, islandname"
94                 );
95
96         my $offers= $dbh->prepare(
97         "SELECT stallname, price, qty
98                 FROM $bs NATURAL JOIN stalls
99                 WHERE commodid = ? AND islandid = ?
100                 ORDER BY price $ascdesc"
101                 );
102
103 </%perl>
104
105 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
106 %       $islands->execute($cmid);
107 %       my $island;
108 %       my %ts_sortkeys;
109 %       my $rowix= 0;
110 %       while ($island= $islands->fetchrow_hashref) {
111 %               if (!$rowix) {
112 <table id="<% $bs %>_table" rules=groups>
113 <colgroup span=2>
114 <colgroup span=1>
115 <colgroup span=2>
116 <colgroup span=3>
117 <tr>
118 <th colspan=2>
119 <th colspan=1>
120 <th colspan=2>Prices
121 <th colspan=3>Quantity at price
122 <tr id="<% $bs %>_table_thr">
123 <th>Archipelago
124 <th>Island
125 <th>Stall(s)
126 <th>Best
127 <th>Median
128 <th>Best
129 <th>+/-10%
130 <th>Any
131 </tr>
132 %               }
133 %               my $islandid= $island->{'islandid'};
134 %               $offers->execute($cmid, $islandid);
135 %               my ($offer, $bestprice, $marginal, @beststalls);
136 %               my $tqty= $island->{'tqty'};
137 %               my $cqty= '';
138 %               my $bestqty= '';
139 %               my $approxqty= '';
140 %               my $median= '-';
141 %               while ($offer= $offers->fetchrow_hashref) {
142 %                       my $price= $offer->{'price'};
143 %                       my $qty= $offer->{'qty'};
144 %                       length $bestqty or $bestprice= $price;
145 %                       if ($price == $bestprice) {
146 %                               $bestqty += $qty;
147 %                               push @beststalls, $offer->{'stallname'};
148 %                       }
149 %                       $cqty += $qty;
150 %                       if ($cqty*2 >= $tqty && $median eq '-') {
151 %                               $median= $price;
152 %                       }
153 %                       if ($bestprice*9 <= $price*10 and
154 %                           $price*10 <= $bestprice*11) {
155 %                               $approxqty += $qty;
156 %                       }
157 %               }
158 %               my $stallname;
159 %
160 %               my $rowid= "id_${bs}_$islandid";
161 %               my $s= [ ];
162 %
163 %               $s->[2]= sprintf "%06d", scalar @beststalls;
164 %               if (!@beststalls) {
165 %                       $stallname= '-';
166 %               } elsif (@beststalls==1) {
167 %                       $stallname= $beststalls[0];
168 %                       $s->[2] .= " $stallname";
169 %               } else {
170 %                       $stallname= sprintf "%d offers", scalar @beststalls;
171 %               }
172 %
173 %               $cqty == $tqty or die "$bs $cqty $tqty $cmid $islandid ";
174 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>">
175      <td><% $s->[0]= $island->{'archipelago'} |h %>
176      <td><% $s->[1]= $island->{'islandname'} |h %>
177      <td><%          $stallname |h %>
178      <td align=right><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
179      <td align=right><% $s->[4]= $median %>
180      <td align=right><% $s->[5]= $bestqty %>
181      <td align=right><% $s->[6]= $approxqty %>
182      <td align=right><% $s->[7]= $cqty %>
183 </tr>
184 %               for my $cix (0..$#$s) {
185 %                       $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
186 %               }
187 %               $rowix++;
188 %       }
189 %       if ($rowix) {
190 </table>
191
192 <&| tabsort,    table => "${bs}_table", sortkeys => "${bs}_sortkeys",
193                 throw => "${bs}_table_thr", rowclass => 'datarow', cols => [
194         {}, {},
195         { DoReverse => 1 },
196         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
197         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
198         { DoReverse => 1, Numeric => 1 },
199         { DoReverse => 1, Numeric => 1 },
200         { DoReverse => 1, Numeric => 1 },
201         ] &>
202   <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
203   function ts_Pricemap_<% $bs %>(price) {
204     if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
205     return price;
206   }
207 </&tabsort>
208 %       } else {
209 No offers.
210 %       }
211
212 <%perl>
213 }
214 </%perl>