Added minecraft id #181

This commit is contained in:
Sven Heidemann 2023-02-12 16:55:35 +01:00
parent bd4e088032
commit 2ef4d079ed
6 changed files with 65 additions and 9 deletions

View File

@ -33,5 +33,20 @@ class UserJoinedGameServerMigration(MigrationABC):
) )
) )
self._cursor.execute(
str(
f"""
ALTER TABLE Users ADD MinecraftId VARCHAR(255) NULL AFTER XP;
"""
)
)
def downgrade(self): def downgrade(self):
self._cursor.execute("DROP TABLE `UserJoinedGameServer`;") self._cursor.execute("DROP TABLE `UserJoinedGameServer`;")
self._cursor.execute(
str(
f"""
ALTER TABLE Users DROP COLUMN MinecraftId;
"""
)
)

View File

@ -11,6 +11,7 @@ class User(TableABC):
self, self,
dc_id: int, dc_id: int,
xp: int, xp: int,
minecraft_id: Optional[str],
server: Optional[Server], server: Optional[Server],
created_at: datetime = None, created_at: datetime = None,
modified_at: datetime = None, modified_at: datetime = None,
@ -19,6 +20,7 @@ class User(TableABC):
self._user_id = id self._user_id = id
self._discord_id = dc_id self._discord_id = dc_id
self._xp = xp self._xp = xp
self._minecraft_id = minecraft_id
self._server = server self._server = server
TableABC.__init__(self) TableABC.__init__(self)
@ -42,6 +44,10 @@ class User(TableABC):
self._modified_at = datetime.now().isoformat() self._modified_at = datetime.now().isoformat()
self._xp = value self._xp = value
@property
def minecraft_id(self) -> Optional[str]:
return self._minecraft_id
@property @property
def server(self) -> Optional[Server]: def server(self) -> Optional[Server]:
return self._server return self._server
@ -96,10 +102,11 @@ class User(TableABC):
return str( return str(
f""" f"""
INSERT INTO `Users` ( INSERT INTO `Users` (
`DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt` `DiscordId`, `XP`, `MinecraftId`, `ServerId`, `CreatedAt`, `LastModifiedAt`
) VALUES ( ) VALUES (
{self._discord_id}, {self._discord_id},
{self._xp}, {self._xp},
'{"NULL" if self._minecraft_id is None else self._minecraft_id}',
{self._server.server_id}, {self._server.server_id},
'{self._created_at}', '{self._created_at}',
'{self._modified_at}' '{self._modified_at}'
@ -113,6 +120,7 @@ class User(TableABC):
f""" f"""
UPDATE `Users` UPDATE `Users`
SET `XP` = {self._xp}, SET `XP` = {self._xp},
`MinecraftId` = '{"NULL" if self._minecraft_id is None else self._minecraft_id}',
`LastModifiedAt` = '{self._modified_at}' `LastModifiedAt` = '{self._modified_at}'
WHERE `UserId` = {self._user_id}; WHERE `UserId` = {self._user_id};
""" """

View File

@ -33,7 +33,10 @@ class UserRepositoryService(UserRepositoryABC):
User( User(
result[1], result[1],
result[2], result[2],
self._servers.get_server_by_id(result[3]), result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0], id=result[0],
) )
) )
@ -47,7 +50,10 @@ class UserRepositoryService(UserRepositoryABC):
return User( return User(
result[1], result[1],
result[2], result[2],
self._servers.get_server_by_id(result[3]), result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0], id=result[0],
) )
@ -62,7 +68,10 @@ class UserRepositoryService(UserRepositoryABC):
return User( return User(
result[1], result[1],
result[2], result[2],
self._servers.get_server_by_id(result[3]), result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0], id=result[0],
) )
@ -78,7 +87,10 @@ class UserRepositoryService(UserRepositoryABC):
User( User(
result[1], result[1],
result[2], result[2],
self._servers.get_server_by_id(result[3]), result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0], id=result[0],
) )
) )
@ -97,7 +109,10 @@ class UserRepositoryService(UserRepositoryABC):
User( User(
result[1], result[1],
result[2], result[2],
self._servers.get_server_by_id(result[3]), result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0], id=result[0],
) )
) )
@ -114,7 +129,10 @@ class UserRepositoryService(UserRepositoryABC):
return User( return User(
result[1], result[1],
result[2], result[2],
self._servers.get_server_by_id(result[3]), result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0], id=result[0],
) )
@ -132,9 +150,10 @@ class UserRepositoryService(UserRepositoryABC):
return User( return User(
result[1], result[1],
result[2], result[2],
self._servers.get_server_by_id(result[3]), result[3],
result[4], self._servers.get_server_by_id(result[4]),
result[5], result[5],
result[6],
id=result[0], id=result[0],
) )

View File

@ -30,6 +30,7 @@ class UserFilter(FilterABC):
self._discord_id = None self._discord_id = None
self._name = None self._name = None
self._xp = None self._xp = None
self._minecraft_id = None
self._ontime = None self._ontime = None
self._level: Optional[LevelFilter] = None self._level: Optional[LevelFilter] = None
@ -46,6 +47,9 @@ class UserFilter(FilterABC):
if "xp" in values: if "xp" in values:
self._xp = int(values["xp"]) self._xp = int(values["xp"])
if "minecraftId" in values:
self._minecraft_id = values["minecraftId"]
if "ontime" in values: if "ontime" in values:
self._ontime = int(values["ontime"]) self._ontime = int(values["ontime"])
@ -75,6 +79,9 @@ class UserFilter(FilterABC):
if self._xp is not None: if self._xp is not None:
query = query.where(lambda x: x.xp == self._xp) query = query.where(lambda x: x.xp == self._xp)
if self._minecraft_id is not None:
query = query.where(lambda x: x.minecraft_id == self._minecraft_id)
if self._ontime is not None: if self._ontime is not None:
query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime)

View File

@ -3,6 +3,7 @@ type User implements TableQuery {
discordId: String discordId: String
name: String name: String
xp: Int xp: Int
minecraftId: String
ontime: Float ontime: Float
level: Level level: Level
@ -26,6 +27,7 @@ input UserFilter {
discordId: String discordId: String
name: String name: String
xp: Int xp: Int
minecraftId: String
ontime: Float ontime: Float
level: LevelFilter level: LevelFilter
server: ServerFilter server: ServerFilter

View File

@ -35,6 +35,7 @@ class UserQuery(DataQueryABC):
self.set_field("discordId", self.resolve_discord_id) self.set_field("discordId", self.resolve_discord_id)
self.set_field("name", self.resolve_name) self.set_field("name", self.resolve_name)
self.set_field("xp", self.resolve_xp) self.set_field("xp", self.resolve_xp)
self.set_field("minecraftId", self.resolve_minecraft_id)
self.set_field("ontime", self.resolve_ontime) self.set_field("ontime", self.resolve_ontime)
self.set_field("level", self.resolve_level) self.set_field("level", self.resolve_level)
self.add_collection( self.add_collection(
@ -71,6 +72,10 @@ class UserQuery(DataQueryABC):
def resolve_xp(user: User, *_): def resolve_xp(user: User, *_):
return user.xp return user.xp
@staticmethod
def resolve_minecraft_id(user: User, *_):
return user.minecraft_id
def resolve_ontime(self, user: User, *_): def resolve_ontime(self, user: User, *_):
return self._client_utils.get_ontime_for_user(user) return self._client_utils.get_ontime_for_user(user)