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 %#---------- textbox, user enters route as string ----------
61 % if (!$qa->{Dropdowns}) {
63 Enter commodity (abbreviations are OK):<br>
65 <&| qtextstring, qa => $qa, dbh => $dbh,
66 thingstring => 'commodstring', emsgstore => \$emsg,
67 perresult => sub { ($commodname,$cmid)= @_; }
72 % } else { #---------- dropdowns, user selects from menus ----------
74 % my $sth= $dbh->prepare("SELECT commodname,commodid FROM commods
75 % ORDER BY commodname");
78 <select name="commodid">
79 <option value="">Select commodity...</option>
80 % while ($row= $sth->fetchrow_arrayref) {
81 % my $selected= $commodid eq $row->[1] ? 'selected' : '';
82 <option value="<% $row->[1] %>" <% $selected %>><% $row->[0] |h %></option>
83 % ($commodname,$cmid) = @$row if $selected;
87 % } #---------- end of dropdowns, now common middle of page code ----------
89 <input type=submit name=submit value="Go">
90 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
91 <& "lookup:formhidden", ours => $ours &>
95 %#========== results ==========
98 $emsgokorprint->($emsg) or $cmid=undef;
99 return unless defined $cmid;
102 #---------- actually compute the results and print them ----------
104 foreach my $bs (split /_/, $ARGS{BuySell}) {
105 $bs =~ m/^(buy|sell)$/ or die;
107 my ($ascdesc) = ($bs eq 'buy')
110 my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
111 my $islands= $dbh->prepare(
112 "SELECT islands.islandid AS islandid, archipelago, islandname,
114 FROM islands $joinkind $bs offers
115 ON islands.islandid == offers.islandid AND commodid == ?
116 GROUP BY islands.islandid
117 ORDER BY archipelago, islandname"
120 my $offers= $dbh->prepare(
121 "SELECT stallname, price, qty
122 FROM $bs NATURAL JOIN stalls
123 WHERE commodid = ? AND islandid = ?
124 ORDER BY price $ascdesc"
129 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
130 % $islands->execute($cmid);
134 % while ($island= $islands->fetchrow_hashref) {
136 <table id="<% $bs %>_table">
140 <th colspan=3>Quantity at price
141 <tr id="<% $bs %>_table_thr">
152 % my $islandid= $island->{'islandid'};
153 % $offers->execute($cmid, $islandid);
154 % my ($offer, $bestprice, $marginal, @beststalls);
155 % my $tqty= $island->{'tqty'};
160 % while ($offer= $offers->fetchrow_hashref) {
161 % my $price= $offer->{'price'};
162 % my $qty= $offer->{'qty'};
163 % length $bestqty or $bestprice= $price;
164 % if ($price == $bestprice) {
166 % push @beststalls, $offer->{'stallname'};
169 % if ($cqty*2 >= $tqty && $median eq '-') {
172 % if ($bestprice*9 <= $price*10 and
173 % $price*10 <= $bestprice*11) {
174 % $approxqty += $qty;
179 % my $rowid= "id_${bs}_$islandid";
182 % $s->[2]= sprintf "%06d", scalar @beststalls;
183 % if (!@beststalls) {
185 % } elsif (@beststalls==1) {
186 % $stallname= $beststalls[0];
187 % $s->[2] .= " $stallname";
189 % $stallname= sprintf "%d offers", scalar @beststalls;
192 % $cqty == $tqty or die "$bs $cqty $tqty $cmid $islandid ";
193 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>">
194 <td><% $s->[0]= $island->{'archipelago'} |h %>
195 <td><% $s->[1]= $island->{'islandname'} |h %>
196 <td><% $stallname |h %>
197 <td><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
198 <td><% $s->[4]= $median %>
199 <td><% $s->[5]= $bestqty %>
200 <td><% $s->[6]= $approxqty %>
201 <td><% $s->[7]= $cqty %>
203 % for my $cix (0..$#$s) {
204 % $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
211 <&| tabsort, table => "${bs}_table", sortkeys => "${bs}_sortkeys",
212 throw => "${bs}_table_thr", rowclass => 'datarow', cols => [
215 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
216 { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
217 { DoReverse => 1, Numeric => 1 },
218 { DoReverse => 1, Numeric => 1 },
219 { DoReverse => 1, Numeric => 1 },
221 <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
222 function ts_Pricemap_<% $bs %>(price) {
223 if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }