3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
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
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.
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.
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/>.
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.
32 This Mason component generates the core of the `commodity' query.
47 my ($commodname,$commodid);
52 <h1>Commodity enquiry</h1>
54 % $prselector->('BuySell');
56 %#---------- textbox, user enters route as string ----------
57 % if (!$qa->{Dropdowns}) {
59 Enter commodity (abbreviations are OK):<br>
61 <form action="<% $quri->() |h %>" method="get">
63 <&| qtextstring, qa => $qa, dbh => $dbh,
64 thingstring => 'commodstring', emsgstore => \$emsg,
65 perresult => sub { ($commodname,$commodid)= @_; }
70 % } else { #---------- dropdowns, user selects from menus ----------
74 % } #---------- end of dropdowns, now common middle of page code ----------
76 <input type=submit name=submit value="Go">
77 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
78 <& "lookup:formhidden", ours => $ours &>
82 %#========== results ==========
85 $emsgokorprint->($emsg) or $commodid=undef;
86 return unless defined $commodid;
89 #---------- actually compute the results and print them ----------
91 foreach my $bs (split /_/, $ARGS{BuySell}) {
92 $bs =~ m/^(buy|sell)$/ or die;
94 my ($ascdesc) = ($bs eq 'buy')
98 my $islands= $dbh->prepare(
99 "SELECT islandid, archipelago, islandname, sum(qty) as tqty
100 FROM $bs NATURAL JOIN islands
103 ORDER BY archipelago, islandname"
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"
112 # fixme this query is utterly wrong
116 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
122 <th colspan=3>Quantities available
126 <th>Unique best stall
133 % $islands->execute($commodid);
135 % while ($island= $islands->fetchrow_hashref) {
136 % my $islandid= $island->{'islandid'};
137 % $offers->execute($commodid, $islandid);
138 % my ($offer, $bestprice, $median, $marginal, @beststalls);
139 % my $tqty= $island->{'tqty'};
143 % while ($offer= $offers->fetchrow_hashref) {
144 % my $price= $offer->{'price'};
145 % my $qty= $offer->{'qty'};
146 % defined $bestprice or $bestprice= $price;
147 % if ($price == $bestprice) {
149 % push @beststalls, $offer->{'stallname'};
152 % if ($cqty*2 >= $tqty && !defined $median) {
155 % if ($bestprice*9 <= $price*10 and
156 % $price*10 <= $bestprice*11) {
157 % $approxqty += $qty;
160 % my $nstalls= @beststalls;
161 % $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
162 <tr> <td><% $island->{'archipelago'} |h %>
163 <td><% $island->{'islandname'} |h %>
164 <td><% $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>