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 create_ships(t_id, tuples_with_basic_info: list = False ): # It's difference because booked_place is list """ this function create list objects(ship), it can download it from bd or create new, return list with objects """ ships = [] if not tuples_with_basic_info: # If create ships for ship_id in ships_ids: size = int(float(ship_id)) # first number it's size ship = some_classes.Ship(size=size, t_id=t_id, ship_id=ship_id) # for save in db tuple_with_args = (size, t_id, ship_id, json.dumps(ship.booked_places), ship.hp, ship.x, ship.y, ship.already_place, ship.orientation) cursor.execute("INSERT INTO ships VALUES(?,?,?,?,?,?,?,?,?)", tuple_with_args) conn.commit() ships.append(ship) else: # download it from bd and create objects keys = ('size', 't_id', 'ship_id', 'booked_places', 'hp', 'x', 'y', 'already_place', 'orientation') for info in tuples_with_basic_info: dict_with_args = get_dict_with_args(keys, info) dict_with_args['booked_places'] = json.loads( dict_with_args['booked_places']) ship = some_classes.Ship(**dict_with_args) ships.append(ship) return ships
def create_phase(t_id): players = get_players('phase') if t_id in players: return else: cursor.execute("INSERT INTO phase VALUES(?, ?)", (t_id, phases.phase_1)) conn.commit()
def create_field(): t_id: int height: int length: int visual_field: list cursor.execute("""CREATE TABLE field (t_id, height, length, visual_field)""") conn.commit()
def clear_table_after_game(t_id): cursor.execute(f"DELETE FROM field WHERE t_id = {t_id}") cursor.execute(f"DELETE FROM messages WHERE t_id = {t_id}") cursor.execute(f"DELETE FROM phase WHERE t_id = {t_id}") cursor.execute( "DELETE FROM sea_battle_game WHERE t_id_1 = ? OR t_id_2 = ?", (t_id, t_id)) cursor.execute(f"DELETE FROM ships WHERE t_id = {t_id}") conn.commit()
async def phase_place_ships(player: some_classes.Player): keyboard = keyboard_generator.get_keyboard_phase_2(player) msg_with_keyboard = await bot.send_message( chat_id=player.t_id, text='Ваш противник кто-то кто-то', reply_markup=keyboard) cursor.execute("INSERT INTO messages VALUES(?, ?)", (msg_with_keyboard.message_id, player.t_id)) conn.commit() '''await bot.edit_message_reply_markup(chat_id=message.from_user.id,
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
def create_ships(): size: int t_id: int ship_id: str booked_places: str # all book coords. First list for the 'z', next for the objects like this: hp: int x: int y: int # [[(1, 2), (3, 4)], [(0, 0)]] cords in example is random, bus is show str already_place: int orientation: str cursor.execute("""CREATE TABLE ships (size, t_id, ship_id, booked_places, hp, x, y, already_place, orientation)""") conn.commit()
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 create_ships_for_random(t_id): # clear ships and field cursor.execute(f"DELETE FROM ships WHERE t_id = {t_id}") cursor.execute(f"DELETE FROM field WHERE t_id = {t_id}") conn.commit() for ship_id in ships_ids: size = int(float(ship_id)) # first number it's size ship = some_classes.Ship(size=size, t_id=t_id, ship_id=ship_id) # for save in db tuple_with_args = (size, t_id, ship_id, json.dumps(ship.booked_places), ship.hp, ship.x, ship.y, ship.already_place, ship.orientation) cursor.execute("INSERT INTO ships VALUES(?,?,?,?,?,?,?,?,?)", tuple_with_args) conn.commit() # ships.append(ship) create_field(t_id)
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_phase(): t_id: int player_phase: str cursor.execute("""CREATE TABLE phase (t_id, player_phase)""") conn.commit()
def create_player(): t_id: int nickname: str cursor.execute("""CREATE TABLE player (t_id, nickname)""") conn.commit()
def change_phase(t_id, phase): cursor.execute("UPDATE phase SET player_phase = ? WHERE t_id = ?", (phase, t_id)) conn.commit() pass