chiark / gitweb /
2cbe719cf4eec1afa16860f947da2b5044ee1490
[ypp-sc-tools.db-live.git] / yarrg / web / check_routestring
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 parses textual strings giving lists of islands
33  and archipelagoes, ie textual route strings.
34
35 </%doc>
36
37 <%flags>
38 inherit => 'qtextstringcheck'
39 </%flags>
40
41 <%args>
42 $ocean
43 $format
44 $ctype => undef
45 $string
46 $returnhash => { }
47 </%args>
48
49 <%perl>
50
51 # typical url for this script:
52 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/routetextstring?format=json&ocean=Midnight&string=d
53
54 use CommodsWeb;
55
56 my $dbh= dbw_connect($ocean);
57
58 my $sth= $dbh->prepare("SELECT archipelago,islandid,islandname
59                                 FROM islands WHERE islandname LIKE ?
60         UNION ALL       SELECT DISTINCT archipelago,NULL,archipelago
61                                 FROM islands WHERE archipelago LIKE ?");
62
63 my $emsg= '';
64 my (@results, $canontext);
65
66 foreach my $each (split m#[/|,]#, $string) {
67         $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
68         next if !length $each;
69         my $err= sub { $emsg= sprintf $_[0], $each; last; };
70         my %m;
71         my $results;
72         foreach my $pat ("$each\%", "\%$each\%") {
73                 $sth->execute($pat,$pat);
74                 $results= $sth->fetchall_arrayref();
75                 last if @$results==1;
76                 map { $m{ $_->[2] }=1 } @$results;
77                 $results= undef;
78         }
79         if (!$results) {
80                 if (!%m) {
81                         $err->('no island or arch matches "%s"');
82                 } elsif (keys(%m) > 5) {
83                         $err->('&nbsp;');
84                 } else {
85                         $err->('ambiguous island or arch "%s",'.
86                                 ' could be '.join(', ', sort keys %m));
87                 }
88         }
89         push @results, $results->[0];
90 };
91
92 $dbh->disconnect();
93
94 return  $emsg,
95         (join ' | ', map { $_->[2] } @results),
96         [ @results ];
97
98 </%perl>