chiark / gitweb /
Right-justify numerical columns in Prices for commodity
[ypp-sc-tools.db-test.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">
113 <tr>
114 <th colspan=3>
115 <th colspan=2>Prices
116 <th colspan=3>Quantity at price
117 <tr id="<% $bs %>_table_thr">
118 <th>Archipelago
119 <th>Island
120 <th>Stall(s)
121 <th>Best
122 <th>Median
123 <th>Best
124 <th>+/-10%
125 <th>Any
126 </tr>
127 %               }
128 %               my $islandid= $island->{'islandid'};
129 %               $offers->execute($cmid, $islandid);
130 %               my ($offer, $bestprice, $marginal, @beststalls);
131 %               my $tqty= $island->{'tqty'};
132 %               my $cqty= '';
133 %               my $bestqty= '';
134 %               my $approxqty= '';
135 %               my $median= '-';
136 %               while ($offer= $offers->fetchrow_hashref) {
137 %                       my $price= $offer->{'price'};
138 %                       my $qty= $offer->{'qty'};
139 %                       length $bestqty or $bestprice= $price;
140 %                       if ($price == $bestprice) {
141 %                               $bestqty += $qty;
142 %                               push @beststalls, $offer->{'stallname'};
143 %                       }
144 %                       $cqty += $qty;
145 %                       if ($cqty*2 >= $tqty && $median eq '-') {
146 %                               $median= $price;
147 %                       }
148 %                       if ($bestprice*9 <= $price*10 and
149 %                           $price*10 <= $bestprice*11) {
150 %                               $approxqty += $qty;
151 %                       }
152 %               }
153 %               my $stallname;
154 %
155 %               my $rowid= "id_${bs}_$islandid";
156 %               my $s= [ ];
157 %
158 %               $s->[2]= sprintf "%06d", scalar @beststalls;
159 %               if (!@beststalls) {
160 %                       $stallname= '-';
161 %               } elsif (@beststalls==1) {
162 %                       $stallname= $beststalls[0];
163 %                       $s->[2] .= " $stallname";
164 %               } else {
165 %                       $stallname= sprintf "%d offers", scalar @beststalls;
166 %               }
167 %
168 %               $cqty == $tqty or die "$bs $cqty $tqty $cmid $islandid ";
169 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>">
170      <td><% $s->[0]= $island->{'archipelago'} |h %>
171      <td><% $s->[1]= $island->{'islandname'} |h %>
172      <td><%          $stallname |h %>
173      <td align=right><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
174      <td align=right><% $s->[4]= $median %>
175      <td align=right><% $s->[5]= $bestqty %>
176      <td align=right><% $s->[6]= $approxqty %>
177      <td align=right><% $s->[7]= $cqty %>
178 </tr>
179 %               for my $cix (0..$#$s) {
180 %                       $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
181 %               }
182 %               $rowix++;
183 %       }
184 %       if ($rowix) {
185 </table>
186
187 <&| tabsort,    table => "${bs}_table", sortkeys => "${bs}_sortkeys",
188                 throw => "${bs}_table_thr", rowclass => 'datarow', cols => [
189         {}, {},
190         { DoReverse => 1 },
191         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
192         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
193         { DoReverse => 1, Numeric => 1 },
194         { DoReverse => 1, Numeric => 1 },
195         { DoReverse => 1, Numeric => 1 },
196         ] &>
197   <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
198   function ts_Pricemap_<% $bs %>(price) {
199     if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
200     return price;
201   }
202 </&tabsort>
203 %       } else {
204 No offers.
205 %       }
206
207 <%perl>
208 }
209 </%perl>