find command now searches more than just players and adjusted string formating
parent
2175e025e5
commit
9a20e0155e
|
@ -162,13 +162,20 @@ class DatabaseInterface:
|
||||||
player = self.database.query_by_filter(Player, expr)[0]
|
player = self.database.query_by_filter(Player, expr)[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise PlayerNotFound
|
raise PlayerNotFound
|
||||||
|
|
||||||
return player
|
return player
|
||||||
|
|
||||||
def get_shop_inventory(self, shop):
|
def search_all_fields(self, search):
|
||||||
expr = ItemListing.shop == shop
|
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):
|
def delete_location(self, owner, name):
|
||||||
expr = (Location.owner == owner) & (Location.name == name)
|
expr = (Location.owner == owner) & (Location.name == name)
|
||||||
|
@ -383,12 +390,14 @@ class Location(SQL_Base):
|
||||||
def pos_to_str(self):
|
def pos_to_str(self):
|
||||||
return '(x= {}, y= {}, z= {}) in the {}'.format(self.x, self.y, self.z, self.dimension.value.title())
|
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):
|
def __str__(self):
|
||||||
if self.tunnel is not None:
|
if self.tunnel is not None:
|
||||||
return "Name: {}, Position: {}, Tunnel: {}".format(self.name, self.pos_to_str(),
|
return "{}, Tunnel: **{}**".format(self.info_str(), self.tunnel)
|
||||||
self.tunnel)
|
|
||||||
else:
|
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):
|
def inv_to_str(self):
|
||||||
inv = ''
|
|
||||||
str_format = '{}\n\t{}'
|
if len(self.inventory.all()) != 0:
|
||||||
|
inv = '\n\t*Inventory*'
|
||||||
|
str_format = '{}\n\t\t{}'
|
||||||
|
|
||||||
for item in self.inventory:
|
for item in self.inventory:
|
||||||
inv = str_format.format(inv, item)
|
inv = str_format.format(inv, item)
|
||||||
|
|
||||||
return inv
|
return inv
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
def __str__(self):
|
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):
|
def __init__(self, name, x, y, z, owner, dimension=None):
|
||||||
Location.__init__(self, name, x, y, z, owner, dimension)
|
Location.__init__(self, name, x, y, z, owner, dimension)
|
||||||
|
|
14
Geoffrey.py
14
Geoffrey.py
|
@ -165,20 +165,18 @@ async def tunnel(ctx, tunnel_color: str, tunnel_number: int, *args):
|
||||||
|
|
||||||
|
|
||||||
@bot.command(pass_context=True)
|
@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.
|
Finds all the locations and tunnels matching the search term
|
||||||
?find [Player name]
|
?find [Search]
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loc_list = database_interface.find_location_by_owner_name(name)
|
result = database_interface.search_all_fields(search)
|
||||||
loc_string = loc_list_to_string(loc_list, '{} \n{}')
|
|
||||||
|
|
||||||
await bot.say('{}, **{}** has **{}** locations(s): \n {}'.format(ctx.message.author.mention, name, len(loc_list),
|
await bot.say('{}, The following entires match **{}**: {}'.format(ctx.message.author.mention, search, result))
|
||||||
loc_string))
|
|
||||||
except PlayerNotFound:
|
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)
|
@bot.command(pass_context=True)
|
||||||
|
|
|
@ -151,6 +151,14 @@ class TestGeoffreyDatabase(TestCase):
|
||||||
|
|
||||||
self.assertEqual(loc_list[0].name, loc.name)
|
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):
|
def test_wrong_case(self):
|
||||||
owner = self.add_player()
|
owner = self.add_player()
|
||||||
loc = self.add_loc()
|
loc = self.add_loc()
|
||||||
|
|
Loading…
Reference in New Issue