chiark / gitweb /
Do queries and get basic debugging-style results
[ypp-sc-tools.web-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 </%perl>
92 <pre>
93 NOT YET IMPLEMENTED
94 bs= <% $qa->{BuySell} %>
95 cmdid= <% $cmid %>
96 islandids= <% join ',', map { defined($_) ? $_ : 'U' } @islandids %>
97 </pre>
98 <%perl>
99
100 my $locdesc;
101 if (@islandids>1) {
102         $locdesc= ' at specified locations';
103 } elsif (defined $islandids[0]) {
104         my $sth= $dbh->prepare("SELECT islandname FROM islands
105                                  WHERE islandid == ?");
106         $sth->execute($islandids[0]);
107         $locdesc= ' at '.($sth->fetchrow_array())[0];
108 } else {
109         $locdesc= ' in '.$archipelagoes[0];
110 }
111
112 my @conds;
113 my @condvals;
114 push @condvals, $cmid;
115 foreach my $ix (0..$#islandids) {
116         my $iid= $islandids[$ix];
117         my $arch= $archipelagoes[$ix];
118         if (defined $iid) {
119                 push @conds, 'offers.islandid == ?';
120                 push @condvals, $iid;
121         } else {
122                 push @conds, 'islands.archipelago == ?';
123                 push @condvals, $arch;
124         }
125 }
126 foreach my $bs (split /_/, $qa->{BuySell}) {
127         die unless grep { $bs eq $_ } qw(buy sell);
128         my $ascdesc= $bs eq 'buy' ? 'DESC' : 'ASC';
129 </%perl>
130 <h2>Offers to <% uc $bs |h %> <% $commodname |h %> <% $locdesc %></h2>
131 <%perl>
132         my $stmt= "
133             SELECT archipelago, islandname, stallname, price, qty, timestamp
134                 FROM $bs AS offers
135                 JOIN islands ON offers.islandid==islands.islandid
136                 JOIN uploads ON offers.islandid==uploads.islandid
137                 JOIN stalls ON offers.stallid==stalls.stallid
138                 WHERE offers.commodid == ?
139                  AND ( ".join("
140                     OR ", @conds)."
141                      )
142                 ORDER BY archipelago, islandname, price $ascdesc, qty ASC,
143                         stallname $ascdesc
144 ";
145         if ($qa->{'debug'}) {
146 </%perl>
147 <pre>
148 <% $stmt %>
149 <% join ',', @condvals |h %>
150 </pre>
151 <%perl>
152         }
153
154         my $sth= $dbh->prepare($stmt);
155         $sth->execute(@condvals);
156 </%perl>
157         <& dumptable, sth => $sth &>
158 <%perl>
159 }
160
161 </%perl>