2018-07-22 12:52:49 +00:00
|
|
|
from DatabaseModels import *
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseInterface:
|
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
def __init__(self, engine_args=None):
|
|
|
|
if engine_args is None:
|
|
|
|
self.database = GeoffreyDatabase()
|
|
|
|
else:
|
|
|
|
self.database = GeoffreyDatabase(engine_args)
|
2018-07-22 12:52:49 +00:00
|
|
|
|
2018-08-01 00:22:17 +00:00
|
|
|
def add_base(self, session, owner, name, x_pos, z_pos, dimension=None):
|
|
|
|
base = Base(name, x_pos, z_pos, owner, dimension)
|
|
|
|
self.database.add_object(session, base)
|
|
|
|
return base
|
2018-07-22 12:52:49 +00:00
|
|
|
|
|
|
|
def add_shop(self, session, owner, name, x_pos, z_pos, dimension=None):
|
|
|
|
shop = Shop(name, x_pos, z_pos, owner, dimension)
|
|
|
|
self.database.add_object(session, shop)
|
|
|
|
return shop
|
|
|
|
|
|
|
|
def add_tunnel(self, session, owner, color, number, location_name):
|
2018-07-29 13:59:11 +00:00
|
|
|
tunnels = self.find_tunnel_by_owner(session, owner)
|
2018-07-22 12:52:49 +00:00
|
|
|
if location_name is None:
|
2018-07-29 13:59:11 +00:00
|
|
|
if len(tunnels):
|
2018-07-22 12:52:49 +00:00
|
|
|
raise EntryNameNotUniqueError
|
|
|
|
else:
|
|
|
|
location = None
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
location = self.find_location_by_name_and_owner(session, owner, location_name)[0]
|
2018-07-29 13:59:11 +00:00
|
|
|
|
|
|
|
if location.tunnel is not None:
|
|
|
|
raise LocationHasTunnelError
|
|
|
|
|
2018-07-22 12:52:49 +00:00
|
|
|
except IndexError:
|
|
|
|
raise LocationLookUpError
|
|
|
|
|
|
|
|
tunnel = Tunnel(owner, color, number, location)
|
|
|
|
self.database.add_object(session, tunnel)
|
2018-07-29 13:59:11 +00:00
|
|
|
|
2018-07-22 12:52:49 +00:00
|
|
|
return tunnel
|
|
|
|
|
|
|
|
def add_item(self, session, owner, shop_name, item_name, price, amount):
|
|
|
|
try:
|
|
|
|
shop = self.find_shop_by_name_and_owner(session, owner, shop_name)
|
|
|
|
|
|
|
|
item = ItemListing(item_name, price, amount, shop[0])
|
|
|
|
self.database.add_object(session, item)
|
|
|
|
except IndexError:
|
|
|
|
raise LocationLookUpError
|
|
|
|
|
|
|
|
return item
|
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
def add_player(self, session, player_name, discord_uuid=None):
|
2018-07-22 12:52:49 +00:00
|
|
|
try:
|
|
|
|
player = self.find_player(session, player_name)
|
|
|
|
except PlayerNotFound:
|
2018-07-22 23:56:07 +00:00
|
|
|
mc_uuid = grab_UUID(player_name)
|
2018-07-22 12:52:49 +00:00
|
|
|
try:
|
2018-07-22 23:56:07 +00:00
|
|
|
player = self.find_player_by_mc_uuid(session, mc_uuid)
|
|
|
|
player.name = player_name
|
2018-07-22 12:52:49 +00:00
|
|
|
except PlayerNotFound:
|
2018-07-22 23:56:07 +00:00
|
|
|
player = Player(player_name, discord_uuid)
|
2018-07-22 12:52:49 +00:00
|
|
|
self.database.add_object(session, player)
|
|
|
|
|
|
|
|
return player
|
|
|
|
|
|
|
|
def find_location_by_name(self, session, name):
|
|
|
|
expr = Location.name.ilike('%{}%'.format(name))
|
|
|
|
return self.database.query_by_filter(session, Location, expr)
|
|
|
|
|
|
|
|
def find_shop_by_name(self, session, name):
|
|
|
|
expr = Location.name.ilike('%{}%'.format(name))
|
|
|
|
return self.database.query_by_filter(session, Shop, expr)
|
|
|
|
|
|
|
|
def find_location_by_owner(self, session, owner):
|
|
|
|
expr = Location.owner == owner
|
|
|
|
return self.database.query_by_filter(session, Location, expr)
|
|
|
|
|
|
|
|
def find_shop_by_owner(self, session, owner):
|
|
|
|
expr = Shop.owner == owner
|
|
|
|
return self.database.query_by_filter(session, Shop, expr)
|
|
|
|
|
|
|
|
def find_location_by_owner_name(self, session, owner_name):
|
|
|
|
expr = Location.owner.has(Player.name.ilike(owner_name))
|
|
|
|
return self.database.query_by_filter(session, Location, expr)
|
|
|
|
|
|
|
|
def find_shop_by_name_and_owner(self, session, owner, name):
|
|
|
|
expr = (Shop.owner == owner) & (Shop.name.ilike(name))
|
|
|
|
return self.database.query_by_filter(session, Shop, expr)
|
|
|
|
|
|
|
|
def find_location_by_name_and_owner(self, session, owner, name):
|
|
|
|
expr = (Location.owner == owner) & (Location.name.ilike(name))
|
|
|
|
return self.database.query_by_filter(session, Location, expr)
|
|
|
|
|
|
|
|
def find_location_around(self, session, x_pos, z_pos, radius, dimension):
|
|
|
|
dimension_obj = Dimension.str_to_dimension(dimension)
|
|
|
|
expr = (Location.x < x_pos + radius + 1) & (Location.x > x_pos - radius - 1) & (Location.z < z_pos + radius + 1) \
|
|
|
|
& (Location.z > z_pos - radius - 1) & (Location.dimension == dimension_obj)
|
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
return list_to_string(self.database.query_by_filter(session, Location, expr))
|
2018-07-22 12:52:49 +00:00
|
|
|
|
|
|
|
def find_tunnel_by_owner(self, session, owner):
|
|
|
|
expr = Tunnel.owner == owner
|
|
|
|
|
|
|
|
return self.database.query_by_filter(session, Tunnel, expr)
|
|
|
|
|
|
|
|
def find_tunnel_by_owner_name(self, session, owner_name):
|
2018-07-24 01:12:23 +00:00
|
|
|
expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(owner_name)))
|
2018-07-22 12:52:49 +00:00
|
|
|
return self.database.query_by_filter(session, Tunnel, expr)
|
|
|
|
|
|
|
|
def find_item(self, session, item_name):
|
|
|
|
expr = ItemListing.name.ilike('%{}%'.format(item_name))
|
|
|
|
return self.database.query_by_filter(session, ItemListing, expr)
|
|
|
|
|
|
|
|
def find_shop_selling_item(self, session, item_name):
|
|
|
|
listings = self.find_item(session, item_name)
|
2018-07-22 23:56:07 +00:00
|
|
|
return list_to_string(listings)
|
2018-07-22 12:52:49 +00:00
|
|
|
|
|
|
|
def find_player(self, session, player_name):
|
|
|
|
expr = Player.name.ilike(player_name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.database.query_by_filter(session, Player, expr)[0]
|
|
|
|
except IndexError:
|
|
|
|
raise PlayerNotFound
|
|
|
|
|
|
|
|
return player
|
|
|
|
|
|
|
|
def find_player_by_mc_uuid(self, session, uuid):
|
|
|
|
expr = Player.id == uuid
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.database.query_by_filter(session, Player, expr)[0]
|
|
|
|
except IndexError:
|
|
|
|
raise PlayerNotFound
|
|
|
|
|
|
|
|
return player
|
|
|
|
|
|
|
|
def find_player_by_discord_uuid(self, session, uuid):
|
|
|
|
expr = Player.discord_uuid == uuid
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.database.query_by_filter(session, Player, expr)[0]
|
2018-07-29 13:59:11 +00:00
|
|
|
|
2018-07-22 12:52:49 +00:00
|
|
|
except IndexError:
|
|
|
|
raise PlayerNotFound
|
|
|
|
return player
|
|
|
|
|
|
|
|
def search_all_fields(self, session, search):
|
2018-07-29 15:50:24 +00:00
|
|
|
loc_string = ''
|
2018-07-29 14:29:27 +00:00
|
|
|
limit = 10
|
2018-07-29 15:50:24 +00:00
|
|
|
|
2018-07-22 12:52:49 +00:00
|
|
|
expr = Location.owner.has(Player.name.ilike('%{}%'.format(search))) | Location.name.ilike('%{}%'.format(search))
|
2018-07-29 15:50:24 +00:00
|
|
|
locations = self.database.query_by_filter(session, Location, expr, limit=limit)
|
|
|
|
|
|
|
|
if len(locations) > 0:
|
|
|
|
loc_string = loc_string + '\n**Locations:**'
|
2018-07-22 12:52:49 +00:00
|
|
|
|
2018-07-29 15:50:24 +00:00
|
|
|
for loc in locations:
|
|
|
|
loc_string = "{}\n{}".format(loc_string, loc)
|
|
|
|
|
|
|
|
if len(locations) == limit:
|
|
|
|
loc_string = loc_string + '\n**. . .**'
|
2018-07-29 14:29:27 +00:00
|
|
|
|
2018-08-11 23:02:50 +00:00
|
|
|
expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(search))) & Tunnel.location is None
|
2018-07-24 01:12:23 +00:00
|
|
|
tunnels = self.database.query_by_filter(session, Tunnel, expr)
|
2018-07-23 02:51:22 +00:00
|
|
|
|
2018-07-24 01:12:23 +00:00
|
|
|
if len(tunnels) > 0:
|
|
|
|
loc_string = loc_string + '\n\n**Tunnels:**'
|
|
|
|
for tunnel in tunnels:
|
|
|
|
loc_string = "{}\n{}".format(loc_string, tunnel.full_str())
|
2018-07-22 12:52:49 +00:00
|
|
|
|
2018-07-29 15:50:24 +00:00
|
|
|
if len(tunnels) == limit:
|
|
|
|
loc_string = loc_string + '\n**. . .**'
|
|
|
|
|
|
|
|
if len(tunnels) + len(locations) == 0:
|
2018-07-22 12:52:49 +00:00
|
|
|
raise LocationLookUpError
|
|
|
|
else:
|
|
|
|
return loc_string
|
|
|
|
|
|
|
|
def delete_location(self, session, owner, name):
|
|
|
|
expr = (Location.owner == owner) & (Location.name == name)
|
|
|
|
self.database.delete_entry(session, Location, expr)
|
|
|
|
|
|
|
|
|
|
|
|
def check_similarity(a, b):
|
|
|
|
ratio = SequenceMatcher(None, a, b).ratio()
|
|
|
|
|
|
|
|
if (ratio > 0.6) or (a[0] == b[0]):
|
|
|
|
return True
|
|
|
|
else:
|
2018-07-22 23:56:07 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def list_to_string(loc_list, str_format='{}\n{}'):
|
|
|
|
loc_string = ''
|
|
|
|
|
|
|
|
for loc in loc_list:
|
|
|
|
loc_string = str_format.format(loc_string, loc)
|
|
|
|
|
2018-08-11 23:02:50 +00:00
|
|
|
return loc_string
|