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.
48 my ($commodname,$cmid);
53 <h1>Commodity enquiry</h1>
55 % $prselector->('BuySell');
56 % $prselector->('ShowBlank');
58 <form action="<% $quri->() |h %>" method="get">
60 <& enter_commod, qa => $qa, dbh => $dbh, emsg_r => \$emsg,
61 commodname_r => \$commodname,
65 <input type=submit name=submit value="Go">
66 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
67 <& "lookup:formhidden", ours => $ours &>
71 %#========== results ==========
74 $emsgokorprint->($emsg) or $cmid=undef;
75 return unless defined $cmid;
78 #---------- actually compute the results and print them ----------
80 foreach my $bs (split /_/, $ARGS{BuySell}) {
81 $bs =~ m/^(buy|sell)$/ or die;
83 my ($ascdesc) = ($bs eq 'buy')
86 my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
87 my $islands= $dbh->prepare(
88 "SELECT islands.islandid AS islandid, archipelago, islandname,
90 FROM islands $joinkind $bs offers
91 ON islands.islandid == offers.islandid AND commodid == ?
92 GROUP BY islands.islandid
93 ORDER BY archipelago, islandname"
96 my $offers= $dbh->prepare(
97 "SELECT stallname, price, qty
98 FROM $bs NATURAL JOIN stalls
99 WHERE commodid = ? AND islandid = ?
100 ORDER BY price $ascdesc"
105 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
106 % $islands->execute($cmid);
110 % while ($island= $islands->fetchrow_hashref) {
112 <table id="<% $bs %>_table" rules=groups>
121 <th colspan=3>Quantity at price
122 <tr id="<% $bs %>_table_thr">
133 % my $islandid= $island->{'islandid'};
134 % $offers->execute($cmid, $islandid);
135 % my ($offer, $bestprice, $marginal, @beststalls);
136 % my $tqty= $island->{'tqty'};
141 % while ($offer= $offers->fetchrow_hashref) {
142 % my $price= $offer->{'price'};
143 % my $qty= $offer->{'qty'};
144 % length $bestqty or $bestprice= $price;
145 % if ($price == $bestprice) {
147 % push @beststalls, $offer->{'stallname'};
150 % if ($cqty*2 >= $tqty && $median eq '-') {
153 % if ($bestprice*9 <= $price*10 and
154 % $price*10 <= $bestprice*11) {
155 % $approxqty += $qty;
160 % my $rowid= "id_${bs}_$islandid";
163 % $s->[2]= sprintf "%06d", scalar @beststalls;
164 % if (!@beststalls) {
166 % } elsif (@beststalls==1) {
167 % $stallname= $beststalls[0];
168 % $s->[2] .= " $stallname";
170 % $stallname= sprintf "%d offers", scalar @beststalls;
173 % $cqty == $tqty or die "$bs $cqty $tqty $cmid $islandid ";
174 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>">
175 <td><% $s->[0]= $island->{'archipelago'} |h %>
176 <td><% $s->[1]= $island->{'islandname'} |h %>
177 <td><% $stallname |h %>
178 <td align=right><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
179 <td align=right><% $s->[4]= $median %>
180 <td align=right><% $s->[5]= $bestqty %>
181 <td align=right><% $s->[6]= $approxqty %>
182 <td align=right><% $s->[7]= $cqty %>
184 % for my $cix (0..$#$s) {
185 % $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
192 <&| tabsort, table => "${bs}_table", sortkeys => "${bs}_sortkeys",
193 throw => "${bs}_table_thr", rowclass => 'datarow', cols => [
196 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
197 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
198 { DoReverse => 1, Numeric => 1 },
199 { DoReverse => 1, Numeric => 1 },
200 { DoReverse => 1, Numeric => 1 },
202 <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
203 function ts_Pricemap_<% $bs %>(price) {
204 if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }