chiark / gitweb /
WIP trade plan
[ypp-sc-tools.db-live.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 $commodstring => '';
40 $prselector
41 $someresults
42 $emsgokorprint
43 </%args>
44
45 <%perl>
46 my $emsg;
47 my ($commodname,$commodid);
48
49 my $qa= \%ARGS;
50 </%perl>
51
52 <h1>Commodity enquiry</h1>
53
54 % $prselector->('BuySell');
55 % $prselector->('ShowBlank');
56
57 %#---------- textbox, user enters route as string ----------
58 % if (!$qa->{Dropdowns}) {
59
60 Enter commodity (abbreviations are OK):<br>
61
62 <form action="<% $quri->() |h %>" method="get">
63
64 <&| qtextstring, qa => $qa, dbh => $dbh,
65     thingstring => 'commodstring', emsgstore => \$emsg,
66     perresult => sub { ($commodname,$commodid)= @_; }
67  &>
68  size=80
69 </&>
70
71 % } else { #---------- dropdowns, user selects from menus ----------
72
73 Not yet implemented.
74
75 % } #---------- end of dropdowns, now common middle of page code ----------
76
77 <input type=submit name=submit value="Go">
78 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
79 <& "lookup:formhidden", ours => $ours &>
80
81 </form>
82
83 %#========== results ==========
84 <%perl>
85
86 $emsgokorprint->($emsg) or $commodid=undef;
87 return unless defined $commodid;
88 $someresults->();
89
90 #---------- actually compute the results and print them ----------
91
92 my $onloads= "";
93
94 foreach my $bs (split /_/, $ARGS{BuySell}) {
95         $bs =~ m/^(buy|sell)$/ or die;
96         $bs= $1;
97         my ($ascdesc) = ($bs eq 'buy')
98                 ? ('DESC')
99                 : ('ASC');
100         my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
101         my $islands= $dbh->prepare(
102         "SELECT islands.islandid AS islandid, archipelago, islandname,
103                         sum(qty) as tqty
104                 FROM islands $joinkind $bs offers
105                 ON islands.islandid == offers.islandid AND commodid == ?
106                 GROUP BY islands.islandid,
107                 ORDER BY archipelago, islandname"
108                 );
109
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"
115                 );
116         # fixme this query is utterly wrong
117
118 </%perl>
119
120 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
121
122 <table id="<% $bs %>_table">
123 <tr>
124 <th colspan=3>
125 <th colspan=2>Prices
126 <th colspan=3>Quantity at price
127 <tr id="<% $bs %>_table_thr">
128 <th>Archipelago
129 <th>Island
130 <th>Stall(s)
131 <th>Best
132 <th>Median
133 <th>Best
134 <th>+/-10%
135 <th>Any
136 </tr>
137 %       $islands->execute($commodid);
138 %       my $island;
139 %       my %ts_sortkeys;
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'};
145 %               my $cqty= '';
146 %               my $bestqty= '';
147 %               my $approxqty= '';
148 %               my $median= '-';
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) {
154 %                               $bestqty += $qty;
155 %                               push @beststalls, $offer->{'stallname'};
156 %                       }
157 %                       $cqty += $qty;
158 %                       if ($cqty*2 >= $tqty && $median eq '-') {
159 %                               $median= $price;
160 %                       }
161 %                       if ($bestprice*9 <= $price*10 and
162 %                           $price*10 <= $bestprice*11) {
163 %                               $approxqty += $qty;
164 %                       }
165 %               }
166 %               my $stallname;
167 %
168 %               my $rowid= "id_${bs}_$islandid";
169 %               my $s= [ ];
170 %
171 %               $s->[2]= sprintf "%06d", scalar @beststalls;
172 %               if (!@beststalls) {
173 %                       $stallname= '-';
174 %               } elsif (@beststalls==1) {
175 %                       $stallname= $beststalls[0];
176 %                       $s->[2] .= " $stallname";
177 %               } else {
178 %                       $stallname= sprintf "%d offers", scalar @beststalls;
179 %               }
180 %
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 %>
191 </tr>
192 %               for my $cix (0..$#$s) {
193 %                       $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
194 %               }
195 %       }
196 </table>
197
198 <& tabsort,     table => "${bs}_table", sortkeys => "${bs}_sortkeys",
199                 throw => "${bs}_table_thr", cols => [
200         {}, {},
201         { DoReverse => 1 },
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 },
207         ] &>
208 <&| script &>
209   <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
210   function ts_Pricemap_<% $bs %>(price) {
211     if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
212     return price;
213   }
214 </&>
215 %       $onloads .= "    ts_onload__${bs}_table();\n";
216
217 <%perl>
218 }
219 </%perl>
220
221 <&| script &>
222   function all_onload() {
223 <% $onloads %>
224   }
225   window.onload= all_onload;
226 </&>