Fixed UUID lookup
parent
26b19f4836
commit
c642f535f8
|
@ -2,7 +2,7 @@ from sqlalchemy import Column, Integer, String, ForeignKey, Enum
|
||||||
import enum
|
import enum
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from BotErrors import *
|
from BotErrors import *
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine, exists
|
||||||
from sqlalchemy.orm import sessionmaker, relationship
|
from sqlalchemy.orm import sessionmaker, relationship
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from MinecraftAccountInfoGrabber import *
|
from MinecraftAccountInfoGrabber import *
|
||||||
|
@ -19,7 +19,10 @@ class GeoffreyDatabase:
|
||||||
SQL_Base.metadata.create_all(self.engine)
|
SQL_Base.metadata.create_all(self.engine)
|
||||||
|
|
||||||
def add_object(self, obj):
|
def add_object(self, obj):
|
||||||
|
ret = not self.session.query(exists().where(type(obj).id == obj.id))
|
||||||
|
if not ret:
|
||||||
self.session.add(obj)
|
self.session.add(obj)
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
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)
|
||||||
|
@ -34,6 +37,15 @@ class GeoffreyDatabase:
|
||||||
else:
|
else:
|
||||||
raise DeleteEntryError
|
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):
|
def combine_filter(self, filter_value):
|
||||||
return sqlalchemy.sql.expression.and_(filter_value[0])
|
return sqlalchemy.sql.expression.and_(filter_value[0])
|
||||||
|
|
||||||
|
@ -61,13 +73,16 @@ class TunnelDirection(enum.Enum):
|
||||||
class Player(SQL_Base):
|
class Player(SQL_Base):
|
||||||
__tablename__ = 'Players'
|
__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):
|
def __init__(self, name):
|
||||||
if name == 'dootb.in ꙩ ⃤':
|
if name == 'dootb.in ꙩ ⃤':
|
||||||
name = 'aeskdar'
|
name = 'aeskdar'
|
||||||
|
|
||||||
self.uuid = grab_UUID(name)
|
self.id = grab_UUID(name)
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
|
||||||
class Location(SQL_Base):
|
class Location(SQL_Base):
|
||||||
|
@ -80,13 +95,15 @@ class Location(SQL_Base):
|
||||||
z = Column(Integer)
|
z = Column(Integer)
|
||||||
tunnelNumber = Column(Integer)
|
tunnelNumber = Column(Integer)
|
||||||
direction = Column(Enum(TunnelDirection))
|
direction = Column(Enum(TunnelDirection))
|
||||||
owner_uuid = Column(String, ForeignKey('Players.uuid'))
|
owner_id = Column(Integer, ForeignKey('Players.id'))
|
||||||
type = Column(String)
|
owner = relationship("Player", back_populates="locations")
|
||||||
|
#type = Column(String)
|
||||||
|
'''
|
||||||
__mapper_args__ = {
|
__mapper_args__ = {
|
||||||
'polymorphic_on': type,
|
'polymorphic_on': type,
|
||||||
'polymorphic_identity': 'Location'
|
'polymorphic_identity': 'Location'
|
||||||
}
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
def __init__(self, name, x, y, z, owner, args):
|
def __init__(self, name, x, y, z, owner, args):
|
||||||
try:
|
try:
|
||||||
|
@ -94,7 +111,7 @@ class Location(SQL_Base):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.z = z
|
self.z = z
|
||||||
self.owner_uuid = owner
|
self.owner = owner
|
||||||
|
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
self.direction = TunnelDirection.str_to_tunnel_dir(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())
|
self.nether_tunnel_addr_to_str())
|
||||||
else:
|
else:
|
||||||
return "Name: {}, Position: {}".format(self.name, self.pos_to_str())
|
return "Name: {}, Position: {}".format(self.name, self.pos_to_str())
|
||||||
|
'''
|
||||||
|
|
||||||
class Shop(Location):
|
class Shop(Location):
|
||||||
__tablename__ = 'Shops'
|
__tablename__ = 'Shops'
|
||||||
|
@ -149,3 +166,4 @@ class ItemListing(SQL_Base):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Item: {}, Price: {}".format(self.name, self.price)
|
return "Item: {}, Price: {}".format(self.name, self.price)
|
||||||
|
'''
|
49
Geoffrey.py
49
Geoffrey.py
|
@ -53,7 +53,6 @@ async def test():
|
||||||
'''
|
'''
|
||||||
await bot.say('I\'m here you ding dong')
|
await bot.say('I\'m here you ding dong')
|
||||||
|
|
||||||
|
|
||||||
@bot.command(pass_context=True)
|
@bot.command(pass_context=True)
|
||||||
async def addbase(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args):
|
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]
|
?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:
|
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:
|
except LocationInitError:
|
||||||
raise commands.UserInputError
|
raise commands.UserInputError
|
||||||
|
|
||||||
database.add_object(owner)
|
owner.locations.append(base)
|
||||||
database.add_object(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'
|
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()))
|
' 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)
|
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)
|
base_list = database.query_by_filter(Location, expr)
|
||||||
|
|
||||||
|
|
||||||
if len(base_list) != 0:
|
if len(base_list) != 0:
|
||||||
base_string = base_list_string(base_list, '{} \n{}')
|
base_string = base_list_string(base_list, '{} \n{}')
|
||||||
|
|
||||||
|
@ -112,9 +145,9 @@ async def deletebase(ctx, name: str):
|
||||||
?deletebase [Base name]
|
?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:
|
try:
|
||||||
database.delete_entry(Location, expr)
|
database.delete_entry(Location, expr)
|
||||||
|
|
|
@ -1,32 +1,42 @@
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
from DatabaseModels import GeoffreyDatabase
|
from DatabaseModels import GeoffreyDatabase
|
||||||
from DatabaseModels import Location
|
from DatabaseModels import Location, Player
|
||||||
from BotErrors import *
|
from BotErrors import *
|
||||||
|
from MinecraftAccountInfoGrabber import *
|
||||||
|
|
||||||
|
|
||||||
class TestGeoffreyDatabase(TestCase):
|
class TestGeoffreyDatabase(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.database = GeoffreyDatabase('sqlite:///:memory:')
|
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):
|
def test_add_object(self):
|
||||||
self.database.add_object(self.loc)
|
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)
|
self.assertEqual(self.loc.id, loc2.id)
|
||||||
|
|
||||||
def test_query_by_filter(self):
|
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)
|
loc2 = self.database.query_by_filter(Location, expr)
|
||||||
self.assertEqual(len(loc2), 0)
|
self.assertEqual(len(loc2), 0)
|
||||||
|
|
||||||
def test_delete_entry(self):
|
def test_delete_entry(self):
|
||||||
self.database.add_object(self.loc)
|
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)
|
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)
|
loc2 = self.database.query_by_filter(Location, expr)
|
||||||
|
|
||||||
self.assertEqual(len(loc2), 0)
|
self.assertEqual(len(loc2), 0)
|
||||||
|
|
Loading…
Reference in New Issue