chiark / gitweb /
f194fc5d56d2cb3f4a6a2e955dd0b84c3493cfc2
[ypp-sc-tools.db-live.git] / yarrg / web / lookup
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 main `lookup' page, including
33  all the entry boxes etc. for every query.
34
35
36 </%doc>
37 <%perl>
38 my %ahtml;
39 my @vars;
40 my %styles;
41
42 #---------- "mode" argument parsing and mode menu at top of page ----------
43
44 # for debugging, invoke as
45 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
46
47 @vars= ({       Name => 'Ocean',
48                 Before => 'Ocean: ',
49                 CmpCanon => sub { ucfirst lc $_[0] },
50                 Values => [ ocean_list() ]
51         }, {    Name => 'Dropdowns',
52                 Before => 'Interface: ',
53                 CmpCanon => sub { !!$_[0] },
54                 Values => [     [ 0, 'Type in names' ],
55                                 [ 4, 'Select from menus' ] ]
56         }, {    Name => 'Query',
57                 Before => 'Query: ',
58                 Values => [     [ 'route', 'Trades for route' ],
59                                 [ 'commod', 'Prices for commodity' ],
60                                 [ 'age', 'Data age' ] ]
61         });
62
63 foreach my $var (@vars) {
64         my $name= $var->{Name};
65         my $lname= lc $name;
66         $var->{Before}= '' unless exists $var->{Before};
67         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
68         foreach my $val (@{ $var->{Values} }) {
69                 next if ref $val;
70                 $val= [ $val, encode_entities($val) ];
71         }
72         if (exists $ARGS{$lname}) {
73                 $styles{$name}= $ARGS{$lname};
74                 my @html= grep { $_->[0] eq $styles{$name} }
75                                 @{ $var->{Values} };
76                 $ahtml{$name}= @html==1 ? $html[0][1] : '???';
77         } else {
78                 $styles{$name}= $var->{Values}[0][0];
79                 $ahtml{$name}= $var->{Values}[0][1];
80         }
81 }
82
83 </%perl>
84
85 <%shared>
86 my %baseqf;
87 my %queryqf;
88 </%shared>
89
90 <%method formhidden>
91 <%args>
92 $ours
93 </%args>
94 % foreach my $n (keys %baseqf, keys %queryqf) {
95 %       next if $ours->($n);
96 %       my $v= exists $baseqf{$n} ? $baseqf{$n} : $queryqf{$n};
97 <input type=hidden name=<% $n %> value="<% $v |h %>">
98 % }
99 </%method>
100
101 <html><head><title><% ucfirst $ahtml{Query} %> - YARRG</title></head><body>
102
103 <a href="<% $m->current_comp()->name() |u %>">YARRG</a> -
104  Yet Another Revenue Research Gatherer
105 |
106 <a href="docs">documentation</a>
107 <p>
108 <%perl>
109
110 foreach my $var (@vars) {
111         my $lname= lc $var->{Name};
112         next unless exists $ARGS{$lname};
113         $baseqf{$lname}= $ARGS{$lname};
114 }
115
116 foreach my $var (keys %ARGS) {
117         next unless $var =~
118                 m/^(?:(?:route|commod)string|islandid\d|archipelago\d|debug)$/;
119         my $val= $ARGS{$var};
120         next if $val eq 'none';
121         $queryqf{$var}= $val;
122 }
123
124 my $quri= sub {
125         my $uri= URI->new('lookup');
126         $uri->query_form(@_);
127         $uri->path_query();
128 };
129
130 foreach my $var (@vars) {
131         my $name= $var->{Name};
132         my $lname= lc $var->{Name};
133         my $delim= $var->{Before};
134         my $canon= &{$var->{CmpCanon}}($styles{$name});
135         my $cvalix= 0;
136         foreach my $valr (@{ $var->{Values} }) {
137                 print $delim;  $delim= "\n|\n";
138                 my ($value,$html) = @$valr;
139                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
140                 my $after;
141                 if ($iscurrent) {
142                         print '<b>';
143                         $after= '</b>';
144                 } else {
145                         my %qf= (%baseqf,%queryqf);
146                         delete $qf{$lname};
147                         $qf{$lname}= $value if $cvalix;
148 </%perl>
149 <a href="<% $quri->(%qf) |h %>">
150 <%perl>
151                         $after= '</a>';
152                 }
153                 print $html, $after;
154                 $cvalix++;
155         }
156         print "<p>\n\n";
157 }
158
159 #---------- initial checks, startup, main entry form ----------
160
161 die if $styles{Query} =~ m/[^a-z]/;
162
163 my $mydbh;
164 my $dbh= ($mydbh= dbw_connect($styles{Ocean}));
165
166 my $results_head_done=0;
167 my $someresults= sub {
168         return if $results_head_done;
169         $results_head_done=1;
170         print "\n<h1>Results</h1>\n";
171 };
172
173 </%perl>
174 <%args>
175 $debug => 0
176 </%args>
177
178 <hr>
179
180 <& "query_$styles{Query}", %baseqf, %queryqf, %styles,
181     quri => $quri, dbh => $dbh,
182     someresults => $someresults,
183     emsgokorprint => sub {
184         my ($emsg) = @_;
185         return 1 unless defined $emsg and length $emsg;
186         $someresults->();
187         print $emsg;
188         return 0;
189     }
190  &>
191
192 <p>
193
194 %#---------- debugging and epilogue ----------
195
196 % if ($debug) {
197 <p>
198 <pre id="debug_log">
199 Debug log:
200 </pre>
201 % }
202
203 <&| script &>
204 function debug (m) {
205 % if ($debug) {
206   var node= document.getElementById('debug_log');
207   node.innerHTML += "\n" + m + "\n";
208 % }
209 }
210 </&script>
211
212 <& footer &>
213
214 <%init>
215 use CommodsWeb;
216 use HTML::Entities;
217 use URI::Escape;
218
219 </%init>
220 <%cleanup>
221
222 $mydbh->rollback() if $mydbh;
223
224 </%cleanup>