From 5b0027668b5b786961797155b0e59a732e2c402a Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Tue, 31 Jul 2018 19:22:17 -0500 Subject: [PATCH] Added edit commands for locations --- Commands.py | 76 +++++++++++++++++++++++++++++++++--- DatabaseInterface.py | 8 ++-- DatabaseModels.py | 16 +++++++- Geoffrey.py | 80 +++++++++++++++++++++++++++++++------- test_commands.py | 83 +++++++++++++++++++++++++++++----------- test_geoffreyDatabase.py | 6 +-- 6 files changed, 219 insertions(+), 50 deletions(-) diff --git a/Commands.py b/Commands.py index 97e4eec..3e9cb45 100644 --- a/Commands.py +++ b/Commands.py @@ -1,4 +1,6 @@ from DatabaseInterface import * +from itertools import zip_longest +from discord.ext.commands import UserInputError class Commands: @@ -33,7 +35,7 @@ class Commands: return player_name - def addbase(self, x_pos, z_pos, base_name=None, discord_uuid=None, mc_uuid=None): + def add_base(self, x_pos, z_pos, base_name=None, discord_uuid=None, mc_uuid=None): session = self.interface.database.Session() @@ -46,7 +48,7 @@ class Commands: elif base_name is None: raise EntryNameNotUniqueError - base = self.interface.add_location(session, player, base_name, x_pos, z_pos) + base = self.interface.add_base(session, player, base_name, x_pos, z_pos) base_str = base.__str__() finally: @@ -54,7 +56,7 @@ class Commands: return base_str - def addshop(self, x_pos, z_pos, shop_str=None, discord_uuid=None, mc_uuid=None): + def add_shop(self, x_pos, z_pos, shop_str=None, discord_uuid=None, mc_uuid=None): session = self.interface.database.Session() try: @@ -74,7 +76,7 @@ class Commands: return shop_str - def addtunnel(self, tunnel_color, tunnel_number, location_name, discord_uuid=None, mc_uuid=None): + def add_tunnel(self, tunnel_color, tunnel_number, location_name, discord_uuid=None, mc_uuid=None): session = self.interface.database.Session() try: @@ -106,7 +108,7 @@ class Commands: finally: session.close() - def findaround(self, x_pos, z_pos, radius=200, dimension='Overworld'): + def find_around(self, x_pos, z_pos, radius=200, dimension='Overworld'): session = self.interface.database.Session() @@ -117,7 +119,7 @@ class Commands: return loc_list - def additem(self, item_name, quantity, diamond_price, shop_name, discord_uuid=None, mc_uuid=None): + def add_item(self, item_name, quantity, diamond_price, shop_name, discord_uuid=None, mc_uuid=None): session = self.interface.database.Session() try: player = self.get_player(session, discord_uuid, mc_uuid) @@ -176,3 +178,65 @@ class Commands: session.close() return tunnel_str + + def edit_pos(self, x, z, loc_name, discord_uuid=None, mc_uuid=None): + session = self.interface.database.Session() + + try: + player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) + location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] + + location.x = x + location.z = z + + session.commit() + + loc_str = location.__str__() + except IndexError: + raise LocationLookUpError + finally: + session.close() + + return loc_str + + def edit_tunnel(self, tunnel_color, tunnel_number, loc_name, discord_uuid=None, mc_uuid=None): + session = self.interface.database.Session() + + try: + player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) + location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] + + if location.tunnel is not None: + location.tunnel.tunnel_direction = TunnelDirection.str_to_tunnel_dir(tunnel_color) + location.tunnel.tunnel_number = tunnel_number + else: + self.interface.add_tunnel(session, player, tunnel_color, tunnel_number, loc_name) + + loc_str = location.__str__() + + session.commit() + except IndexError: + raise LocationLookUpError + finally: + session.close() + + return loc_str + + def edit_name(self, new_name, loc_name, discord_uuid=None, mc_uuid=None): + session = self.interface.database.Session() + + try: + player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) + location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] + + location.name = new_name + loc_str = location.__str__() + session.commit() + except IndexError: + raise LocationLookUpError + finally: + session.close() + + return loc_str + + diff --git a/DatabaseInterface.py b/DatabaseInterface.py index ae006f9..63077e5 100644 --- a/DatabaseInterface.py +++ b/DatabaseInterface.py @@ -6,10 +6,10 @@ class DatabaseInterface: def __init__(self, db_engine_arg): self.database = GeoffreyDatabase(db_engine_arg) - def add_location(self, session, owner, name, x_pos, z_pos, dimension=None): - location = Location(name, x_pos, z_pos, owner, dimension) - self.database.add_object(session, location) - return location + def add_base(self, session, owner, name, x_pos, z_pos, dimension=None): + base = Base(name, x_pos, z_pos, owner, dimension) + self.database.add_object(session, base) + return base def add_shop(self, session, owner, name, x_pos, z_pos, dimension=None): shop = Shop(name, x_pos, z_pos, owner, dimension) diff --git a/DatabaseModels.py b/DatabaseModels.py index 62fbf69..202ad54 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -8,6 +8,8 @@ from difflib import SequenceMatcher from BotErrors import * from MinecraftAccountInfoGrabber import * +world_name = 'season3' + SQL_Base = declarative_base() @@ -204,8 +206,8 @@ class Location(SQL_Base): raise LocationInitError def dynmap_link(self): - return ''.\ - format(self.x, self.z) + return ''.\ + format(world_name, self.x, self.z) def pos_to_str(self): return '(x= {}, z= {}) in the {}'.format(self.x, self.z, self.dimension.value.title()) @@ -224,6 +226,16 @@ class Location(SQL_Base): return self.info_str() +class Base(Location): + __tablename__ = 'Bases' + base_id = Column(Integer, ForeignKey('Locations.id', ondelete='CASCADE'), primary_key=True) + name = column_property(Column(String(128)), Location.name) + + __mapper_args__ = { + 'polymorphic_identity': 'Base', + } + + class Shop(Location): __tablename__ = 'Shops' shop_id = Column(Integer, ForeignKey('Locations.id', ondelete='CASCADE'), primary_key=True) diff --git a/Geoffrey.py b/Geoffrey.py index 84e361b..4d359a6 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -90,13 +90,13 @@ async def addbase(ctx, x_pos: int, z_pos: int, * args): ''' Adds your base to the database. The name is optional. - ?addbase [X Coordinate] [Y Coordinate] [Z Coordinate] [Base Name] + ?addbase [X Coordinate] [Z Coordinate] [Base Name] ''' name = get_name(args) try: - base = bot_commands.addbase(x_pos, z_pos, base_name=name, discord_uuid=ctx.message.author.id) + base = bot_commands.add_base(x_pos, z_pos, base_name=name, discord_uuid=ctx.message.author.id) await bot.say('{}, your base has been added to the database: \n\n{}'.format(ctx.message.author.mention, base)) except LocationInitError: raise commands.UserInputError @@ -115,13 +115,13 @@ async def addshop(ctx, x_pos: int, z_pos: int, *args): ''' Adds your shop to the database. The name is optional. - ?addshop [X Coordinate] [Y Coordinate] [Z Coordinate] [Shop Name] + ?addshop [X Coordinate] [Z Coordinate] [Shop Name] ''' name = get_name(args) try: - shop = bot_commands.addshop(x_pos, z_pos, shop_str=name, discord_uuid=ctx.message.author.id) + shop = bot_commands.add_shop(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 @@ -143,9 +143,9 @@ async def addtunnel(ctx, tunnel_color: str, tunnel_number: int, *args): ?tunnel [Tunnel Color] [Tunnel Number] [Location Name] ''' try: - location_name = get_name(args) + loc_name = get_name(args) - bot_commands.addtunnel(tunnel_color, tunnel_number, discord_uuid=ctx.message.author.id, location_name=location_name) + bot_commands.add_tunnel(tunnel_color, tunnel_number, discord_uuid=ctx.message.author.id, location_name=loc_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( @@ -153,9 +153,9 @@ async def addtunnel(ctx, tunnel_color: str, tunnel_number: int, *args): return except LocationLookUpError: await bot.say('{}, you do not have a location called **{}**.'.format( - ctx.message.author.mention, args[0])) + ctx.message.author.mention, loc_name)) except LocationHasTunnelError: - await bot.say('{}, **{}** already has a tunnel.'.format(ctx.message.author.mention, args[0])) + await bot.say('{}, **{}** already has a tunnel.'.format(ctx.message.author.mention, loc_name)) except TunnelInitError: await bot.say('{}, invalid tunnel color.'.format(ctx.message.author.mention)) except InvalidTunnelError: @@ -245,7 +245,7 @@ async def findaround(ctx, x_pos: int, z_pos: int, * args): if args[1] == '-d': dimension = args[2] - base_string = bot_commands.findaround(x_pos, z_pos, radius, dimension) + base_string = bot_commands.find_around(x_pos, z_pos, radius, dimension) if len(base_string) != 0: await bot.say('{}, the following locations(s) within **{}** blocks of that point: \n {}'.format( @@ -269,8 +269,8 @@ async def additem(ctx, item_name: str, quantity: int, diamond_price: int, * args try: shop_name = get_name(args) - bot_commands.additem(item_name, quantity, diamond_price, shop_name=shop_name, - discord_uuid=ctx.message.author.id) + bot_commands.add_item(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: @@ -317,9 +317,63 @@ async def info(ctx, * args): info_str = bot_commands.info(loc) await bot.say(info_str) except IndexError: - await bot.say('{}, no locations in the database match {}.'.format(ctx.message.author.mention, name)) + await bot.say('{}, no locations in the database match {}.'.format(ctx.message.author.mention, loc)) return + +@commands.cooldown(5, 60, commands.BucketType.user) +@bot.command(pass_context=True) +async def edit_pos(ctx, x_pos: int, y_pos: int, * args): + ''' + Edits the position of a location + + ?edit_pos [X Coordinate] [Z Coordinate] [Location Name] + ''' + try: + loc = get_name(args) + loc_str = bot_commands.edit_pos(x_pos, y_pos, loc, discord_uuid=ctx.message.author.id) + + await bot.say('{}, the following location has been updated: \n\n{}'.format(ctx.message.author.mention, loc_str)) + except LocationLookUpError: + await bot.say('{}, you do not have a location called **{}**.'.format( + ctx.message.author.mention, loc)) + + +@commands.cooldown(5, 60, commands.BucketType.user) +@bot.command(pass_context=True) +async def edit_tunnel(ctx, tunnel_color: str, tunnel_number: int, * args): + ''' + Edits the tunnel of a location + + ?edit_tunnel [Tunnel Color] [Tunnel Number] [Location Name] + ''' + try: + loc = get_name(args) + loc_str = bot_commands.edit_tunnel(tunnel_color, tunnel_number, loc, discord_uuid=ctx.message.author.id) + + await bot.say('{}, the following location has been updated: \n\n{}'.format(ctx.message.author.mention, loc_str)) + except LocationLookUpError: + await bot.say('{}, you do not have a location called **{}**.'.format( + ctx.message.author.mention, loc)) + +@commands.cooldown(5, 60, commands.BucketType.user) +@bot.command(pass_context=True) +async def edit_name(ctx, new_name: str, current_name: str): + ''' + Edits the name of a location + + IF A NAME HAS SPACES IN IT YOU NEED TO WRAP IT IN QUOTATION MARKS. ie "Cool Shop 123" + ?edit_name [New Name] [Current Name] + ''' + try: + loc_str = bot_commands.edit_name(new_name, current_name, discord_uuid=ctx.message.author.id) + + await bot.say('{}, the following location has been updated: \n\n{}'.format(ctx.message.author.mention, loc_str)) + except LocationLookUpError: + await bot.say('{}, you do not have a location called **{}**.'.format( + ctx.message.author.mention, current_name)) + + # Helper Functions ************************************************************ def get_name(args): @@ -344,7 +398,7 @@ def get_nickname(discord_user): def get_args_dict(args): if len(args) != 0: - return dict(zip_longest(*[iter(args)] * 2, fillvalue="")) + return dict(zip_longest(*[iter(args)] * 2, fillvalue=" ")) else: return {} diff --git a/test_commands.py b/test_commands.py index e95d8fb..16fc66c 100644 --- a/test_commands.py +++ b/test_commands.py @@ -33,7 +33,7 @@ class TestCommands(TestCase): def test_addbase(self): player_name = self.commands.register('ZeroHD', '143072699567177728') - base = self.commands.addbase(0, 0, discord_uuid='143072699567177728') + base = self.commands.add_base(0, 0, discord_uuid='143072699567177728') if player_name not in base: self.fail() @@ -42,7 +42,7 @@ class TestCommands(TestCase): def test_addshop(self): player_name = self.commands.register('ZeroHD', '143072699567177728') - shop = self.commands.addshop(0, 0, discord_uuid='143072699567177728') + shop = self.commands.add_shop(0, 0, discord_uuid='143072699567177728') if player_name not in shop: self.fail() @@ -51,24 +51,24 @@ class TestCommands(TestCase): def test_addtunnel(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') - tunnel1 = self.commands.addtunnel('green', 50, None, discord_uuid='143072699567177728') + tunnel1 = self.commands.add_tunnel('green', 50, None, discord_uuid='143072699567177728') self.assertGreater(len(tunnel1), 0) - tunnel2 = self.commands.addtunnel('Green', 50, location_name='test_shop', discord_uuid='143072699567177728') + tunnel2 = self.commands.add_tunnel('Green', 50, location_name='test_shop', discord_uuid='143072699567177728') if 'Green' not in tunnel2: self.fail() - self.assertRaises(LocationHasTunnelError, self.commands.addtunnel, 'Blue', 50, location_name='test_shop', - discord_uuid='143072699567177728') + self.assertRaises(LocationHasTunnelError, self.commands.add_tunnel, 'Blue', 50, location_name='test_shop', + discord_uuid='143072699567177728') def test_find(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') - self.commands.addbase(0, 0, 'heck', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='frick', discord_uuid='143072699567177728') + self.commands.add_base(0, 0, 'heck', discord_uuid='143072699567177728') result = self.commands.find('zerohd') @@ -79,7 +79,7 @@ class TestCommands(TestCase): def test_delete(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='frick', discord_uuid='143072699567177728') self.commands.delete('frick', discord_uuid='143072699567177728') @@ -87,9 +87,9 @@ class TestCommands(TestCase): def test_findaround(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='frick', discord_uuid='143072699567177728') - result = self.commands.findaround(0, 0) + result = self.commands.find_around(0, 0) if 'frick' in result: pass @@ -98,18 +98,18 @@ class TestCommands(TestCase): def test_additem(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, discord_uuid='143072699567177728') - result = self.commands.additem('dirt', 5, 5, None, discord_uuid='143072699567177728') + result = self.commands.add_item('dirt', 5, 5, None, discord_uuid='143072699567177728') if 'dirt' in result: pass else: self.fail() - self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='frick', discord_uuid='143072699567177728') - result = self.commands.additem('cool', 5, 5, shop_name='frick', discord_uuid='143072699567177728') + result = self.commands.add_item('cool', 5, 5, shop_name='frick', discord_uuid='143072699567177728') if 'cool' in result: pass @@ -118,9 +118,9 @@ class TestCommands(TestCase): def test_selling(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='frick', discord_uuid='143072699567177728') - self.commands.additem('cool', 5, 5, shop_name='frick', discord_uuid='143072699567177728') + self.commands.add_item('cool', 5, 5, shop_name='frick', discord_uuid='143072699567177728') result = self.commands.selling('cool') @@ -131,9 +131,9 @@ class TestCommands(TestCase): def test_info(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_str='frick', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='frick', discord_uuid='143072699567177728') - self.commands.addtunnel('Green', 50, location_name='frick', discord_uuid='143072699567177728') + self.commands.add_tunnel('Green', 50, location_name='frick', discord_uuid='143072699567177728') result = self.commands.info('frick') @@ -144,9 +144,9 @@ class TestCommands(TestCase): def test_tunnel(self): self.commands.register('ZeroHD', '143072699567177728') - self.commands.addshop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') - tunnel = self.commands.addtunnel('green', 50, None, discord_uuid='143072699567177728') + tunnel = self.commands.add_tunnel('green', 50, None, discord_uuid='143072699567177728') result = self.commands.tunnel('ZeroHD') @@ -155,4 +155,43 @@ class TestCommands(TestCase): else: self.fail() + def test_edit_name(self): + self.commands.register('ZeroHD', '143072699567177728') + self.commands.add_shop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') + + self.commands.edit_name('cool shop', 'test shop', discord_uuid='143072699567177728') + + result = self.commands.info('cool shop') + + if 'cool' in result: + pass + else: + self.fail() + + def test_edit_pos(self): + self.commands.register('ZeroHD', '143072699567177728') + self.commands.add_shop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') + + self.commands.edit_pos(500, 500, 'test shop', discord_uuid='143072699567177728') + + result = self.commands.info('test shop') + + if '500' in result: + pass + else: + self.fail() + + def test_edit_tunnel(self): + self.commands.register('ZeroHD', '143072699567177728') + self.commands.add_shop(0, 0, shop_str='test shop', discord_uuid='143072699567177728') + + self.commands.edit_tunnel('green', 500, 'test shop', discord_uuid='143072699567177728') + + result = self.commands.info('test shop') + + if 'Green' in result: + pass + else: + self.fail() + diff --git a/test_geoffreyDatabase.py b/test_geoffreyDatabase.py index bdff309..5042d8b 100644 --- a/test_geoffreyDatabase.py +++ b/test_geoffreyDatabase.py @@ -31,7 +31,7 @@ class TestGeoffreyDatabase(TestCase): return player def add_loc(self, player): - loc = self.commands.interface.add_location(self.session, player, 'test', 0, 0) + loc = self.commands.interface.add_base(self.session, player, 'test', 0, 0) return loc def test_add_object(self): @@ -205,7 +205,7 @@ class TestGeoffreyDatabase(TestCase): def test_big_input(self): owner = self.add_player() - self.assertRaises(DatabaseValueError, self.commands.interface.add_location, self.session, owner, + self.assertRaises(DatabaseValueError, self.commands.interface.add_base, self.session, owner, 'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT' 'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT' 'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT', 0, 0,) @@ -214,7 +214,7 @@ class TestGeoffreyDatabase(TestCase): owner = self.add_player() self.add_loc(owner) - self.assertRaises(EntryNameNotUniqueError, self.commands.interface.add_location, self.session, + self.assertRaises(EntryNameNotUniqueError, self.commands.interface.add_base, self.session, owner, 'test', 0, 0, 0) def test_delete_parent(self):