| 1 | package Vee::Schema::Pokemon; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use base 'DBIx::Class'; |
|---|
| 6 | |
|---|
| 7 | use Vee::Dex::EncounterSet; |
|---|
| 8 | |
|---|
| 9 | =head1 NAME |
|---|
| 10 | |
|---|
| 11 | Vee::Schema::Pokemon - DBIC class for the C<pokemon> table |
|---|
| 12 | |
|---|
| 13 | =cut |
|---|
| 14 | |
|---|
| 15 | __PACKAGE__->load_components('Core'); |
|---|
| 16 | __PACKAGE__->table('pokemon'); |
|---|
| 17 | __PACKAGE__->add_columns(qw/ |
|---|
| 18 | id |
|---|
| 19 | id_johto |
|---|
| 20 | id_hoenn |
|---|
| 21 | id_sinnoh |
|---|
| 22 | |
|---|
| 23 | name |
|---|
| 24 | alt_form |
|---|
| 25 | name_jp |
|---|
| 26 | name_romaji |
|---|
| 27 | |
|---|
| 28 | evo_chain_id |
|---|
| 29 | evo_parent_id |
|---|
| 30 | evo_method |
|---|
| 31 | evo_param |
|---|
| 32 | |
|---|
| 33 | height |
|---|
| 34 | weight |
|---|
| 35 | type1 |
|---|
| 36 | type2 |
|---|
| 37 | species |
|---|
| 38 | color |
|---|
| 39 | habitat |
|---|
| 40 | |
|---|
| 41 | stat_at |
|---|
| 42 | stat_de |
|---|
| 43 | stat_sa |
|---|
| 44 | stat_sd |
|---|
| 45 | stat_sp |
|---|
| 46 | stat_hp |
|---|
| 47 | |
|---|
| 48 | effort |
|---|
| 49 | gameshark_rby |
|---|
| 50 | capture_rate |
|---|
| 51 | base_exp |
|---|
| 52 | gender_rate |
|---|
| 53 | base_happiness |
|---|
| 54 | notes |
|---|
| 55 | flags |
|---|
| 56 | real_pokemon_id |
|---|
| 57 | /); |
|---|
| 58 | __PACKAGE__->set_primary_key('id'); |
|---|
| 59 | |
|---|
| 60 | __PACKAGE__->belongs_to(evo_chain => 'Vee::Schema::EvoChains', 'evo_chain_id'); |
|---|
| 61 | __PACKAGE__->has_many(pokemon_moves => 'Vee::Schema::PokemonMoves', 'pokemon_id'); |
|---|
| 62 | __PACKAGE__->has_many(flavors => 'Vee::Schema::FlavorText', 'pokemon_id'); |
|---|
| 63 | __PACKAGE__->has_many(encounters => 'Vee::Schema::LocationEncounters', 'pokemon_id'); |
|---|
| 64 | __PACKAGE__->has_many(breeds => 'Vee::Schema::PokemonBreeds', 'pokemon_id'); |
|---|
| 65 | |
|---|
| 66 | __PACKAGE__->has_many(pokemon_abilities => 'Vee::Schema::PokemonAbilities', 'pokemon_id', { order_by => 'slot ASC' }); |
|---|
| 67 | __PACKAGE__->many_to_many(abilities => 'pokemon_abilities', 'ability'); |
|---|
| 68 | |
|---|
| 69 | __PACKAGE__->has_many(pokemon_items => 'Vee::Schema::PokemonItems', 'pokemon_id'); |
|---|
| 70 | __PACKAGE__->many_to_many(items => 'pokemon_items', 'item'); |
|---|
| 71 | |
|---|
| 72 | __PACKAGE__->might_have(parent => 'Vee::Schema::Pokemon', 'evo_parent_id'); |
|---|
| 73 | __PACKAGE__->has_many(descendants => 'Vee::Schema::Pokemon', 'evo_parent_id'); |
|---|
| 74 | |
|---|
| 75 | =head1 METHODS |
|---|
| 76 | |
|---|
| 77 | =head2 can_breed |
|---|
| 78 | |
|---|
| 79 | Returns true if this Pokemon can breed, false otherwise. |
|---|
| 80 | |
|---|
| 81 | =cut |
|---|
| 82 | |
|---|
| 83 | sub can_breed { |
|---|
| 84 | my ($self) = @_; |
|---|
| 85 | |
|---|
| 86 | return not grep { $_ == 15 } $self->breeding_groups; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | =head2 breeding_groups |
|---|
| 90 | |
|---|
| 91 | Returns a list of egg group codes. |
|---|
| 92 | |
|---|
| 93 | =cut |
|---|
| 94 | |
|---|
| 95 | # TODO: rename this 'breeds' and rename the above join |
|---|
| 96 | sub breeding_groups { |
|---|
| 97 | my ($self) = @_; |
|---|
| 98 | return map { $_->breed } $self->breeds; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | =head2 breeding_code |
|---|
| 102 | |
|---|
| 103 | Returns the in-game breeding code for this Pokemon. |
|---|
| 104 | |
|---|
| 105 | =cut |
|---|
| 106 | |
|---|
| 107 | sub breeding_code { |
|---|
| 108 | my ($self) = @_; |
|---|
| 109 | my @breeds = sort $self->breeding_groups; |
|---|
| 110 | @breeds = (@breeds) x 2 if @breeds == 1; # need two digits! |
|---|
| 111 | |
|---|
| 112 | return $breeds[0] * 16 + $breeds[1]; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | =head2 can_breed_with($pokemon) |
|---|
| 116 | |
|---|
| 117 | Returns true if this Pokemon can breed with the given Pokemon, false otherwise. |
|---|
| 118 | |
|---|
| 119 | =cut |
|---|
| 120 | |
|---|
| 121 | sub can_breed_with { |
|---|
| 122 | my ($poke1, $poke2) = @_; |
|---|
| 123 | my %breeds; |
|---|
| 124 | |
|---|
| 125 | # Egg groups cannot appear twice due to keying, so this is guaranteed to |
|---|
| 126 | # work |
|---|
| 127 | $breeds{$_}++ for $poke1->breeding_groups, $poke2->breeding_groups; |
|---|
| 128 | delete $breeds{15}; |
|---|
| 129 | |
|---|
| 130 | # Compatible if at least one egg group appears twice |
|---|
| 131 | return grep { $_ > 1 } values %breeds; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | =head2 sane_encounters |
|---|
| 135 | |
|---|
| 136 | Returns the following data structure representing this Pokemon's wild |
|---|
| 137 | encounters: |
|---|
| 138 | |
|---|
| 139 | [ |
|---|
| 140 | { |
|---|
| 141 | location => $location_obj, |
|---|
| 142 | section => $section, |
|---|
| 143 | methods => { |
|---|
| 144 | grass => [ |
|---|
| 145 | { |
|---|
| 146 | levels => [ 5, 7 ], |
|---|
| 147 | rarity => 100, |
|---|
| 148 | }, ... |
|---|
| 149 | ], ... |
|---|
| 150 | } |
|---|
| 151 | }, ... |
|---|
| 152 | ] |
|---|
| 153 | |
|---|
| 154 | =cut |
|---|
| 155 | |
|---|
| 156 | sub sane_encounters { |
|---|
| 157 | my ($self) = @_; |
|---|
| 158 | |
|---|
| 159 | my @encounter_sets; |
|---|
| 160 | |
|---|
| 161 | my $grouped_sections_rs = $self->encounters_rs( undef, { |
|---|
| 162 | group_by => 'section_id', |
|---|
| 163 | } ); |
|---|
| 164 | |
|---|
| 165 | while (my $enc_section = $grouped_sections_rs->next) { |
|---|
| 166 | my $encounters_rs = $enc_section->section->encounters_rs( { |
|---|
| 167 | version => 'diamond', |
|---|
| 168 | section_id => $enc_section->section->id, |
|---|
| 169 | } ); |
|---|
| 170 | |
|---|
| 171 | push @encounter_sets, Vee::Dex::EncounterSet->new($encounters_rs, $self); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | return @encounter_sets; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | =head1 SEE ALSO |
|---|
| 178 | |
|---|
| 179 | L<Vee::Schema>, L<DBIx::Class> |
|---|
| 180 | |
|---|
| 181 | =head1 AUTHOR |
|---|
| 182 | |
|---|
| 183 | Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>) |
|---|
| 184 | |
|---|
| 185 | See the included F<AUTHORS> file for a full list of contributers. |
|---|
| 186 | |
|---|
| 187 | =head1 LICENSE |
|---|
| 188 | |
|---|
| 189 | See the included F<LICENSE> file. |
|---|
| 190 | |
|---|
| 191 | =cut |
|---|
| 192 | |
|---|
| 193 | 1; |
|---|