From 91999322ec0c73cbcf5466ac9aa79c2eb933a70c Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 23 Jul 2018 20:12:23 -0500 Subject: [PATCH] Added a new command for finding tunnels and fixed a few issues. --- Commands.py | 39 +++++++++++++++++++++++++++++---------- DatabaseInterface.py | 13 +++++++------ DatabaseModels.py | 2 +- Geoffrey.py | 34 ++++++++++++++++++++++++---------- test_commands.py | 35 ++++++++++++++++++++++++----------- 5 files changed, 85 insertions(+), 38 deletions(-) diff --git a/Commands.py b/Commands.py index 840e3b0..ef2773f 100644 --- a/Commands.py +++ b/Commands.py @@ -47,33 +47,33 @@ class Commands: base = self.interface.add_location(session, player, base_name, x_pos, z_pos) - base_name = base.name + base_str = base.__str__() finally: session.close() - return base_name + return base_str - def addshop(self, x_pos, z_pos, shop_name=None, discord_uuid=None, mc_uuid=None): + def addshop(self, x_pos, z_pos, shop_str=None, discord_uuid=None, mc_uuid=None): session = self.interface.database.Session() try: player = self.get_player(session, discord_uuid, mc_uuid) if len(self.interface.find_shop_by_owner(session, player)) == 0: - if shop_name is None: - shop_name = "{}'s Shop".format(player.name) - elif shop_name is None: + if shop_str is None: + shop_str = "{}'s Shop".format(player.name) + elif shop_str is None: raise EntryNameNotUniqueError - shop = self.interface.add_shop(session, player, shop_name, x_pos, z_pos) + shop = self.interface.add_shop(session, player, shop_str, x_pos, z_pos) - shop_name = shop.name + shop_str = shop.__str__() finally: session.close() - return shop_name + return shop_str - def tunnel(self, tunnel_color, tunnel_number, location_name, discord_uuid=None, mc_uuid=None): + def addtunnel(self, tunnel_color, tunnel_number, location_name, discord_uuid=None, mc_uuid=None): session = self.interface.database.Session() try: @@ -153,3 +153,22 @@ class Commands: session.close() return loc + + def tunnel(self, player_name): + session = self.interface.database.Session() + + try: + tunnel_list = self.interface.find_tunnel_by_owner_name(session, player_name) + + if len(tunnel_list) == 0: + raise LocationLookUpError + + tunnel_str = '' + + for tunnel in tunnel_list: + tunnel_str = '{}\n{}'.format(tunnel_str, tunnel.full_str()) + + finally: + session.close() + + return tunnel_str diff --git a/DatabaseInterface.py b/DatabaseInterface.py index c442da6..205c40b 100644 --- a/DatabaseInterface.py +++ b/DatabaseInterface.py @@ -99,7 +99,7 @@ class DatabaseInterface: return self.database.query_by_filter(session, Tunnel, expr) def find_tunnel_by_owner_name(self, session, owner_name): - expr = Tunnel.owner.has(Player.name.ilike(owner_name)) + expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(owner_name))) return self.database.query_by_filter(session, Tunnel, expr) def find_item(self, session, item_name): @@ -150,13 +150,14 @@ class DatabaseInterface: loc_string = "{}\n{}".format(loc_string, loc) count += 1 - loc_string = loc_string + '\n\n**Tunnels:**' - expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(search))) & Tunnel.location == None + tunnels = self.database.query_by_filter(session, Tunnel, expr) - for tunnel in self.database.query_by_filter(session, Tunnel, expr): - loc_string = "{}\n{}".format(loc_string, tunnel.full_str()) - count += 1 + if len(tunnels) > 0: + loc_string = loc_string + '\n\n**Tunnels:**' + for tunnel in tunnels: + loc_string = "{}\n{}".format(loc_string, tunnel.full_str()) + count += 1 if count == 0: raise LocationLookUpError diff --git a/DatabaseModels.py b/DatabaseModels.py index 4db4293..effab64 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -155,7 +155,7 @@ class Tunnel(SQL_Base): raise TunnelInitError def full_str(self): - return 'Owner: **{}**\'s Tunnel: **{}**'.format(self.owner.name, self.__str__()) + return 'Owner: **{}** Tunnel: **{}**'.format(self.owner.name, self.__str__()) def __str__(self): return '{} {}'.format(self.tunnel_direction.value.title(), self.tunnel_number) diff --git a/Geoffrey.py b/Geoffrey.py index d9e4c2b..2584b98 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -96,7 +96,7 @@ async def addbase(ctx, x_pos: int, z_pos: int, * args): try: base = bot_commands.addbase(x_pos, z_pos, base_name=name, discord_uuid=ctx.message.author.id) - await bot.say('{}, your base has been added to the database: {}'.format(ctx.message.author.mention, base)) + await bot.say('{}, your base has been added to the database: \n\n{}'.format(ctx.message.author.mention, base)) except LocationInitError: raise commands.UserInputError except EntryNameNotUniqueError: @@ -121,8 +121,8 @@ async def addshop(ctx, x_pos: int, z_pos: int, *args): name = None try: - shop = bot_commands.addshop(x_pos, z_pos, shop_name=name, discord_uuid=ctx.message.author.id) - await bot.say('{}, your shop has been added to the database: {}'.format(ctx.message.author.mention, shop)) + shop = bot_commands.addshop(x_pos, z_pos, shop_str=name, discord_uuid=ctx.message.author.id) + await bot.say('{}, your shop has been added to the database: \n\n{}'.format(ctx.message.author.mention, shop)) except LocationInitError: raise commands.UserInputError except EntryNameNotUniqueError: @@ -134,7 +134,7 @@ async def addshop(ctx, x_pos: int, z_pos: int, *args): ctx.message.author.mention, name)) @bot.command(pass_context=True) -async def tunnel(ctx, tunnel_color: str, tunnel_number: int, *args): +async def addtunnel(ctx, tunnel_color: str, tunnel_number: int, *args): ''' Adds your tunnel to the database. The location name is optional. If the location has a tunnel, it is updated. @@ -146,7 +146,7 @@ async def tunnel(ctx, tunnel_color: str, tunnel_number: int, *args): else: location_name = None - bot_commands.tunnel(tunnel_color, tunnel_number, discord_uuid=ctx.message.author.id, location_name=location_name) + bot_commands.addtunnel(tunnel_color, tunnel_number, discord_uuid=ctx.message.author.id, location_name=location_name) await bot.say('{}, your tunnel has been added to the database'.format(ctx.message.author.mention)) except EntryNameNotUniqueError: await bot.say('{}, you already have one tunnel in the database, please specify a location.'.format( @@ -175,6 +175,20 @@ async def find(ctx, * args): except LocationLookUpError: await bot.say('{}, no matches to **{}** were found in the database'.format(ctx.message.author.mention, search)) +@bot.command(pass_context=True) +async def tunnel(ctx, player: str): + ''' + Finds all the tunnels a player owns. + ?tunnel [Player] + ''' + try: + result = bot_commands.tunnel(player) + + await bot.say('{}, **{}** owns the following tunnels: \n{}'.format(ctx.message.author.mention, player, result)) + except LocationLookUpError: + await bot.say('{}, no tunnels for the player **{}** were found in the database' + .format(ctx.message.author.mention, player)) + @bot.command(pass_context=True) async def delete(ctx, * args): ''' @@ -242,9 +256,10 @@ async def additem(ctx, item_name: str, quantity: int, diamond_price: int, * args else: shop_name = None - bot_commands.additem(item_name, quantity, diamond_price, shop_name=shop_name) - await bot.say('{}, **{}** has been added to the inventory of **{}**.'.format(ctx.message.author.mention, - item_name, shop_name)) + bot_commands.additem(item_name, quantity, diamond_price, shop_name=shop_name, + discord_uuid=ctx.message.author.id) + await bot.say('{}, **{}** has been added to the inventory of your shop.'.format(ctx.message.author.mention, + item_name)) except PlayerNotFound: await bot.say('{}, you don\'t have any shops in the database.'.format(ctx.message.author.mention)) except LocationInitError: @@ -315,13 +330,12 @@ def get_args_dict(args): def update_user_names(bot_commands): threading.Timer(600, update_user_names, [bot_commands]).start() session = bot_commands.interface.database.Session() - + print("Updating MC usernames...") player_list = session.query(Player).all() for player in player_list: player.name = grab_playername(player.mc_uuid) - print("Updating MC usernames...") session.commit() session.close() diff --git a/test_commands.py b/test_commands.py index 10f273a..0d13316 100644 --- a/test_commands.py +++ b/test_commands.py @@ -49,15 +49,15 @@ class TestCommands(TestCase): else: pass - def test_tunnel(self): + def test_addtunnel(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_name='test shop', discord_uuid='143072699567177728') + self.commands.addshop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') - tunnel1 = self.commands.tunnel('green', 50, None, discord_uuid='143072699567177728') + tunnel1 = self.commands.addtunnel('green', 50, None, discord_uuid='143072699567177728') self.assertGreater(len(tunnel1), 0) - tunnel2 = self.commands.tunnel('Green', 50, location_name='test_shop', discord_uuid='143072699567177728') + tunnel2 = self.commands.addtunnel('Green', 50, location_name='test_shop', discord_uuid='143072699567177728') if 'Green' not in tunnel2: self.fail() @@ -66,7 +66,7 @@ class TestCommands(TestCase): def test_find(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_name='frick', discord_uuid='143072699567177728') + self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') self.commands.addbase(0, 0, 'heck', discord_uuid='143072699567177728') result = self.commands.find('zerohd') @@ -78,7 +78,7 @@ class TestCommands(TestCase): def test_delete(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_name='frick', discord_uuid='143072699567177728') + self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') self.commands.delete('frick', discord_uuid='143072699567177728') @@ -87,7 +87,7 @@ class TestCommands(TestCase): def test_findaround(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_name='frick', discord_uuid='143072699567177728') + self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') result = self.commands.findaround(0, 0) @@ -107,7 +107,7 @@ class TestCommands(TestCase): else: self.fail() - self.commands.addshop(0, 0, shop_name='frick', discord_uuid='143072699567177728') + self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') result = self.commands.additem('cool', 5, 5, shop_name='frick', discord_uuid='143072699567177728') @@ -118,7 +118,7 @@ class TestCommands(TestCase): def test_selling(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_name='frick', discord_uuid='143072699567177728') + self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') self.commands.additem('cool', 5, 5, shop_name='frick', discord_uuid='143072699567177728') @@ -131,9 +131,9 @@ class TestCommands(TestCase): def test_info(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_name='frick', discord_uuid='143072699567177728') + self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') - self.commands.tunnel('Green', 50, location_name='frick', discord_uuid='143072699567177728') + self.commands.addtunnel('Green', 50, location_name='frick', discord_uuid='143072699567177728') result = self.commands.info('frick') @@ -141,3 +141,16 @@ class TestCommands(TestCase): pass else: self.fail() + + def test_tunnel(self): + self.commands.register('ZeroHD', '143072699567177728') + self.commands.addshop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') + + tunnel = self.commands.addtunnel('green', 50, None, discord_uuid='143072699567177728') + + result = self.commands.tunnel('ZeroHD') + + if 'Green' in result: + pass + else: + self.fail()