Fixed UUID lookup

doc_update
Joey Hines 2018-06-23 10:17:51 -05:00
parent 26b19f4836
commit c642f535f8
3 changed files with 85 additions and 24 deletions

View File

@ -2,7 +2,7 @@ from sqlalchemy import Column, Integer, String, ForeignKey, Enum
import enum
from sqlalchemy.ext.declarative import declarative_base
from BotErrors import *
from sqlalchemy import create_engine
from sqlalchemy import create_engine, exists
from sqlalchemy.orm import sessionmaker, relationship
import sqlalchemy
from MinecraftAccountInfoGrabber import *
@ -19,7 +19,10 @@ class GeoffreyDatabase:
SQL_Base.metadata.create_all(self.engine)
def add_object(self, obj):
self.session.add(obj)
ret = not self.session.query(exists().where(type(obj).id == obj.id))
if not ret:
self.session.add(obj)
self.session.commit()
def query_by_filter(self, obj_type, * args):
filter_value = self.combine_filter(args)
@ -34,6 +37,15 @@ class GeoffreyDatabase:
else:
raise DeleteEntryError
def print_database(self, obj_type):
obj_list = self.session.query(obj_type).all()
s = ''
for obj in obj_list:
s = s + '\n' + obj.id
return s
def combine_filter(self, filter_value):
return sqlalchemy.sql.expression.and_(filter_value[0])
@ -61,13 +73,16 @@ class TunnelDirection(enum.Enum):
class Player(SQL_Base):
__tablename__ = 'Players'
uuid = Column(String, primary_key=True)
id = Column(String, primary_key=True, autoincrement=False)
name = Column(String)
locations = relationship("Location", back_populates="owner", lazy='dynamic')
def __init__(self, name):
if name == 'dootb.in ꙩ ⃤' :
if name == 'dootb.in ꙩ ⃤':
name = 'aeskdar'
self.uuid = grab_UUID(name)
self.id = grab_UUID(name)
self.name = name
class Location(SQL_Base):
@ -80,13 +95,15 @@ class Location(SQL_Base):
z = Column(Integer)
tunnelNumber = Column(Integer)
direction = Column(Enum(TunnelDirection))
owner_uuid = Column(String, ForeignKey('Players.uuid'))
type = Column(String)
owner_id = Column(Integer, ForeignKey('Players.id'))
owner = relationship("Player", back_populates="locations")
#type = Column(String)
'''
__mapper_args__ = {
'polymorphic_on': type,
'polymorphic_identity': 'Location'
}
'''
def __init__(self, name, x, y, z, owner, args):
try:
@ -94,7 +111,7 @@ class Location(SQL_Base):
self.x = x
self.y = y
self.z = z
self.owner_uuid = owner
self.owner = owner
if len(args) > 0:
self.direction = TunnelDirection.str_to_tunnel_dir(args[0])
@ -115,7 +132,7 @@ class Location(SQL_Base):
self.nether_tunnel_addr_to_str())
else:
return "Name: {}, Position: {}".format(self.name, self.pos_to_str())
'''
class Shop(Location):
__tablename__ = 'Shops'
@ -149,3 +166,4 @@ class ItemListing(SQL_Base):
def __str__(self):
return "Item: {}, Price: {}".format(self.name, self.price)
'''

View File

@ -53,7 +53,6 @@ async def test():
'''
await bot.say('I\'m here you ding dong')
@bot.command(pass_context=True)
async def addbase(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args):
'''
@ -62,16 +61,46 @@ async def addbase(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args):
?addbase [Base Name] [X Coordinate] [Y Coordinate] [Z Coordinate] [Tunnel Color] [Tunnel Position]
'''
owner = Player(str(ctx.message.author.nick))
if ctx.message.author.nick is None:
player_name = ctx.message.author.display_name
else:
player_name = ctx.message.author.nick
expr = Player.name == player_name
player_list = database.query_by_filter(Player, expr)
if len(player_list) == 0:
uuid = grab_UUID(player_name)
expr = Player.id == uuid
player_list = database.query_by_filter(Player, expr)
if len(player_list) == 0:
owner = Player(player_name)
else:
player_list[0].name == player_name
else:
owner = player_list[0]
try:
base = Location(name, x_pos, y_pos, z_pos, owner.uuid, args)
base = Location(name, x_pos, y_pos, z_pos, owner, args)
except LocationInitError:
raise commands.UserInputError
database.add_object(owner)
database.add_object(base)
owner.locations.append(base)
database.add_object(base)
database.add_object(owner)
#expr = Player.id == owner.id
#player_list = database.query_by_filter(Player, expr)
'''
if len(player_list) == 0:
database.add_object(owner)
else:
if player_list[0].name != owner.name:
player_list[0].name = owner.name
database.session.commit()
'''
await bot.say('{}, your base named {} located at {} has been added'
' to the database.'.format(ctx.message.author.mention, base.name, base.pos_to_str()))
@ -84,9 +113,13 @@ async def findbase(ctx, name: str):
'''
uuid = grab_UUID(name)
expr = Location.owner_uuid == uuid
expr = Player.id == uuid
p = database.query_by_filter(Player, expr)[0]
expr = Location.owner == p
base_list = database.query_by_filter(Location, expr)
if len(base_list) != 0:
base_string = base_list_string(base_list, '{} \n{}')
@ -112,9 +145,9 @@ async def deletebase(ctx, name: str):
?deletebase [Base name]
'''
user = str(ctx.message.author.nick)
uuid = grab_UUID(str(ctx.message.author.nick))
expr = (Location.owner_uuid == user) & (Location.name == name)
expr = (Location.owner_id == uuid) & (Location.name == name)
try:
database.delete_entry(Location, expr)

View File

@ -1,32 +1,42 @@
from unittest import TestCase
from DatabaseModels import GeoffreyDatabase
from DatabaseModels import Location
from DatabaseModels import Location, Player
from BotErrors import *
from MinecraftAccountInfoGrabber import *
class TestGeoffreyDatabase(TestCase):
def setUp(self):
self.database = GeoffreyDatabase('sqlite:///:memory:')
self.loc = Location('test', 1, 2, 3, 'ZeroHD', ['Green', 0])
self.owner = Player('ZeroHD')
self.loc = Location('test', 1, 2, 3, self.owner, ['Green', 0])
def test_add_object(self):
self.database.add_object(self.loc)
self.database.add_object(self.owner)
loc2 = self.database.query_by_filter(Location, Location.owner_uuid == 'ZeroHD')[0]
uuid = grab_UUID('ZeroHD')
expr = Player.id == uuid
p = self.database.query_by_filter(Player, expr)[0]
expr = Location.owner == p
loc2 = self.database.query_by_filter(Location, expr)[0]
self.assertEqual(self.loc.id, loc2.id)
def test_query_by_filter(self):
expr = (Location.owner_uuid == 'ZeroHD') & (Location.x == 0)
expr = (Location.owner_id == 'fe7e84132570458892032b69ff188bc3') & (Location.x == 0)
loc2 = self.database.query_by_filter(Location, expr)
self.assertEqual(len(loc2), 0)
def test_delete_entry(self):
self.database.add_object(self.loc)
expr = (Location.owner_uuid == 'ZeroHD') & (Location.name == 'test')
self.database.add_object(self.owner)
expr = (Location.owner_id == 'fe7e84132570458892032b69ff188bc3') & (Location.name == 'test')
self.database.delete_entry(Location, expr)
expr = (Location.owner_uuid == 'ZeroHD') & (Location.x == 0)
expr = (Location.owner_id == 'fe7e84132570458892032b69ff188bc3') & (Location.x == 0)
loc2 = self.database.query_by_filter(Location, expr)
self.assertEqual(len(loc2), 0)