diff --git a/DatabaseModels.py b/DatabaseModels.py index 3795560..f884194 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -31,6 +31,13 @@ class GeoffreyDatabase: self.add_object(shop) return shop + def add_item(self, player_name, shop_name, item_name, price): + shop = self.find_location_by_name_and_owner(player_name, shop_name) + + item = ItemListing(item_name, price, shop[0]) + + return item + def add_player(self, player_name): expr = Player.name == player_name player_list = self.query_by_filter(Player, expr) @@ -61,12 +68,30 @@ class GeoffreyDatabase: expr = Location.owner == player return self.query_by_filter(Location, expr) + def find_location_by_name_and_owner(self, owner_name, name): + player = self.add_player(owner_name) + expr = (Location.owner == player) & (Location.name == name) + return self.query_by_filter(Location, expr) + 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) return self.query_by_filter(Location, expr) + def find_item(self, item_name): + expr = ItemListing.name == item_name + return self.query_by_filter(ItemListing, expr) + + def find_shop_selling_item(self, item_name): + listings = self.find_item(item_name) + + shops = [] + for listing in listings: + shops.append(listing.shop) + + return shops + def query_by_filter(self, obj_type, * args): filter_value = self.combine_filter(args) return self.session.query(obj_type).filter(filter_value).all() @@ -184,11 +209,9 @@ class Location(SQL_Base): class Shop(Location): __tablename__ = 'Shops' - shop_id = Column(Integer, ForeignKey('Locations.id'), primary_key=True) name = Column(String) - inventory = relationship('ItemListing', back_populates='shop') - + inventory = relationship('ItemListing', back_populates='shop', lazy='dynamic') __mapper_args__ = { 'polymorphic_identity': 'Shop', } @@ -200,17 +223,17 @@ class Shop(Location): class ItemListing(SQL_Base): __tablename__ = 'Items' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String) price = Column(Integer) - shop_id = Column(Integer, ForeignKey('Locations.id')) + shop_id = Column(Integer, ForeignKey('Shops.shop_id')) + shop = relationship("Shop", back_populates="inventory") - shop = relationship('Shop', back_populates='inventory') - - def __init__(self, name, price) : + def __init__(self, name, price, shop): self.name = name self.price = price + self.shop = shop def __str__(self): return "Item: {}, Price: {}".format(self.name, self.price) diff --git a/Geoffrey.py b/Geoffrey.py index d16c68c..813de45 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -101,7 +101,7 @@ async def find(ctx, name: str): base_list = database.find_location_by_owner(name) if len(base_list) != 0: - base_string = base_list_string(base_list, '{} \n{}') + base_string = loc_list_to_string(base_list, '{} \n{}') await bot.say('{}, {} has {} base(s): \n {}'.format(ctx.message.author.mention, name, len(base_list), base_string)) @@ -142,7 +142,7 @@ async def findaround(ctx, x_pos: int, z_pos: int, * args): base_list = database.find_location_around(x_pos, z_pos, radius) if len(base_list) != 0: - base_string = base_list_string(base_list, '{} \n{}') + base_string = loc_list_to_string(base_list, '{} \n{}') await bot.say('{}, there are {} base(s) within {} blocks of that point: \n {}'.format( ctx.message.author.mention, len(base_list), radius, base_string)) @@ -150,6 +150,24 @@ async def findaround(ctx, x_pos: int, z_pos: int, * args): await bot.say('{}, there are no bases within {} blocks of that point' .format(ctx.message.author.mention, radius)) + +@bot.command(pass_context=True) +async def additem(ctx, shop_name: str, item_name: str, diamond_price: int): + player_name = get_nickname(ctx.message.author) + database.add_item(player_name, shop_name, item_name, diamond_price) + + await bot.say('{}, {} has been added to the inventory of {}.'.format(ctx.message.author.mention, + item_name, shop_name)) + + +@bot.command(pass_context=True) +async def selling(ctx, item_name: str): + shop_list = database.find_shop_selling_item(item_name) + + shop_list_str = loc_list_to_string(shop_list) + await bot.say('The following shops sell {}: \n {}'.format(item_name, shop_list_str)) + + # Helper Functions ************************************************************ @@ -160,13 +178,13 @@ def get_nickname(discord_user) : return discord_user.nick -def base_list_string(base_list, str_format): - base_string = '' +def loc_list_to_string(loc_list, str_format='{}\n{}'): + loc_string = '' - for base in base_list: - base_string = str_format.format(base_string, base) + for loc in loc_list: + loc_string = str_format.format(loc_string, loc) - return base_string + return loc_string # Bot Startup ****************************************************************** diff --git a/test_geoffreyDatabase.py b/test_geoffreyDatabase.py index 9e4e9a2..d65a904 100644 --- a/test_geoffreyDatabase.py +++ b/test_geoffreyDatabase.py @@ -16,6 +16,13 @@ class TestGeoffreyDatabase(TestCase): self.assertEqual(type(shop), Shop) + def test_add_item(self): + self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0]) + self.database.add_item('ZeroHD', 'test', 'dirt', 1) + + shops = self.database.find_shop_selling_item('dirt') + self.assertEqual(shops[0].name, 'test') + def test_add_object(self): self.database.add_object(self.loc) self.database.add_object(self.owner)