Changeset 344
- Timestamp:
- 10/15/07 21:45:11 (2 years ago)
- Location:
- veekun/trunk
- Files:
-
- 5 modified
-
lib/Vee/Controller/Dex.pm (modified) (3 diffs)
-
lib/Vee/Controller/Dex/Search.pm (modified) (2 diffs)
-
t/dex-search.t (modified) (2 diffs)
-
templates/dex/lookup/wildcard.tt (modified) (1 diff)
-
templates/dex/search/moves.tt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
veekun/trunk/lib/Vee/Controller/Dex.pm
r315 r344 13 13 use Storable qw/dclone/; 14 14 use Data::Dumper; 15 16 # TODO: move this somewhere shared 17 my $MAX_WILDCARD_RESULTS = 10; # maximum wildcard matches per category 15 18 16 19 =head1 NAME … … 171 174 172 175 (my $test = $entry) =~ s/[^-_ 0-9a-zA-Z]//g; 173 if ( 3> length $test) {176 if (2 > length $test) { 174 177 $c->vee_abort('Your entry ', $entry, ' is too vague. Please try to tone down the use of wildcards.'); 175 178 die; … … 184 187 185 188 my $re = qr/^$entry$/i; 186 my %results; 187 188 # TODO: use %FuzzyMatches somehow 189 $results{abilities} = [ grep { $AbilityNames[$_] =~ $re } 1 .. $#AbilityNames ]; 190 $results{pokemon} = [ grep { $PokemonNames[$_] =~ $re } 1 .. $#PokemonNames ]; 191 $results{types} = [ grep { $TypeNames[$_] =~ $re } 0 .. $#TypeNames ]; 192 $results{moves} = [ grep { $MoveData[$_]->name =~ $re } 1 .. $#MoveData ]; 193 194 my $total = 0; 195 $total += $#{$results{$_}} + 1 for keys %results; 189 my (%results, %overflow); 190 191 # Arrange the results by type 192 my @keys = grep { $_ =~ $re } keys %FuzzyMatches; 193 for my $key (@keys) { 194 my $type = $FuzzyMatches{$key}{type}; 195 my $array = ($results{$type} ||= []); 196 197 push @$array, $FuzzyMatches{$key}; 198 } 199 200 # Sort by name, for lack of anything particularly better 201 for my $type (keys %results) { 202 my $array = $results{$type}; 203 @$array = sort { 204 $a->{name} cmp $b->{name} 205 } @$array; 206 207 # Don't go crazy listing matches 208 if (@$array > $MAX_WILDCARD_RESULTS) { 209 @$array = @$array[ 0 .. $MAX_WILDCARD_RESULTS - 1 ]; 210 $overflow{$type} = 1; 211 } 212 } 213 214 my $total = @keys; 196 215 $c->vee_stop('No match for ', $orig, ' was found. You may wish to try the <a href="' . $c->uri('Dex', 'pokemon_list') . '">Pokémon list</a>, <a href="' . $c->uri('Dex', 'move_list') . '">move list</a>, <a href="' . $c->uri('Dex', 'type_list') . '">type list</a>, etc. to find what you are looking for.') unless $total; 197 $c->vee_stop('', $orig, ' returned too many matches. Please try something more specific.') if $total > 50;198 216 217 $s->{entry} = $orig; 199 218 $s->{results} = \%results; 219 $s->{overflow} = \%overflow; 200 220 $s->{template} = 'dex/lookup/wildcard.tt'; 201 221 } -
veekun/trunk/lib/Vee/Controller/Dex/Search.pm
r332 r344 109 109 # BASIC 110 110 if ($p->{name}) { 111 $criteria{'me.name'} = { like => '%' . $p->{name} . '%' }; 111 my $name = $p->{name}; 112 if ($name =~ /[*?]/) { 113 $name =~ tr/*?/%_/; 114 $criteria{'me.name'} = { like => $name }; 115 } else { 116 $criteria{'me.name'} = { like => "%$name%" }; 117 } 112 118 } 113 119 if ($p->{habitat} ne 'any') { $criteria{'me.habitat'} = $p->{habitat} } … … 328 334 # BASIC 329 335 if ($p->{name}) { 330 $criteria{name} = { LIKE => '%' . $p->{name} . '%' }; 336 my $name = $p->{name}; 337 if ($name =~ /[*?]/) { 338 $name =~ tr/*?/%_/; 339 $criteria{'me.name'} = { like => $name }; 340 } else { 341 $criteria{'me.name'} = { like => "%$name%" }; 342 } 331 343 } 332 344 if ($p->{class} ne 'any') { $criteria{class} = $p->{class} } -
veekun/trunk/t/dex-search.t
r343 r344 5 5 use lib 'lib'; 6 6 7 use Test::More tests => 3 4;7 use Test::More tests => 35; 8 8 9 9 BEGIN { $ENV{CATALYST_DEBUG} = 0; } … … 21 21 [qw/ 5 134 135 136 196 197 352 395 456 457 470 471 /], 22 22 "Name substring"; 23 24 search_ok { name => '???' }, 25 [qw/ 89 151 /], 26 "Name with wildcards"; 23 27 24 28 search_ok { ability => 'Drizzle' }, -
veekun/trunk/templates/dex/lookup/wildcard.tt
r282 r344 7 7 <h2>Pokémon</h2> 8 8 <ul> 9 [% FOREACH p IN results.pokemon.nsort%]10 <li><a href="[% dex_uri('pokemon', PokemonNames.$p) %]"><img src="/dex-images/icons/[% p.pad(3) %].png" alt=""/> [% PokemonNames.$p%]</a></li>9 [% FOREACH match IN results.pokemon %] 10 <li><a href="[% dex_uri('pokemon', match.name) %]"><img src="/dex-images/icons/[% match.id.pad(3) %].png" alt=""/> [% match.name %]</a></li> 11 11 [% END %] 12 12 </ul> 13 [% IF overflow.pokemon %] 14 <p> Too many Pokémon matched to list. Try the <a href="[% c.uri_for('/dex/pokemon/search', { name => entry }) %]">search</a> for more, or to narrow the list down. </p> 15 [% END %] 13 16 [% END %] 14 17 15 [% IF results.move s.size %]18 [% IF results.move.size %] 16 19 <h2>Moves</h2> 17 20 <ul> 18 [% FOREACH m IN results.moves.nsort%]19 <li><a href="[% dex_uri('moves', MoveData.$m.name) %]">[% MoveData.$m.name %]</a></li>21 [% FOREACH match IN results.move %] 22 <li><a href="[% dex_uri('moves', match.name) %]">[% match.name %]</a></li> 20 23 [% END %] 21 24 </ul> 25 [% IF overflow.move %] 26 <p> Too many moves matched to list. Try the <a href="[% c.uri_for('/dex/moves/search', { name => entry }) %]">search</a> for more, or to narrow the list down. </p> 27 [% END %] 22 28 [% END %] 23 29 24 [% IF results.abilit ies.size %]30 [% IF results.ability.size %] 25 31 <h2>Abilities</h2> 26 32 <ul> 27 [% FOREACH a IN results.abilities.nsort%]28 <li><a href="[% dex_uri('abilities', AbilityNames.$a) %]">[% AbilityNames.$a%]</a></li>33 [% FOREACH match IN results.ability %] 34 <li><a href="[% dex_uri('abilities', match.name) %]">[% match.name %]</a></li> 29 35 [% END %] 30 36 </ul> 37 [% IF overflow.ability %] 38 <p> Too many abilities matched to list. Try the <a href="[% c.uri_for('/dex/abilities') %]">list</a> instead? </p> 39 [% END %] 31 40 [% END %] 32 41 33 [% IF results.type s.size %]42 [% IF results.type.size %] 34 43 <h2>Types</h2> 35 44 <ul> 36 [% FOREACH t IN results.types.nsort%]37 <li><a href="[% dex_uri('types', TypeNames.$t) %]">[% TypeNames.$t%]</a></li>45 [% FOREACH match IN results.type %] 46 <li><a href="[% dex_uri('types', match.name) %]">[% match.name %]</a></li> 38 47 [% END %] 39 48 </ul> 49 [% IF overflow.type %] 50 <p> Too many types matched to list. Try the <a href="[% c.uri_for('/dex/types') %]">list</a> instead? </p> 51 [% END %] 40 52 [% END %] -
veekun/trunk/templates/dex/search/moves.tt
r291 r344 43 43 <ul> 44 44 [% FOREACH m IN results %] 45 <li><a href="[% dex_uri('moves', MoveData.${m.id}.name) %]">[% m.id %]: [%MoveData.${m.id}.name %]</a></li>45 <li><a href="[% dex_uri('moves', MoveData.${m.id}.name) %]">[% MoveData.${m.id}.name %]</a></li> 46 46 [% END %] 47 47 </ul>
