def gameover(bot, cplayer, nplayer): """ Function that executes during gameover. Args: bot: from telegram bot cplayer: current player(player that won) nplayer: next player(player that lost) """ #display quit button after game cplayer_reply_markup = mc.show_user_options(cplayer.username, nplayer.username, 1, ["Quit"], ["quit"]) nplayer_reply_markup = mc.show_user_options(nplayer.username, cplayer.username, 1, ["Quit"], ["quit"]) bot.sendDocument( chat_id=cplayer.userid, document= "https://media3.giphy.com/media/WKBAB3YpXPVDO/giphy.gif?cid=790b76115ca4d7867156474432c0afef", caption="You lost! The <b>" + nplayer.civilization + "</b> are victorious!", reply_markup=cplayer_reply_markup, parse_mode=ParseMode.HTML) bot.sendDocument( chat_id=nplayer.userid, document= "https://media1.giphy.com/media/Yb9lErqzrRr3O/giphy.gif?cid=790b76115ca4de1069482e65775e3b6e", caption="You are victorious!", reply_markup=nplayer_reply_markup) return None
def reshow_main_options(update, context): """ Function that takes in the back response from user to show the main prompt again. Args: update: default telegram arg context: default telegram arg """ #answer query and load users context.bot.answer_callback_query(update.callback_query.id) data = update.callback_query.data usernames = re.match(r'-reshow_main-(\S+)-(\S+)', data) username1, username2 = usernames.group(1), usernames.group(2) user1, user2 = um.load_user_data([username1, username2]) #prevent user from executing if status is not 2 if user1["user_status"] != "2": return None reply_markup = mc.show_user_options(user1["username"], user2["username"], 4, ["Attack", "Repair", "Hire", "Flee"], ["1", "2", "3", "flee"]) context.bot.editMessageText( chat_id=user1["userid"], message_id=update.callback_query.message.message_id, text="What will you do?", reply_markup=reply_markup) return None
def display(bot, username1, username2, display_status_only=False): """ Function that displays and stats and prompt users every turn. Args: bot: from telegram bot username1: usernane of current player username2: username of next player display_status_only: if true, will only show status(used at end of turn to reflect user actions) """ cplayer, nplayer = load_game_stats(username1, username2) if display_status_only is False: cplayer.gold['amount'] += 100 html_display = """Status\n <b>---------------- Castle ----------------</b>\n {} Castle Health: {}\n {} Castle Health: {}\n <b>---------------- Income ---------------</b>\n Gold {} | +100 Every Turn\n <b>---------------- Units ------------------</b>\n Soldier(s): {}\n Warriors(s): {}\n Knight(s): {}\n <b>------------------------------------------</b>\n """.format(cplayer.civilization, cplayer.castle['current_castle_health'], nplayer.civilization, nplayer.castle['current_castle_health'], cplayer.gold['amount'], cplayer.soldier['num'], cplayer.warrior['num'], cplayer.knight['num']) bot.send_message(chat_id=cplayer.userid, text=html_display, parse_mode=ParseMode.HTML) if display_status_only is True: return None #if any side's health is 0, game is over if cplayer.castle['current_castle_health'] <= 0: gameover(bot, cplayer, nplayer) return None if nplayer.castle['current_castle_health'] <= 0: gameover(bot, nplayer, cplayer) return None reply_markup = mc.show_user_options(cplayer.username, nplayer.username, 4, ["Attack", "Repair", "Hire", "Flee"], ["1", "2", "3", "flee"]) user_choose = bot.send_message(chat_id=cplayer.userid, text="What will you do?", reply_markup=reply_markup) #start a new thread to enforce 30 second timeout for user choice threading.Thread(target=um.choice_timeout_user, args=(bot, cplayer.username, nplayer.username, user_choose.message_id, 0.5)).start() bot.send_message(chat_id=nplayer.userid, text="<b>{}</b> are thinking...".format( cplayer.civilization), parse_mode=ParseMode.HTML) save_game_stats(cplayer, nplayer) return None
def initiate_fight_user(update, context): """ Function that executes from the /fight command. Initiates a battle request against another user. Args: update: default telegram arg context: default telegram arg """ #if invalid user, the validate_user function will prompt to register with /war command if um.validate_user(update, context, update.message.chat_id, update.message.from_user.username) is False: return None else: #if valid user, load in user data user1 = um.load_user_data([update.message.from_user.username]) #prompt usage if no context.args provided, else check user current status to make sure eligibility for making request if context.args == []: context.bot.send_message(chat_id=user1["userid"], text="Usage: <b>/fight <username></b>", parse_mode=ParseMode.HTML) return None elif user1["user_status"] == "1": context.bot.send_message(chat_id=user1["userid"], text="You already have a pending request!") return None elif user1["user_status"] == "2" or user1["user_status"] == "3": context.bot.send_message(chat_id=user1["userid"], text="You are in the midst of a war!") return None else: username2 = context.args[0] #check if specified user exist(registered user) if um.check_exist_user("./userinfo/" + username2 + ".json") is False: context.bot.send_message(chat_id=user1["userid"], text="User cannot be found!") return None else: user2 = um.load_user_data([username2]) #refuse the request if specified user has blocked requesting user if user1["userid"] in user2["block_list"]: context.bot.send_message(chat_id=user1["userid"], text="<b>" + user2["username"] + "</b> is unavailable for war!", parse_mode=ParseMode.HTML) elif user2["user_status"] == "1": context.bot.send_message(chat_id=user1["userid"], text="<b>" + user2["username"] + "</b> has a pending request!", parse_mode=ParseMode.HTML) elif user2["user_status"] == "2" or user1["user_status"] == "3": context.bot.send_message(chat_id=user1["userid"], text="<b>" + user2["username"] + "</b> is in the midst of a war!", parse_mode=ParseMode.HTML) else: #switch user statuses to pending and send out battle declaration user1["user_status"], user2["user_status"] = "1", "1" um.save_user_data([user1, user2]) reply_markup = mc.show_user_options(user1["username"], user2["username"], 2, ["Yes", "No"], ["accept", "decline"]) context.bot.send_message(chat_id=user1["userid"], text="Enemy Detected...") context.bot.send_message(chat_id=user1["userid"], text="Declaring Battle...") declare_war = context.bot.send_message(chat_id=user2["userid"], text="<b>" + user1["username"] + "</b> has declared battle! Are you ready?", reply_markup=reply_markup, parse_mode=ParseMode.HTML) #start a thread to enforce a 30 second timeout rule threading.Thread(target=um.connect_users, args=(context.bot, user1, user2, declare_war.message_id, 0.5)).start() return None
def repair(update, context): """ Function that takes in the repair response from user. Args: update: default telegram arg context: default telegram arg """ #answer query and load users context.bot.answer_callback_query(update.callback_query.id) data = update.callback_query.data usernames = re.match(r'-2\S*-(\S+)-(\S+)', data) username1, username2 = usernames.group(1), usernames.group(2) user1, user2 = um.load_user_data([username1, username2]) #prevent user from executing if status is not 2 if user1["user_status"] != "2": return None #variable to control actions approved_action = [] #list of possible actions to take depending on button pressed (callback_query.data) if "-2-" in data and gm.check_gold(context.bot, user1["username"], int(config['castle']['repair_1'])): reply_markup = mc.show_user_options( user1["username"], user2["username"], 5, ["100 Gold", "200 Gold", "300 Gold", "Flee", "Back"], ["2.1", "2.2", "2.3", "flee", "reshow_main"]) context.bot.editMessageText( chat_id=user1["userid"], message_id=update.callback_query.message.message_id, text="How much do you wish to repair? (1 Gold repairs 1 Health)", reply_markup=reply_markup) elif "-2.1-" in data and gm.check_gold(context.bot, user1["username"], int(config['castle']['repair_1'])): approved_action = [ "-2.1-", "You chose to repair <b>100</b> Health with <b>100</b> Gold!" ] elif "-2.2-" in data and gm.check_gold(context.bot, user1["username"], int(config['castle']['repair_2'])): approved_action = [ "-2.2-", "You chose to repair <b>200</b> Health with <b>200</b> Gold!" ] elif "-2.3-" in data and gm.check_gold(context.bot, user1["username"], int(config['castle']['repair_3'])): approved_action = [ "-2.3-", "You chose to repair <b>300</b> Health with <b>300</b> Gold!" ] if approved_action != []: um.switch_user_status(user1, user2, "3", "2") context.bot.deleteMessage( chat_id=user1["userid"], message_id=update.callback_query.message.message_id) context.bot.send_message(chat_id=user1["userid"], text=approved_action[1], parse_mode=ParseMode.HTML) gm.repair(context.bot, user1["username"], user2["username"], approved_action[0][1:4]) elif "-2-" in data: pass else: context.bot.send_message(chat_id=user1["userid"], text="You do not have enough gold!") return None
def hire(update, context): """ Function that takes in the hire response from user. Args: update: default telegram arg context: default telegram arg """ #answer query and load users context.bot.answer_callback_query(update.callback_query.id) data = update.callback_query.data usernames = re.match(r'-3\S*-(\S+)-(\S+)', data) username1, username2 = usernames.group(1), usernames.group(2) user1, user2 = um.load_user_data([username1, username2]) #prevent user from executing if status is not 2 if user1["user_status"] != "2": return None #variable to control actions approved_action = [] #list of possible actions to take depending on button pressed (callback_query.data) if "-3-" in data and gm.check_gold(context.bot, user1["username"], 5 * int(config['soldier']['price'])): reply_markup = mc.show_user_options( user1["username"], user2["username"], 5, ["Soldiers", "Warriors", "Knights", "Flee", "Back"], ["3.1", "3.2", "3.3", "flee", "reshow_main"]) context.bot.editMessageText( chat_id=user1["userid"], message_id=update.callback_query.message.message_id, text="Which unit do you wish to hire?", reply_markup=reply_markup) elif "-3.1-" in data and gm.check_gold(context.bot, user1["username"], 5 * int(config['soldier']['price'])): reply_markup = mc.show_user_options( user1["username"], user2["username"], 5, ["5", "10", "15", "Flee", "Back"], ["3.1.1", "3.1.2", "3.1.3", "flee", "3"]) context.bot.editMessageText( chat_id=user1["userid"], message_id=update.callback_query.message.message_id, text= "How many Soldiers do you wish to hire? (20 Gold, 5 Attack Damage each)", reply_markup=reply_markup) elif "-3.2-" in data and gm.check_gold(context.bot, user1["username"], 5 * int(config['warrior']['price'])): reply_markup = mc.show_user_options( user1["username"], user2["username"], 5, ["5", "10", "15", "Flee", "Back"], ["3.2.1", "3.2.2", "3.2.3", "flee", "3"]) context.bot.editMessageText( chat_id=user1["userid"], message_id=update.callback_query.message.message_id, text= "How many Warriors do you wish to hire? (50 Gold, 10 Attack Damage each)", reply_markup=reply_markup) elif "-3.3-" in data and gm.check_gold(context.bot, user1["username"], 5 * int(config['knight']['price'])): reply_markup = mc.show_user_options( user1["username"], user2["username"], 5, ["5", "10", "15", "Flee", "Back"], ["3.3.1", "3.3.2", "3.3.3", "flee", "3"]) context.bot.editMessageText( chat_id=user1["userid"], message_id=update.callback_query.message.message_id, text= "How many Knights do you wish to hire? (100 Gold, 20 Attack Damage each)", reply_markup=reply_markup) elif "-3.1.1-" in data and gm.check_gold( context.bot, user1["username"], 5 * int(config['soldier']['price'])): approved_action = ["-3.1.1-", "You chose to hire <b>5</b> Soldiers!"] elif "-3.1.2-" in data and gm.check_gold( context.bot, user1["username"], 10 * int(config['soldier']['price'])): approved_action = ["-3.1.2-", "You chose to hire <b>10</b> Soldiers!"] elif "-3.1.3-" in data and gm.check_gold( context.bot, user1["username"], 15 * int(config['soldier']['price'])): approved_action = ["-3.1.3-", "You chose to hire <b>15</b> Soldiers!"] elif "-3.2.1-" in data and gm.check_gold( context.bot, user1["username"], 5 * int(config['warrior']['price'])): approved_action = ["-3.2.1-", "You chose to hire <b>5</b> Warriors!"] elif "-3.2.2-" in data and gm.check_gold( context.bot, user1["username"], 10 * int(config['warrior']['price'])): approved_action = ["-3.2.2-", "You chose to hire <b>10</b> Warriors!"] elif "-3.2.3-" in data and gm.check_gold( context.bot, user1["username"], 15 * int(config['warrior']['price'])): approved_action = ["-3.2.3-", "You chose to hire <b>15</b> Warriors!"] elif "-3.3.1-" in data and gm.check_gold( context.bot, user1["username"], 5 * int(config['knight']['price'])): approved_action = ["-3.3.1-", "You chose to hire <b>5</b> Knights!"] elif "-3.3.2-" in data and gm.check_gold( context.bot, user1["username"], 10 * int(config['knight']['price'])): approved_action = ["-3.3.2-", "You chose to hire <b>10</b> Knights!"] elif "-3.3.3-" in data and gm.check_gold( context.bot, user1["username"], 15 * int(config['knight']['price'])): approved_action = ["-3.3.3-", "You chose to hire <b>15</b> Knights!"] if approved_action != []: um.switch_user_status(user1, user2, "3", "2") context.bot.deleteMessage( chat_id=user1["userid"], message_id=update.callback_query.message.message_id) context.bot.send_message(chat_id=user1["userid"], text=approved_action[1], parse_mode=ParseMode.HTML) gm.hire(context.bot, user1["username"], user2["username"], approved_action[0][1:6]) elif "-3-" in data or "-3.1-" in data or "-3.2" in data or "-3.3-" in data: pass else: context.bot.send_message(chat_id=user1["userid"], text="You do not have enough gold!") return None