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');
55 % $prselector->('ShowBlank');
57 %#---------- textbox, user enters route as string ----------
58 % if (!$qa->{Dropdowns}) {
60 Enter commodity (abbreviations are OK):<br>
62 <form action="<% $quri->() |h %>" method="get">
64 <&| qtextstring, qa => $qa, dbh => $dbh,
65 thingstring => 'commodstring', emsgstore => \$emsg,
66 perresult => sub { ($commodname,$commodid)= @_; }
71 % } else { #---------- dropdowns, user selects from menus ----------
75 % } #---------- end of dropdowns, now common middle of page code ----------
77 <input type=submit name=submit value="Go">
78 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
79 <& "lookup:formhidden", ours => $ours &>
83 %#========== results ==========
86 $emsgokorprint->($emsg) or $commodid=undef;
87 return unless defined $commodid;
90 #---------- actually compute the results and print them ----------
94 foreach my $bs (split /_/, $ARGS{BuySell}) {
95 $bs =~ m/^(buy|sell)$/ or die;
97 my ($ascdesc) = ($bs eq 'buy')
100 my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
101 my $islands= $dbh->prepare(
102 "SELECT islands.islandid AS islandid, archipelago, islandname,
104 FROM islands $joinkind $bs offers
105 ON islands.islandid == offers.islandid AND commodid == ?
106 GROUP BY islands.islandid,
107 ORDER BY archipelago, islandname"
110 my $offers= $dbh->prepare(
111 "SELECT stallname, price, qty
112 FROM $bs NATURAL JOIN stalls
113 WHERE commodid = ? AND islandid = ?
114 ORDER BY price $ascdesc"
116 # fixme this query is utterly wrong
120 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
122 <table id="<% $bs %>_table">
126 <th colspan=3>Quantity at price
127 <tr id="<% $bs %>_table_thr">
137 % $islands->execute($commodid);
140 % while ($island= $islands->fetchrow_hashref) {
141 % my $islandid= $island->{'islandid'};
142 % $offers->execute($commodid, $islandid);
143 % my ($offer, $bestprice, $marginal, @beststalls);
144 % my $tqty= $island->{'tqty'};
149 % while ($offer= $offers->fetchrow_hashref) {
150 % my $price= $offer->{'price'};
151 % my $qty= $offer->{'qty'};
152 % length $bestqty or $bestprice= $price;
153 % if ($price == $bestprice) {
155 % push @beststalls, $offer->{'stallname'};
158 % if ($cqty*2 >= $tqty && $median eq '-') {
161 % if ($bestprice*9 <= $price*10 and
162 % $price*10 <= $bestprice*11) {
163 % $approxqty += $qty;
168 % my $rowid= "id_${bs}_$islandid";
171 % $s->[2]= sprintf "%06d", scalar @beststalls;
172 % if (!@beststalls) {
174 % } elsif (@beststalls==1) {
175 % $stallname= $beststalls[0];
176 % $s->[2] .= " $stallname";
178 % $stallname= sprintf "%d offers", scalar @beststalls;
181 % $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
182 <tr id=<% $rowid %> >
183 <td><% $s->[0]= $island->{'archipelago'} |h %>
184 <td><% $s->[1]= $island->{'islandname'} |h %>
185 <td><% $stallname |h %>
186 <td><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
187 <td><% $s->[4]= $median %>
188 <td><% $s->[5]= $bestqty %>
189 <td><% $s->[6]= $approxqty %>
190 <td><% $s->[7]= $cqty %>
192 % for my $cix (0..$#$s) {
193 % $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
198 <& tabsort, table => "${bs}_table", sortkeys => "${bs}_sortkeys",
199 throw => "${bs}_table_thr", cols => [
202 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
203 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
204 { DoReverse => 1, Numeric => 1 },
205 { DoReverse => 1, Numeric => 1 },
206 { DoReverse => 1, Numeric => 1 },
209 <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
210 function ts_Pricemap_<% $bs %>(price) {
211 if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
215 % $onloads .= " ts_onload__${bs}_table();\n";
222 function all_onload() {
225 window.onload= all_onload;