chiark / gitweb /
New onload arrangments; make tabsort take script argument
[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 $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 foreach my $bs (split /_/, $ARGS{BuySell}) {
93         $bs =~ m/^(buy|sell)$/ or die;
94         $bs= $1;
95         my ($ascdesc) = ($bs eq 'buy')
96                 ? ('DESC')
97                 : ('ASC');
98         my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
99         my $islands= $dbh->prepare(
100         "SELECT islands.islandid AS islandid, archipelago, islandname,
101                         sum(qty) as tqty
102                 FROM islands $joinkind $bs offers
103                 ON islands.islandid == offers.islandid AND commodid == ?
104                 GROUP BY islands.islandid
105                 ORDER BY archipelago, islandname"
106                 );
107
108         my $offers= $dbh->prepare(
109         "SELECT stallname, price, qty
110                 FROM $bs NATURAL JOIN stalls
111                 WHERE commodid = ? AND islandid = ?
112                 ORDER BY price $ascdesc"
113                 );
114         # fixme this query is utterly wrong
115
116 </%perl>
117
118 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
119
120 <table id="<% $bs %>_table">
121 <tr>
122 <th colspan=3>
123 <th colspan=2>Prices
124 <th colspan=3>Quantity at price
125 <tr id="<% $bs %>_table_thr">
126 <th>Archipelago
127 <th>Island
128 <th>Stall(s)
129 <th>Best
130 <th>Median
131 <th>Best
132 <th>+/-10%
133 <th>Any
134 </tr>
135 %       $islands->execute($commodid);
136 %       my $island;
137 %       my %ts_sortkeys;
138 %       my $rowix= 0;
139 %       while ($island= $islands->fetchrow_hashref) {
140 %               my $islandid= $island->{'islandid'};
141 %               $offers->execute($commodid, $islandid);
142 %               my ($offer, $bestprice, $marginal, @beststalls);
143 %               my $tqty= $island->{'tqty'};
144 %               my $cqty= '';
145 %               my $bestqty= '';
146 %               my $approxqty= '';
147 %               my $median= '-';
148 %               while ($offer= $offers->fetchrow_hashref) {
149 %                       my $price= $offer->{'price'};
150 %                       my $qty= $offer->{'qty'};
151 %                       length $bestqty or $bestprice= $price;
152 %                       if ($price == $bestprice) {
153 %                               $bestqty += $qty;
154 %                               push @beststalls, $offer->{'stallname'};
155 %                       }
156 %                       $cqty += $qty;
157 %                       if ($cqty*2 >= $tqty && $median eq '-') {
158 %                               $median= $price;
159 %                       }
160 %                       if ($bestprice*9 <= $price*10 and
161 %                           $price*10 <= $bestprice*11) {
162 %                               $approxqty += $qty;
163 %                       }
164 %               }
165 %               my $stallname;
166 %
167 %               my $rowid= "id_${bs}_$islandid";
168 %               my $s= [ ];
169 %
170 %               $s->[2]= sprintf "%06d", scalar @beststalls;
171 %               if (!@beststalls) {
172 %                       $stallname= '-';
173 %               } elsif (@beststalls==1) {
174 %                       $stallname= $beststalls[0];
175 %                       $s->[2] .= " $stallname";
176 %               } else {
177 %                       $stallname= sprintf "%d offers", scalar @beststalls;
178 %               }
179 %
180 %               $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
181 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>">
182      <td><% $s->[0]= $island->{'archipelago'} |h %>
183      <td><% $s->[1]= $island->{'islandname'} |h %>
184      <td><%          $stallname |h %>
185      <td><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
186      <td><% $s->[4]= $median %>
187      <td><% $s->[5]= $bestqty %>
188      <td><% $s->[6]= $approxqty %>
189      <td><% $s->[7]= $cqty %>
190 </tr>
191 %               for my $cix (0..$#$s) {
192 %                       $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
193 %               }
194 %               $rowix++;
195 %       }
196 </table>
197
198 <&| tabsort,    table => "${bs}_table", sortkeys => "${bs}_sortkeys",
199                 throw => "${bs}_table_thr", rowclass => 'datarow', cols => [
200         {}, {},
201         { DoReverse => 1 },
202         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
203         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
204         { DoReverse => 1, Numeric => 1 },
205         { DoReverse => 1, Numeric => 1 },
206         { DoReverse => 1, Numeric => 1 },
207         ] &>
208   <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
209   function ts_Pricemap_<% $bs %>(price) {
210     if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
211     return price;
212   }
213 </&tabsort>
214
215 <%perl>
216 }
217 </%perl>