2018-08-21 20:36:51 +00:00
|
|
|
from geoffrey.DatabaseInterface import *
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
|
2018-09-20 17:25:42 +00:00
|
|
|
def list_to_string(loc_list, str_format='{}\n{}'):
|
|
|
|
loc_string = ''
|
|
|
|
|
|
|
|
for loc in loc_list:
|
|
|
|
loc_string = str_format.format(loc_string, loc)
|
|
|
|
|
|
|
|
return loc_string
|
|
|
|
|
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
class Commands:
|
2018-08-29 21:10:35 +00:00
|
|
|
def __init__(self, bot_config, debug=False):
|
|
|
|
self.bot_config = bot_config
|
|
|
|
self.interface = DatabaseInterface(bot_config, debug)
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
def get_player(self, session, discord_uuid=None, mc_uuid=None):
|
|
|
|
if discord_uuid is not None:
|
|
|
|
player = self.interface.find_player_by_discord_uuid(session, discord_uuid)
|
|
|
|
elif mc_uuid is not None:
|
|
|
|
player = self.interface.find_player_by_mc_uuid(session, mc_uuid)
|
|
|
|
else:
|
|
|
|
raise AttributeError
|
|
|
|
|
|
|
|
return player
|
|
|
|
|
2018-09-20 14:20:30 +00:00
|
|
|
def get_location(self, session, owner, name=None, loc_type=Location):
|
|
|
|
if name is None:
|
|
|
|
loc_list = self.interface.find_location_by_owner(session, owner, loc_type)
|
|
|
|
if len(loc_list) == 1:
|
|
|
|
loc = loc_list[0]
|
|
|
|
elif len(loc_list) == 0:
|
|
|
|
raise NoLocationsInDatabase
|
|
|
|
else:
|
|
|
|
raise EntryNameNotUniqueError
|
|
|
|
else:
|
|
|
|
loc_list = self.interface.find_location_by_name_and_owner(session, owner, name, loc_type)
|
|
|
|
if len(loc_list) == 1:
|
|
|
|
loc = loc_list[0]
|
|
|
|
else:
|
|
|
|
raise LocationLookUpError
|
|
|
|
|
|
|
|
return loc
|
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
def register(self, player_name, discord_uuid):
|
|
|
|
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
2018-09-16 18:34:33 +00:00
|
|
|
player = self.interface.add_player(session, player_name, discord_uuid)
|
|
|
|
player_name = player.name
|
2018-07-29 13:59:11 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return player_name
|
|
|
|
|
2018-08-01 00:22:17 +00:00
|
|
|
def add_base(self, x_pos, z_pos, base_name=None, discord_uuid=None, mc_uuid=None):
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid, mc_uuid)
|
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
if len(self.interface.find_location_by_owner(session, player, loc_type=Base)) == 0:
|
2018-07-22 23:56:07 +00:00
|
|
|
if base_name is None:
|
2018-07-23 02:51:22 +00:00
|
|
|
base_name = "{}'s Base".format(player.name)
|
2018-07-22 23:56:07 +00:00
|
|
|
elif base_name is None:
|
|
|
|
raise EntryNameNotUniqueError
|
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
base = self.interface.add_loc(session, player, base_name, x_pos, z_pos, loc_type=Base)
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-07-24 01:12:23 +00:00
|
|
|
base_str = base.__str__()
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-07-24 01:12:23 +00:00
|
|
|
return base_str
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-08-01 01:16:14 +00:00
|
|
|
def add_shop(self, x_pos, z_pos, shop_name=None, discord_uuid=None, mc_uuid=None):
|
2018-07-22 23:56:07 +00:00
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid, mc_uuid)
|
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
if len(self.interface.find_location_by_owner(session, player, loc_type=Shop)) == 0:
|
2018-08-01 01:16:14 +00:00
|
|
|
if shop_name is None:
|
|
|
|
shop_name = "{}'s Shop".format(player.name)
|
|
|
|
elif shop_name is None:
|
2018-07-22 23:56:07 +00:00
|
|
|
raise EntryNameNotUniqueError
|
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
shop = self.interface.add_loc(session, player, shop_name, x_pos, z_pos, loc_type=Shop)
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-08-01 01:16:14 +00:00
|
|
|
shop_name = shop.__str__()
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-08-01 01:16:14 +00:00
|
|
|
return shop_name
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-09-13 17:06:37 +00:00
|
|
|
def add_tunnel(self, tunnel_direction, tunnel_number, location_name=None, discord_uuid=None, mc_uuid=None):
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
try:
|
|
|
|
|
|
|
|
player = self.get_player(session, discord_uuid, mc_uuid)
|
2018-08-01 01:44:06 +00:00
|
|
|
if location_name is None:
|
2018-09-20 14:20:30 +00:00
|
|
|
loc = self.get_location(session, player, name=location_name)
|
|
|
|
location_name = loc.name
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-09-13 17:06:37 +00:00
|
|
|
tunnel = self.interface.add_tunnel(session, player, tunnel_direction, tunnel_number, location_name)
|
2018-07-22 23:56:07 +00:00
|
|
|
tunnel_info = tunnel.__str__()
|
2018-08-01 01:44:06 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return tunnel_info
|
|
|
|
|
|
|
|
def find(self, search):
|
2018-09-20 17:25:42 +00:00
|
|
|
limit = 25
|
2018-07-22 23:56:07 +00:00
|
|
|
session = self.interface.database.Session()
|
|
|
|
try:
|
2018-09-20 17:25:42 +00:00
|
|
|
locations = self.interface.search_all_fields(session, search, limit)
|
|
|
|
locations_string = ''
|
|
|
|
|
|
|
|
if len(locations) > 0:
|
|
|
|
|
|
|
|
for loc in locations:
|
|
|
|
locations_string = "{}\n{}".format(locations_string, loc)
|
|
|
|
|
|
|
|
if len(locations) == limit:
|
|
|
|
locations_string = locations_string + '\n**. . .**'
|
|
|
|
else:
|
|
|
|
raise LocationLookUpError
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-09-20 17:25:42 +00:00
|
|
|
return locations_string
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
def delete(self, name, discord_uuid=None, mc_uuid=None):
|
|
|
|
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid, mc_uuid)
|
|
|
|
self.interface.delete_location(session, player, name)
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-08-01 00:22:17 +00:00
|
|
|
def find_around(self, x_pos, z_pos, radius=200, dimension='Overworld'):
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
loc_list = self.interface.find_location_around(session, x_pos, z_pos, radius, dimension)
|
2018-09-20 17:25:42 +00:00
|
|
|
|
|
|
|
loc_list_str = list_to_string(loc_list)
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-09-20 17:25:42 +00:00
|
|
|
return loc_list_str
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-09-20 14:20:30 +00:00
|
|
|
def add_item(self, item_name, quantity, diamond_price, shop_name=None, discord_uuid=None, mc_uuid=None):
|
2018-07-22 23:56:07 +00:00
|
|
|
session = self.interface.database.Session()
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid, mc_uuid)
|
|
|
|
|
2018-09-20 14:20:30 +00:00
|
|
|
shop = self.get_location(session, player, shop_name, Shop)
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-09-20 14:20:30 +00:00
|
|
|
item_listing = self.interface.add_item(session, player, shop.name, item_name, diamond_price, quantity)
|
2018-07-22 23:56:07 +00:00
|
|
|
item_listing_str = item_listing.__str__()
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return item_listing_str
|
|
|
|
|
|
|
|
def selling(self, item_name: str):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
2018-09-25 14:39:30 +00:00
|
|
|
shop_list = self.interface.find_top_shops_selling_item(session, item_name)
|
2018-07-29 13:59:11 +00:00
|
|
|
|
|
|
|
if len(shop_list) == 0:
|
|
|
|
raise ItemNotFound
|
2018-09-20 17:25:42 +00:00
|
|
|
|
2018-09-26 12:44:42 +00:00
|
|
|
shop_list_str = "\n"
|
2018-09-25 14:39:30 +00:00
|
|
|
for shop in shop_list:
|
2018-09-26 12:44:42 +00:00
|
|
|
matches = self.interface.get_inventory_matches(session, shop[0], item_name)
|
|
|
|
shop_list_str = shop_list_str + shop[0].selling_str() + list_to_string(matches)
|
|
|
|
|
|
|
|
if len(matches) == 5:
|
|
|
|
shop_list_str = shop_list_str + '\n**...**'
|
|
|
|
|
|
|
|
shop_list_str = shop_list_str + '\n\n'
|
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-09-20 17:25:42 +00:00
|
|
|
return shop_list_str
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
def info(self, location_name):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
try:
|
2018-08-29 21:10:35 +00:00
|
|
|
loc = self.interface.find_location_by_name(session, location_name)[0].full_str(self.bot_config)
|
2018-07-22 23:56:07 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return loc
|
2018-07-24 01:12:23 +00:00
|
|
|
|
|
|
|
def tunnel(self, player_name):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
tunnel_list = self.interface.find_tunnel_by_owner_name(session, player_name)
|
|
|
|
|
|
|
|
if len(tunnel_list) == 0:
|
|
|
|
raise LocationLookUpError
|
|
|
|
|
|
|
|
tunnel_str = ''
|
|
|
|
|
|
|
|
for tunnel in tunnel_list:
|
|
|
|
tunnel_str = '{}\n{}'.format(tunnel_str, tunnel.full_str())
|
|
|
|
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return tunnel_str
|
2018-08-01 00:22:17 +00:00
|
|
|
|
|
|
|
def edit_pos(self, x, z, loc_name, discord_uuid=None, mc_uuid=None):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
2018-09-20 14:20:30 +00:00
|
|
|
location = self.get_location(session, player, loc_name)
|
2018-08-01 00:22:17 +00:00
|
|
|
|
|
|
|
location.x = x
|
|
|
|
location.z = z
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
loc_str = location.__str__()
|
2018-08-01 00:31:21 +00:00
|
|
|
except DataError:
|
|
|
|
session.rollback()
|
|
|
|
raise DatabaseValueError
|
2018-08-01 00:22:17 +00:00
|
|
|
except IndexError:
|
|
|
|
raise LocationLookUpError
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return loc_str
|
|
|
|
|
2018-09-13 17:06:37 +00:00
|
|
|
def edit_tunnel(self, tunnel_direction, tunnel_number, loc_name, discord_uuid=None, mc_uuid=None):
|
2018-08-01 00:22:17 +00:00
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
|
|
|
location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0]
|
|
|
|
|
|
|
|
if location.tunnel is not None:
|
2018-09-13 17:06:37 +00:00
|
|
|
location.tunnel.tunnel_direction = TunnelDirection.str_to_tunnel_dir(tunnel_direction)
|
2018-08-01 00:22:17 +00:00
|
|
|
location.tunnel.tunnel_number = tunnel_number
|
|
|
|
else:
|
2018-09-13 17:06:37 +00:00
|
|
|
self.interface.add_tunnel(session, player, tunnel_direction, tunnel_number, loc_name)
|
2018-08-01 00:22:17 +00:00
|
|
|
|
|
|
|
loc_str = location.__str__()
|
|
|
|
|
|
|
|
session.commit()
|
2018-08-01 00:31:21 +00:00
|
|
|
except IntegrityError:
|
|
|
|
session.rollback()
|
|
|
|
raise EntryNameNotUniqueError
|
|
|
|
except DataError:
|
|
|
|
session.rollback()
|
|
|
|
raise DatabaseValueError
|
2018-08-01 00:22:17 +00:00
|
|
|
except IndexError:
|
|
|
|
raise LocationLookUpError
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return loc_str
|
|
|
|
|
|
|
|
def edit_name(self, new_name, loc_name, discord_uuid=None, mc_uuid=None):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
2018-09-20 14:20:30 +00:00
|
|
|
location = self.get_location(session, player, loc_name)
|
2018-08-01 00:22:17 +00:00
|
|
|
|
|
|
|
location.name = new_name
|
|
|
|
loc_str = location.__str__()
|
|
|
|
session.commit()
|
2018-08-01 00:31:21 +00:00
|
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
session.rollback()
|
|
|
|
raise EntryNameNotUniqueError
|
|
|
|
except DataError:
|
|
|
|
session.rollback()
|
|
|
|
raise DatabaseValueError
|
2018-08-01 00:22:17 +00:00
|
|
|
except IndexError:
|
|
|
|
raise LocationLookUpError
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return loc_str
|
|
|
|
|
2018-08-01 01:16:14 +00:00
|
|
|
def delete_item(self, item, shop_name, discord_uuid=None, mc_uuid=None):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
2018-08-05 14:08:20 +00:00
|
|
|
|
2018-09-20 14:20:30 +00:00
|
|
|
shop = self.get_location(session, player, shop_name, Shop)
|
2018-08-01 01:16:14 +00:00
|
|
|
|
2018-09-20 17:25:42 +00:00
|
|
|
self.interface.delete_item(session, shop, item)
|
2018-08-01 01:16:14 +00:00
|
|
|
|
2018-09-12 21:11:56 +00:00
|
|
|
shop_str = shop.name
|
2018-08-01 01:16:14 +00:00
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return shop_str
|
|
|
|
|
2018-08-10 23:03:30 +00:00
|
|
|
def me(self, discord_uuid=None, mc_uuid=None):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
|
|
|
|
|
|
|
loc_list = self.interface.find_location_by_owner(session, player)
|
|
|
|
|
|
|
|
if len(loc_list) == 0:
|
|
|
|
raise PlayerNotFound
|
|
|
|
|
|
|
|
loc_str = list_to_string(loc_list)
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return loc_str
|
2018-08-01 00:22:17 +00:00
|
|
|
|
2018-09-13 16:25:40 +00:00
|
|
|
def update_mc_uuid(self, discord_uuid, mc_uuid):
|
2018-08-11 16:20:40 +00:00
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.interface.find_player_by_discord_uuid(session, discord_uuid)
|
|
|
|
|
|
|
|
player.mc_uuid = mc_uuid
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
session.rollback()
|
|
|
|
raise e
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
def update_discord_uuid(self, old_discord_uuid, new_discord_uuid):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.interface.find_player_by_discord_uuid(session, old_discord_uuid)
|
|
|
|
|
|
|
|
player.discord_uuid = new_discord_uuid
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
session.rollback()
|
|
|
|
raise e
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-08-11 18:46:03 +00:00
|
|
|
def update_mc_name(self, discord_uuid):
|
2018-08-11 16:20:40 +00:00
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.interface.find_player_by_discord_uuid(session, discord_uuid)
|
|
|
|
|
2018-08-11 18:46:03 +00:00
|
|
|
player.name = grab_playername(player.mc_uuid)
|
2018-08-11 16:20:40 +00:00
|
|
|
|
|
|
|
session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
session.rollback()
|
|
|
|
raise e
|
|
|
|
finally:
|
|
|
|
session.close()
|
2018-09-14 13:21:02 +00:00
|
|
|
|
|
|
|
def add_player(self, discord_uuid, mc_name):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.interface.find_player_by_discord_uuid(session, discord_uuid)
|
2018-09-21 12:27:32 +00:00
|
|
|
raise PlayerInDBError
|
2018-09-14 13:21:02 +00:00
|
|
|
except PlayerNotFound:
|
|
|
|
player = Player(mc_name, discord_id=discord_uuid)
|
|
|
|
self.interface.database.add_object(session, player)
|
|
|
|
|
|
|
|
player = self.interface.find_player_by_discord_uuid(session, discord_uuid)
|
2018-09-21 12:27:32 +00:00
|
|
|
id = player.id
|
2018-09-14 13:21:02 +00:00
|
|
|
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2018-09-21 12:27:32 +00:00
|
|
|
return id
|
2018-09-14 13:21:02 +00:00
|
|
|
|
|
|
|
def find_player(self, discord_uuid):
|
|
|
|
session = self.interface.database.Session()
|
|
|
|
|
|
|
|
try:
|
|
|
|
player = self.interface.find_player_by_discord_uuid(session, discord_uuid)
|
|
|
|
id = player.id
|
|
|
|
username = player.name
|
|
|
|
discord_uuid = player.discord_uuid
|
|
|
|
minecraft_uuid = player.mc_uuid
|
|
|
|
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return id, username, discord_uuid, minecraft_uuid
|