?selling now groups items sold at the same shop together.

doc_update
Joey Hines 2018-09-25 09:39:30 -05:00
parent 930d9bbd8c
commit 2dab55ab78
5 changed files with 46 additions and 4 deletions

View File

@ -176,12 +176,15 @@ class Commands:
session = self.interface.database.Session()
try:
shop_list = self.interface.find_shop_selling_item(session, item_name)
shop_list = self.interface.find_top_shops_selling_item(session, item_name)
if len(shop_list) == 0:
raise ItemNotFound
shop_list_str = list_to_string(shop_list)
shop_list_str = ""
for shop in shop_list:
shop_list_str = shop_list_str + shop[0].selling_str() + list_to_string(
self.interface.get_inventory_matches(session, shop[0], item_name)) + '\n\n'
finally:
session.close()

View File

@ -1,3 +1,4 @@
from sqlalchemy import func
from geoffrey.DatabaseModels import *
@ -106,6 +107,24 @@ class DatabaseInterface:
def find_shop_selling_item(self, session, item_name):
return self.find_item(session, item_name)
def find_top_shops_selling_item(self, session, item_name):
expr = ItemListing.name.ilike('%{}%'.format(item_name))
result = session.query(func.min(ItemListing.normalized_price), ItemListing.shop_id).group_by(
ItemListing.shop_id).filter(expr).order_by(func.min(ItemListing.normalized_price)).all()
shop_list = []
for item in result:
expr = Shop.shop_id == item[1]
shop = self.database.query_by_filter(session, Shop, expr, limit=10)
shop_list.append(shop)
return shop_list
def get_inventory_matches(self, session, shop, item_name):
expr = (ItemListing.shop_id == shop.id) & (ItemListing.name.ilike('%{}%'.format(item_name)))
return self.database.query_by_filter(session, ItemListing, expr, limit=5, sort=ItemListing.normalized_price)
def find_player(self, session, player_name):
expr = Player.name.ilike(player_name)

View File

@ -267,6 +267,9 @@ class Shop(Location):
def full_str(self, bot_config):
return Location.full_str(self, bot_config) + self.inv_to_str()
def selling_str(self):
return "**{}** @ {}, Owner: **{}**".format(self.name, self.pos_to_str(), self.owner.name)
def __str__(self):
return Location.__str__(self)
@ -303,4 +306,4 @@ class ItemListing(SQL_Base):
return '**{}** **{}** for **{}D**'.format(self.amount, self.name, self.price)
def __str__(self):
return '**{}** @ {}, selling {}'.format(self.shop.name, self.shop.pos_to_str(), self.listing_str())
return self.listing_str()

View File

@ -172,10 +172,13 @@ class TestCommands(TestCase):
result = self.commands.selling('cool')
print(result)
'''
if 'cool' in result:
pass
else:
self.fail()
'''
def test_info(self):
self.commands.register(zerohd, '143072699567177728')
@ -347,4 +350,4 @@ class TestCommands(TestCase):
if zerohd in self.commands.find_player('143072699567177728'):
pass
else:
self.fail()
self.fail()

View File

@ -22,6 +22,20 @@ class TestGeoffreyDatabase(TestCase):
self.session.commit()
self.session.close()
def test_find_top_shops_selling_items(self):
owner = self.add_player()
self.add_shop(owner)
self.interface.add_item(self.session, owner, 'test', 'dirt', 1, 15)
self.interface.add_item(self.session, owner, 'test', 'dirt', 1, 5)
self.interface.add_loc(self.session, owner, 'test2', 1, 3, "nether", loc_type=Shop)
self.interface.add_item(self.session, owner, 'test2', 'dirt', 1, 5)
shop_list = self.interface.find_top_shops_selling_item(self.session, "Dirt")
for shop in shop_list:
item_list = self.interface.get_inventory_matches(self.session, shop[0], "dirt")
print(item_list)
def add_shop(self, player):
shop = self.interface.add_loc(self.session, player, 'test', 1, 3, "nether", loc_type=Shop)
return shop