diff --git a/DatabaseModels.py b/DatabaseModels.py index 8b2a5cc..3795560 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -25,6 +25,12 @@ class GeoffreyDatabase: return base + def add_shop(self, player_name, name, x_pos, y_pos, z_pos, args): + owner = self.add_player(player_name) + shop = Shop(name, x_pos, y_pos, z_pos, owner, args) + self.add_object(shop) + return shop + def add_player(self, player_name): expr = Player.name == player_name player_list = self.query_by_filter(Player, expr) @@ -50,12 +56,12 @@ class GeoffreyDatabase: self.session.add(obj) self.session.commit() - def find_base_by_owner(self, owner_name): + def find_location_by_owner(self, owner_name): player = self.add_player(owner_name) expr = Location.owner == player return self.query_by_filter(Location, expr) - def find_base_around(self, x_pos, z_pos, radius): + def find_location_around(self, x_pos, z_pos, radius): expr = (Location.x < x_pos + radius) & (Location.x > x_pos - radius) & (Location.z < z_pos + radius) & \ (Location.z > z_pos - radius) @@ -89,7 +95,6 @@ class GeoffreyDatabase: s = s + '\n' + obj.id return s - def combine_filter(self, filter_value): return sqlalchemy.sql.expression.and_(filter_value[0]) @@ -141,13 +146,12 @@ class Location(SQL_Base): direction = Column(Enum(TunnelDirection)) owner_id = Column(Integer, ForeignKey('Players.id')) owner = relationship("Player", back_populates="locations") - #type = Column(String) - ''' + type = Column(String) + __mapper_args__ = { 'polymorphic_on': type, 'polymorphic_identity': 'Location' } - ''' def __init__(self, name, x, y, z, owner, args): try: @@ -176,21 +180,21 @@ class Location(SQL_Base): self.nether_tunnel_addr_to_str()) else: return "Name: {}, Position: {}".format(self.name, self.pos_to_str()) -''' + class Shop(Location): __tablename__ = 'Shops' - id = Column(Integer, ForeignKey('Locations.id'), primary_key=True) + shop_id = Column(Integer, ForeignKey('Locations.id'), primary_key=True) name = Column(String) inventory = relationship('ItemListing', back_populates='shop') __mapper_args__ = { - 'polymorphic_identity': 'Shop' + 'polymorphic_identity': 'Shop', } def __init__(self, name, x, y, z, owner, args): - Location.__init__(name, x, y, z, owner, args) + Location.__init__(self, name, x, y, z, owner, args) class ItemListing(SQL_Base): @@ -200,7 +204,7 @@ class ItemListing(SQL_Base): name = Column(String) price = Column(Integer) - shop_id = Column(Integer, ForeignKey('Shops.id')) + shop_id = Column(Integer, ForeignKey('Locations.id')) shop = relationship('Shop', back_populates='inventory') @@ -210,4 +214,3 @@ class ItemListing(SQL_Base): def __str__(self): return "Item: {}, Price: {}".format(self.name, self.price) -''' \ No newline at end of file diff --git a/Geoffrey.py b/Geoffrey.py index 0a2e336..d16c68c 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -72,15 +72,33 @@ async def addbase(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args): await bot.say('{}, your base named {} located at {} has been added' ' to the database.'.format(ctx.message.author.mention, base.name, base.pos_to_str())) +@bot.command(pass_context=True) +async def addshop(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args): + ''' + Add your base to the database. + The tunnel address is optional. + ?addbase [Base Name] [X Coordinate] [Y Coordinate] [Z Coordinate] [Tunnel Color] [Tunnel Position] + ''' + + player_name = get_nickname(ctx.message.author) + + try: + base = database.add_shop(player_name, name, x_pos, y_pos, z_pos, args) + except LocationInitError: + raise commands.UserInputError + + await bot.say('{}, your shop named {} located at {} has been added' + ' to the database.'.format(ctx.message.author.mention, base.name, base.pos_to_str())) + @bot.command(pass_context=True) -async def findbase(ctx, name: str): +async def find(ctx, name: str): ''' Finds a base in the database. ?findbase [Player name] ''' - base_list = database.find_base_by_owner(name) + base_list = database.find_location_by_owner(name) if len(base_list) != 0: base_string = base_list_string(base_list, '{} \n{}') @@ -92,7 +110,7 @@ async def findbase(ctx, name: str): @bot.command(pass_context=True) -async def deletebase(ctx, name: str): +async def delete(ctx, name: str): ''' Deletes a base from the database. ?deletebase [Base name] @@ -107,7 +125,7 @@ async def deletebase(ctx, name: str): @bot.command(pass_context=True) -async def findbasearound(ctx, x_pos: int, z_pos: int, * args): +async def findaround(ctx, x_pos: int, z_pos: int, * args): ''' Finds all the base around a certain point that are registered in the database The Radius argument defaults to 200 blocks if no value is given @@ -121,7 +139,7 @@ async def findbasearound(ctx, x_pos: int, z_pos: int, * args): except ValueError: raise commands.UserInputError - base_list = database.find_base_around(x_pos, z_pos, radius) + base_list = database.find_location_around(x_pos, z_pos, radius) if len(base_list) != 0: base_string = base_list_string(base_list, '{} \n{}') diff --git a/test_geoffreyDatabase.py b/test_geoffreyDatabase.py index 41ffdca..9e4e9a2 100644 --- a/test_geoffreyDatabase.py +++ b/test_geoffreyDatabase.py @@ -1,6 +1,5 @@ from unittest import TestCase -from DatabaseModels import GeoffreyDatabase -from DatabaseModels import Location, Player +from DatabaseModels import * from BotErrors import * from MinecraftAccountInfoGrabber import * @@ -10,6 +9,12 @@ class TestGeoffreyDatabase(TestCase): self.database = GeoffreyDatabase('sqlite:///:memory:') self.owner = Player('ZeroHD') self.loc = Location('test', 1, 2, 3, self.owner, ['Green', 0]) + #self.shop = Location('test', 1, 2, 3, self.owner, ['Green', 0]) + + def test_add_shop(self): + shop = self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0]) + + self.assertEqual(type(shop), Shop) def test_add_object(self): self.database.add_object(self.loc)