def abort(msg): try: if msg.from_user.id == players_id[msg.chat.id].creator: for id in players_id[msg.chat.id].del_msg: bot.delete_message(chat_id=msg.chat.id, message_id=id) players_id[msg.chat.id].del_msg = [] for player in players_id[msg.chat.id].players.keys(): chat_of_player.pop(player) players_id.pop(msg.chat.id) bot.reply_to(msg, 'Game aborted') else: bot.reply_to(msg, 'You`re not creator of this game!') except KeyError: bot.reply_to(msg, 'No game to be aborted')
def callback_inline(call): chat_id = int(call.message.chat.id) try: if call.message: if call.data[0] == 'a' and call.data[1] == '@': nickname = call.data.split()[0][1:len(call.data)] in_chat = int(call.data.split()[1]) # players_nick_to_id = players_id[chat_id].players_nick_to_id role = players_id[in_chat].players[ players_id[in_chat].players_nick_to_id[nickname]] # bot_send_message(chat_id, nickname + " was " + role) gameplay.endgame( in_chat, players_id[in_chat], players_id[in_chat].players_nick_to_id[nickname]) if role == 'Merlin': bot_send_message(in_chat, "Mordred wins") else: bot_send_message(in_chat, 'Avalon wins') for key in players_id[in_chat].players.keys(): chat_of_player.pop(key) players_id.pop(in_chat) elif call.data == "register": try: players_id[int(call.message.chat.id)] except KeyError: bot.reply_to( call.message, 'No registration started!\nRun /start_registration') return if players_id[call.message.chat.id].state == 'reg': if not call.from_user.username: bot_send_message( call.from_user.id, "Your telegram account has no username") return if int(call.from_user.id) not in players_id[int( call.message.chat.id)].players: if int(call.from_user.id) in chat_of_player.keys(): bot_send_message(int(call.from_user.id), 'You are in not ended game!') return try: bot.send_message( call.from_user.id, 'You`re registered for the Avalon game in ' + str(call.message.chat.title)) except telebot.apihelper.ApiException: bot_send_message( chat_id, 'To be able to register in game, ' 'say /start to @Avalon117bot in private messages ' 'and push registration button again') return chat_of_player[int(call.from_user.id)] = int( call.message.chat.id) players_id[int(call.message.chat.id)].players[int( call.from_user.id)] = None players_id[int(call.message.chat.id)].players_nick_to_id['@' + call.from_user.username] = \ int(call.from_user.id) keyboard = telebot.types.InlineKeyboardMarkup() add_button = telebot.types.InlineKeyboardButton( text="Register", callback_data="register") keyboard.add(add_button) text = call.message.text + ' @' + call.from_user.username bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=text, reply_markup=keyboard) players_id[call.message.chat.id].reg_btn = call.message # bot_send_message(call.from_user.id, 'You`re registered for the Avalon game in ' + # call.message.chat.title) # bot_send_message(chat_id, len(players_id[int(chat_id)])) else: bot.reply_to(call.message, 'Game is on!') elif call.data == "Lady of the Lake": if players_id[chat_id].creator != int(call.from_user.id): return out = players_id[chat_id].change_lady() keyboard = vote.add_roles_btn(players_id[chat_id]) bot.edit_message_reply_markup( chat_id=chat_id, message_id=players_id[chat_id].additional_id, reply_markup=keyboard) elif call.data == "Morgana" or call.data == "Oberon" or call.data == "Mordred": if players_id[chat_id].creator != int(call.from_user.id): return out = players_id[chat_id].change_roles(call.data) keyboard = vote.add_roles_btn(players_id[chat_id]) bot.edit_message_reply_markup( chat_id=chat_id, message_id=players_id[chat_id].additional_id, reply_markup=keyboard) elif call.data[0] == 'v': if call.from_user.id != players_id[chat_id].order[ players_id[chat_id].cur_king]: return arr = call.data.split() data = arr[1] if int(data) in players_id[chat_id].cur_exp: for i in range(0, len(players_id[chat_id].cur_exp)): if int(data) == players_id[chat_id].cur_exp[i]: players_id[chat_id].cur_exp.pop(i) break keyboard = vote.vote_keyboard(chat_id, players_id[chat_id]) bot.edit_message_reply_markup( chat_id=chat_id, message_id=players_id[chat_id].vote_msg_id, reply_markup=keyboard) else: players_id[chat_id].cur_exp.append(int(data)) keyboard = vote.vote_keyboard(chat_id, players_id[chat_id]) bot.edit_message_reply_markup( chat_id=chat_id, message_id=players_id[chat_id].vote_msg_id, reply_markup=keyboard) elif call.data == 'send_expedition': if call.from_user.id != players_id[chat_id].order[ players_id[chat_id].cur_king]: return if len(players_id[chat_id].cur_exp ) != players_id[chat_id].exp_size[ players_id[chat_id].get_num_of_exp()]: bot_send_message(chat_id, 'Wrong number of expeditors') return string = '' for i in players_id[chat_id].cur_exp: string += '\n@' + str( bot.get_chat_member(chat_id, i).user.username) bot_send_message(chat_id, "The expedition is:" + string) for i in players_id[chat_id].order: bot_send_message(i, "The expedition is:" + string) players_id[chat_id].state = 'vote' gameplay.vote_for_exp(players_id[chat_id].order, chat_id) for id in players_id[chat_id].del_msg: bot.delete_message(chat_id=chat_id, message_id=id) players_id[chat_id].del_msg = [] else: data = call.data.split() chat = int(data[1]) user = int(data[0]) nickname = str(bot.get_chat_member(chat, user).user.username) lady_id = players_id[chat].order[players_id[chat].cur_lady] bot_send_message(chat, "Lady of the Lake has checked @" + nickname) players_id[chat].pass_lady(user) players_id[chat].past_lady.append(user) check = '' if players_id[chat].players[user] in players_id[chat].peaceful: check = " is servant of Arthur" else: check = " is servant of Mordred" bot_send_message(lady_id, '@' + nickname + check) bot_send_message(chat, '@' + nickname + " is new Lady of the Lake") try: players_id[chat].cur_exp = [] for player in players_id[chat].players.keys(): players_id[chat].cur_voting_for_exp[player] = None vote.send_voting(chat, players_id[chat]) except KeyError: print(KeyError) except telebot.apihelper.ApiException: print("stop DDOSing buttons!") except KeyError: bot_send_message( chat_id, "Don`t touch old buttons, run /start_registration first!")
def end_reg(msg): try: players_id[msg.chat.id] except KeyError: bot.reply_to(msg, 'No registration started!\nRun /start_registration') return try: if players_id[msg.chat.id].state == 'game': bot.reply_to(msg, 'Game is on!') else: if msg.from_user.id == players_id[msg.chat.id].creator: players_id[msg.chat.id].exp_size = list.copy( tools.GameInfo.expedition_size[len( players_id[msg.chat.id].players)]) for id in players_id[msg.chat.id].del_msg: bot.delete_message(chat_id=msg.chat.id, message_id=id) players_id[msg.chat.id].del_msg = [] # players_id[msg.chat.id].players = role.make_roles(players_id[msg.chat.id].players) players_id[msg.chat.id].cur_voting_for_exp = dict.copy( players_id[msg.chat.id].players) bot_send_message( msg.from_user.id, " You have launched the game in" + str(msg.chat.id)) roles.make_roles(players_id[msg.chat.id].players, players_id[msg.chat.id].additional_roles) players_id[msg.chat.id].order = list( players_id[msg.chat.id].players.keys()) random.shuffle(players_id[msg.chat.id].order) string = '' players_id[msg.chat.id].cur_king = 0 players_id[msg.chat.id].cur_lady = -1 for i in range(0, len(players_id[msg.chat.id].order)): string += '\n' + str(i + 1) + '. @' \ + str(bot.get_chat_member(msg.chat.id, players_id[msg.chat.id].order[i]).user.username) if i == players_id[msg.chat.id].cur_king % len( players_id[msg.chat.id].order): string += '👑' if players_id[msg.chat.id].lady_lake and i == players_id[ msg.chat.id].cur_lady % len( players_id[msg.chat.id].order): string += '👸' bot_send_message(msg.chat.id, 'Players order:' + string) players_id[msg.chat.id].checked.append( players_id[msg.chat.id].order[-1]) king_id = players_id[msg.chat.id].order[players_id[ msg.chat.id].cur_king] bot_send_message( msg.chat.id, "King is @" + str( bot.get_chat_member(msg.chat.id, king_id).user.username)) if players_id[msg.chat.id].lady_lake: lady_id = players_id[msg.chat.id].order[players_id[ msg.chat.id].cur_lady] bot_send_message( msg.chat.id, "Lady of the Lake is @" + str( bot.get_chat_member(msg.chat.id, lady_id).user.username)) players_id[msg.chat.id].past_lady.append(lady_id) vote.send_voting(msg.chat.id, players_id[msg.chat.id]) players_id[msg.chat.id].state = 'game' else: bot.reply_to(msg, 'You`re not creator of this game!') print(players_id[msg.chat.id].players) except KeyError: bot.reply_to(msg, 'too few players to start') except: return
def leave(msg): if msg.from_user.id not in chat_of_player: bot_send_message(msg.from_user.id, "You don`t take part in any game") return if players_id[chat_of_player[msg.from_user.id]].state != 'reg': if msg.from_user.id not in chat_of_player.keys(): return bot.reply_to(msg, 'You have left the game') currChat = chat_of_player[msg.from_user.id] if players_id[currChat].players[msg.from_user.id] == "Merlin": bot_send_message(currChat, 'Merlin has left the game\n\nRIP Avalon!!') for id in players_id[currChat].del_msg: bot.delete_message(chat_id=currChat, message_id=id) string = "" for item in players_id[currChat].players.items(): string += '\n@' + bot.get_chat_member(currChat, item[0]).user.username + ' was ' + \ ('❤️' if item[1] in tools.GameInfo.peaceful else '🖤') + item[1] bot_send_message(currChat, 'Roles in this game:' + string) for id in players_id[currChat].players: chat_of_player.pop(id) players_id.pop(currChat) return if players_id[currChat].players[msg.from_user.id] == "Assassin": for item in players_id[currChat].players.keys(): if players_id[currChat].players[item] not in players_id[ currChat].peaceful: bot.send_message(item, "You are the new Assassin") players_id[currChat].players[item] = 'Assassin' break print(msg.from_user) #print(chat_of_player[currChat].order) #print(chat_of_player[currChat].cur_king) #print(msg.from_user.username) if players_id[currChat].order[ players_id[currChat].cur_king] == msg.from_user.username: players_id[currChat].cur_king = (players_id[currChat].cur_king + 1) % \ (len(players_id[currChat].order) - 1) if players_id[currChat].lady_lake and players_id[currChat].order[ players_id[currChat].cur_lady] == msg.from_user.username: players_id[currChat].cur_lady = (players_id[currChat].cur_lady + 1) % \ (len(players_id[currChat].order) - 1) if msg.from_user.id in players_id.keys(): players_id.pop(msg.from_user.id) print(players_id[currChat].order) for i in range(len(players_id[currChat].order)): if players_id[currChat].order[i] == msg.from_user.id: players_id[currChat].order.pop(i) break #players_id[currChat].order.pop(msg.from_user.id) chat_of_player.pop(msg.from_user.id) players_id[currChat].players.pop(msg.from_user.id) if len(players_id[currChat].players) == 0: bot.send_message(currChat, "All players have left the game") for id in players_id[currChat].del_msg: bot.delete_message(chat_id=currChat, message_id=id) players_id[currChat].del_msg = [] for player in players_id[currChat].players.keys(): chat_of_player.pop(player) players_id.pop(currChat) bot.send_message(currChat, 'Game aborted') return if msg.from_user.id in chat_of_player.keys(): chat_of_player.pop(msg.from_user.id) for chat in players_id: if msg.from_user.id in players_id[chat].players: del players_id[chat].players[msg.from_user.id] keyboard = telebot.types.InlineKeyboardMarkup() add_button = telebot.types.InlineKeyboardButton( text="Register", callback_data="register") keyboard.add(add_button) reg_msg = players_id[chat].reg_btn reg = 'Registration is on\nPlayers in game:' for user in players_id[chat].players: reg = reg + ' @' + str( bot.get_chat_member(chat, user).user.username) print(reg) try: bot.edit_message_text(chat_id=chat, message_id=reg_msg.message_id, text=reg, reply_markup=keyboard) except: return bot_send_message(msg.from_user.id, "You have left the game")
def end_reg(msg): try: players_id[msg.chat.id] except KeyError: try: bot.reply_to( msg, languages[msg.chat.id]['No registration started!'] + '\n' + languages[msg.chat.id]['Run /start_registration']) return except KeyError: bot.reply_to( msg, 'No registration started!' + '\n' + 'Run /start_registration') return try: if players_id[msg.chat.id].state == 'game': bot.reply_to(msg, languages[msg.chat.id]['Game is on!']) else: if msg.from_user.id == players_id[msg.chat.id].creator: players_id[msg.chat.id].exp_size = list.copy( tools.GameInfo.expedition_size[len( players_id[msg.chat.id].players)]) for id in players_id[msg.chat.id].del_msg: bot.delete_message(chat_id=msg.chat.id, message_id=id) players_id[msg.chat.id].del_msg = [] # players_id[msg.chat.id].players = role.make_roles(players_id[msg.chat.id].players) players_id[msg.chat.id].cur_voting_for_exp = dict.copy( players_id[msg.chat.id].players) bot_send_message( msg.from_user.id, languages[msg.chat.id]["You have launched the game in"] + str(msg.chat.id)) roles.make_roles(players_id[msg.chat.id].players, players_id[msg.chat.id].additional_roles, msg.chat.id) players_id[msg.chat.id].order = list( players_id[msg.chat.id].players.keys()) random.shuffle(players_id[msg.chat.id].order) string = '' players_id[msg.chat.id].cur_king = 0 players_id[msg.chat.id].cur_lady = -1 for i in range(0, len(players_id[msg.chat.id].order)): string += '\n' + str(i + 1) + '. @' \ + str(bot.get_chat_member(msg.chat.id, players_id[msg.chat.id].order[i]).user.username) if i == players_id[msg.chat.id].cur_king % len( players_id[msg.chat.id].order): string += '👑' if players_id[msg.chat.id].lady_lake and i == players_id[ msg.chat.id].cur_lady % len( players_id[msg.chat.id].order): string += '👸' bot_send_message( msg.chat.id, languages[msg.chat.id]['Players order:'] + string) players_id[msg.chat.id].checked.append( players_id[msg.chat.id].order[-1]) king_id = players_id[msg.chat.id].order[players_id[ msg.chat.id].cur_king] bot_send_message( msg.chat.id, languages[msg.chat.id]["King is"] + " @" + str( bot.get_chat_member(msg.chat.id, king_id).user.username)) chat_id = msg.chat.id bot_send_message( chat_id, languages[msg.chat.id]['This expedition is for '] + str(players_id[chat_id].exp_size[ players_id[chat_id].get_num_of_exp()]) + languages[msg.chat.id][' people']) if players_id[msg.chat.id].lady_lake: lady_id = players_id[msg.chat.id].order[players_id[ msg.chat.id].cur_lady] bot_send_message( msg.chat.id, languages[msg.chat.id]["Lady of the Lake is"] + " @" + str( bot.get_chat_member(msg.chat.id, lady_id).user.username)) players_id[msg.chat.id].past_lady.append(lady_id) vote.send_voting(msg.chat.id, players_id[msg.chat.id]) players_id[msg.chat.id].state = 'game' else: bot.reply_to( msg, languages[msg.chat.id]['You`re not creator of this game!']) print(players_id[msg.chat.id].players) except KeyError: bot.reply_to( msg, languages[msg.chat.id] ['Wrong number of players to start, this game is for 5 - 10 players'] ) except: print("govniashki")