def inventory_modify_menu(self, inventory): while True: print() print(inventory.describe_current_inventory()) print("What would you like to do to this inventory?") options = [ 'add_item', 'remove_item', 'modify_attachments', 'finished' ] descriptions = [ 'Add a weapon or armor', 'Remove a weapon or armor', 'Modify attachments for a weapon', 'Finish modifying, go back to main menu.' ] option = prompts.select_from_list( options, descriptions, "Please choose an option from this list.", interactive_with_single_option=True) if option == 'add_item': self.add_item_menu(inventory) elif option == 'remove_item': self.remove_item_menu(inventory) elif option == 'modify_attachments': self.modify_attachments_menu(inventory) elif option == 'finished': return self.connection.commit() inventory.invalidate_cache()
def run(self): if self.player.guild_name is not None: self.show_guild_members() print() print("You've been a member of %s since %s" % (self.player.guild_name, self.player.guild_join_date.strftime('%Y-%m-%d'))) print( "If you want to join/create another guild you must leave first." ) print( "Would you like to leave the guild? Enter y or Y to leave, and enter anything else to continue" ) value = prompts.get_input_value_with_quit('Input: ') if value.lower().strip() == 'y': self.leave_guild_prompt() else: options = ['join_guild', 'create_guild', 'finished'] descriptions = [ 'Join a guild', 'Create a new guild', 'Exit back to main menu' ] option = prompts.select_from_list(options, descriptions, 'What would you like to do?') if option == 'create_guild': print("Creating a guild") self.create_new_guild() elif option == 'join_guild': self.join_a_guild()
def add_item_menu(self, inventory): # Add a nil_type so that the NOT IN clause always has a valid syntax if there are no weapons in the # inventory weapon_types = [w.type for w in inventory.weapons()] + ['nil_type'] fetch_armors = len(inventory.armors()) == 0 weapons_query = """ SELECT items.*, weapons.* FROM weapons INNER JOIN items ON weapons.item_id = items.id WHERE weapons.type NOT IN {0} AND weapons.item_id IN (SELECT item_id FROM owns WHERE username=%(username)s) """.format(sql.sql_format_tuple(weapon_types)) armors_query = """ SELECT items.*, armors.* FROM armors INNER JOIN items ON armors.item_id = items.id WHERE armors.item_id IN (SELECT item_id FROM owns WHERE username=%(username)s) """ armors = [] with self.connection.cursor() as cursor: cursor.execute(weapons_query, {"username": self.player.username}) weapons = list(sql.objects(cursor)) if fetch_armors: cursor.execute(armors_query, {"username": self.player.username}) armors = list(sql.objects(cursor)) options = weapons + armors descriptions = [] for weapon in weapons: descriptions.append('%s -- %s, weight: %d, range: %d, damage: %d' % (weapon.name, weapon.type, weapon.weight, weapon.range, weapon.damage)) for armor in armors: descriptions.append('%s -- Armor, weight: %d, protection: %d' % (armor.name, armor.weight, armor.protection)) if len(options) == 0: print("Your inventory is already full! Please remove items first" " if you want to add a different weapon or armor.") else: new_item = prompts.select_from_list( options, descriptions, "Please select an item from this list to add" " to your inventory", interactive_with_single_option=True) with self.connection.cursor() as cursor: cursor.execute( "INSERT INTO inventory_contains (username, name, item_id)" "VALUES (%(username)s, %(name)s, %(item_id)s)", { "username": self.player.username, "name": inventory.name, "item_id": new_item.id })
def choose_inventory(self): inventories = models.Inventory.get_for_player(self.connection, self.player.username) inventory_descriptions = [ inventory.describe_current_inventory() for inventory in inventories ] return prompts.select_from_list( inventories, inventory_descriptions, "Select an inventory to bring into the game.")
def leave_guild_prompt(self): guild_name = self.player.guild_name query = """ SELECT guilds.admin_username, count(*) - 1 as num_non_admins FROM guilds INNER JOIN players ON guilds.name = players.guild_name WHERE guild_name=%(guild_name)s GROUP BY guilds.admin_username; """ with self.connection.cursor() as cursor: cursor.execute(query, {"guild_name": guild_name}) admin, num_non_admins = cursor.fetchone() if admin == self.player.username: if num_non_admins == 0: print( "Since you're the admin and the sole member of the guild, the guild will be deleted." ) value = prompts.get_input_value_with_quit( 'Confirm that this is what you want to do by entering y or Y: ' ) if value == 'y': self.leave_guild() self.delete_guild(guild_name) else: print( "Since you're the admin you should assign a new admin first before leaving." ) with self.connection.cursor() as cursor: cursor.execute( "SELECT username FROM players WHERE guild_name=%(guild_name)s AND username<>%(username)s", { "guild_name": guild_name, "username": self.player.username }) members = [row[0] for row in cursor] new_admin = prompts.select_from_list( members, members, 'Who would you like to make the admin?', interactive_with_single_option=True) query = """ UPDATE guilds SET admin_username=%(new_admin)s WHERE name=%(guild_name)s """ cursor.execute(query, { "new_admin": new_admin, "guild_name": guild_name }) self.leave_guild() else: self.leave_guild()
def modify_attachments_menu(self, inventory): descriptions = [w.name for w in inventory.weapons()] print("Which weapon would you like to modify attachments for?") weapon = prompts.select_from_list(inventory.weapons(), descriptions, "Choose a weapon from this list: ", interactive_with_single_option=True) while True: current_attachments = [ a for a in inventory.attachments() if a.attaches_to_id == weapon.id ] options = ['add_new'] descriptions = ['Add a new attachment'] if len(current_attachments) > 0: options.append('remove') descriptions.append('Remove an attachment') options.append('finished') descriptions.append('Exit to previous menu.') print("What would you like to do?") option = prompts.select_from_list( options, descriptions, "Please select a number from this list: ", interactive_with_single_option=True) if option == 'add_new': self.add_new_attachment(inventory, weapon, current_attachments) elif option == 'remove': self.remove_attachment(inventory, current_attachments) else: return inventory.invalidate_cache() self.connection.commit()
def main(): with database.connect() as connection: with connection.cursor() as cursor: cursor.execute( "SELECT *, exp_requirement(level) as exp_requirement FROM players WHERE banned_date IS NULL " "ORDER BY username") players = list(sql.objects(cursor)) try: print("Type q or Q at any time to exit the application.") # First step is to select a player player = prompts.select_from_list( players, [p.username for p in players], "Please pick a user from this list to play as") print("Welcome back %s!" % player.username) menu_item_descriptions = [ flow.prompt_text for flow in flows.menu_items ] while True: print(end='\n\n') flow_class = prompts.select_from_list( flows.menu_items, menu_item_descriptions, "Please select something to do!") with database.connect() as connection: flow_object = flow_class(player, connection) flow_object.run() # Reload the player as data may have changed player = reload_player(player, connection) except prompts.QuitException: return
def remove_attachment(self, inventory, current_attachments): print("Which attachment would you like to remove?") attachment = prompts.select_from_list( current_attachments, [a.name for a in current_attachments], "Enter a number from this list:", interactive_with_single_option=True) with self.connection.cursor() as cursor: cursor.execute( "DELETE FROM inventory_contains WHERE " "username=%(username)s AND name=%(name)s AND item_id=%(item_id)s", { "username": self.player.username, "name": inventory.name, "item_id": attachment.id }) print("Attachment removed!")
def add_new_attachment(self, inventory, weapon, current_attachments): query = """ SELECT items.id, items.name, items.weight FROM attachments INNER JOIN items ON attachments.item_id = items.id WHERE attachments.attaches_to_id = %(weapon_item_id)s AND attachments.item_id NOT IN ( SELECT item_id AS id FROM inventory_contains WHERE username=%(username)s AND name=%(name)s ) AND attachments.item_id IN ( SELECT item_id AS id FROM owns WHERE username=%(username)s ) """ with self.connection.cursor() as cursor: cursor.execute( query, { "weapon_item_id": weapon.id, "username": self.player.username, "name": inventory.name }) attachments = list(sql.objects(cursor)) if len(attachments) == 0: print("You don't own any additional attachments for this item!") else: print("Which attachment would you like to add.") descriptions = [ "%s, weight: %d" % (a.name, a.weight) for a in attachments ] attachment = prompts.select_from_list( attachments, descriptions, "Choose a number from this list:", interactive_with_single_option=True) with self.connection.cursor() as cursor: cursor.execute( "INSERT INTO inventory_contains (username, name, item_id)" "VALUES (%(username)s, %(name)s, %(item_id)s)", { "username": self.player.username, "name": inventory.name, "item_id": attachment.id })
def prompt_for_inventory_option(self, inventories): # The options are rename inventory, modify inventory or create a new one options = [] descriptions = [] for inventory in inventories: options.append(('modify', inventory)) descriptions.append('Modify %s' % inventory.name) options.append(('rename', inventory)) descriptions.append('Rename %s' % inventory.name) options.append(('create', None)) descriptions.append('Create a new inventory') return prompts.select_from_list( options, descriptions, "What would you like to do? Please choose an option from this list.", interactive_with_single_option=True)
def run(self): print("You have %d coins in your balance" % self.player.coin_balance) while True: options = ['weapons', 'armors', 'attachments', 'finished'] descriptions = [ 'Browse weapons', 'Browse armors', 'Browse attachments for your weapons', 'Exit the marketplace' ] option = prompts.select_from_list(options, descriptions, 'What would you like to do?') if option == 'weapons': self.weapons_menu() elif option == 'armors': self.armors_menu() elif option == 'attachments': self.attachments_menu() else: return
def join_a_guild(self): query = "SELECT admin_username, name, created_date FROM guilds" with self.connection.cursor() as cursor: cursor.execute(query) guilds = list(sql.objects(cursor)) descriptions = [ '%s -- Admin: %s, active since %s' % (g.name, g.admin_username, g.created_date.strftime("%Y-%m-%d")) for g in guilds ] + ['Exit without joining anything'] guilds.append('exit') option = prompts.select_from_list( guilds, descriptions, "Which guild would you like to join?") if option == 'exit': return self.set_player_guild(self.player, option) self.connection.commit() print("Welcome to %s!" % option.name)
def purchase_menu_loop(self, items, formatter, empty_message): updated_balance = self.player.coin_balance while True: if len(items) == 0: print(empty_message) self.reload_player() return print("Your balance is now %d" % updated_balance) options = items + ['exit'] descriptions = [formatter(item) for item in items] + ['Exit'] choice = prompts.select_from_list( options, descriptions, 'What would you like to purchase? You may exit without purchasing anything.' ) if choice == 'exit': # Reload player so balance is up to date in future menus self.reload_player() return try: # Purchase the item using the stored procedure we created with self.connection.cursor() as cursor: cursor.execute( "SELECT purchase_item(%(item_id)s, %(username)s)", { "item_id": choice.id, "username": self.player.username }) updated_balance = cursor.fetchone()[0] self.connection.commit() print("You now own %s" % choice.name) items.remove(choice) except psycopg2.errors.RaiseException as err: # The transaction likely raised due to insufficient balance print("Your balance is insufficient to purchase this item.")
def remove_item_menu(self, inventory): options = inventory.weapons() + inventory.armors() descriptions = [item.name for item in options] item_to_remove = prompts.select_from_list( options, descriptions, "Choose an item to remove:", interactive_with_single_option=True) delete_query = """ DELETE FROM inventory_contains WHERE username=%(username)s AND name=%(name)s AND item_id=%(item_id)s """ with self.connection.cursor() as cursor: cursor.execute( delete_query, { "username": self.player.username, "name": inventory.name, "item_id": item_to_remove.id }) print("Item successfully removed!")
def run(self): while True: options = ['Revenue', 'Friendship', 'finished'] descriptions = [ 'Display monthly revenue for 2019', 'Display friendship graph', 'Exit' ] option = prompts.select_from_list(options, descriptions, 'What would you like to do?') if option == 'Revenue': x = [] y = [] l = [] with self.connection.cursor() as cursor: cursor.execute( 'SELECT amount_paid, trans_date FROM coin_purchases') l = cursor.fetchall() l.sort(key=lambda la: (la[1])) last_purchase_year = l[-1][1].year for i in range(2019, last_purchase_year): for j in range(1, 13): s = str(i) + '-' + str(j) x.append(s) for month in x: total = 0 for purchase in l: if int(month[0:4]) == purchase[1].year and int( month[5:]) == purchase[1].month: total += purchase[0] y.append(total) plt.title('Revenue per Month') plt.xlabel('Month') plt.ylabel('Revenue (Coins)') plt.xticks(rotation=45) plt.plot(x, y, color='r') plt.show() elif option == 'Friendship': l = [] with self.connection.cursor() as cursor: cursor.execute( 'SELECT requester_username, requestee_username FROM friends' ) l = cursor.fetchall() G = nx.Graph() for (f1, f2) in l: G.add_edge(f1, f2) nx.draw(G, with_labels=True, node_size=1000, node_color='red') plt.show() else: return None