Browse Source

Improve db-related commands

develop
Anton Tarasenko 1 year ago
parent
commit
6ca93f0ade
  1. 30
      sources/Commands/ACommandDB.uc
  2. 113
      sources/Commands/ACommandUserData.uc

30
sources/Commands/ACommandDB.uc

@ -1,6 +1,6 @@
/** /**
* Command for working with databases. * Command for working with databases.
* Copyright 2021-2022 Anton Tarasenko * Copyright 2021-2023 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -329,7 +329,8 @@ protected function OutputStatus(
protected function DisplayData( protected function DisplayData(
Database.DBQueryResult result, Database.DBQueryResult result,
AcediaObject data, AcediaObject data,
Database source) Database source,
int requestID)
{ {
local Text printedJSON; local Text printedJSON;
local EPlayer instigator; local EPlayer instigator;
@ -350,7 +351,8 @@ protected function DisplayData(
protected function DisplaySize( protected function DisplaySize(
Database.DBQueryResult result, Database.DBQueryResult result,
int size, int size,
Database source) Database source,
int requestID)
{ {
local Text sizeAsText; local Text sizeAsText;
local EPlayer instigator; local EPlayer instigator;
@ -373,7 +375,8 @@ protected function DisplaySize(
protected function DisplayKeys( protected function DisplayKeys(
Database.DBQueryResult result, Database.DBQueryResult result,
ArrayList keys, ArrayList keys,
Database source) Database source,
int requestID)
{ {
local int i; local int i;
local Text nextKey; local Text nextKey;
@ -407,7 +410,8 @@ protected function DisplayKeys(
protected function DisplayResponse( protected function DisplayResponse(
Database.DBQueryResult result, Database.DBQueryResult result,
Database source) Database source,
int requestID)
{ {
local EPlayer instigator; local EPlayer instigator;
@ -449,25 +453,25 @@ defaultproperties
TQUERY_COMPLETED = 14 TQUERY_COMPLETED = 14
stringConstants(14) = "{$TextPositive Database query was completed!}" stringConstants(14) = "{$TextPositive Database query was completed!}"
TQUERY_INVALID_POINTER = 15 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 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 TQUERY_INVALID_DATA = 17
stringConstants(17) = "{$TextNegative Query data is invalid.}" stringConstants(17) = "{$TextFailure Query data is invalid.}"
TAVAILABLE_DATABASES = 18 TAVAILABLE_DATABASES = 18
stringConstants(18) = "{$TextEmphasis Available databases:} " stringConstants(18) = "{$TextEmphasis Available databases:} "
TDA_DELETED = 19 TDA_DELETED = 19
stringConstants(19) = "{$TextPositive Database was deleted.}" stringConstants(19) = "{$TextPositive Database was deleted.}"
TDB_DOESNT_EXIST = 20 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 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 TDB_CREATED = 22
stringConstants(22) = "{$TextPositive Database was created.}" stringConstants(22) = "{$TextPositive Database was created.}"
TDB_CANNOT_BE_CREATED = 23 TDB_CANNOT_BE_CREATED = 23
stringConstants(23) = "{$TextNegative Database cannot be created.}" stringConstants(23) = "{$TextFailure Database cannot be created.}"
TNO_DEFAULT_COMMAND = 24 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 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.}"
} }

113
sources/Commands/ACommandUserData.uc

@ -1,6 +1,6 @@
/** /**
* Command for changing amount of money players have. * Command for changing amount of money players have.
* Copyright 2022 Anton Tarasenko * Copyright 2022-2023 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -21,7 +21,6 @@ class ACommandUserData extends Command;
var private array<EPlayer> playerQueue; var private array<EPlayer> playerQueue;
// TODO: Finish this command after JSON parameters are added
protected function BuildData(CommandDataBuilder builder) protected function BuildData(CommandDataBuilder builder)
{ {
builder.Name(P("userdata")).Group(P("admin")) 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" .Describe(F("Reads user data stored for targeted user under group"
@ "{$TextEmphasis `groupName`} and name" @ "{$TextEmphasis `groupName`} and name"
@ "{$TextEmphasis `dataName`}. If {$TextEmphasis `dataName`} is" @ "{$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")) builder.SubCommand(P("write"))
.ParamText(P("groupName")) .ParamText(P("groupName"))
.ParamText(P("dataName")) .ParamText(P("dataName"))
@ -49,69 +48,79 @@ protected function ExecutedFor(
EPlayer instigator) EPlayer instigator)
{ {
local AcediaObject userData; local AcediaObject userData;
local Text groupName, dataName;
if (arguments.subCommandName.IsEmpty()) groupName = arguments.parameters.GetText(P("groupName"));
{ dataName = arguments.parameters.GetText(P("dataName"));
target userData = arguments.parameters.GetItem(P("newData"));
.GetIdentity() if (arguments.subCommandName.IsEmpty()) {
.ReadPersistentData( ReadUserData(target, groupName, dataName);
arguments.parameters.GetText(P("groupName")),
arguments.parameters.GetText(P("dataName")))
.connect = UserDataRead;
} }
else else {
{ WriteUserData(target, groupName, dataName, userData);
userData = arguments.parameters.GetHashTable(P("newData"));
target
.GetIdentity()
.WritePersistentData(
arguments.parameters.GetText(P("groupName")),
arguments.parameters.GetText(P("dataName")),
userData);
} }
playerQueue[playerQueue.length] = target; _.memory.Free(dataName);
target.NewRef(); _.memory.Free(groupName);
} }
private final function UserDataRead( private final function ReadUserData(
Database.DBQueryResult result, EPlayer targetPlayer,
AcediaObject userData, BaseText groupName,
Database source) BaseText dataName)
{ {
local User identity;
local AcediaObject rawData;
local MutableText dataAsJSON;
local Text targetPlayerName; local Text targetPlayerName;
local EPlayer targetPlayer;
local MutableText asJSON;
if (playerQueue.length <= 0) identity = targetPlayer.GetIdentity();
{ if (identity == none) {
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)));
return; return;
} }
targetPlayerName = targetPlayer.GetName(); targetPlayerName = targetPlayer.GetName();
asJSON = _.json.PrettyPrint(userData); rawData = identity.GetPersistentData(groupName, dataName);
dataAsJSON = _.json.PrettyPrint(rawData);
targetPlayer.BorrowConsole() targetPlayer.BorrowConsole()
.Write(F("{$TextPositive User data for player}")) .Write(F("User data for player "))
.Write(targetPlayerName) .Write(targetPlayerName)
.Write(P(":")) .Write(P(": "))
.WriteLine(asJSON); .WriteLine(dataAsJSON);
_.memory.Free(targetPlayer); _.memory.Free(dataAsJSON);
_.memory.Free(asJSON); _.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(targetPlayerName);
_.memory.Free(identity);
} }
defaultproperties defaultproperties

Loading…
Cancel
Save