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