Changeset 179

Show
Ignore:
Timestamp:
05/25/07 21:59:01 (3 years ago)
Author:
eevee
Message:

Added Perlbui's code!
Refactored Vee::Dex a bit: it now requires a call to initialize() to actually pull anything from the db, so apps besides Catalyst (like, say, Perlbui) can use it and pass in their own schema object.

Location:
veekun/trunk
Files:
2 added
2 modified

Legend:

Unmodified
Added
Removed
  • veekun/trunk/lib/Vee.pm

    r32 r179  
    2626use Vee::Authorization; 
    2727use Vee::Utils; 
     28use Vee::Dex; 
    2829use YAML qw//; 
    2930use LWP::UserAgent; 
     
    5253 
    5354__PACKAGE__->setup; 
     55 
     56Vee::Dex::initialize( __PACKAGE__->model('DBIC') ); 
    5457 
    5558{ 
  • veekun/trunk/lib/Vee/Dex.pm

    r174 r179  
    192192# DB CACHED STUFF 
    193193 
    194 my @pokenames = Vee->model('DBIC::Pokemon')->search({ 
    195     real_id => { '<=', $Generations[-1]{maxid} }, 
    196 }, { 
    197     order_by => 'id ASC', 
    198     columns => [qw/id name alt_form name_romaji name_jp/], 
    199 }); 
    200 our @PokemonNames  = map { $_->name . ($_->alt_form ? ' (' . $_->alt_form . ')' : '') } @pokenames; 
    201 our @PokemonRomaji = map { $_->name_romaji } @pokenames; 
    202 our @PokemonKana   = map { $_->name_jp     } @pokenames; 
    203  
    204 our @PokemonJohto  = Vee->model('DBIC::Pokemon')->search({ id_johto  => { '>', 0 } }, { order_by => 'id_johto',  columns => ['id'] })->get_column('id')->all; 
    205 our @PokemonHoenn  = Vee->model('DBIC::Pokemon')->search({ id_hoenn  => { '>', 0 } }, { order_by => 'id_hoenn',  columns => ['id'] })->get_column('id')->all; 
    206 our @PokemonSinnoh = Vee->model('DBIC::Pokemon')->search({ id_sinnoh => { '>', 0 } }, { order_by => 'id_sinnoh', columns => ['id'] })->get_column('id')->all; 
    207  
    208 # TODO: investigate DBIC caching; possibly turn this into a boring hash and change all references to it 
    209 our @MoveData = Vee->model('DBIC::Moves')->search(undef, { order_by => 'me.id ASC', prefetch => 'effect' }); 
    210 our $DamagingMoveCount = Vee->model('DBIC::Moves')->search({ power => { '!=' => undef } })->count; 
    211  
    212 our %TypeData = map { $_->name => $_ } Vee->model('DBIC::Types')->search({ internalid => { '!=' => -1 } }); 
    213 our @TypeNames = sort keys %TypeData; 
    214 our %OldTypeOrder = map { $_->internalid + 1 => $_->name } values %TypeData;  # for Compat.pm 
    215  
    216 our @AbilityNames = Vee->model('DBIC::Abilities')->search({ id => { '<', 200 } }, { order_by => 'id ASC', columns => ['name'] })->get_column('name')->all; 
    217  
    218 # machines 
    219 our @TMs; 
    220 my @tms = Vee->model('DBIC::Machines')->search(undef, { columns => ['id', 'generation', 'moveid'], order_by => 'id ASC' }); 
    221 for my $row (@tms) { 
    222     $TMs[ $row->generation ][ $row->id ] = $row->moveid 
    223 } 
    224  
    225 our %MoveTMs; 
    226 for my $gen (0 .. $#Generations) { 
    227     for my $tm (1 .. $#{$TMs[$gen]}) { 
    228         next unless $TMs[$gen][$tm]; 
    229         $MoveTMs{ $TMs[$gen][$tm] }[ $gen ] = $tm; 
    230     } 
    231 } 
    232  
    233 # so I can use one-based ids with these 
    234 unshift @PokemonNames, undef; 
    235 unshift @PokemonJohto, undef; 
    236 unshift @PokemonHoenn, undef; 
    237 unshift @PokemonSinnoh, undef; 
    238 unshift @PokemonKana, undef; 
    239 unshift @PokemonRomaji, undef; 
    240 unshift @MoveData, {}; 
    241 unshift @AbilityNames, undef; 
    242  
    243 # temporary, just for the below code; possibly to be promoted later 
    244 my @movenames = Vee->model('DBIC::Moves')->search(undef, { order_by => 'id ASC', columns => [qw/name name_romaji name_jp/] }); 
    245 my @movekana   = undef, map { $_->name_jp     } @movenames; 
    246 my @moveromaji = undef, map { $_->name_romaji } @movenames; 
    247  
    248 # create an index of fuzzy matches 
     194# You must call initialize with a $schema object to load all this, as this code 
     195# is used by both Catalyst and an outside app! 
     196 
     197our (@PokemonNames, @PokemonRomaji, @PokemonKana); 
     198our (@PokemonJohto, @PokemonHoenn, @PokemonSinnoh); 
     199our (@MoveData, $DamagingMoveCount); 
     200our (%TypeData, @TypeNames, %OldTypeOrder); 
     201our (@AbilityNames); 
     202our (@TMs, %MoveTMs); 
    249203our %FuzzyMatches; 
    250 $FuzzyMatches{ lc $TypeNames[$_]       } = { type => 'type',    id => $_, name => $TypeNames[$_]       } for 0 .. $#TypeNames; 
    251 $FuzzyMatches{ lc $movekana[$_]        } = 
    252 $FuzzyMatches{ lc $moveromaji[$_]      } = 
    253 $FuzzyMatches{ lc $MoveData[$_]->name  } = { type => 'move',    id => $_, name => $MoveData[$_]->name  } for 1 .. $#MoveData; 
    254 $FuzzyMatches{ lc $AbilityNames[$_]    } = { type => 'ability', id => $_, name => $AbilityNames[$_]    } for 1 .. $#AbilityNames; 
    255  
    256 # @pokenames is used over @PokemonNames here, due to the alt_name being embedded in @PokemonNames entries 
    257 $FuzzyMatches{ lc $_->name_jp       } = 
    258 $FuzzyMatches{ lc $_->name_romaji   } = 
    259 $FuzzyMatches{ lc $_->name          } = { type => 'pokemon', id => $_->id, name => $_->name } for @pokenames; 
     204 
     205sub initialize { 
     206    my ($schema) = @_; 
     207 
     208    my @pokenames = $schema->resultset('Pokemon')->search({ 
     209        real_id => { '<=', $Generations[-1]{maxid} }, 
     210    }, { 
     211        order_by => 'id ASC', 
     212        columns => [qw/id name alt_form name_romaji name_jp/], 
     213    }); 
     214    @PokemonNames  = map { $_->name . ($_->alt_form ? ' (' . $_->alt_form . ')' : '') } @pokenames; 
     215    @PokemonRomaji = map { $_->name_romaji } @pokenames; 
     216    @PokemonKana   = map { $_->name_jp     } @pokenames; 
     217 
     218    @PokemonJohto  = $schema->resultset('Pokemon')->search({ id_johto  => { '>', 0 } }, { order_by => 'id_johto',  columns => ['id'] })->get_column('id')->all; 
     219    @PokemonHoenn  = $schema->resultset('Pokemon')->search({ id_hoenn  => { '>', 0 } }, { order_by => 'id_hoenn',  columns => ['id'] })->get_column('id')->all; 
     220    @PokemonSinnoh = $schema->resultset('Pokemon')->search({ id_sinnoh => { '>', 0 } }, { order_by => 'id_sinnoh', columns => ['id'] })->get_column('id')->all; 
     221 
     222    # TODO: investigate DBIC caching; possibly turn this into a boring hash and change all references to it 
     223    @MoveData = $schema->resultset('Moves')->search(undef, { order_by => 'me.id ASC', prefetch => 'effect' }); 
     224    $DamagingMoveCount = $schema->resultset('Moves')->search({ power => { '!=' => undef } })->count; 
     225 
     226    %TypeData = map { $_->name => $_ } $schema->resultset('Types')->search({ internalid => { '!=' => -1 } }); 
     227    @TypeNames = sort keys %TypeData; 
     228    %OldTypeOrder = map { $_->internalid + 1 => $_->name } values %TypeData;  # for Compat.pm 
     229 
     230    @AbilityNames = $schema->resultset('Abilities')->search({ id => { '<', 200 } }, { order_by => 'id ASC', columns => ['name'] })->get_column('name')->all; 
     231 
     232    # so I can use one-based ids with these 
     233    unshift @PokemonNames, undef; 
     234    unshift @PokemonJohto, undef; 
     235    unshift @PokemonHoenn, undef; 
     236    unshift @PokemonSinnoh, undef; 
     237    unshift @PokemonKana, undef; 
     238    unshift @PokemonRomaji, undef; 
     239    unshift @MoveData, {}; 
     240    unshift @AbilityNames, undef; 
     241 
     242    # machines 
     243    my @tms = $schema->resultset('Machines')->search(undef, { columns => ['id', 'generation', 'moveid'], order_by => 'id ASC' }); 
     244    for my $row (@tms) { 
     245        $TMs[ $row->generation ][ $row->id ] = $row->moveid 
     246    } 
     247 
     248    for my $gen (0 .. $#Generations) { 
     249        for my $tm (1 .. $#{$TMs[$gen]}) { 
     250            next unless $TMs[$gen][$tm]; 
     251            $MoveTMs{ $TMs[$gen][$tm] }[ $gen ] = $tm; 
     252        } 
     253    } 
     254 
     255    # ------------------------------------------------------------------------------ 
     256    # fuzzy cache 
     257 
     258    # temporary, just for the below code; possibly to be promoted later 
     259    my @movenames = $schema->resultset('Moves')->search(undef, { order_by => 'id ASC', columns => [qw/name name_romaji name_jp/] }); 
     260    my @movekana   = undef, map { $_->name_jp     } @movenames; 
     261    my @moveromaji = undef, map { $_->name_romaji } @movenames; 
     262 
     263    # create an index of fuzzy matches 
     264    our %FuzzyMatches; 
     265    $FuzzyMatches{ lc $TypeNames[$_]       } = { type => 'type',    id => $_, name => $TypeNames[$_]       } for 0 .. $#TypeNames; 
     266    $FuzzyMatches{ lc $movekana[$_]        } = 
     267    $FuzzyMatches{ lc $moveromaji[$_]      } = 
     268    $FuzzyMatches{ lc $MoveData[$_]->name  } = { type => 'move',    id => $_, name => $MoveData[$_]->name  } for 1 .. $#MoveData; 
     269    $FuzzyMatches{ lc $AbilityNames[$_]    } = { type => 'ability', id => $_, name => $AbilityNames[$_]    } for 1 .. $#AbilityNames; 
     270 
     271    # @pokenames is used over @PokemonNames here, due to the alt_name being embedded in @PokemonNames entries 
     272    $FuzzyMatches{ lc $_->name_jp       } = 
     273    $FuzzyMatches{ lc $_->name_romaji   } = 
     274    $FuzzyMatches{ lc $_->name          } = { type => 'pokemon', id => $_->id, name => $_->name } for @pokenames; 
     275 
     276    return 1; 
     277} 
     278 
     279 
    260280 
    261281################################################################################ 
     
    636656my %vartypes = ( ARRAY => '@', HASH => '%', SCALAR => '$', CODE => '' ); 
    637657for my $var (keys %Vee::Dex::) { 
    638     next if $var eq 'EXPORT_FAIL' || $var eq 'EXPORT' || $var eq 'import' || $var eq 'all' || $var =~ /^_/; 
     658    next if $var =~ /^_/ or grep { $var eq $_ } qw/EXPORT_FAIL EXPORT import all initialize/; 
    639659     
    640660    my @valid_types = grep { defined *{ $Vee::Dex::{$var} }{$_} } keys %vartypes;