<%doc> This is part of the YARRG website. YARRG is a tool and website for assisting players of Yohoho Puzzle Pirates. Copyright (C) 2009 Ian Jackson Copyright (C) 2009 Clare Boothby YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later). The YARRG website is covered by the GNU Affero GPL v3 or later, which basically means that every installation of the website will let you download the source. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . Yohoho and Puzzle Pirates are probably trademarks of Three Rings and are used without permission. This program is not endorsed or sponsored by Three Rings. This Mason component generates the main `lookup' page, including all the entry boxes etc. for every query. <%perl> my %ahtml; my @vars; my %styles; #---------- "mode" argument parsing and mode menu at top of page ---------- # for debugging, invoke as # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1 @vars= ({ Name => 'Ocean', Before => 'Ocean: ', CmpCanon => sub { ucfirst lc $_[0] }, Values => [ ocean_list() ] }, { Name => 'Dropdowns', Before => 'Interface: ', CmpCanon => sub { !!$_[0] }, Values => [ [ 0, 'Type in names' ], [ 4, 'Select from menus' ] ] }, { Name => 'Query', Before => 'Query: ', Values => [ [ 'route', 'Trades for route' ], [ 'commod', 'Prices for commodity' ], [ 'age', 'Data age' ] ] }, { Name => 'BuySell', Before => '', Values => [ [ 'buy_sell', 'Buy and sell' ], [ 'sell_buy', 'Sell and buy' ], [ 'buy', 'Buy offers only' ], [ 'sell', 'Sell offers only' ], ], QuerySpecific => 1, }); foreach my $var (@vars) { my $name= $var->{Name}; my $lname= lc $name; $var->{Before}= '' unless exists $var->{Before}; $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon}; foreach my $val (@{ $var->{Values} }) { next if ref $val; $val= [ $val, encode_entities($val) ]; } if (exists $ARGS{$lname}) { $styles{$name}= $ARGS{$lname}; my @html= grep { $_->[0] eq $styles{$name} } @{ $var->{Values} }; $ahtml{$name}= @html==1 ? $html[0][1] : '???'; } else { $styles{$name}= $var->{Values}[0][0]; $ahtml{$name}= $var->{Values}[0][1]; } } <%shared> my %baseqf; my %queryqf; <%method formhidden> <%args> $ours % foreach my $n (keys %baseqf, keys %queryqf) { % next if $ours->($n); % my $v= exists $baseqf{$n} ? $baseqf{$n} : $queryqf{$n}; value="<% $v |h %>"> % } <% ucfirst $ahtml{Query} %> - YARRG YARRG - Yet Another Revenue Research Gatherer | documentation

<%perl> foreach my $var (@vars) { my $lname= lc $var->{Name}; next unless exists $ARGS{$lname}; $baseqf{$lname}= $ARGS{$lname}; } foreach my $var (keys %ARGS) { next unless $var =~ m/^(?:(?:route|commod)string|islandid\d|archipelago\d|debug)$/; my $val= $ARGS{$var}; next if $val eq 'none'; $queryqf{$var}= $val; } my $quri= sub { my $uri= URI->new('lookup'); $uri->query_form(@_); $uri->path_query(); }; my $prselector_core= sub { my ($var)= @_; my $name= $var->{Name}; my $lname= lc $var->{Name}; my $delim= $var->{Before}; my $canon= &{$var->{CmpCanon}}($styles{$name}); my $cvalix= 0; foreach my $valr (@{ $var->{Values} }) { print $delim; $delim= "\n|\n"; my ($value,$html) = @$valr; my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon; my $after; if ($iscurrent) { print ''; $after= ''; } else { my %qf= (%baseqf,%queryqf); delete $qf{$lname}; $qf{$lname}= $value if $cvalix; <%perl> $after= ''; } print $html, $after; $cvalix++; } print "

\n\n"; }; my $prselector= sub { my ($name)= @_; foreach my $var (@vars) { if ($var->{Name} eq $name) { $prselector_core->($var); return; } } die $name; }; foreach my $var (@vars) { next if $var->{QuerySpecific}; $prselector_core->($var); } #---------- initial checks, startup, main entry form ---------- die if $styles{Query} =~ m/[^a-z]/; my $mydbh; my $dbh= ($mydbh= dbw_connect($styles{Ocean})); my $results_head_done=0; my $someresults= sub { return if $results_head_done; $results_head_done=1; print "\n

Results

\n"; }; <%args> $debug => 0
<& "query_$styles{Query}", %baseqf, %queryqf, %styles, quri => $quri, dbh => $dbh, prselector => $prselector, someresults => $someresults, emsgokorprint => sub { my ($emsg) = @_; return 1 unless defined $emsg and length $emsg; $someresults->(); print $emsg; return 0; } &>

%#---------- debugging and epilogue ---------- % if ($debug) {

Debug log:
% } <&| script &> function debug (m) { % if ($debug) { var node= document.getElementById('debug_log'); node.innerHTML += "\n" + m + "\n"; % } } <& footer &> <%init> use CommodsWeb; use HTML::Entities; use URI::Escape; <%cleanup> $mydbh->rollback() if $mydbh;