chiark / gitweb /
Midnight has some Lime dye; Cobalt has some Navy dye
[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 <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 print("</div>"), return
91         unless defined $cmid and @islandids;
92
93 foreach my $wf (@warningfs) { $wf->(); }
94
95 if ($qa->{'debug'}) {
96 </%perl>
97 <pre>
98 bs= <% $qa->{BuySell} %>
99 cmdid= <% $cmid %>
100 islandids= <% join ',', map { defined($_) ? $_ : 'U' } @islandids %>
101 </pre>
102 <%perl>
103 }
104
105 my $locdesc;
106 if (@islandids>1) {
107         $locdesc= ' at specified locations';
108 } elsif (defined $islandids[0]) {
109         my $sth= $dbh->prepare("SELECT islandname FROM islands
110                                  WHERE islandid == ?");
111         $sth->execute($islandids[0]);
112         $locdesc= ' at '.($sth->fetchrow_array())[0];
113 } else {
114         $locdesc= ' in '.$archipelagoes[0];
115 }
116
117 my $now= time;
118
119 my @conds;
120 my @condvals;
121 push @condvals, $cmid;
122 foreach my $ix (0..$#islandids) {
123         my $iid= $islandids[$ix];
124         my $arch= $archipelagoes[$ix];
125         if (defined $iid) {
126                 push @conds, 'offers.islandid == ?';
127                 push @condvals, $iid;
128         } else {
129                 push @conds, 'islands.archipelago == ?';
130                 push @condvals, $arch;
131         }
132 }
133 foreach my $bs (split /_/, $qa->{BuySell}) {
134         my %da_ages;
135         my %ts_sortkeys;
136
137         die unless grep { $bs eq $_ } qw(buy sell);
138         my $ascdesc= $bs eq 'buy' ? 'DESC' : 'ASC';
139 </%perl>
140 <h2>Offers to <% uc $bs |h %> <% $commodname |h %> <% $locdesc %></h2>
141 <%perl>
142         my $stmt= "
143             SELECT      archipelago, islandname,
144                         stallname, price, qty, timestamp,
145                         offers.stallid
146                 FROM $bs AS offers
147                 JOIN islands ON offers.islandid==islands.islandid
148                 JOIN uploads ON offers.islandid==uploads.islandid
149                 JOIN stalls ON offers.stallid==stalls.stallid
150                 WHERE offers.commodid == ?
151                  AND ( ".join("
152                     OR ", @conds)."
153                      )
154                 ORDER BY archipelago, islandname, price $ascdesc, qty ASC,
155                         stallname $ascdesc
156 ";
157         if ($qa->{'debug'}) {
158 </%perl>
159 <pre>
160 <% $stmt %>
161 <% join ',', @condvals |h %>
162 </pre>
163 <%perl>
164         }
165
166         my $row;
167         my $sth= $dbh->prepare($stmt);
168         $sth->execute(@condvals);
169         my $rowix= 0;
170 </%perl>
171 %       while ($row= $sth->fetchrow_arrayref) {
172 %               if (!$rowix) {
173 <table class="data" id="<% $bs %>_table" rules=groups>
174 <colgroup span=2>
175 <colgroup span=3>
176 <colgroup span=1>
177 <tr>
178 <th>Archipelago
179 <th>Island
180 <th>Stall or Shoppe
181 <th>Price
182 <th>Quantity
183 <th>Data age
184 </tr>
185 %               }
186 %               my $rowid= ${bs}.$row->[6];
187 %               my $tscellid= "c$rowid";
188 %               my $age= $now - $row->[5];
189 %               $da_ages{$rowid}= $age;
190 %               $row->[5]= 
191 <tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>" >
192 %               foreach my $ci (0..4) {
193 %                       my $val= $row->[$ci];
194 %                       $ts_sortkeys{$ci}{$rowid}= $val;
195 <td <% $ci >= 3 ? 'align=right' : '' %> ><% $val |h %>
196 %               }
197 <td id="<% $tscellid %>" align=right><% prettyprint_age($age) %>
198 </tr>
199 %               $rowix++;
200 %       }
201 %       if ($rowix) {
202 </table>
203
204 <&| tabsort, table => "${bs}_table", rowclass => 'datarow', cols => [
205         {}, {}, {},
206         { Numeric => 1, DoReverse => 1 },
207         { Numeric => 1, DoReverse => 1 },
208         { Numeric => 1, DoReverse => 1, SortKey => "${bs}_ages[rowid]" }],
209         sortkeys => "${bs}_sortkeys"
210   &>
211   <%$bs%>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
212   <%$bs%>_ages= <% to_json_protecttags(\%da_ages) %>;
213 </&tabsort>
214 %       } else {
215 No offers.
216 %       }
217
218 <%perl>
219 }
220 </%perl>
221
222 <p>
223 (Please don't use these pages to scrape data out of the YARRG
224 database.  This will be a pain for you to program, slow to run, and
225 pointlessly overload our server.  Instead, see our
226 <a href="devel">information for developers</a>
227 to find out how to get testing data or a real-time feed.)
228
229 </div>