chiark / gitweb /
Make including islands with no offers optional
[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} eq 'show'
99                 ? 'LEFT OUTER JOIN' : 'INNER JOIN';
100         my $islands= $dbh->prepare(
101         "SELECT islands.islandid AS islandid, archipelago, islandname,
102                         sum(qty) as tqty
103                 FROM islands $joinkind $bs offers
104                 ON islands.islandid == offers.islandid AND commodid == ?
105                 GROUP BY islands.islandid,
106                 ORDER BY archipelago, islandname"
107                 );
108
109         my $offers= $dbh->prepare(
110         "SELECT stallname, price, qty
111                 FROM $bs NATURAL JOIN stalls
112                 WHERE commodid = ? AND islandid = ?
113                 ORDER BY price $ascdesc"
114                 );
115         # fixme this query is utterly wrong
116
117 </%perl>
118
119 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
120
121 <table>
122 <tr>
123 <th colspan=3>
124 <th colspan=2>Prices
125 <th colspan=3>Quantities available
126 <tr>
127 <th>Archipelago
128 <th>Island
129 <th>Unique best stall
130 <th>Best
131 <th>Median
132 <th>At best
133 <th>Within 10%
134 <th>Total
135 </tr>
136 %       $islands->execute($commodid);
137 %       my $island;
138 %       while ($island= $islands->fetchrow_hashref) {
139 %               my $islandid= $island->{'islandid'};
140 %               $offers->execute($commodid, $islandid);
141 %               my ($offer, $bestprice, $marginal, @beststalls);
142 %               my $tqty= $island->{'tqty'};
143 %               my $cqty= '';
144 %               my $bestqty= '';
145 %               my $approxqty= '';
146 %               my $median= '-';
147 %               while ($offer= $offers->fetchrow_hashref) {
148 %                       my $price= $offer->{'price'};
149 %                       my $qty= $offer->{'qty'};
150 %                       length $bestqty or $bestprice= $price;
151 %                       if ($price == $bestprice) {
152 %                               $bestqty += $qty;
153 %                               push @beststalls, $offer->{'stallname'};
154 %                       }
155 %                       $cqty += $qty;
156 %                       if ($cqty*2 >= $tqty && $median eq '-') {
157 %                               $median= $price;
158 %                       }
159 %                       if ($bestprice*9 <= $price*10 and
160 %                           $price*10 <= $bestprice*11) {
161 %                               $approxqty += $qty;
162 %                       }
163 %               }
164 %               my $nstalls= @beststalls;
165 %               $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
166 <tr> <td><% $island->{'archipelago'} |h %>
167      <td><% $island->{'islandname'} |h %>
168      <td><% $nstalls==0 ? '-' :
169             $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>
170      <td><% length $bestqty ? $bestprice : '-' %>
171      <td><% $median %>
172      <td><% $bestqty %>
173      <td><% $approxqty %>
174      <td><% $cqty %>
175 </tr>
176 %       }
177 </table>
178
179 <%perl>
180 }
181 </%perl>