chiark / gitweb /
Minor layout fixes
[ypp-sc-tools.main.git] / yarrg / web / query_commod
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
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
12    download the source.
13
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.
18
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.
23
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/>.
26
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.
30
31
32  This Mason component generates the core of the `commodity' query.
33
34
35 </%doc>
36 <%args>
37 $quri
38 $dbh
39 $commodid => undef;
40 $commodstring => '';
41 $prselector
42 $someresults
43 $emsgokorprint
44 </%args>
45
46 <%perl>
47 my $emsg;
48 my ($commodname,$cmid);
49
50 my $qa= \%ARGS;
51 </%perl>
52
53 <div class="query">
54 <h1>Commodity enquiry</h1>
55
56 % $prselector->('BuySell');
57 % $prselector->('ShowBlank');
58
59 <form action="<% $quri->() |h %>" method="get">
60
61 <& enter_commod, qa => $qa, dbh => $dbh, emsg_r => \$emsg,
62         commodname_r => \$commodname,
63         cmid_r => \$cmid
64  &>
65
66 <input type=submit name=submit value="Go">
67 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
68 <& "lookup:formhidden", ours => $ours &>
69
70 </form>
71
72 %#========== results ==========
73 </div>
74 <div class="results">
75 <%perl>
76
77 $emsgokorprint->($emsg) or $cmid=undef;
78 print("</div>"), return
79         unless defined $cmid;
80 $someresults->();
81
82 #---------- actually compute the results and print them ----------
83
84 foreach my $bs (split /_/, $ARGS{BuySell}) {
85         $bs =~ m/^(buy|sell)$/ or die;
86         $bs= $1;
87         my ($ascdesc) = ($bs eq 'buy')
88                 ? ('DESC')
89                 : ('ASC');
90         my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
91         my $islands= $dbh->prepare(
92         "SELECT islands.islandid AS islandid, archipelago, islandname,
93                         sum(qty) as tqty
94                 FROM islands $joinkind $bs offers
95                 ON islands.islandid == offers.islandid AND commodid == ?
96                 GROUP BY islands.islandid
97                 ORDER BY archipelago, islandname"
98                 );
99
100         my $offers= $dbh->prepare(
101         "SELECT stallname, price, qty
102                 FROM $bs NATURAL JOIN stalls
103                 WHERE commodid = ? AND islandid = ?
104                 ORDER BY price $ascdesc"
105                 );
106
107 </%perl>
108
109 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
110 %       $islands->execute($cmid);
111 %       my $island;
112 %       my %ts_sortkeys;
113 %       my $rowix= 0;
114 %       while ($island= $islands->fetchrow_hashref) {
115 %               if (!$rowix) {
116 <table class="data" id="<% $bs %>_table" rules=groups>
117 <colgroup span=2>
118 <colgroup span=1>
119 <colgroup span=2>
120 <colgroup span=3>
121 <tr>
122 <th colspan=2>
123 <th colspan=1>
124 <th colspan=2>Prices
125 <th colspan=3>Quantity at price
126 <tr id="<% $bs %>_table_thr">
127 <th>Archipelago
128 <th>Island
129 <th>Stall(s)
130 <th>Best
131 <th>Median
132 <th>Best
133 <th>+/-10%
134 <th>Any
135 </tr>
136 %               }
137 %               my $islandid= $island->{'islandid'};
138 %               $offers->execute($cmid, $islandid);
139 %               my ($offer, $bestprice, $marginal, @beststalls);
140 %               my $tqty= $island->{'tqty'};
141 %               my $cqty= '';
142 %               my $bestqty= '';
143 %               my $approxqty= '';
144 %               my $median= '-';
145 %               while ($offer= $offers->fetchrow_hashref) {
146 %                       my $price= $offer->{'price'};
147 %                       my $qty= $offer->{'qty'};
148 %                       length $bestqty or $bestprice= $price;
149 %                       if ($price == $bestprice) {
150 %                               $bestqty += $qty;
151 %                               push @beststalls, $offer->{'stallname'};
152 %                       }
153 %                       $cqty += $qty;
154 %                       if ($cqty*2 >= $tqty && $median eq '-') {
155 %                               $median= $price;
156 %                       }
157 %                       if ($bestprice*9 <= $price*10 and
158 %                           $price*10 <= $bestprice*11) {
159 %                               $approxqty += $qty;
160 %                       }
161 %               }
162 %               my $stallname;
163 %
164 %               my $rowid= "id_${bs}_$islandid";
165 %               my $s= [ ];
166 %
167 %               $s->[2]= sprintf "%06d", scalar @beststalls;
168 %               if (!@beststalls) {
169 %                       $stallname= '-';
170 %               } elsif (@beststalls==1) {
171 %                       $stallname= $beststalls[0];
172 %                       $s->[2] .= " $stallname";
173 %               } else {
174 %                       $stallname= sprintf "%d offers", scalar @beststalls;
175 %               }
176 %
177 %               $cqty == $tqty or die "$bs $cqty $tqty $cmid $islandid ";
178 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>">
179      <td><% $s->[0]= $island->{'archipelago'} |h %>
180      <td><% $s->[1]= $island->{'islandname'} |h %>
181      <td><%          $stallname |h %>
182      <td align=right><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
183      <td align=right><% $s->[4]= $median %>
184      <td align=right><% $s->[5]= $bestqty %>
185      <td align=right><% $s->[6]= $approxqty %>
186      <td align=right><% $s->[7]= $cqty %>
187 </tr>
188 %               for my $cix (0..$#$s) {
189 %                       $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
190 %               }
191 %               $rowix++;
192 %       }
193 %       if ($rowix) {
194 </table>
195
196 <&| tabsort,    table => "${bs}_table", sortkeys => "${bs}_sortkeys",
197                 throw => "${bs}_table_thr", rowclass => 'datarow', cols => [
198         {}, {},
199         { DoReverse => 1 },
200         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
201         { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
202         { DoReverse => 1, Numeric => 1 },
203         { DoReverse => 1, Numeric => 1 },
204         { DoReverse => 1, Numeric => 1 },
205         ] &>
206   <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
207   function ts_Pricemap_<% $bs %>(price) {
208     if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
209     return price;
210   }
211 </&tabsort>
212 %       } else {
213 No offers.
214 %       }
215
216 <%perl>
217 }
218 </%perl>
219 </div>