def collect_full_player(t_id, enemy_id):
    cursor.execute(f"SELECT * FROM ships WHERE t_id = {t_id}")
    tuple_with_basic_info_for_ships = cursor.fetchall()

    cursor.execute(f"SELECT * FROM field WHERE t_id = {t_id}")
    tuple_with_basic_info_for_field = cursor.fetchall()

    ships = create_ships(t_id, tuple_with_basic_info_for_ships)
    field = create_field(t_id, tuple_with_basic_info_for_field, ships=ships)

    player = create_player(t_id, enemy_id, field, ships)

    return player
def collect_current_game_from_in_queue(t_id=False):
    if not t_id:
        cursor.execute("SELECT * FROM in_queue")
        players_ids = cursor.fetchall()
        t_id_1 = players_ids[0][0]
        t_id_2 = players_ids[1][0]
        players_ids = [t_id_1, t_id_2]
    else:
        cursor.execute(
            f"SELECT * FROM sea_battle_game WHERE t_id_1 = ? OR t_id_2 = ?",
            (t_id, t_id))
        players_ids = cursor.fetchone()
        t_id_1 = players_ids[0]
        t_id_2 = players_ids[1]
        players_ids = [t_id_1, t_id_2]

    players_objects = []

    player = collect_full_player(t_id_1, t_id_2)
    players_objects.append(player)

    player = collect_full_player(t_id_2, t_id_1)
    players_objects.append(player)

    player_1 = players_objects[0]
    player_2 = players_objects[1]
    t_id_1 = player_1.t_id
    t_id_2 = player_2.t_id

    cursor.execute(
        "SELECT turn FROM sea_battle_game WHERE t_id_1 = ? OR t_id_2 = ?",
        (t_id_1, t_id_1))
    turn_in_tuple = cursor.fetchall()  # [(turn)]

    if not turn_in_tuple:
        turn = 1
        cursor.execute("INSERT INTO sea_battle_game VALUES(?, ?, ?)",
                       (t_id_1, t_id_2, turn))
    else:
        turn = turn_in_tuple[0][0]

    current_game = some_classes.SeaBattleGame(player_1=player_1,
                                              player_2=player_2,
                                              t_id_1=t_id_1,
                                              t_id_2=t_id_2,
                                              turn=turn)

    cursor.execute("DELETE FROM in_queue")  # clear table
    conn.commit()

    return current_game
示例#3
0
async def call_back_for_random(callback: CallbackQuery):
    # check for free ships
    t_id = callback.from_user.id

    cursor.execute(f"SELECT * FROM ships WHERE t_id = {t_id}")
    tuple_with_basic_info_for_ships = cursor.fetchall()

    ships = functions_for_create_objects.create_ships(
        t_id, tuple_with_basic_info_for_ships)

    # create ships if all already placed
    for ship in ships:
        if not ship.already_place:
            break
        if ship.ship_id == '1.3':
            functions_for_create_objects.create_ships_for_random(t_id)

    game = functions_for_create_objects.collect_current_game_from_in_queue(
        t_id)

    player = game.player_1 if game.player_1.t_id == t_id else game.player_2

    logic.random_place(player)

    await for_keyboard.edit_message_after_random(player)
示例#4
0
async def call_back_for_phase_2(callback: CallbackQuery):
    t_id = callback.from_user.id
    game = functions_for_create_objects.collect_current_game_from_in_queue(
        callback.from_user.id)
    player = game.player_1 if game.player_1.t_id == t_id else game.player_2

    if callback.data == 'ready':
        functions_for_work_with_bd.change_phase(t_id=callback.from_user.id,
                                                phase=phases.phase_3)
        await for_keyboard.edit_message_after_ready(player)
        cursor.execute(
            "SELECT player_phase FROM phase WHERE t_id = ? OR t_id = ?",
            (game.player_1.t_id, game.player_2.t_id))
        phases_from_db = cursor.fetchall()  # [(),()]
        for phase in phases_from_db:
            if phase[0] != phases.phase_3:
                return

        functions_for_work_with_bd.change_phase(game.player_1.t_id,
                                                phases.phase_4)
        functions_for_work_with_bd.change_phase(game.player_2.t_id,
                                                phases.phase_4)

        await call_back_for_show_phase_4(callback, game)

    elif callback.data == 'random':
        await call_back_for_random(callback)

    else:
        await call_back_for_hit(callback, game)
def create_field(t_id,
                 tuples_with_basic_info: list = False,
                 ships: list = False):
    if not tuples_with_basic_info:
        field = some_classes.Field(t_id, height, length)
        tuple_with_args = (t_id, height, length,
                           json.dumps(field.visual_field))

        cursor.execute("INSERT INTO field VALUES(?, ?, ?, ?)", tuple_with_args)
        conn.commit()

        return field

    else:
        keys = ('t_id', 'height', 'length', 'visual_field')
        dict_with_args = get_dict_with_args(keys, tuples_with_basic_info[0])
        dict_with_args['field'] = json.loads(dict_with_args['visual_field'])
        dict_with_args['visual_field'] = json.loads(
            dict_with_args['visual_field'])

        cursor.execute(f"SELECT * FROM ships WHERE t_id = {t_id}")
        tuples_with_basic_info_for_ships = cursor.fetchall()
        #ships = create_ships(t_id, tuples_with_basic_info=tuples_with_basic_info_for_ships)

        for list_with_cells in dict_with_args[
                'field']:  # visual_field = [[1, 2, 3], [3, 2, 1]] 12 X 8
            for i in range(length):
                if list_with_cells[i] in ships_ids:
                    for ship in ships:
                        if ship.ship_id == list_with_cells[i]:
                            list_with_cells[i] = ship
        field = some_classes.Field(**dict_with_args)
        return field
def insert_player_to_queue(message: Message):
    """
    Insert player to the queue if he's only one write start, else insert player to the table
    sea_battle and start game
    return: False if only one player, and [t_id_1, t_id_2] if 2 players in the queue
    """
    t_id = message.from_user.id
    nickname = message.from_user.username
    command = "SELECT * from in_queue"
    cursor.execute(command)
    queue = cursor.fetchall()  # queue right now

    if len(queue) == 1 and t_id in queue[0]:  # if already one in queue
        raise errors.already_in_queue

    # if 2 players in the queue
    elif len(queue) == 1:
        cursor.execute("INSERT INTO in_queue VALUES(?, ?)", (t_id, nickname))

        create_phase(t_id)  # create value in table phase
        conn.commit()

        players = get_players(table='in_queue')
        for player in players:
            change_phase(player, phases.phase_2)

        cursor.execute("SELECT * FROM in_queue")  # get players in the queue
        result = cursor.fetchall()  # [(666, 555)]

        return result

    else:
        create_phase(t_id)  # create value in table phase

        cursor.execute("INSERT INTO in_queue VALUES(?, ?)", (t_id, nickname))
        conn.commit()
        return False
def create_player(t_id, enemy_id, field, ships):

    # if player not in bd
    if not (check_player_in_bd(t_id)):
        cursor.execute(f"SELECT nickname FROM in_queue WHERE t_id = {t_id}")
        nickname = cursor.fetchall()[0][0]

        print(nickname)

        tuple_with_args = (t_id, nickname)
        cursor.execute("INSERT INTO player VALUES(?, ?)",
                       tuple_with_args)  #WARNING!
        conn.commit()

    player = some_classes.Player(t_id=t_id,
                                 enemy_id=enemy_id,
                                 field=field,
                                 ships=ships)
    return player
def check_player_in_bd(t_id):
    cursor.execute(f"SELECT * FROM player WHERE t_id = {t_id}")
    return bool(cursor.fetchall())
def get_players(table):
    cursor.execute(f"SELECT t_id FROM {table}")
    players = cursor.fetchall()
    players = [tuple_[0] for tuple_ in players]
    return players