Fixed case sensitivity and added a shopinfo command. (Resolves BackLog entries 1 and 4)

doc_update
Joey Hines 2018-07-01 10:10:10 -05:00
parent 933b7c7ba0
commit 1783c58e2a
3 changed files with 65 additions and 11 deletions

View File

@ -32,9 +32,10 @@ class GeoffreyDatabase:
def add_item(self, player_name, shop_name, item_name, price): def add_item(self, player_name, shop_name, item_name, price):
try: try:
shop = self.find_location_by_name_and_owner(player_name, shop_name) shop = self.find_shop_by_name_and_owner(player_name, shop_name)
item = ItemListing(item_name, price, shop[0]) item = ItemListing(item_name, price, shop[0])
self.add_object(item)
except IndexError: except IndexError:
raise LocationLookUpError raise LocationLookUpError
@ -63,11 +64,24 @@ class GeoffreyDatabase:
self.session.add(obj) self.session.add(obj)
self.session.commit() self.session.commit()
def find_location_by_name(self, name):
expr = Location.name.like('%{}%'.format(name))
return self.query_by_filter(Location, expr)
def find_shop_by_name(self, name):
expr = Location.name.like('%{}%'.format(name))
return self.query_by_filter(Shop, expr)
def find_location_by_owner(self, owner_name): def find_location_by_owner(self, owner_name):
player = self.find_player(owner_name) player = self.find_player(owner_name)
expr = Location.owner == player expr = Location.owner == player
return self.query_by_filter(Location, expr) return self.query_by_filter(Location, expr)
def find_shop_by_name_and_owner(self, owner_name, name):
player = self.find_player(owner_name)
expr = (Shop.owner == player) & (Shop.name == name)
return self.query_by_filter(Shop, expr)
def find_location_by_name_and_owner(self, owner_name, name): def find_location_by_name_and_owner(self, owner_name, name):
player = self.find_player(owner_name) player = self.find_player(owner_name)
expr = (Location.owner == player) & (Location.name == name) expr = (Location.owner == player) & (Location.name == name)
@ -80,7 +94,7 @@ class GeoffreyDatabase:
return self.query_by_filter(Location, expr) return self.query_by_filter(Location, expr)
def find_item(self, item_name): def find_item(self, item_name):
expr = ItemListing.name == item_name expr = ItemListing.name.like(item_name)
return self.query_by_filter(ItemListing, expr) return self.query_by_filter(ItemListing, expr)
def find_shop_selling_item(self, item_name): def find_shop_selling_item(self, item_name):
@ -93,7 +107,7 @@ class GeoffreyDatabase:
return shops return shops
def find_player(self, player_name): def find_player(self, player_name):
expr = func.lower(Player.name) == func.lower(player_name) expr = Player.name.like(player_name)
try: try:
player = self.query_by_filter(Player, expr)[0] player = self.query_by_filter(Player, expr)[0]
@ -112,6 +126,11 @@ class GeoffreyDatabase:
return player return player
def get_shop_inventory(self, shop):
expr = ItemListing.shop == shop
return self.query_by_filter(ItemListing, expr)
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()
@ -180,7 +199,7 @@ class Location(SQL_Base):
__tablename__ = 'Locations' __tablename__ = 'Locations'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
name = Column(String) name = Column(String, unique=True)
x = Column(Integer) x = Column(Integer)
y = Column(Integer) y = Column(Integer)
z = Column(Integer) z = Column(Integer)
@ -253,4 +272,4 @@ class ItemListing(SQL_Base):
self.shop = shop self.shop = shop
def __str__(self): def __str__(self):
return "Item: {}, Price: {}".format(self.name, self.price) return "Item: {}, Price: {}D".format(self.name, self.price)

View File

@ -3,6 +3,7 @@ from DatabaseModels import *
from BotErrors import * from BotErrors import *
from MinecraftAccountInfoGrabber import * from MinecraftAccountInfoGrabber import *
import configparser import configparser
#from WebInterface import *
TOKEN = '' TOKEN = ''
command_prefix = '?' command_prefix = '?'
@ -10,7 +11,7 @@ description = '''
Geoffrey started his life as inside joke none of you will understand. Geoffrey started his life as inside joke none of you will understand.
At some point, she was to become an airhorn bot. Now, they know where your bases/shops are. At some point, she was to become an airhorn bot. Now, they know where your bases/shops are.
Please respect Geoffrey, the bot is very sensitive.w Please respect Geoffrey, the bot is very sensitive.
''' '''
bad_error_message = 'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our ' \ bad_error_message = 'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our ' \
@ -177,8 +178,23 @@ async def selling(ctx, item_name: str):
shop_list_str = loc_list_to_string(shop_list) shop_list_str = loc_list_to_string(shop_list)
await bot.say('The following shops sell {}: \n {}'.format(item_name, shop_list_str)) await bot.say('The following shops sell {}: \n {}'.format(item_name, shop_list_str))
# Helper Functions ************************************************************
@bot.command(pass_context=True)
async def shopinfo(ctx, shop_name: str):
'''
Lists the information and inventory of a shop
?shopinfo [Shop Name]
'''
shop = database.find_shop_by_name(shop_name)[0]
inv_list = database.get_shop_inventory(shop)
item_list = ''
for item in inv_list:
item_list = item_list + '{}\n'.format(item.__str__())
await bot.say('{} \n Inventory:\n {}'.format(shop.__str__(), item_list))
# Helper Functions ************************************************************
def get_nickname(discord_user): def get_nickname(discord_user):
if discord_user.nick is None: if discord_user.nick is None:
@ -202,7 +218,6 @@ def loc_list_to_string(loc_list, str_format='{}\n{}'):
def create_config(): def create_config():
'sqlite:///:memory:'
config['Discord'] = {'Token': ''} config['Discord'] = {'Token': ''}
config['SQL'] = {'Dialect+Driver': 'test', 'username': '', 'password':'', 'host': '', 'port': '', 'database':''} config['SQL'] = {'Dialect+Driver': 'test', 'username': '', 'password':'', 'host': '', 'port': '', 'database':''}
@ -237,10 +252,11 @@ else:
TOKEN = config['Discord']['Token'] TOKEN = config['Discord']['Token']
if config['SQL']['dialect+driver'] == 'Test': if config['SQL']['dialect+driver'] == 'Test':
engine_arg = 'sqlite:///:memory:' engine_arg = 'sqlite:///temp.db'
else: else:
engine_arg = get_engine_arg(config) engine_arg = get_engine_arg(config)
database = GeoffreyDatabase(engine_arg) database = GeoffreyDatabase(engine_arg)
#WebInterface('127.0.0.1', 8081, database)
bot.run(TOKEN) bot.run(TOKEN)

View File

@ -9,7 +9,6 @@ class TestGeoffreyDatabase(TestCase):
self.database = GeoffreyDatabase('sqlite:///:memory:') self.database = GeoffreyDatabase('sqlite:///:memory:')
self.owner = Player('ZeroHD') self.owner = Player('ZeroHD')
self.loc = Location('test', 1, 2, 3, self.owner, ['Green', 0]) 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_object(self): def test_add_object(self):
self.database.add_object(self.loc) self.database.add_object(self.loc)
@ -97,6 +96,13 @@ class TestGeoffreyDatabase(TestCase):
self.assertEqual(len(loc_list), 0) self.assertEqual(len(loc_list), 0)
def test_find_location_by_name(self):
loc = self.database.add_location('ZeroHD', 'test', 0, 0, 0, ['Green', 0])
loc_list = self.database.find_location_by_name('test')
self.assertEqual(loc_list[0].name, loc.name)
def test_wrong_case(self): def test_wrong_case(self):
loc = self.database.add_location('ZeroHD', 'test', 0, 0, 0, ['Green', 0]) loc = self.database.add_location('ZeroHD', 'test', 0, 0, 0, ['Green', 0])
@ -104,6 +110,19 @@ class TestGeoffreyDatabase(TestCase):
self.assertEqual(loc_list[0].id, loc.id) self.assertEqual(loc_list[0].id, loc.id)
self.database.add_shop('ZeroHD', 'testshop', 1, 2, 3, ['Green', 0])
self.database.add_item('ZeroHD', 'testshop', 'dirt', 1)
shops = self.database.find_shop_selling_item('Dirt')
self.assertEqual(shops[0].name, 'testshop')
loc_list = self.database.find_location_by_name('TEST')
self.assertEqual(loc_list[0].name, 'test')