kipyin / pokemaster

Checkout kipyin/pokemaster2 !
https://github.com/kipyin/pokemaster2
2 stars 2 forks source link

Feature: `GameVersion` API #18

Closed kipyin closed 5 years ago

kipyin commented 5 years ago

Feature Description and Rationale

The game Ruby (version ID 7) is a Generation 3 game, and it belongs to the "version group" ruby-sapphire (version group ID 5). Currently, the only way to get the "version group" name and ID mapping is via tables such as pokedex.db.tables.VersionGroup or pokedex.db.tables.Version. But making a query every time we want to retrieve the data is unnecessarily costly. Thus, adding a GameVersion class will help getting the right version/version group ID at ease.

Usage Example

>>> from pokemaster.game_version import GameVersion
>>> GameVersion.EMERALD.generation
<Gen.THREE: 3>
>>> # What's the version group ID for Platinum?
>>> GameVersion.PLATINUM.version_group
<VersionGroup.PLATINUM: 9>

Implementation Suggestions

First, we need a Gen enum and VersionGroup enum:

class Gen(enum.IntEnum):
    ONE = 1
    TWO = 2
    THREE = 3
    ...

class VersionGroup(enum.IntEnum):
    RED_BLUE = 1
    YELLOW = 2
    GOLD_SILVER = 3
    ...

Then, we can make a GameVersion enum that combines Gen and VersionGroup:

class GameVersion(enum.Enum):
    RED = (1, 1, 1)
    BLUE = (1, 1, 2)
    YELLOW = (1, 2, 3)
    ...
    def __init__(self, generation, version_group, version):
        self.generation = Gen(generation)
        self.version_group = VersionGroup(version_group)
        self.version = version

And then we can do something like:

>>> Gen(3) == Gen.THREE
True
>>> VersionGroup(17)
<VersionGroup.SUN_MOON: 17>
>>> GameVersion.PLATINUM
<GameVersion.PLATINUM: (4, 9, 14)>

If we make the values of GameVersion to namedtuple, we can even have something more descriptive:

>>> GameVersion.PLATINUM
<GameVersion.PLATINUM: VersionTuple(generation=4, version_group=9, version=14)>