From 9a20e0155ecb82ff810267d81162eb32441fb638 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Fri, 20 Jul 2018 23:44:41 -0500 Subject: [PATCH] find command now searches more than just players and adjusted string formating --- DatabaseModels.py | 39 ++++++++++++++++++++++++++------------- Geoffrey.py | 14 ++++++-------- test_geoffreyDatabase.py | 8 ++++++++ 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/DatabaseModels.py b/DatabaseModels.py index 8e0a983..eba5480 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -162,13 +162,20 @@ class DatabaseInterface: player = self.database.query_by_filter(Player, expr)[0] except IndexError: raise PlayerNotFound - return player - def get_shop_inventory(self, shop): - expr = ItemListing.shop == shop + def search_all_fields(self, search): + loc_string = '' - return self.database.query_by_filter(ItemListing, expr) + expr = Location.owner.has(Player.name.ilike('%{}%'.format(search))) | Location.name.ilike('%{}%'.format(search)) + for loc in self.database.query_by_filter(Location, expr): + loc_string = "{}\n\t{}".format(loc_string, loc) + + expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(search))) & Tunnel.location is None + for loc in self.database.query_by_filter(Tunnel, expr): + loc_string = "{}\n\t{}".format(loc_string, loc) + + return loc_string def delete_location(self, owner, name): expr = (Location.owner == owner) & (Location.name == name) @@ -383,12 +390,14 @@ class Location(SQL_Base): def pos_to_str(self): return '(x= {}, y= {}, z= {}) in the {}'.format(self.x, self.y, self.z, self.dimension.value.title()) + def info_str(self): + return "Name: **{}**, Type: **{}** Position: **{}**".format(self.name, self.type, self.pos_to_str()) + def __str__(self): if self.tunnel is not None: - return "Name: {}, Position: {}, Tunnel: {}".format(self.name, self.pos_to_str(), - self.tunnel) + return "{}, Tunnel: **{}**".format(self.info_str(), self.tunnel) else: - return "Name: {}, Position: {}".format(self.name, self.pos_to_str()) + return self.info_str(self) @@ -402,16 +411,20 @@ class Shop(Location): } def inv_to_str(self): - inv = '' - str_format = '{}\n\t{}' - for item in self.inventory: - inv = str_format.format(inv, item) + if len(self.inventory.all()) != 0: + inv = '\n\t*Inventory*' + str_format = '{}\n\t\t{}' - return inv + for item in self.inventory: + inv = str_format.format(inv, item) + + return inv + else: + return '' def __str__(self): - return Location.__str__(self) + "\n\t*Inventory*: {}".format(self.inv_to_str()) + return Location.__str__(self) + self.inv_to_str() def __init__(self, name, x, y, z, owner, dimension=None): Location.__init__(self, name, x, y, z, owner, dimension) diff --git a/Geoffrey.py b/Geoffrey.py index 1e284ce..756e961 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -165,20 +165,18 @@ async def tunnel(ctx, tunnel_color: str, tunnel_number: int, *args): @bot.command(pass_context=True) -async def find(ctx, name: str): +async def find(ctx, search: str): ''' - Finds all the locations a player has in the database. - ?find [Player name] + Finds all the locations and tunnels matching the search term + ?find [Search] ''' try: - loc_list = database_interface.find_location_by_owner_name(name) - loc_string = loc_list_to_string(loc_list, '{} \n{}') + result = database_interface.search_all_fields(search) - await bot.say('{}, **{}** has **{}** locations(s): \n {}'.format(ctx.message.author.mention, name, len(loc_list), - loc_string)) + await bot.say('{}, The following entires match **{}**: {}'.format(ctx.message.author.mention, search, result)) except PlayerNotFound: - await bot.say('{}, the player **{}** is not in the database'.format(ctx.message.author.mention, name)) + await bot.say('{}, no matches **{}** were found in the database'.format(ctx.message.author.mention, name)) @bot.command(pass_context=True) diff --git a/test_geoffreyDatabase.py b/test_geoffreyDatabase.py index 3dc5fb8..ac8151f 100644 --- a/test_geoffreyDatabase.py +++ b/test_geoffreyDatabase.py @@ -151,6 +151,14 @@ class TestGeoffreyDatabase(TestCase): self.assertEqual(loc_list[0].name, loc.name) + def test_search_all(self): + owner = self.add_player() + loc = self.add_loc() + + loc_list = self.interface.search_all_fields('ZeroHD') + + self.assertEqual(type(loc_list), str) + def test_wrong_case(self): owner = self.add_player() loc = self.add_loc()