chiark / gitweb /
Invokes "routetrade" component to get answers
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 Aug 2009 19:39:13 +0000 (20:39 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 Aug 2009 19:39:13 +0000 (20:39 +0100)
yarrg/web/route
yarrg/web/routetextstring
yarrg/web/routetrade [new file with mode: 0644]

index 1366345c44ab2d0e2361c7779ae589192919ac47..595976f8293a7cde6b6cbe56871ba5e90f47595b 100644 (file)
@@ -4,6 +4,11 @@
 my %a;
 my @vars;
 
+# for output:
+my @archipelagoes;
+my @islandids;
+my %islandid2;
+
 #---------- "mode" argument parsing and mode menu at top of page ----------
 
 # for debugging, invoke as
@@ -141,6 +146,7 @@ function tr_Ready() {
   tr_request= false;
   tr_Request();
 }
+window.onload= tr_Needed;
 </script>
 
 <input type="text" id="routestring" name="routestring" size=80
@@ -149,16 +155,6 @@ function tr_Ready() {
  onkeyup="tr_Later();"><br>
 <div id="routeresults">&nbsp;</div><br/>
 
-% if (length $routestring) {
-
-<pre>
-DATA FOR 
-<% $routestring |h %>
-WOULD GO HERE
-</pre>
-
-% }
-
 % } else { #---------- dropdowns, user selects from menus ----------
 
 <%perl>
@@ -199,6 +195,7 @@ while ($row=$sth->fetchrow_arrayref) {
        my $arch= $row->[2];
        push @{ $islandlistdata{'none'} }, [ @$row ];
        push @{ $islandlistdata{$arch} }, [ @$row ];
+       $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
 }
 
 my %resetislandlistdata;
@@ -249,11 +246,73 @@ function ms_Setarch(dd) {
 
 </table>
 
-% } #---------- end of dropdowns, now common code ----------
+% } #---------- end of dropdowns, now common middle of page code ----------
 
 <input type=submit name=submit value="Go">
 </form>
 
+<h1>Results</h1>
+
+<%perl>
+#========== result computations ==========
+
+#---------- result computation - textstring ----------
+if (!$a{Dropdowns}) {
+  if (length $routestring) {
+       my $rsr= $m->comp('routetextstring',
+               ocean => $a{Ocean},
+               string => $routestring,
+               format => 'return'
+       );
+       if (length $rsr->{Error}) {
+               print encode_entities($rsr->{Error});
+       } else {
+               foreach my $entry (@{ $rsr->{Results} }) {
+                       push @archipelagoes,
+                               defined $entry->[1] ? undef : $entry->[0];
+                       push @islandids, $entry->[1];
+               } 
+       }
+  }
+
+} else { #---------- results - dropdowns ----------
+
+my $argorundef= sub {
+       my ($dd,$base) = @_;
+       my $thing= $ARGS{"${base}${dd}"};
+       $thing= undef if defined $thing and $thing eq 'none';
+       return $thing;
+};
+
+for my $dd (0..$a{Dropdowns}-1) {
+       my $arch= &$argorundef($dd,'archipelago');
+       my $island= &$argorundef($dd,'islandid');
+       next unless defined $arch or defined $island;
+       if (defined $island and defined $arch) {
+               my $ii= $islandid2{$island};
+               my $iarch= $ii->{Arch};
+               if ($iarch ne $arch) {
+</%perl>
+ Specified archipelago <% $arch %> but
+ island <% $ii->{Name} %>
+ which is in <% $iarch %>; using the island.<br>
+<%perl>
+               }
+               $arch= undef;
+       }
+       push @archipelagoes, $arch;
+       push @islandids, $island;
+}
+
+}#---------- result processing, common stuff
+</%perl>
+
+% if (@islandids) {
+
+<& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
+
+% }
+
 %#---------- debugging and epilogue ----------
 
 % if ($debug) {
index 4cec5f9d337b1f994f694857ec528887a5c2828a..685b465255325fe566c6ed0f5468b51fa492c711 100644 (file)
@@ -31,12 +31,16 @@ if ($format =~ 'json') {
                success => 1,
                show => length $canontext ? encode_entities($canontext)
                        : '&nbsp;',
-       })};
+               })};
        $output_wrong= sub { print to_json({
                success => 0,
                show => $_[0],
-       })};
-}              
+               })};
+}
+if ($format =~ 'return') {
+       $output= sub { return { Error => '', Results => \@results }; };
+       $output_wrong= sub { return { Error => $_[0] }; };
+}
 
 foreach my $each (split m#[/|,]#, $string) {
        $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
@@ -57,12 +61,12 @@ foreach my $each (split m#[/|,]#, $string) {
        }
        if (!$results) {
                if (!%m) {
-                       $err->('no island or arch matches "%s"');
+                       return $err->('no island or arch matches "%s"');
                } elsif (%m > 5) {
-                       $err->('&nbsp;');
+                       return $err->('&nbsp;');
                } else {
-                       $err->('ambiguous island or arch "%s", could be '.
-                               join(', ', sort keys %m));
+                       return $err->('ambiguous island or arch "%s",'.
+                               ' could be '.join(', ', sort keys %m));
                }
        }
        push @results, $results->[0];
@@ -70,6 +74,6 @@ foreach my $each (split m#[/|,]#, $string) {
 
 $canontext= join ' | ', map { $_->[2] } @results;
 
-$output->();
+return $output->();
 
 </%perl>
diff --git a/yarrg/web/routetrade b/yarrg/web/routetrade
new file mode 100644 (file)
index 0000000..d0d4a76
--- /dev/null
@@ -0,0 +1,27 @@
+<%args>
+@islandids
+@archipelagoes
+</%args>
+
+%# So, add code to do right thing here:
+
+<pre>
+Route is as follows:
+
+% foreach my $i (0..$#islandids) {
+%   my $islandid= $islandids[$i];
+%   my $archipelago= $archipelagoes[$i];
+%   $islandid= '<undef>' unless defined $islandid;
+%   $archipelago= '<undef>' unless defined $archipelago;
+%
+  islandid=<% $islandid %>  archipelago=<% $archipelago %>
+%
+% }
+
+</pre>
+
+<%init>
+use CommodsWeb;
+db_setocean('Midnight');
+db_connect();
+</%init>