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

Revision 350, 2.9 KB (checked in by eevee, 2 years ago)

I discovered that DBIx::Class::Schema::Loader had been filling my table classes with lines of the form __PACKAGE__->add_unique_constraint(['id'], undef); erroneously, due to someone's inadvertently iterating over %hash rather than keys %hash. Not only were these lines redundant in the case of a primary key, but the parameters are supposed to be a name and a list of columns: thus I had a lot of constraints with ARRAY(0x...) as names and no columns. This was thoroughly breaking $schema->deploy, as it was trying to create indices with garbage names that looked like functions. I have, obviously, removed them all.
In the process, I have done something I've been meaning to do for a while: cleaned up the source in every single file to match my usual style. No auto-gen comment, no redundant loading of PK::Auto, no double-newline after the 1;, proper POD, use of qw// for passing lists, etc. This is actually a teeny part of my db cleanup tracking bug. (#58)

Line 
1package Vee::Schema::Pokemon;
2
3use strict;
4use warnings;
5use base 'DBIx::Class';
6
7=head1 NAME
8
9Vee::Schema::Pokemon - DBIC class for the C<pokemon> table
10
11=cut
12
13__PACKAGE__->load_components('Core');
14__PACKAGE__->table('pokemon');
15__PACKAGE__->add_columns(qw/
16    id
17    id_johto
18    id_hoenn
19    id_sinnoh
20
21    name
22    alt_form
23    name_jp
24    name_romaji
25
26    evid
27    evparent
28    evmethod
29    evparam
30
31    height
32    weight
33    type1
34    type2
35    species
36    color
37    habitat
38
39    stat_at
40    stat_de
41    stat_sa
42    stat_sd
43    stat_sp
44    stat_hp
45
46    effort
47    oldgs
48    caprate
49    baseexp
50    gender
51    happiness
52    root
53    eventred
54    eventblue
55    notes
56    flags
57    real_id
58/);
59__PACKAGE__->set_primary_key('id');
60
61__PACKAGE__->belongs_to(evchain => 'Vee::Schema::EvChains', 'evid');
62__PACKAGE__->has_many(pokemoves => 'Vee::Schema::PokeMoves', 'pokeid');
63__PACKAGE__->has_many(flavors => 'Vee::Schema::FlavorText', 'pokeid');
64__PACKAGE__->has_many(encounters => 'Vee::Schema::LocationEncounters', 'pokemon_id');
65__PACKAGE__->has_many(breeds => 'Vee::Schema::PokemonBreeds', 'pokeid');
66
67__PACKAGE__->has_many(pokemon_abilities => 'Vee::Schema::PokemonAbilities', 'pokeid', { order_by => 'slot ASC' });
68__PACKAGE__->many_to_many(abilities => 'pokemon_abilities', 'ability');
69
70__PACKAGE__->has_many(pokemon_items => 'Vee::Schema::PokemonItems', 'pokemon_id');
71__PACKAGE__->many_to_many(items => 'pokemon_items', 'item');
72
73__PACKAGE__->might_have(parent => 'Vee::Schema::Pokemon', 'evparent');
74__PACKAGE__->has_many(descendants => 'Vee::Schema::Pokemon', 'evparent');
75
76=head1 METHODS
77
78=head2 can_breed
79
80Returns true if this Pokemon can breed, false otherwise.
81
82=cut
83
84sub can_breed {
85    my ($self) = @_;
86
87    return not grep { $_ == 15 } $self->breeding_groups;
88}
89
90=head2 can_breed
91
92Returns a list of egg group codes.
93
94=cut
95
96# TODO: rename this 'breeds' and rename the above join
97sub breeding_groups {
98    my ($self) = @_;
99    return map { $_->breed } $self->breeds;
100}
101
102=head2 can_breed
103
104Returns the in-game breeding code for this Pokemon.
105
106=cut
107
108sub breeding_code {
109    my ($self) = @_;
110    my @breeds = sort $self->breeding_groups;
111    @breeds = (@breeds) x 2 if @breeds == 1;  # need two digits!
112
113    return $breeds[0] * 16 + $breeds[1];
114}
115
116=head2 can_breed_with($pokemon)
117
118Returns true if this Pokemon can breed with the given Pokemon, false otherwise.
119
120=cut
121
122sub can_breed_with {
123    my ($poke1, $poke2) = @_;
124    my %breeds;
125
126    # Egg groups cannot appear twice due to keying, so this is guaranteed to
127    # work
128    $breeds{$_}++ for $poke1->breeding_groups, $poke2->breeding_groups;
129    delete $breeds{15};
130
131    # Compatible if at least one egg group appears twice
132    return grep { $_ > 1 } values %breeds;
133}
134
135=head1 SEE ALSO
136
137L<Vee::Schema>, L<DBIx::Class>
138
139=head1 AUTHOR
140
141Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>)
142
143See the included F<AUTHORS> file for a full list of contributers.
144
145=head1 LICENSE
146
147See the included F<LICENSE> file.
148
149=cut
150
1511;
Note: See TracBrowser for help on using the browser.