def cmd_game(message): user_id = message.from_user.id db = Db(db_dir) user = db.get_user_by_id(user_id) if user[1]: markup = types.ReplyKeyboardMarkup() markup.row('1', '2', '3') markup.row('4', '5', '6') markup.row('7', '8', '9') bot.send_message(message.from_user.id, "Ваш ход:", reply_markup=markup)
def repeat_all_messages(message): user_id = message.from_user.id db = Db(db_dir) user = db.get_user_by_id(user_id) if user: if user[1]: if message.text in ['1', '2', '3', '4', '5', '6', '7', '8', '9']: pos = int(message.text) user_game = db.get_game(user_id) if '0' == user_game[2][pos]: field = db.set_step(user_id, pos) winner = who_win(field) if winner: markup = types.ReplyKeyboardHide() field = draw_field(field) bot.send_message(user_id, field + "\n" + winner, reply_markup=markup, parse_mode='HTML') db.set_type_to_user(user_id, None) if 'Ничья' == winner: db.user_draw(user_id) else: db.user_win(user_id) db.end_game(user_id) return True field = bot_step(user_id, field) winner = who_win(field) if winner: markup = types.ReplyKeyboardHide() field = draw_field(field) bot.send_message(user_id, field + "\n" + winner, reply_markup=markup, parse_mode='HTML') db.set_type_to_user(user_id, None) if 'Ничья' == winner: db.user_draw(user_id) else: db.user_lose(user_id) db.end_game(user_id) return True field = draw_field(field) bot.send_message(user_id, field, parse_mode='HTML') else: bot.send_message(user_id, 'Так сходить нельзя') else: markup = types.ReplyKeyboardHide() if message.text in ['X', 'O']: db.set_type_to_user(user_id, message.text) bot.send_message(user_id, 'Вы выбрали ' + str(message.text) + '!', reply_markup=markup) db.create_game(user_id) cmd_game(message)
def register(message): db = Db(db_dir) user = db.get_user_by_id(message.from_user.id) if not user: if message.from_user.username: db.add_user(message.from_user.id, message.from_user.username) elif message.from_user.first_name: db.add_user(message.from_user.id, message.from_user.first_name) else: bot.send_message(chat_id, "У Вас нету имени, бот не может Вас зарегистрировать!") bot.send_message(chat_id, "Поздравляем с регистрацией!") else: print(user)
def bot_step(user_id, field): db = Db(db_dir) user = str(db.get_user_by_id(user_id)[1]) type_ = other_type(user) right_step = check_can_win_lose(field, type_) if right_step: return db.set_step(user_id, right_step, True) # if '0' == field[5]: # return db.set_step(user_id, 5, True) while True: pos = random.randint(1, 9) if '0' == field[pos]: return db.set_step(user_id, pos, True)
def end_game(message): user_id = message.from_user.id db = Db(db_dir) user = db.get_user_by_id(user_id) if not user: return False if user[5]: markup = types.ReplyKeyboardHide() db.set_type_to_user(user_id, None) db.end_game(user_id) bot.send_message(message.from_user.id, "Игра окончена!", reply_markup=markup)
def register(message): db = Db(db_dir) user = db.get_user_by_id(message.from_user.id) if not user: if message.from_user.username: db.add_user(message.from_user.id, message.from_user.username) elif message.from_user.first_name: db.add_user(message.from_user.id, message.from_user.first_name) else: bot.send_message( chat_id, "У Вас нету имени, бот не может Вас зарегистрировать!") bot.send_message(chat_id, "Поздравляем с регистрацией!") else: print(user)
def repeat_all_messages(message): user_id = message.from_user.id db = Db(db_dir) user = db.get_user_by_id(user_id) if not user: bot.send_message(user_id, "Вам нужно зарегистрироваться командой: /register") else: game_ = db.get_game(user_id) if not game_: markup = types.ReplyKeyboardMarkup() markup.row('X', 'O') bot.send_message(user_id, "Выбирите Х или О:", reply_markup=markup) else: cmd_game(message)