From 6ca93f0ade47d711df35905b6026c5b4fbf5a529 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 21 Aug 2023 03:13:29 +0700 Subject: [PATCH] Improve db-related commands --- sources/Commands/ACommandDB.uc | 30 ++++--- sources/Commands/ACommandUserData.uc | 113 +++++++++++++++------------ 2 files changed, 78 insertions(+), 65 deletions(-) diff --git a/sources/Commands/ACommandDB.uc b/sources/Commands/ACommandDB.uc index 8f6cca9..bbc2b09 100644 --- a/sources/Commands/ACommandDB.uc +++ b/sources/Commands/ACommandDB.uc @@ -1,6 +1,6 @@ /** * Command for working with databases. - * Copyright 2021-2022 Anton Tarasenko + * Copyright 2021-2023 Anton Tarasenko *------------------------------------------------------------------------------ * This file is part of Acedia. * @@ -329,7 +329,8 @@ protected function OutputStatus( protected function DisplayData( Database.DBQueryResult result, AcediaObject data, - Database source) + Database source, + int requestID) { local Text printedJSON; local EPlayer instigator; @@ -350,7 +351,8 @@ protected function DisplayData( protected function DisplaySize( Database.DBQueryResult result, int size, - Database source) + Database source, + int requestID) { local Text sizeAsText; local EPlayer instigator; @@ -373,7 +375,8 @@ protected function DisplaySize( protected function DisplayKeys( Database.DBQueryResult result, ArrayList keys, - Database source) + Database source, + int requestID) { local int i; local Text nextKey; @@ -407,7 +410,8 @@ protected function DisplayKeys( protected function DisplayResponse( Database.DBQueryResult result, - Database source) + Database source, + int requestID) { local EPlayer instigator; @@ -449,25 +453,25 @@ defaultproperties TQUERY_COMPLETED = 14 stringConstants(14) = "{$TextPositive Database query was completed!}" TQUERY_INVALID_POINTER = 15 - stringConstants(15) = "{$TextNegative Query was provided with an invalid JSON pointer.}" + stringConstants(15) = "{$TextFailure Query was provided with an invalid JSON pointer.}" TQUERY_INVALID_DB = 16 - stringConstants(16) = "{$TextNegative Operation could not finish because database is damaged and unusable.}" + stringConstants(16) = "{$TextFailure Operation could not finish because database is damaged and unusable.}" TQUERY_INVALID_DATA = 17 - stringConstants(17) = "{$TextNegative Query data is invalid.}" + stringConstants(17) = "{$TextFailure Query data is invalid.}" TAVAILABLE_DATABASES = 18 stringConstants(18) = "{$TextEmphasis Available databases:} " TDA_DELETED = 19 stringConstants(19) = "{$TextPositive Database was deleted.}" TDB_DOESNT_EXIST = 20 - stringConstants(20) = "{$TextNegative Database with specified name does not exist.}" + stringConstants(20) = "{$TextFailure Database with specified name does not exist.}" TDB_ALREADY_EXISTS = 21 - stringConstants(21) = "{$TextNegative Database with specified name already exists.}" + stringConstants(21) = "{$TextFailure Database with specified name already exists.}" TDB_CREATED = 22 stringConstants(22) = "{$TextPositive Database was created.}" TDB_CANNOT_BE_CREATED = 23 - stringConstants(23) = "{$TextNegative Database cannot be created.}" + stringConstants(23) = "{$TextFailure Database cannot be created.}" TNO_DEFAULT_COMMAND = 24 - stringConstants(24) = "{$TextNegative Default command does nothing. Use on of the sub-commands.}" + stringConstants(24) = "{$TextFailure Default command does nothing. Use on of the sub-commands.}" TBAD_DBLINK = 25 - stringConstants(25) = "{$TextNegative Database could not be read for the specified link.}" + stringConstants(25) = "{$TextFailure Database could not be read for the specified link.}" } \ No newline at end of file diff --git a/sources/Commands/ACommandUserData.uc b/sources/Commands/ACommandUserData.uc index 510580f..b080764 100644 --- a/sources/Commands/ACommandUserData.uc +++ b/sources/Commands/ACommandUserData.uc @@ -1,6 +1,6 @@ /** * Command for changing amount of money players have. - * Copyright 2022 Anton Tarasenko + * Copyright 2022-2023 Anton Tarasenko *------------------------------------------------------------------------------ * This file is part of Acedia. * @@ -21,7 +21,6 @@ class ACommandUserData extends Command; var private array playerQueue; -// TODO: Finish this command after JSON parameters are added protected function BuildData(CommandDataBuilder builder) { builder.Name(P("userdata")).Group(P("admin")) @@ -33,7 +32,7 @@ protected function BuildData(CommandDataBuilder builder) .Describe(F("Reads user data stored for targeted user under group" @ "{$TextEmphasis `groupName`} and name" @ "{$TextEmphasis `dataName`}. If {$TextEmphasis `dataName`} is" - @ "omitted, the data inside the whjole group will be read.")); + @ "omitted, the data inside the whole group will be read.")); builder.SubCommand(P("write")) .ParamText(P("groupName")) .ParamText(P("dataName")) @@ -49,69 +48,79 @@ protected function ExecutedFor( EPlayer instigator) { local AcediaObject userData; + local Text groupName, dataName; - if (arguments.subCommandName.IsEmpty()) - { - target - .GetIdentity() - .ReadPersistentData( - arguments.parameters.GetText(P("groupName")), - arguments.parameters.GetText(P("dataName"))) - .connect = UserDataRead; + groupName = arguments.parameters.GetText(P("groupName")); + dataName = arguments.parameters.GetText(P("dataName")); + userData = arguments.parameters.GetItem(P("newData")); + if (arguments.subCommandName.IsEmpty()) { + ReadUserData(target, groupName, dataName); } - else - { - userData = arguments.parameters.GetHashTable(P("newData")); - target - .GetIdentity() - .WritePersistentData( - arguments.parameters.GetText(P("groupName")), - arguments.parameters.GetText(P("dataName")), - userData); + else { + WriteUserData(target, groupName, dataName, userData); } - playerQueue[playerQueue.length] = target; - target.NewRef(); + _.memory.Free(dataName); + _.memory.Free(groupName); } -private final function UserDataRead( - Database.DBQueryResult result, - AcediaObject userData, - Database source) +private final function ReadUserData( + EPlayer targetPlayer, + BaseText groupName, + BaseText dataName) { + local User identity; + local AcediaObject rawData; + local MutableText dataAsJSON; local Text targetPlayerName; - local EPlayer targetPlayer; - local MutableText asJSON; - if (playerQueue.length <= 0) - { - targetPlayer - .BorrowConsole() - .UseColorOnce(_.color.TextFailure) - .Write(P("There was an internal error with `userdata` command. ")) - .Write(P("Please report it!")); - return; - } - targetPlayer = playerQueue[0]; - playerQueue.Remove(0, 1); - if (result != DBR_Success) - { - targetPlayer - .BorrowConsole() - .UseColorOnce(_.color.TextFailure) - .Write(F("There was an error reading user data, error code: ")) - .WriteLine(_.text.FromInt(int(result))); + identity = targetPlayer.GetIdentity(); + if (identity == none) { return; } targetPlayerName = targetPlayer.GetName(); - asJSON = _.json.PrettyPrint(userData); + rawData = identity.GetPersistentData(groupName, dataName); + dataAsJSON = _.json.PrettyPrint(rawData); targetPlayer.BorrowConsole() - .Write(F("{$TextPositive User data for player}")) + .Write(F("User data for player ")) .Write(targetPlayerName) - .Write(P(":")) - .WriteLine(asJSON); - _.memory.Free(targetPlayer); - _.memory.Free(asJSON); + .Write(P(": ")) + .WriteLine(dataAsJSON); + _.memory.Free(dataAsJSON); + _.memory.Free(rawData); + _.memory.Free(targetPlayerName); + _.memory.Free(identity); +} + +private final function WriteUserData( + EPlayer targetPlayer, + BaseText groupName, + BaseText dataName, + AcediaObject rawData) +{ + local User identity; + local Text targetPlayerName; + + identity = targetPlayer.GetIdentity(); + if (identity == none) { + return; + } + targetPlayerName = targetPlayer.GetName(); + if (identity.SetPersistentData(groupName, dataName, rawData)) + { + targetPlayer.BorrowConsole() + .Write(P("User data for player ")) + .Write(targetPlayerName) + .WriteLine(F(" was {$TextPositive successfully} changed!")); + } + else + { + targetPlayer.BorrowConsole() + .Write(P("User data for player ")) + .Write(targetPlayerName) + .WriteLine(F(" has {$TextPositive failed} to change!")); + } _.memory.Free(targetPlayerName); + _.memory.Free(identity); } defaultproperties