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} eq 'show'
101 ? 'LEFT OUTER JOIN' : 'INNER JOIN';
102 my $islands= $dbh->prepare(
103 "SELECT islands.islandid AS islandid, archipelago, islandname,
105 FROM islands $joinkind $bs offers
106 ON islands.islandid == offers.islandid AND commodid == ?
107 GROUP BY islands.islandid,
108 ORDER BY archipelago, islandname"
111 my $offers= $dbh->prepare(
112 "SELECT stallname, price, qty
113 FROM $bs NATURAL JOIN stalls
114 WHERE commodid = ? AND islandid = ?
115 ORDER BY price $ascdesc"
117 # fixme this query is utterly wrong
121 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
123 <table id="<% $bs %>_table">
127 <th colspan=3>Quantity at price
128 <tr id="<% $bs %>_table_thr">
138 % $islands->execute($commodid);
141 % while ($island= $islands->fetchrow_hashref) {
142 % my $islandid= $island->{'islandid'};
143 % $offers->execute($commodid, $islandid);
144 % my ($offer, $bestprice, $marginal, @beststalls);
145 % my $tqty= $island->{'tqty'};
150 % while ($offer= $offers->fetchrow_hashref) {
151 % my $price= $offer->{'price'};
152 % my $qty= $offer->{'qty'};
153 % length $bestqty or $bestprice= $price;
154 % if ($price == $bestprice) {
156 % push @beststalls, $offer->{'stallname'};
159 % if ($cqty*2 >= $tqty && $median eq '-') {
162 % if ($bestprice*9 <= $price*10 and
163 % $price*10 <= $bestprice*11) {
164 % $approxqty += $qty;
169 % my $rowid= "id_${bs}_$islandid";
172 % $s->[2]= sprintf "%06d", scalar @beststalls;
173 % if (!@beststalls) {
175 % } elsif (@beststalls==1) {
176 % $stallname= $beststalls[0];
177 % $s->[2] .= " $stallname";
179 % $stallname= sprintf "%d offers", scalar @beststalls;
182 % $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
183 <tr id=<% $rowid %> >
184 <td><% $s->[0]= $island->{'archipelago'} |h %>
185 <td><% $s->[1]= $island->{'islandname'} |h %>
186 <td><% $stallname |h %>
187 <td><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
188 <td><% $s->[4]= $median %>
189 <td><% $s->[5]= $bestqty %>
190 <td><% $s->[6]= $approxqty %>
191 <td><% $s->[7]= $cqty %>
193 % for my $cix (0..$#$s) {
194 % $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
199 <& tabsort, table => "${bs}_table", sortkeys => "${bs}_sortkeys",
200 throw => "${bs}_table_thr", cols => [
203 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
204 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
205 { DoReverse => 1, Numeric => 1 },
206 { DoReverse => 1, Numeric => 1 },
207 { DoReverse => 1, Numeric => 1 },
210 <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
211 function ts_Pricemap_<% $bs %>(price) {
212 if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
216 % $onloads .= " ts_onload__${bs}_table();\n";
223 function all_onload() {
226 window.onload= all_onload;