MC UUID is now used to track players
parent
70ff8624f9
commit
26b19f4836
|
@ -11,4 +11,8 @@ class LocationLookUpError(DataBaseError) :
|
||||||
'''Error in finding location in database'''
|
'''Error in finding location in database'''
|
||||||
|
|
||||||
class DeleteEntryError(DataBaseError) :
|
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*'''
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ from BotErrors import *
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker, relationship
|
from sqlalchemy.orm import sessionmaker, relationship
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
from MinecraftAccountInfoGrabber import *
|
||||||
|
|
||||||
SQL_Base = declarative_base()
|
SQL_Base = declarative_base()
|
||||||
|
|
||||||
|
@ -60,11 +61,13 @@ class TunnelDirection(enum.Enum):
|
||||||
class Player(SQL_Base):
|
class Player(SQL_Base):
|
||||||
__tablename__ = 'Players'
|
__tablename__ = 'Players'
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
uuid = Column(String, primary_key=True)
|
||||||
in_game_name = Column(String)
|
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.in_game_name = name
|
if name == 'dootb.in ꙩ ⃤' :
|
||||||
|
name = 'aeskdar'
|
||||||
|
|
||||||
|
self.uuid = grab_UUID(name)
|
||||||
|
|
||||||
|
|
||||||
class Location(SQL_Base):
|
class Location(SQL_Base):
|
||||||
|
@ -77,7 +80,7 @@ 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 = Column(String, ForeignKey('Players.in_game_name'))
|
owner_uuid = Column(String, ForeignKey('Players.uuid'))
|
||||||
type = Column(String)
|
type = Column(String)
|
||||||
|
|
||||||
__mapper_args__ = {
|
__mapper_args__ = {
|
||||||
|
@ -91,7 +94,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 = owner
|
self.owner_uuid = 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])
|
||||||
|
|
13
Geoffrey.py
13
Geoffrey.py
|
@ -1,6 +1,8 @@
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from DatabaseModels import *
|
from DatabaseModels import *
|
||||||
from BotErrors import *
|
from BotErrors import *
|
||||||
|
from MinecraftAccountInfoGrabber import *
|
||||||
|
|
||||||
|
|
||||||
TOKEN = ''
|
TOKEN = ''
|
||||||
command_prefix = '?'
|
command_prefix = '?'
|
||||||
|
@ -18,6 +20,8 @@ bot = commands.Bot(command_prefix=command_prefix, description=description, case_
|
||||||
|
|
||||||
database = GeoffreyDatabase('sqlite:///:memory:')
|
database = GeoffreyDatabase('sqlite:///:memory:')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Bot Commands ******************************************************************
|
# Bot Commands ******************************************************************
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
|
@ -33,6 +37,8 @@ async def on_command_error(error, ctx):
|
||||||
elif isinstance(error, commands.UserInputError):
|
elif isinstance(error, commands.UserInputError):
|
||||||
error_str = 'Invalid syntax for {} you ding dong, please read ?help {}.'\
|
error_str = 'Invalid syntax for {} you ding dong, please read ?help {}.'\
|
||||||
.format(ctx.invoked_with, ctx.invoked_with)
|
.format(ctx.invoked_with, ctx.invoked_with)
|
||||||
|
elif isinstance(error.original, UsernameLookupFailed):
|
||||||
|
error_str = error.original.__doc__
|
||||||
else:
|
else:
|
||||||
error_str = bad_error_message.format(ctx.invoked_with)
|
error_str = bad_error_message.format(ctx.invoked_with)
|
||||||
print(error)
|
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))
|
owner = Player(str(ctx.message.author.nick))
|
||||||
|
|
||||||
try:
|
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:
|
except LocationInitError:
|
||||||
raise commands.UserInputError
|
raise commands.UserInputError
|
||||||
|
|
||||||
|
@ -77,7 +83,8 @@ async def findbase(ctx, name: str):
|
||||||
?findbase [Player name]
|
?findbase [Player name]
|
||||||
'''
|
'''
|
||||||
|
|
||||||
expr = Location.owner == name
|
uuid = grab_UUID(name)
|
||||||
|
expr = Location.owner_uuid == uuid
|
||||||
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:
|
||||||
|
@ -107,7 +114,7 @@ async def deletebase(ctx, name: str):
|
||||||
|
|
||||||
user = str(ctx.message.author.nick)
|
user = str(ctx.message.author.nick)
|
||||||
|
|
||||||
expr = (Location.owner == user) & (Location.name == name)
|
expr = (Location.owner_uuid == user) & (Location.name == name)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
database.delete_entry(Location, expr)
|
database.delete_entry(Location, expr)
|
||||||
|
|
|
@ -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']
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,26 +7,26 @@ from BotErrors 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, 'owner', ['Green', 0])
|
self.loc = Location('test', 1, 2, 3, 'ZeroHD', ['Green', 0])
|
||||||
|
|
||||||
def test_add_object(self):
|
def test_add_object(self):
|
||||||
self.database.add_object(self.loc)
|
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)
|
self.assertEqual(self.loc.id, loc2.id)
|
||||||
|
|
||||||
def test_query_by_filter(self):
|
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)
|
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 == 'owner') & (Location.name == 'test')
|
expr = (Location.owner_uuid == 'ZeroHD') & (Location.name == 'test')
|
||||||
self.database.delete_entry(Location, expr)
|
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)
|
loc2 = self.database.query_by_filter(Location, expr)
|
||||||
|
|
||||||
self.assertEqual(len(loc2), 0)
|
self.assertEqual(len(loc2), 0)
|
||||||
|
@ -37,4 +37,3 @@ class TestGeoffreyDatabase(TestCase):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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')
|
Loading…
Reference in New Issue