コード例 #1
0
def clear_game_state(conn: Connectable, guild_id: int) -> ResultProxy:
    """Removes an existing game state from the database.

    :param c conn:
    :param int guild_id:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :type r: sqlalchemy.engine.result.ResultProxy
    :rtype: r
    :return:
    """

    query = sa.select([guilds.c.id]) \
        .where(guilds.c.discord_guild_id == guild_id) \
        .limit(1)
    guild_id_fkey = conn.execute(query).first()

    query = active_games.delete(None) \
        .where(active_games.c.guild_id == guild_id_fkey[0])
    return conn.execute(query)
コード例 #2
0
def get_game_state(conn: Connectable, guild_id: int) -> tuple:
    """Retrieves an existing game state.

    :param c conn:
    :param int guild_id:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :rtype: tuple
    :return:
    """

    query = sa.select([guilds.c.id]) \
        .where(guilds.c.discord_guild_id == guild_id) \
        .limit(1)
    guild_id_fkey = conn.execute(query).first()

    query = sa.select([active_games.c.game_state]) \
        .where(active_games.c.guild_id == guild_id_fkey[0]) \
        .limit(1)
    return conn.execute(query).first()
コード例 #3
0
def save_game_state(conn: Connectable, game_state) -> ResultProxy:
    """Saves a game state to the database.

    :param c conn:
    :param g game_state:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :type r: sqlalchemy.engine.result.ResultProxy
    :type g: game_state.GameState
    :rtype: r
    :return:
    """

    query = sa.select([guilds.c.id]) \
        .where(guilds.c.discord_guild_id == game_state.guild_id) \
        .limit(1)
    guild_id_fkey = conn.execute(query).first()

    query = sa.select([active_games.c.id]) \
        .where(active_games.c.guild_id == guild_id_fkey[0]) \
        .limit(1)
    existing_game_id = conn.execute(query).first()

    if existing_game_id is not None:
        LOGGER.debug("Updating existing game state.")

        query = active_games.update(None).values({
            "game_state":
            game_state.serialize(),
        }).where(active_games.c.id == existing_game_id[0])
        return conn.execute(query)
    else:
        LOGGER.debug("Creating new game state.")

        query = active_games.insert(None).values({
            "guild_id":
            guild_id_fkey[0],
            "game_state":
            game_state.serialize(),
        })
        return conn.execute(query)
コード例 #4
0
def get_guild(conn: Connectable, guild_id: int) -> tuple:
    """Retrieves config guild information.

    :param c conn:
    :param int guild_id:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :rtype: tuple
    :return:
    """

    query = sa.select([GUILDS.c.discord_guild_id, GUILDS.c.config]) \
        .where(GUILDS.c.discord_guild_id == guild_id) \
        .limit(1)
    return conn.execute(query).first()
コード例 #5
0
def save_scoreboard(conn: Connectable, guild_id: int, scores) -> ResultProxy:
    """Saves a game state to the database.

    :param c conn:
    :param int guild_id:
    :param dict scores:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :type r: sqlalchemy.engine.result.ResultProxy
    :rtype: r
    :return:
    """

    query = sa.select([guilds.c.id]) \
        .where(guilds.c.discord_guild_id == guild_id) \
        .limit(1)
    guild_id_fkey = conn.execute(query).first()

    query = sa.select([scoreboards.c.id]) \
        .where(scoreboards.c.guild_id == guild_id_fkey[0]) \
        .limit(1)
    existing_scoreboard_id = conn.execute(query).first()

    if existing_scoreboard_id is not None:
        LOGGER.debug("Updating existing scoreboard.")

        query = scoreboards.update(None).values({
            "scores": scores or {},
        }).where(scoreboards.c.id == existing_scoreboard_id[0])
        return conn.execute(query)
    else:
        LOGGER.debug("Creating new scoreboard.")

        query = scoreboards.insert(None).values({
            "guild_id": guild_id_fkey[0],
            "scores": scores or {},
        })
        return conn.execute(query)
コード例 #6
0
def get_incomplete_games(conn: Connectable) -> list:
    """Queries for games that haven't finished (usually present on restart).

    :param c conn:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :rtype: list
    :return:
    """

    query = sa.select([guilds.c.discord_guild_id, active_games.c.game_state]) \
        .select_from(
            sa.join(active_games, guilds,
                    active_games.c.guild_id == guilds.c.id)) \
        .where(active_games.c.game_state['complete'] == "false")
    return conn.execute(query).fetchall()
コード例 #7
0
def update_guild_config(conn: Connectable,
                        guild_id: int,
                        config: dict = None) -> ResultProxy:
    """Updates an existing guild's configuration.

    :param c conn:
    :param int guild_id:
    :param dict config:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :type r: sqlalchemy.engine.result.ResultProxy
    :rtype: r
    :return:
    """

    query = GUILDS.update(None).values({
        "config": config or {},
    }).where(GUILDS.c.discord_guild_id == guild_id)
    return conn.execute(query)
コード例 #8
0
def create_guild(conn: Connectable,
                 guild_id: int,
                 config: dict = None) -> ResultProxy:
    """Ensures a specified Discord guild is in the database.

    :param c conn:
    :param int guild_id:
    :param dict config:
    :type c: sqlalchemy.engine.interfaces.Connectable
    :type r: sqlalchemy.engine.result.ResultProxy
    :rtype: r
    :return:
    """

    query = GUILDS.insert(None).values({
        "discord_guild_id": guild_id,
        "config": config or {},
    })
    return conn.execute(query)