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