chiark / gitweb /
Show medians etc.
[ypp-sc-tools.web-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
56 %#---------- textbox, user enters route as string ----------
57 % if (!$qa->{Dropdowns}) {
58
59 Enter commodity (abbreviations are OK):<br>
60
61 <form action="<% $quri->() |h %>" method="get">
62
63 <&| qtextstring, qa => $qa, dbh => $dbh,
64     thingstring => 'commodstring', emsgstore => \$emsg,
65     perresult => sub { ($commodname,$commodid)= @_; }
66  &>
67  size=80
68 </&>
69
70 % } else { #---------- dropdowns, user selects from menus ----------
71
72 Not yet implemented.
73
74 % } #---------- end of dropdowns, now common middle of page code ----------
75
76 <input type=submit name=submit value="Go">
77 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
78 <& "lookup:formhidden", ours => $ours &>
79
80 </form>
81
82 %#========== results ==========
83 <%perl>
84
85 $emsgokorprint->($emsg) or $commodid=undef;
86 return unless defined $commodid;
87 $someresults->();
88
89 #---------- actually compute the results and print them ----------
90
91 foreach my $bs (split /_/, $ARGS{BuySell}) {
92         $bs =~ m/^(buy|sell)$/ or die;
93         $bs= $1;
94         my ($ascdesc) = ($bs eq 'buy')
95                 ? ('DESC')
96                 : ('ASC');
97
98         my $islands= $dbh->prepare(
99         "SELECT islandid, archipelago, islandname, sum(qty) as tqty
100                 FROM $bs NATURAL JOIN islands
101                 WHERE commodid = ?
102                 GROUP BY islandid,
103                 ORDER BY archipelago, islandname"
104                 );
105
106         my $offers= $dbh->prepare(
107         "SELECT stallname, price, qty
108                 FROM $bs NATURAL JOIN stalls
109                 WHERE commodid = ? AND islandid = ?
110                 ORDER BY price $ascdesc"
111                 );
112         # fixme this query is utterly wrong
113
114 </%perl>
115
116 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
117
118 <table>
119 <tr>
120 <th>Archipelago
121 <th>Island
122 <th>Unique best stall
123 <th>Best price
124 <th>Qty at best
125 <th>Median price
126 <th>Total qty
127 </tr>
128 %       $islands->execute($commodid);
129 %       my $island;
130 %       while ($island= $islands->fetchrow_hashref) {
131 %               my $islandid= $island->{'islandid'};
132 %               $offers->execute($commodid, $islandid);
133 %               my ($offer, $best, $median);
134 %               my $tqty= $island->{'tqty'};
135 %               my $cqty= 0;
136 %               while ($offer= $offers->fetchrow_hashref) {
137 %                       if (!$best) {
138 %                               $best= { 'price' => $offer->{'price'} };
139 %                       }
140 %                       if ($offer->{'price'} == $best->{'price'}) {
141 %                               $best->{'qty'} += $offer->{'qty'};
142 %                               push @{ $best->{'stalls'} },
143 %                                       $offer->{'stallname'};
144 %                       }
145 %                       $cqty += $offer->{'qty'};
146 %                       if ($cqty*2 >= $tqty && !defined $median) {
147 %                               $median= $offer->{'price'};
148 %                       }
149 %               }
150 %               $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
151 %               my $nstalls= @{ $best->{'stalls'} };
152 <tr> <td><% $island->{'archipelago'} |h %>
153      <td><% $island->{'islandname'} |h %>
154      <td><% $nstalls==1 ? $best->{'stalls'}[0] : "$nstalls offers" |h %>
155      <td><% $best->{'price'} %>
156      <td><% $best->{'qty'} %>
157      <td><% $median %>
158      <td><% $tqty %>
159 </tr>
160 %       }
161 </table>
162
163 <%perl>
164 }
165 </%perl>