Ejemplo n.º 1
0
async def formats(msg: ProtocolMessage) -> None:
    formatslist = msg.params

    tiers: dict[str, Tier] = {}
    section: str | None = None
    section_next = False
    for tier in formatslist:
        if tier[0] == ",":
            section_next = True
            continue
        if section_next:
            section = tier
            section_next = False
            continue
        parts = tier.split(",")
        if section is not None:
            tier_id = utils.to_id(parts[0])
            code = int(parts[1], 16)
            tiers[tier_id] = Tier(
                tier_id,
                parts[0],  # name
                section,
                bool(code & 1),  # random
                # bool(code & 2),  # ladder
                # bool(code & 4),  # challenge
                bool(code & 8),  # tournament
                # bool(code & 16),  # teambuilder_level
            )
    msg.conn.tiers = tiers
Ejemplo n.º 2
0
async def icon(msg: Message) -> None:
    if len(msg.args) < 1:
        await msg.reply(
            "You can set your Pokémon using "
            f"``{msg.conn.command_character}icon <pokemon>``, "
            "or install the userstyle (https://git.io/JuoFg) "
            "using Stylus (https://add0n.com/stylus.html)."
        )
        return

    search_query = utils.to_id(msg.args[0])
    dex_entry = utils.get_ps_dex_entry(search_query)
    if dex_entry is None or dex_entry["num"] <= 0:
        await msg.reply("Pokémon not found.")
        return

    db = Database.open()
    with db.get_session() as session:
        userid = msg.user.userid
        session.add(d.Users(userid=userid))
        stmt = (
            update(d.Users)
            .filter_by(userid=userid)
            .values(icon=utils.to_id(dex_entry["dex_name"]))
        )
        session.execute(stmt)

        # Update the CSV file
        stmt_csv = (
            select(d.Users.userid, d.Users.icon)
            .where(d.Users.icon.is_not(None))
            .order_by(d.Users.userid)
        )
        with utils.get_config_file("userlist_icons.csv").open(
            "w", encoding="utf-8"
        ) as f:
            f.writelines(
                [f"{userid},{icon}\n" for userid, icon in session.execute(stmt_csv)]
            )

    await msg.reply(
        "Done. Your Pokémon might take up to 24 hours to appear on the userstyle."
    )
Ejemplo n.º 3
0
async def encounters(msg: Message) -> None:
    if len(msg.args) < 1:
        return

    location_id = utils.to_id(utils.remove_diacritics(msg.args[0].lower()))

    language_id = msg.language_id
    if len(msg.args) >= 2:
        language_id = utils.get_language_id(msg.args[1], fallback=language_id)

    db = Database.open("veekun")

    with db.get_session(language_id) as session:

        stmt = (select(v.Locations).options(
            selectinload(v.Locations.location_areas).options(
                selectinload(v.LocationAreas.location_area_prose),
                selectinload(v.LocationAreas.encounters).options(
                    selectinload(v.Encounters.version).selectinload(
                        v.Versions.version_names),
                    selectinload(v.Encounters.pokemon).selectinload(
                        v.Pokemon.species).selectinload(
                            v.PokemonSpecies.pokemon_species_names),
                    selectinload(v.Encounters.encounter_slot).selectinload(
                        v.EncounterSlots.encounter_method).selectinload(
                            v.EncounterMethods.encounter_method_prose),
                    selectinload(v.Encounters.encounter_condition_value_map).
                    selectinload(v.EncounterConditionValueMap.
                                 encounter_condition_value).selectinload(
                                     v.EncounterConditionValues.
                                     encounter_condition_value_prose),
                ),
            )).filter_by(identifier=location_id))
        # TODO: remove annotation
        location: v.Locations | None = session.scalar(stmt)
        if location is None:
            await msg.reply("Location not found.")
            return

        results = Encounters()

        for area in location.location_areas:
            for encounter in area.encounters:
                results.add_encounter_slot(area, encounter)

        html = EncountersHTML(encounters_data=results)

        if not html:
            await msg.reply("No data available.")
            return

        await msg.reply_htmlbox(html.doc)
Ejemplo n.º 4
0
def get_sprite_parameters(args: list[str]) -> tuple[bool, bool, str]:
    back = False
    shiny = False
    category = "ani"

    for arg in args:
        arg = to_id(arg)
        if arg == "back":
            back = True
        elif arg == "shiny":
            shiny = True
        else:
            category = SPRITE_CATEGORIES.get(arg, category)

    return back, shiny, category
Ejemplo n.º 5
0
def _get_translations(
        word: str, languages: tuple[int,
                                    int]) -> dict[tuple[str, str], set[str]]:
    word = utils.to_user_id(utils.remove_diacritics(utils.get_alias(word)))

    results: dict[tuple[str, str], set[str]] = {}

    db = Database.open("veekun")

    with db.get_session() as session:

        tables: dict[str, tuple[type[v.TranslatableMixin],
                                type[TranslatableTableNames]]] = {
                                    "ability": (v.Abilities, v.AbilityNames),
                                    "item": (v.Items, v.ItemNames),
                                    "move": (v.Moves, v.MoveNames),
                                    "nature": (v.Natures, v.NatureNames),
                                }

        for category_name, t in tables.items():
            stmt = (select(t[0], t[1].local_language_id).select_from(
                t[0]).join(t[1]).where(
                    t[1].local_language_id.in_(languages),
                    t[1].name_normalized == word,
                ))
            # TODO: remove annotations
            row: v.TranslatableMixin
            language_id: int
            for row, language_id in session.execute(stmt):
                translation = row.get_translation(
                    f"{category_name}_names",
                    language_id=list(set(languages) - {language_id})[0],
                    fallback_english=False,
                )

                if translation is not None:
                    res = (
                        category_name,
                        utils.to_id(utils.remove_diacritics(translation)),
                    )
                    if res not in results:
                        results[res] = set()
                    results[res].add(translation)

    return results
Ejemplo n.º 6
0
async def sprite(msg: Message) -> None:
    if len(msg.args) < 1:
        return

    search_query = to_id(msg.args[0])
    dex_entry = get_ps_dex_entry(search_query)
    if dex_entry is None:
        await msg.reply("Nome pokemon non valido")
        return

    back, shiny, category = get_sprite_parameters(msg.args[1:])

    url = generate_sprite_url(dex_entry,
                              back=back,
                              shiny=shiny,
                              category=category)

    try:
        html = await image_url_to_html(url)
        await msg.reply_htmlbox(html)
    except UnsupportedFormat:
        # Missing sprite. We received a generic Apache error webpage.
        await msg.reply("Sprite non trovato")
Ejemplo n.º 7
0
async def learnset(msg: Message) -> None:
    if len(msg.args) < 2:
        return

    pokemon_id = utils.to_id(utils.remove_diacritics(msg.args[0].lower()))
    version_id = utils.to_id(utils.remove_diacritics(msg.args[1].lower()))

    language_id = msg.language_id
    if len(msg.args) >= 3:
        language_id = utils.get_language_id(msg.args[2], fallback=language_id)

    db = Database.open("veekun")

    with db.get_session(language_id) as session:

        stmt = select(v.VersionGroups).filter_by(identifier=version_id)
        # TODO: remove annotation
        version_group: v.VersionGroups | None = session.scalar(stmt)

        if version_group is None:
            stmt = select(v.Versions).filter_by(identifier=version_id)
            # TODO: remove annotation
            version: v.Versions | None = session.scalar(stmt)
            if version is None:
                await msg.reply("Game version not found.")
                return
            version_group = version.version_group

        stmt = (select(v.PokemonSpecies).options(
            selectinload(v.PokemonSpecies.pokemon).selectinload(
                v.Pokemon.pokemon_moves.and_(
                    v.PokemonMoves.version_group_id == version_group.id)).
            options(
                selectinload(v.PokemonMoves.move).options(
                    selectinload(v.Moves.move_names),
                    selectinload(
                        v.Moves.machines.and_(
                            v.Machines.version_group_id ==
                            version_group.id)).selectinload(
                                v.Machines.item).selectinload(
                                    v.Items.item_names),
                ),
                selectinload(v.PokemonMoves.pokemon_move_method).selectinload(
                    v.PokemonMoveMethods.pokemon_move_method_prose),
            )).filter_by(identifier=pokemon_id))
        # TODO: remove annotation
        pokemon_species: v.PokemonSpecies | None = session.scalar(stmt)
        if pokemon_species is None:
            await msg.reply("Pokémon not found.")
            return

        results = Learnset()

        all_forms = set(pokemon_species.pokemon)

        for pokemon in pokemon_species.pokemon:
            for pokemon_move in pokemon.pokemon_moves:
                results.add_move(pokemon, pokemon_move)

        for method_data in results.methods.values():
            for move_data in method_data.moves.values():
                if move_data.forms == all_forms:
                    move_data.forms = set()
                else:
                    method_data.form_column = True

        html = LearnsetHTML(learnset_data=results)

        if not html:
            await msg.reply("No data available.")
            return

        await msg.reply_htmlbox(html.doc)