chiark / gitweb /
Better answers for arch queries
[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 $commodstring => '';
40 $prselector
41 $someresults
42 $emsgokorprint
43 </%args>
44
45 <%perl>
46 my $emsg;
47 my ($commodname,$commodid);
48
49 my $qa= \%ARGS;
50 </%perl>
51
52 <h1>Commodity enquiry</h1>
53
54 % $prselector->('BuySell');
55 % $prselector->('ShowBlank');
56
57 %#---------- textbox, user enters route as string ----------
58 % if (!$qa->{Dropdowns}) {
59
60 Enter commodity (abbreviations are OK):<br>
61
62 <form action="<% $quri->() |h %>" method="get">
63
64 <&| qtextstring, qa => $qa, dbh => $dbh,
65     thingstring => 'commodstring', emsgstore => \$emsg,
66     perresult => sub { ($commodname,$commodid)= @_; }
67  &>
68  size=80
69 </&>
70
71 % } else { #---------- dropdowns, user selects from menus ----------
72
73 Not yet implemented.
74
75 % } #---------- end of dropdowns, now common middle of page code ----------
76
77 <input type=submit name=submit value="Go">
78 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
79 <& "lookup:formhidden", ours => $ours &>
80
81 </form>
82
83 %#========== results ==========
84 <%perl>
85
86 $emsgokorprint->($emsg) or $commodid=undef;
87 return unless defined $commodid;
88 $someresults->();
89
90 #---------- actually compute the results and print them ----------
91
92 my $onloads= "";
93
94 foreach my $bs (split /_/, $ARGS{BuySell}) {
95         $bs =~ m/^(buy|sell)$/ or die;
96         $bs= $1;
97         my ($ascdesc) = ($bs eq 'buy')
98                 ? ('DESC')
99                 : ('ASC');
100         my $joinkind= $ARGS{ShowBlank} eq 'show'
101                 ? 'LEFT OUTER JOIN' : 'INNER JOIN';
102         my $islands= $dbh->prepare(
103         "SELECT islands.islandid AS islandid, archipelago, islandname,
104                         sum(qty) as tqty
105                 FROM islands $joinkind $bs offers
106                 ON islands.islandid == offers.islandid AND commodid == ?
107                 GROUP BY islands.islandid,
108                 ORDER BY archipelago, islandname"
109                 );
110
111         my $offers= $dbh->prepare(
112         "SELECT stallname, price, qty
113                 FROM $bs NATURAL JOIN stalls
114                 WHERE commodid = ? AND islandid = ?
115                 ORDER BY price $ascdesc"
116                 );
117         # fixme this query is utterly wrong
118
119 </%perl>
120
121 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
122
123 <table id="<% $bs %>_table">
124 <tr>
125 <th colspan=3>
126 <th colspan=2>Prices
127 <th colspan=3>Quantity at price
128 <tr id="<% $bs %>_table_thr">
129 <th>Archipelago
130 <th>Island
131 <th>Stall(s)
132 <th>Best
133 <th>Median
134 <th>Best
135 <th>+/-10%
136 <th>Any
137 </tr>
138 %       $islands->execute($commodid);
139 %       my $island;
140 %       my %ts_sortkeys;
141 %       while ($island= $islands->fetchrow_hashref) {
142 %               my $islandid= $island->{'islandid'};
143 %               $offers->execute($commodid, $islandid);
144 %               my ($offer, $bestprice, $marginal, @beststalls);
145 %               my $tqty= $island->{'tqty'};
146 %               my $cqty= '';
147 %               my $bestqty= '';
148 %               my $approxqty= '';
149 %               my $median= '-';
150 %               while ($offer= $offers->fetchrow_hashref) {
151 %                       my $price= $offer->{'price'};
152 %                       my $qty= $offer->{'qty'};
153 %                       length $bestqty or $bestprice= $price;
154 %                       if ($price == $bestprice) {
155 %                               $bestqty += $qty;
156 %                               push @beststalls, $offer->{'stallname'};
157 %                       }
158 %                       $cqty += $qty;
159 %                       if ($cqty*2 >= $tqty && $median eq '-') {
160 %                               $median= $price;
161 %                       }
162 %                       if ($bestprice*9 <= $price*10 and
163 %                           $price*10 <= $bestprice*11) {
164 %                               $approxqty += $qty;
165 %                       }
166 %               }
167 %               my $stallname;
168 %
169 %               my $rowid= "id_${bs}_$islandid";
170 %               my $s= [ ];
171 %
172 %               $s->[2]= sprintf "%06d", scalar @beststalls;
173 %               if (!@beststalls) {
174 %                       $stallname= '-';
175 %               } elsif (@beststalls==1) {
176 %                       $stallname= $beststalls[0];
177 %                       $s->[2] .= " $stallname";
178 %               } else {
179 %                       $stallname= sprintf "%d offers", scalar @beststalls;
180 %               }
181 %
182 %               $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
183 <tr id=<% $rowid %> >
184      <td><% $s->[0]= $island->{'archipelago'} |h %>
185      <td><% $s->[1]= $island->{'islandname'} |h %>
186      <td><%          $stallname |h %>
187      <td><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
188      <td><% $s->[4]= $median %>
189      <td><% $s->[5]= $bestqty %>
190      <td><% $s->[6]= $approxqty %>
191      <td><% $s->[7]= $cqty %>
192 </tr>
193 %               for my $cix (0..$#$s) {
194 %                       $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
195 %               }
196 %       }
197 </table>
198
199 <& tabsort,     table => "${bs}_table", sortkeys => "${bs}_sortkeys",
200                 throw => "${bs}_table_thr", cols => [
201         {}, {},
202         { DoReverse => 1 },
203         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
204         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
205         { DoReverse => 1, Numeric => 1 },
206         { DoReverse => 1, Numeric => 1 },
207         { DoReverse => 1, Numeric => 1 },
208         ] &>
209 <&| script &>
210   <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
211   function ts_Pricemap_<% $bs %>(price) {
212     if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
213     return price;
214   }
215 </&>
216 %       $onloads .= "    ts_onload__${bs}_table();\n";
217
218 <%perl>
219 }
220 </%perl>
221
222 <&| script &>
223   function all_onload() {
224 <% $onloads %>
225   }
226   window.onload= all_onload;
227 </&>