MC UUID is now used to track players

doc_update
Joey Hines 2018-06-02 21:42:31 -05:00
parent 70ff8624f9
commit 26b19f4836
6 changed files with 64 additions and 15 deletions

View File

@ -11,4 +11,8 @@ class LocationLookUpError(DataBaseError) :
'''Error in finding location in database'''
class DeleteEntryError(DataBaseError) :
'''Error in deleting entry'''
'''Error in deleting entry'''
class UsernameLookupFailed(Exception):
'''Error in username lookup, is the player's nickname set correctly? *stares at aeskdar*'''

View File

@ -5,6 +5,7 @@ from BotErrors import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, relationship
import sqlalchemy
from MinecraftAccountInfoGrabber import *
SQL_Base = declarative_base()
@ -60,11 +61,13 @@ class TunnelDirection(enum.Enum):
class Player(SQL_Base):
__tablename__ = 'Players'
id = Column(Integer, primary_key=True)
in_game_name = Column(String)
uuid = Column(String, primary_key=True)
def __init__(self, name):
self.in_game_name = name
if name == 'dootb.in ꙩ ⃤' :
name = 'aeskdar'
self.uuid = grab_UUID(name)
class Location(SQL_Base):
@ -77,7 +80,7 @@ class Location(SQL_Base):
z = Column(Integer)
tunnelNumber = Column(Integer)
direction = Column(Enum(TunnelDirection))
owner = Column(String, ForeignKey('Players.in_game_name'))
owner_uuid = Column(String, ForeignKey('Players.uuid'))
type = Column(String)
__mapper_args__ = {
@ -91,7 +94,7 @@ class Location(SQL_Base):
self.x = x
self.y = y
self.z = z
self.owner = owner
self.owner_uuid = owner
if len(args) > 0:
self.direction = TunnelDirection.str_to_tunnel_dir(args[0])

View File

@ -1,6 +1,8 @@
from discord.ext import commands
from DatabaseModels import *
from BotErrors import *
from MinecraftAccountInfoGrabber import *
TOKEN = ''
command_prefix = '?'
@ -18,6 +20,8 @@ bot = commands.Bot(command_prefix=command_prefix, description=description, case_
database = GeoffreyDatabase('sqlite:///:memory:')
# Bot Commands ******************************************************************
@bot.event
async def on_ready():
@ -33,6 +37,8 @@ async def on_command_error(error, ctx):
elif isinstance(error, commands.UserInputError):
error_str = 'Invalid syntax for {} you ding dong, please read ?help {}.'\
.format(ctx.invoked_with, ctx.invoked_with)
elif isinstance(error.original, UsernameLookupFailed):
error_str = error.original.__doc__
else:
error_str = bad_error_message.format(ctx.invoked_with)
print(error)
@ -59,7 +65,7 @@ async def addbase(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args):
owner = Player(str(ctx.message.author.nick))
try:
base = Location(name, x_pos, y_pos, z_pos, owner.in_game_name, args)
base = Location(name, x_pos, y_pos, z_pos, owner.uuid, args)
except LocationInitError:
raise commands.UserInputError
@ -77,7 +83,8 @@ async def findbase(ctx, name: str):
?findbase [Player name]
'''
expr = Location.owner == name
uuid = grab_UUID(name)
expr = Location.owner_uuid == uuid
base_list = database.query_by_filter(Location, expr)
if len(base_list) != 0:
@ -107,7 +114,7 @@ async def deletebase(ctx, name: str):
user = str(ctx.message.author.nick)
expr = (Location.owner == user) & (Location.name == name)
expr = (Location.owner_uuid == user) & (Location.name == name)
try:
database.delete_entry(Location, expr)

View File

@ -0,0 +1,25 @@
import requests
from json import JSONDecodeError
from BotErrors import UsernameLookupFailed
uuid_lookup_url = 'https://api.mojang.com/users/profiles/minecraft/{}'
username_lookup_url = 'https://api.mojang.com/user/profiles/{}/names'
def grab_json(url):
return requests.get(url).json()
def grab_UUID(username):
try:
player_data = grab_json(uuid_lookup_url.format(username))
return player_data['id']
except JSONDecodeError:
raise UsernameLookupFailed
def grab_playername(uuid):
player_data = grab_json(username_lookup_url.format(uuid))
return player_data[0]['name']

View File

@ -7,26 +7,26 @@ from BotErrors import *
class TestGeoffreyDatabase(TestCase):
def setUp(self):
self.database = GeoffreyDatabase('sqlite:///:memory:')
self.loc = Location('test', 1, 2, 3, 'owner', ['Green', 0])
self.loc = Location('test', 1, 2, 3, 'ZeroHD', ['Green', 0])
def test_add_object(self):
self.database.add_object(self.loc)
loc2 = self.database.query_by_filter(Location, Location.owner == 'owner')[0]
loc2 = self.database.query_by_filter(Location, Location.owner_uuid == 'ZeroHD')[0]
self.assertEqual(self.loc.id, loc2.id)
def test_query_by_filter(self):
expr = (Location.owner == 'owner') & (Location.x == 0)
expr = (Location.owner_uuid == 'ZeroHD') & (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 == 'owner') & (Location.name == 'test')
expr = (Location.owner_uuid == 'ZeroHD') & (Location.name == 'test')
self.database.delete_entry(Location, expr)
expr = (Location.owner == 'owner') & (Location.x == 0)
expr = (Location.owner_uuid == 'ZeroHD') & (Location.x == 0)
loc2 = self.database.query_by_filter(Location, expr)
self.assertEqual(len(loc2), 0)
@ -37,4 +37,3 @@ class TestGeoffreyDatabase(TestCase):

View File

@ -0,0 +1,11 @@
from unittest import TestCase
from MinecraftAccountInfoGrabber import *
class TestMinecraftInfoGrabber(TestCase):
def test_handle_data(self):
self.assertEqual(grab_UUID('ZeroHD'), 'fe7e84132570458892032b69ff188bc3')
def test_grab_playername(self):
self.assertEqual(grab_playername('fe7e84132570458892032b69ff188bc3'), 'ZeroHD')