chiark / gitweb /
routesearch: proper titles and pretty up the results
[ypp-sc-tools.db-live.git] / yarrg / web / query_offers
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 `offers' query.
33
34
35 </%doc>
36 <%args>
37 $quri
38 $dbh
39 $commodid => undef;
40 $commodstring => '';
41 $islandid => undef;
42 $prselector
43 $someresults
44 $emsgokorprint
45 </%args>
46
47 <%perl>
48 my $emsg;
49 my @warningfs;
50 my @islandids;
51 my @archipelagoes;
52 my ($commodname,$cmid);
53
54 my $qa= \%ARGS;
55 </%perl>
56
57 <h1>Prices for commodity at location(s)</h1>
58
59 % $prselector->('BuySell');
60
61 <form action="<% $quri->() |h %>" method="get">
62
63 <& enter_commod, qa => $qa, dbh => $dbh, emsg_r => \$emsg,
64         commodname_r => \$commodname,
65         cmid_r => \$cmid
66  &>
67
68 <& enter_route, qa => $qa, dbh => $dbh, emsg_r => \$emsg,
69         warningfs_r => \@warningfs,
70         enterwhat => 'Enter location',
71         islandids_r => \@islandids,
72         archipelagoes_r => \@archipelagoes
73  &>
74
75 <input type=submit name=submit value="Go">
76 % my $ours= sub { $_[0] =~
77 %    m/^commodstring|^commodid|^routestring|^archipelago|^island/;
78 % };
79 <& "lookup:formhidden", ours => $ours &>
80
81 </form>
82
83 %#========== results ==========
84 <%perl>
85
86 $emsgokorprint->($emsg) or $cmid=undef;
87 return unless defined $cmid and @islandids;
88
89 foreach my $wf (@warningfs) { $wf->(); }
90
91 if ($qa->{'debug'}) {
92 </%perl>
93 <pre>
94 bs= <% $qa->{BuySell} %>
95 cmdid= <% $cmid %>
96 islandids= <% join ',', map { defined($_) ? $_ : 'U' } @islandids %>
97 </pre>
98 <%perl>
99 }
100
101 my $locdesc;
102 if (@islandids>1) {
103         $locdesc= ' at specified locations';
104 } elsif (defined $islandids[0]) {
105         my $sth= $dbh->prepare("SELECT islandname FROM islands
106                                  WHERE islandid == ?");
107         $sth->execute($islandids[0]);
108         $locdesc= ' at '.($sth->fetchrow_array())[0];
109 } else {
110         $locdesc= ' in '.$archipelagoes[0];
111 }
112
113 my $now= time;
114
115 my @conds;
116 my @condvals;
117 push @condvals, $cmid;
118 foreach my $ix (0..$#islandids) {
119         my $iid= $islandids[$ix];
120         my $arch= $archipelagoes[$ix];
121         if (defined $iid) {
122                 push @conds, 'offers.islandid == ?';
123                 push @condvals, $iid;
124         } else {
125                 push @conds, 'islands.archipelago == ?';
126                 push @condvals, $arch;
127         }
128 }
129 foreach my $bs (split /_/, $qa->{BuySell}) {
130         my %da_ages;
131         my %ts_sortkeys;
132
133         die unless grep { $bs eq $_ } qw(buy sell);
134         my $ascdesc= $bs eq 'buy' ? 'DESC' : 'ASC';
135 </%perl>
136 <h2>Offers to <% uc $bs |h %> <% $commodname |h %> <% $locdesc %></h2>
137 <%perl>
138         my $stmt= "
139             SELECT      archipelago, islandname,
140                         stallname, price, qty, timestamp,
141                         offers.stallid
142                 FROM $bs AS offers
143                 JOIN islands ON offers.islandid==islands.islandid
144                 JOIN uploads ON offers.islandid==uploads.islandid
145                 JOIN stalls ON offers.stallid==stalls.stallid
146                 WHERE offers.commodid == ?
147                  AND ( ".join("
148                     OR ", @conds)."
149                      )
150                 ORDER BY archipelago, islandname, price $ascdesc, qty ASC,
151                         stallname $ascdesc
152 ";
153         if ($qa->{'debug'}) {
154 </%perl>
155 <pre>
156 <% $stmt %>
157 <% join ',', @condvals |h %>
158 </pre>
159 <%perl>
160         }
161
162         my $row;
163         my $sth= $dbh->prepare($stmt);
164         $sth->execute(@condvals);
165         my $rowix= 0;
166 </%perl>
167 %       while ($row= $sth->fetchrow_arrayref) {
168 %               if (!$rowix) {
169 <table id="<% $bs %>_table" rules=groups>
170 <colgroup span=2>
171 <colgroup span=3>
172 <colgroup span=1>
173 <tr>
174 <th>Archipelago
175 <th>Island
176 <th>Stall or Shoppe
177 <th>Price
178 <th>Quantity
179 <th>Data age
180 </tr>
181 %               }
182 %               my $rowid= ${bs}.$row->[6];
183 %               my $tscellid= "c$rowid";
184 %               my $age= $now - $row->[5];
185 %               $da_ages{$rowid}= $age;
186 %               $row->[5]= 
187 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>" >
188 %               foreach my $ci (0..4) {
189 %                       my $val= $row->[$ci];
190 %                       $ts_sortkeys{$ci}{$rowid}= $val;
191 <td <% $ci >= 3 ? 'align=right' : '' %> ><% $val |h %>
192 %               }
193 <td id="<% $tscellid %>" align=right><% prettyprint_age($age) %>
194 </tr>
195 %               $rowix++;
196 %       }
197 %       if ($rowix) {
198 </table>
199
200 <&| tabsort, table => "${bs}_table", rowclass => 'datarow', cols => [
201         {}, {}, {},
202         { Numeric => 1, DoReverse => 1 },
203         { Numeric => 1, DoReverse => 1 },
204         { Numeric => 1, DoReverse => 1, SortKey => "${bs}_ages[rowid]" }],
205         sortkeys => "${bs}_sortkeys"
206   &>
207   <%$bs%>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
208   <%$bs%>_ages= <% to_json_protecttags(\%da_ages) %>;
209 </&tabsort>
210 %       } else {
211 No offers.
212 %       }
213
214 <%perl>
215 }
216 </%perl>
217
218 <p>
219 (Please don't use these pages to scrape data out of the YARRG
220 database.  This will be a pain for you to program, slow to run, and
221 pointlessly overload our server.  Instead, see our
222 <a href="devel">information for developers</a>
223 to find out how to get testing data or a real-time feed.)