Added commands to add items to shop inventories and to lookup items for sale

doc_update
Joey Hines 2018-06-23 15:27:00 -05:00
parent 484f1ed260
commit 08bc344922
3 changed files with 63 additions and 15 deletions

View File

@ -31,6 +31,13 @@ class GeoffreyDatabase:
self.add_object(shop) self.add_object(shop)
return 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): def add_player(self, player_name):
expr = Player.name == player_name expr = Player.name == player_name
player_list = self.query_by_filter(Player, expr) player_list = self.query_by_filter(Player, expr)
@ -61,12 +68,30 @@ class GeoffreyDatabase:
expr = Location.owner == player expr = Location.owner == player
return self.query_by_filter(Location, expr) 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): 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) & \ expr = (Location.x < x_pos + radius) & (Location.x > x_pos - radius) & (Location.z < z_pos + radius) & \
(Location.z > z_pos - radius) (Location.z > z_pos - radius)
return self.query_by_filter(Location, expr) 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): def query_by_filter(self, obj_type, * args):
filter_value = self.combine_filter(args) filter_value = self.combine_filter(args)
return self.session.query(obj_type).filter(filter_value).all() return self.session.query(obj_type).filter(filter_value).all()
@ -184,11 +209,9 @@ class Location(SQL_Base):
class Shop(Location): class Shop(Location):
__tablename__ = 'Shops' __tablename__ = 'Shops'
shop_id = Column(Integer, ForeignKey('Locations.id'), primary_key=True) shop_id = Column(Integer, ForeignKey('Locations.id'), primary_key=True)
name = Column(String) name = Column(String)
inventory = relationship('ItemListing', back_populates='shop') inventory = relationship('ItemListing', back_populates='shop', lazy='dynamic')
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': 'Shop', 'polymorphic_identity': 'Shop',
} }
@ -200,17 +223,17 @@ class Shop(Location):
class ItemListing(SQL_Base): class ItemListing(SQL_Base):
__tablename__ = 'Items' __tablename__ = 'Items'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String) name = Column(String)
price = Column(Integer) 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, shop):
def __init__(self, name, price) :
self.name = name self.name = name
self.price = price self.price = price
self.shop = shop
def __str__(self): def __str__(self):
return "Item: {}, Price: {}".format(self.name, self.price) return "Item: {}, Price: {}".format(self.name, self.price)

View File

@ -101,7 +101,7 @@ async def find(ctx, name: str):
base_list = database.find_location_by_owner(name) base_list = database.find_location_by_owner(name)
if len(base_list) != 0: 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), await bot.say('{}, {} has {} base(s): \n {}'.format(ctx.message.author.mention, name, len(base_list),
base_string)) 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) base_list = database.find_location_around(x_pos, z_pos, radius)
if len(base_list) != 0: 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( await bot.say('{}, there are {} base(s) within {} blocks of that point: \n {}'.format(
ctx.message.author.mention, len(base_list), radius, base_string)) 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' await bot.say('{}, there are no bases within {} blocks of that point'
.format(ctx.message.author.mention, radius)) .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 ************************************************************ # Helper Functions ************************************************************
@ -160,13 +178,13 @@ def get_nickname(discord_user) :
return discord_user.nick return discord_user.nick
def base_list_string(base_list, str_format): def loc_list_to_string(loc_list, str_format='{}\n{}'):
base_string = '' loc_string = ''
for base in base_list: for loc in loc_list:
base_string = str_format.format(base_string, base) loc_string = str_format.format(loc_string, loc)
return base_string return loc_string
# Bot Startup ****************************************************************** # Bot Startup ******************************************************************

View File

@ -16,6 +16,13 @@ class TestGeoffreyDatabase(TestCase):
self.assertEqual(type(shop), Shop) 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): def test_add_object(self):
self.database.add_object(self.loc) self.database.add_object(self.loc)
self.database.add_object(self.owner) self.database.add_object(self.owner)