Pokemon species split: media accessors

This commit is contained in:
Petr Viktorin 2011-04-30 03:21:37 +03:00
parent ab2baaa759
commit c710457717
2 changed files with 62 additions and 57 deletions

View file

@ -39,15 +39,15 @@ def if_available(func):
@if_available @if_available
def test_totodile(): def test_totodile():
"""Totodile's female sprite -- same as male""" """Totodile's female sprite -- same as male"""
totodile = session.query(tables.Pokemon).filter_by(identifier=u'totodile').one() totodile = session.query(tables.PokemonSpecies).filter_by(identifier=u'totodile').one()
accessor = media.PokemonMedia(root, totodile) accessor = media.PokemonSpeciesMedia(root, totodile)
assert accessor.sprite() == accessor.sprite(female=True) assert accessor.sprite() == accessor.sprite(female=True)
@if_available @if_available
def test_chimecho(): def test_chimecho():
"""Chimecho's Platinum female backsprite -- diffeent from male""" """Chimecho's Platinum female backsprite -- diffeent from male"""
chimecho = session.query(tables.Pokemon).filter_by(identifier=u'chimecho').one() chimecho = session.query(tables.PokemonSpecies).filter_by(identifier=u'chimecho').one()
accessor = media.PokemonMedia(root, chimecho) accessor = media.PokemonSpeciesMedia(root, chimecho)
male = accessor.sprite('platinum', back=True, frame=2) male = accessor.sprite('platinum', back=True, frame=2)
female = accessor.sprite('platinum', back=True, female=True, frame=2) female = accessor.sprite('platinum', back=True, female=True, frame=2)
assert male != female assert male != female
@ -55,16 +55,16 @@ def test_chimecho():
@if_available @if_available
def test_venonat(): def test_venonat():
"""Venonat's shiny Yellow sprite -- same as non-shiny""" """Venonat's shiny Yellow sprite -- same as non-shiny"""
venonat = session.query(tables.Pokemon).filter_by(identifier=u'venonat').one() venonat = session.query(tables.PokemonSpecies).filter_by(identifier=u'venonat').one()
accessor = media.PokemonMedia(root, venonat) accessor = media.PokemonSpeciesMedia(root, venonat)
assert accessor.sprite('yellow') == accessor.sprite('yellow', shiny=True) assert accessor.sprite('yellow') == accessor.sprite('yellow', shiny=True)
@if_available @if_available
def test_arceus_icon(): def test_arceus_icon():
"""Arceus fire-form icon -- same as base icon""" """Arceus fire-form icon -- same as base icon"""
arceus = session.query(tables.Pokemon).filter_by(identifier=u'arceus').one() arceus = session.query(tables.PokemonSpecies).filter_by(identifier=u'arceus').one()
accessor = media.PokemonMedia(root, arceus) accessor = media.PokemonSpeciesMedia(root, arceus)
fire_arceus = [f for f in arceus.forms if f.identifier == 'fire'][0] fire_arceus = [f for f in arceus.forms if f.form_identifier == 'fire'][0]
fire_accessor = media.PokemonFormMedia(root, fire_arceus) fire_accessor = media.PokemonFormMedia(root, fire_arceus)
assert accessor.icon() == fire_accessor.icon() assert accessor.icon() == fire_accessor.icon()
@ -72,8 +72,9 @@ def test_arceus_icon():
@raises(ValueError) @raises(ValueError)
def test_strict_castform(): def test_strict_castform():
"""Castform rainy form overworld with strict -- unavailable""" """Castform rainy form overworld with strict -- unavailable"""
castform = session.query(tables.Pokemon).filter_by(identifier=u'castform').first() castform = session.query(tables.PokemonSpecies).filter_by(identifier=u'castform').first()
rainy_castform = [f for f in castform.forms if f.identifier == 'rainy'][0] rainy_castform = [f for f in castform.forms if f.form_identifier == 'rainy'][0]
print rainy_castform
rainy_castform = media.PokemonFormMedia(root, rainy_castform) rainy_castform = media.PokemonFormMedia(root, rainy_castform)
rainy_castform.overworld('up', strict=True) rainy_castform.overworld('up', strict=True)
@ -81,8 +82,8 @@ def test_strict_castform():
@raises(ValueError) @raises(ValueError)
def test_strict_exeggcute(): def test_strict_exeggcute():
"""Exeggcutes's female backsprite, with strict -- unavailable""" """Exeggcutes's female backsprite, with strict -- unavailable"""
exeggcute = session.query(tables.Pokemon).filter_by(identifier=u'exeggcute').one() exeggcute = session.query(tables.PokemonSpecies).filter_by(identifier=u'exeggcute').one()
accessor = media.PokemonMedia(root, exeggcute) accessor = media.PokemonSpeciesMedia(root, exeggcute)
accessor.sprite(female=True, strict=True) accessor.sprite(female=True, strict=True)
@ -194,26 +195,26 @@ def check_get_everything():
accessors.append(media.UnknownPokemonMedia(root)) accessors.append(media.UnknownPokemonMedia(root))
accessors.append(media.EggMedia(root)) accessors.append(media.EggMedia(root))
manaphy = session.query(tables.Pokemon).filter_by(identifier=u'manaphy').one() manaphy = session.query(tables.PokemonSpecies).filter_by(identifier=u'manaphy').one()
accessors.append(media.EggMedia(root, manaphy)) accessors.append(media.EggMedia(root, manaphy))
accessors.append(media.SubstituteMedia(root)) accessors.append(media.SubstituteMedia(root))
for form in session.query(tables.PokemonForm).filter(tables.PokemonForm.identifier != '').all(): for form in session.query(tables.PokemonForm).all():
accessors.append(media.PokemonFormMedia(root, form)) accessors.append(media.PokemonFormMedia(root, form))
for pokemon in session.query(tables.Pokemon).all(): for pokemon in session.query(tables.PokemonSpecies).all():
accessors.append(media.PokemonMedia(root, pokemon)) accessors.append(media.PokemonSpeciesMedia(root, pokemon))
for accessor in accessors: for accessor in accessors:
assert hit(filenames, accessor.footprint) or not accessor.form assert hit(filenames, accessor.footprint) or not accessor.is_proper
assert hit(filenames, accessor.trozei) or not accessor.form or ( assert hit(filenames, accessor.trozei) or not accessor.is_proper or (
accessor.form.pokemon.generation.id > 3) accessor.introduced_in > 3)
assert hit(filenames, accessor.cry) or not accessor.form assert hit(filenames, accessor.cry) or not accessor.is_proper
assert hit(filenames, accessor.cropped_sprite) or not accessor.form assert hit(filenames, accessor.cropped_sprite) or not accessor.is_proper
for female in (True, False): for female in (True, False):
assert hit(filenames, accessor.icon, female=female) or not accessor.form assert hit(filenames, accessor.icon, female=female) or not accessor.is_proper
assert hit(filenames, accessor.sugimori, female=female) or ( assert hit(filenames, accessor.sugimori, female=female) or (
not accessor.form or accessor.form.pokemon.id >= 647) not accessor.is_proper or int(accessor.species_id) >= 647)
for shiny in (True, False): for shiny in (True, False):
for frame in (1, 2): for frame in (1, 2):
for direction in 'up down left right'.split(): for direction in 'up down left right'.split():
@ -222,8 +223,8 @@ def check_get_everything():
shiny=shiny, shiny=shiny,
female=female, female=female,
frame=frame, frame=frame,
) or not accessor.form or ( ) or not accessor.is_proper or (
accessor.form.pokemon.generation.id > 4) accessor.introduced_in > 4)
for version in versions: for version in versions:
for animated in (True, False): for animated in (True, False):
for back in (True, False): for back in (True, False):
@ -243,8 +244,8 @@ def check_get_everything():
shiny and not female and shiny and not female and
frame == 1): frame == 1):
# All pokemon are in Black # All pokemon are in Black
assert success or not accessor.form assert success or not accessor.is_proper
if (str(accessor.pokemon_id) == '1' if (str(accessor.species_id) == '1'
and not animated and not color and and not animated and not color and
frame == 1): frame == 1):
# Bulbasaur is in all versions # Bulbasaur is in all versions

View file

@ -105,7 +105,8 @@ class BaseMedia(object):
class _BasePokemonMedia(BaseMedia): class _BasePokemonMedia(BaseMedia):
toplevel_dir = 'pokemon' toplevel_dir = 'pokemon'
has_gender_differences = False has_gender_differences = False
form = None is_species = False
is_proper = False
introduced_in = 0 introduced_in = 0
# Info about of what's inside the pokemon main sprite directories, so we # Info about of what's inside the pokemon main sprite directories, so we
@ -126,13 +127,13 @@ class _BasePokemonMedia(BaseMedia):
'black-white': (5, set('back shiny female'.split())), 'black-white': (5, set('back shiny female'.split())),
} }
def __init__(self, root, pokemon_id, form_postfix=None): def __init__(self, root, species_id, form_postfix=None):
BaseMedia.__init__(self, root) BaseMedia.__init__(self, root)
self.pokemon_id = str(pokemon_id) self.species_id = str(species_id)
self.form_postfix = form_postfix self.form_postfix = form_postfix
def _get_file(self, path_elements, extension, strict, surely_exists=False): def _get_file(self, path_elements, extension, strict, surely_exists=False):
basename = str(self.pokemon_id) basename = str(self.species_id)
if self.form_postfix: if self.form_postfix:
fullname = basename + self.form_postfix fullname = basename + self.form_postfix
try: try:
@ -195,7 +196,7 @@ class _BasePokemonMedia(BaseMedia):
generation, info = self._pokemon_sprite_info[version_dir] generation, info = self._pokemon_sprite_info[version_dir]
if generation < self.introduced_in: if generation < self.introduced_in:
raise ValueError("Pokemon %s didn't exist in %s" % ( raise ValueError("Pokemon %s didn't exist in %s" % (
self.pokemon_id, version_dir)) self.species_id, version_dir))
path_elements = ['main-sprites', version_dir] path_elements = ['main-sprites', version_dir]
if animated: if animated:
if 'animated' not in info: if 'animated' not in info:
@ -235,7 +236,7 @@ class _BasePokemonMedia(BaseMedia):
# Chimecho's female back frame 2 sprite has one hand in # Chimecho's female back frame 2 sprite has one hand in
# a slightly different pose, in Platinum and HGSS # a slightly different pose, in Platinum and HGSS
# (we have duplicate sprites frame 1, for convenience) # (we have duplicate sprites frame 1, for convenience)
if self.pokemon_id == '358' and back and version_dir in ( if self.species_id == '358' and back and version_dir in (
'platinum', 'heartgold-soulsilver'): 'platinum', 'heartgold-soulsilver'):
female_sprite = True female_sprite = True
female_sprite = female_sprite and 'female' in info female_sprite = female_sprite and 'female' in info
@ -243,7 +244,7 @@ class _BasePokemonMedia(BaseMedia):
path_elements.append('female') path_elements.append('female')
elif strict: elif strict:
raise ValueError( raise ValueError(
'Pokemon %s has no gender differences' % self.pokemon_id) 'Pokemon %s has no gender differences' % self.species_id)
if not frame or frame == 1: if not frame or frame == 1:
pass pass
elif frame == 2: elif frame == 2:
@ -255,9 +256,8 @@ class _BasePokemonMedia(BaseMedia):
raise ValueError("Bad frame %s" % frame) raise ValueError("Bad frame %s" % frame)
return self._get_file(path_elements, extension, strict=strict, return self._get_file(path_elements, extension, strict=strict,
# Avoid a stat in the common case # Avoid a stat in the common case
surely_exists=(self.form and version_dir == 'black-white' surely_exists=(self.is_species and version_dir == 'black-white'
and not back and not female and not back and not female))
and not self.form_postfix))
def _maybe_female(self, path_elements, female, strict): def _maybe_female(self, path_elements, female, strict):
if female: if female:
@ -270,7 +270,7 @@ class _BasePokemonMedia(BaseMedia):
raise raise
elif strict: elif strict:
raise ValueError( raise ValueError(
'Pokemon %s has no gender differences' % self.pokemon_id) 'Pokemon %s has no gender differences' % self.species_id)
return self._get_file(path_elements, '.png', strict=strict) return self._get_file(path_elements, '.png', strict=strict)
def icon(self, female=False, strict=False): def icon(self, female=False, strict=False):
@ -336,28 +336,32 @@ class _BasePokemonMedia(BaseMedia):
return self._get_file(['cropped'], '.png', strict=strict) return self._get_file(['cropped'], '.png', strict=strict)
class PokemonFormMedia(_BasePokemonMedia): class PokemonFormMedia(_BasePokemonMedia):
"""Media related to a Pokemon form """Media related to a PokemonForm
""" """
is_proper = True
def __init__(self, root, pokemon_form): def __init__(self, root, pokemon_form):
pokemon_id = pokemon_form.form_base_pokemon_id species_id = pokemon_form.species.id
if pokemon_form.identifier: if pokemon_form.form_identifier:
form_postfix = '-' + pokemon_form.identifier form_postfix = '-' + pokemon_form.form_identifier
else: else:
form_postfix = None form_postfix = None
_BasePokemonMedia.__init__(self, root, pokemon_id, form_postfix) _BasePokemonMedia.__init__(self, root, species_id, form_postfix)
self.form = pokemon_form self.form = pokemon_form
pokemon = pokemon_form.form_base_pokemon species = pokemon_form.species
self.has_gender_differences = pokemon.has_gender_differences self.has_gender_differences = species.has_gender_differences
self.introduced_in = pokemon.generation_id self.introduced_in = pokemon_form.version_group.generation_id
class PokemonMedia(_BasePokemonMedia): class PokemonSpeciesMedia(_BasePokemonMedia):
"""Media related to a Pokemon """Media related to a PokemonSpecies
""" """
def __init__(self, root, pokemon): is_species = True
_BasePokemonMedia.__init__(self, root, pokemon.id) is_proper = True
self.form = pokemon.default_form
self.has_gender_differences = (pokemon.has_gender_differences) def __init__(self, root, species):
self.introduced_in = pokemon.generation_id _BasePokemonMedia.__init__(self, root, species.id)
self.has_gender_differences = species.has_gender_differences
self.introduced_in = species.generation_id
class UnknownPokemonMedia(_BasePokemonMedia): class UnknownPokemonMedia(_BasePokemonMedia):
"""Media related to the unknown Pokemon ("?") """Media related to the unknown Pokemon ("?")
@ -372,10 +376,10 @@ class EggMedia(_BasePokemonMedia):
Note that not a lot of files are available for these. Note that not a lot of files are available for these.
Give a Manaphy as `pokemon` to get the Manaphy egg. Give a Manaphy as `species` to get the Manaphy egg.
""" """
def __init__(self, root, pokemon=None): def __init__(self, root, species=None):
if pokemon and pokemon.identifier == 'manaphy': if species and species.identifier == 'manaphy':
postfix = '-manaphy' postfix = '-manaphy'
else: else:
postfix = None postfix = None