root/veekun/trunk/lib/Vee/Schema/Pokemon.pm @ 406

Revision 406, 4.0 KB (checked in by eevee, 22 months ago)

Database refactoring. Renamed columns and tables to be more consistent and more readable. (#58)

Line 
1package Vee::Schema::Pokemon;
2
3use strict;
4use warnings;
5use base 'DBIx::Class';
6
7use Vee::Dex::EncounterSet;
8
9=head1 NAME
10
11Vee::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
79Returns true if this Pokemon can breed, false otherwise.
80
81=cut
82
83sub can_breed {
84    my ($self) = @_;
85
86    return not grep { $_ == 15 } $self->breeding_groups;
87}
88
89=head2 breeding_groups
90
91Returns a list of egg group codes.
92
93=cut
94
95# TODO: rename this 'breeds' and rename the above join
96sub breeding_groups {
97    my ($self) = @_;
98    return map { $_->breed } $self->breeds;
99}
100
101=head2 breeding_code
102
103Returns the in-game breeding code for this Pokemon.
104
105=cut
106
107sub 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
117Returns true if this Pokemon can breed with the given Pokemon, false otherwise.
118
119=cut
120
121sub 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
136Returns the following data structure representing this Pokemon's wild
137encounters:
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
156sub 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
179L<Vee::Schema>, L<DBIx::Class>
180
181=head1 AUTHOR
182
183Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>)
184
185See the included F<AUTHORS> file for a full list of contributers.
186
187=head1 LICENSE
188
189See the included F<LICENSE> file.
190
191=cut
192
1931;
Note: See TracBrowser for help on using the browser.