Browse Source

Refactor `InventoryTool` to use `TextTemplates`

feature_improvement
Anton Tarasenko 2 years ago
parent
commit
1390d3ad47
  1. 32
      sources/Commands/ACommandInventory.uc
  2. 253
      sources/Tools/InventoryTool.uc
  3. 14
      sources/Tools/ListBuilder.uc

32
sources/Commands/ACommandInventory.uc

@ -92,38 +92,40 @@ protected function BuildData(CommandDataBuilder builder)
}
protected function ExecutedFor(
EPlayer player,
CallData result,
EPlayer callerPlayer)
EPlayer target,
CallData arguments,
EPlayer instigator)
{
local InventoryTool tool;
local DynamicArray itemsArray, specifiedLists;
LoadUserFlags(result.options);
tool = class'InventoryTool'.static.CreateFor(player);
LoadUserFlags(arguments.options);
tool = class'InventoryTool'.static.CreateFor(target);
if (tool == none) {
return;
}
itemsArray = result.parameters.GetDynamicArray(T(TITEMS));
specifiedLists = result.options.GetDynamicArrayBy(P("/list/lists names"));
if (result.subCommandName.IsEmpty()) {
itemsArray = arguments.parameters.GetDynamicArray(T(TITEMS));
specifiedLists = arguments.options
.GetDynamicArrayBy(P("/list/lists names"));
if (arguments.subCommandName.IsEmpty()) {
tool.ReportInventory(callerConsole, flagHidden);
}
else if (result.subCommandName.Compare(T(TADD))) {
else if (arguments.subCommandName.Compare(T(TADD))) {
SubCommandAdd(tool, itemsArray, specifiedLists);
}
else if (result.subCommandName.Compare(T(TREMOVE))) {
else if (arguments.subCommandName.Compare(T(TREMOVE))) {
SubCommandRemove(tool, itemsArray, specifiedLists);
}
else if (result.subCommandName.Compare(T(TSET)))
else if (arguments.subCommandName.Compare(T(TSET)))
{
tool.RemoveAllItems(flagKeep, flagForce, flagHidden);
SubCommandAdd(tool, itemsArray, specifiedLists);
}
if (!callerPlayer.SameAs(player)) {
tool.ReportChanges(callerPlayer, targetConsole, IRT_Target);
tool.SetupReportInstigator(instigator);
if (!instigator.SameAs(target)) {
tool.ReportChanges(instigator, targetConsole, IRT_Target);
}
tool.ReportChanges(callerPlayer, callerConsole, IRT_Instigator);
tool.ReportChanges(callerPlayer, othersConsole, IRT_Others);
tool.ReportChanges(instigator, callerConsole, IRT_Instigator);
tool.ReportChanges(instigator, othersConsole, IRT_Others);
_.memory.Free(tool);
}

253
sources/Tools/InventoryTool.uc

@ -67,14 +67,11 @@ var private TextTemplate templateItemsAdded, templateItemsRemoved;
var private TextTemplate templateItemsAddedVerbose, templateItemsRemovedVerbose;
var private TextTemplate templateAdditionFailed, templateRemovalFailed;
var const int TINSTIGATOR, TTARGET;
var const int TITEMS_REMOVED_MESSAGE, TITEMS_REMOVED_VERBOSE_MESSAGE;
var const int TITEMS_ADDITION_FAILED_MESSAGE, TITEMS_REMOVAL_FAILED_MESSAGE;
var const int TRESOLVED_INTO, TTILDE_QUOTE, TFAULTY_INVENTORY_IMPLEMENTATION;
var const int TINSTIGATOR, TTARGET, TRESOLVED_INTO, TTILDE_QUOTE;
var const int TITEM_MISSING, TITEM_NOT_REMOVABLE, TUNKNOWN, TVISIBLE;
var const int TDISPLAYING_INVENTORY, THEADER_COLON, TDOT_SPACE, TCOLON_SPACE;
var const int TCOMMA_SPACE, TSPACE, TOUT_OF, THIDDEN_ITEMS, TDOLLAR, TYOU;
var const int TYOURSELF, TTHEMSELVES;
var const int TTHEMSELVES, TFAULTY_INVENTORY_IMPLEMENTATION;
public static function StaticConstructor()
{
@ -85,10 +82,10 @@ public static function StaticConstructor()
"%%instigator%% {$TextPositive added} following weapons to"
@ "%%target%%: ");
default.templateItemsRemoved = __().text.MakeTemplate_S(
"Weapons {$TextPositive added} to %%target%%: ");
default.templateItemsAddedVerbose = __().text.MakeTemplate_S(
"%%instigator%% {$TextNegative removed} following weapons from"
@ "%%target%%: ");
default.templateItemsAddedVerbose = __().text.MakeTemplate_S(
"Weapons {$TextPositive added} to %%target%%: ");
default.templateItemsRemovedVerbose = __().text.MakeTemplate_S(
"Weapons {$TextNegative removed} from %%target%%: ");
default.templateAdditionFailed = __().text.MakeTemplate_S(
@ -490,6 +487,52 @@ public function RemoveEquippedItems(
_.memory.FreeMany(equippedItems);
}
/**
* Tells `InventoryTool` which player is responsible for the changes it is
* reporting. This information is used to choose the phrasing of the reported
* messages.
*
* @param instigator Player that supposedly requested all the changes done by
* the calller `InventoryTool`.
*/
public final function SetupReportInstigator(EPlayer instigator)
{
local MutableText instigatorName, targetName;
if (TargetPlayerIsInvalid()) return;
if (instigator == none) return;
instigatorName = ColorNickname(instigator.GetName());
if (!targetPlayer.SameAs(instigator)) {
targetName = ColorNickname(targetPlayer.GetName());
}
else {
targetName = T(TYOU).MutableCopy();
}
// For instigator
default.templateItemsAdded.Reset().TextArg(T(TINSTIGATOR), instigatorName);
default.templateItemsRemoved
.Reset()
.TextArg(T(TINSTIGATOR), instigatorName);
// For everybody else
default.templateAdditionFailed.Reset().TextArg(T(TTARGET), targetName);
default.templateRemovalFailed.Reset().TextArg(T(TTARGET), targetName);
default.templateItemsAddedVerbose.Reset().TextArg(T(TTARGET), targetName);
default.templateItemsRemovedVerbose.Reset().TextArg(T(TTARGET), targetName);
_.memory.Free(instigatorName);
_.memory.Free(targetName);
}
private final function MutableText ColorNickname(/* take */ BaseText nickname)
{
if (nickname == none) {
return none;
}
return nickname
.IntoMutableText()
.ChangeDefaultColor(_.color.Gray);
}
/**
* Reports changes made to the player's inventory so far.
*
@ -505,79 +548,58 @@ public function RemoveEquippedItems(
* be reported.
*/
public final function ReportChanges(
EPlayer blamedPlayer,
EPlayer instigator,
ConsoleWriter writer,
InventoryReportTarget reportTarget)
{
local Text blamedName, targetName;
if (TargetPlayerIsInvalid()) return;
if (blamedPlayer == none) return;
blamedName = blamedPlayer.GetName();
if (!targetPlayer.SameAs(blamedPlayer)) {
targetName = targetPlayer.GetName();
}
else
{
if (reportTarget == IRT_Instigator) {
targetName = T(TYOURSELF).Copy();
}
else if (reportTarget == IRT_Target) {
targetName = T(TYOU).Copy();
}
else {
targetName = T(TTHEMSELVES).Copy();
}
if (TargetPlayerIsInvalid()) {
return;
}
default.templateItemsAdded
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
default.templateItemsRemoved
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
if (reportTarget == IRT_Others)
if (reportTarget != IRT_Instigator)
{
ReportWeaponList(writer, default.templateItemsAdded, itemsAdded);
SwapTargetNameInTemplates(instigator, reportTarget);
ReportWeaponList(writer, default.templateItemsRemoved, itemsRemoved);
}
else if (reportTarget == IRT_Target)
{
ReportWeaponList(writer, default.templateItemsAdded, itemsAdded);
ReportWeaponList(writer, default.templateItemsRemoved, itemsRemoved);
return;
}
else if (reportTarget == IRT_Instigator)
{
default.templateItemsAddedVerbose
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
default.templateItemsRemovedVerbose
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
default.templateAdditionFailed
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
default.templateremovalFailed
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
ReportWeaponList(
writer,
default.templateItemsAddedVerbose,
itemsAddedPrivate);
ReportWeaponList(
writer,
default.templateItemsRemovedVerbose,
itemsRemovedPrivate);
ReportWeaponList(
writer,
default.templateAdditionFailed,
itemsAdditionFailed);
default.templateRemovalFailed,
itemsRemovalFailed);
ReportWeaponList(
writer,
default.templateremovalFailed,
itemsRemovalFailed);
default.templateItemsAddedVerbose,
itemsAddedPrivate);
ReportWeaponList(
writer,
default.templateAdditionFailed,
itemsAdditionFailed);
}
private final function SwapTargetNameInTemplates(
EPlayer instigator,
InventoryReportTarget reportTarget)
{
local MutableText targetName;
if (TargetPlayerIsInvalid()) {
return;
}
if (!targetPlayer.SameAs(instigator)) {
targetName = ColorNickname(targetPlayer.GetName());
}
else if (reportTarget == IRT_Target) {
targetName = T(TYOU).MutableCopy();
}
else {
targetName = T(TTHEMSELVES).MutableCopy();
}
_.memory.Free(blamedName);
default.templateItemsAdded.TextArg(T(TTARGET), targetName);
default.templateItemsRemoved.TextArg(T(TTARGET), targetName);
_.memory.Free(targetName);
}
@ -588,9 +610,10 @@ private final function ReportWeaponList(
{
local MutableText output;
if (writer == none) {
return;
}
if (writer == none) return;
if (builder == none) return;
if (builder.IsEmpty()) return;
if (header != none)
{
output = header.CollectFormattedM();
@ -598,20 +621,12 @@ private final function ReportWeaponList(
_.memory.Free(output);
output = none;
}
if (builder != none) {
output = builder.GetMutable();
}
writer.WriteLine(output);
_.memory.Free(output);
}
/* itemsAdded.Initialize(T(TITEMS_ADDED_MESSAGE));
itemsRemoved.Initialize(T(TITEMS_REMOVED_MESSAGE));
itemsAddedPrivate.Initialize(T(TITEMS_ADDED_VEROBSE_MESSAGE));
itemsRemovedPrivate.Initialize(T(TITEMS_REMOVED_VERBOSE_MESSAGE));
itemsAdditionFailed.Initialize(T(TITEMS_ADDITION_FAILED_MESSAGE));
itemsRemovalFailed.Initialize(T(TITEMS_REMOVAL_FAILED_MESSAGE)); */
// TODO: Use `ListBuilder` for the below method?
/**
* Command that outputs summary of the player's inventory.
*
@ -738,56 +753,44 @@ private final function AppendAmmoInstanceInfo(MutableText builder, EAmmo ammo)
defaultproperties
{
// TODO remove old constants
// TODO refactor `ReportChanges()`
TINSTIGATOR = 0
stringConstants(0) = "%%instigator%%"
stringConstants(0) = "instigator"
TTARGET = 1
stringConstants(1) = "%%target%%"
TITEMS_REMOVED_MESSAGE = 2
stringConstants(2) = "%%instigator%% {$TextNegative removed} following weapons from %%target%%:"
TITEMS_REMOVED_VERBOSE_MESSAGE = 3
stringConstants(3) = "Weapons {$TextNegative removed} from %%target%%:"
TITEMS_ADDITION_FAILED_MESSAGE = 4
stringConstants(4) = "Weapons we've {$TextFailure failed} to add to %%target%%:"
TITEMS_REMOVAL_FAILED_MESSAGE = 5
stringConstants(5) = "Weapons we've {$TextFailure failed} to remove from %%target%%:"
TRESOLVED_INTO = 6
stringConstants(6) = "` resolved into `"
TTILDE_QUOTE = 7
stringConstants(7) = "`"
TFAULTY_INVENTORY_IMPLEMENTATION = 8
stringConstants(8) = "faulty inventory implementation"
TITEM_MISSING = 9
stringConstants(9) = "item missing"
TITEM_NOT_REMOVABLE = 10
stringConstants(10) = "item not removable"
TUNKNOWN = 11
stringConstants(11) = "unknown"
TVISIBLE = 12
stringConstants(12) = "visible"
TDISPLAYING_INVENTORY = 13
stringConstants(13) = "{$TextHeader Displaying inventory for player }"
THEADER_COLON = 14
stringConstants(14) = "{$TextHeader :}"
TDOT_SPACE = 15
stringConstants(15) = ". "
TCOLON_SPACE = 16
stringConstants(16) = ": "
TCOMMA_SPACE = 17
stringConstants(17) = ", "
TSPACE = 18
stringConstants(18) = " "
TOUT_OF = 19
stringConstants(19) = " out of "
THIDDEN_ITEMS = 20
stringConstants(20) = "{$TextSubHeader Hidden items:}"
TDOLLAR = 21
stringConstants(21) = "$"
TYOU = 22
stringConstants(22) = "you"
TYOURSELF = 23
stringConstants(23) = "yourself"
TTHEMSELVES = 24
stringConstants(24) = "themselves"
stringConstants(1) = "target"
TRESOLVED_INTO = 2
stringConstants(2) = "` resolved into `"
TTILDE_QUOTE = 3
stringConstants(3) = "`"
TFAULTY_INVENTORY_IMPLEMENTATION = 4
stringConstants(4) = "faulty inventory implementation"
TITEM_MISSING = 5
stringConstants(5) = "item missing"
TITEM_NOT_REMOVABLE = 6
stringConstants(6) = "item not removable"
TUNKNOWN = 7
stringConstants(7) = "unknown"
TVISIBLE = 8
stringConstants(8) = "visible"
TDISPLAYING_INVENTORY = 9
stringConstants(9) = "{$TextHeader Displaying inventory for player }"
THEADER_COLON = 10
stringConstants(10) = "{$TextHeader :}"
TDOT_SPACE = 11
stringConstants(11) = ". "
TCOLON_SPACE = 12
stringConstants(12) = ": "
TCOMMA_SPACE = 13
stringConstants(13) = ", "
TSPACE = 14
stringConstants(14) = " "
TOUT_OF = 15
stringConstants(15) = " out of "
THIDDEN_ITEMS = 16
stringConstants(16) = "{$TextSubHeader Hidden items:}"
TDOLLAR = 17
stringConstants(17) = "$"
TYOU = 18
stringConstants(18) = "you"
TTHEMSELVES = 19
stringConstants(19) = "themselves"
}

14
sources/Tools/ListBuilder.uc

@ -62,6 +62,17 @@ protected function Finalizer()
Reset();
}
/**
* Checks if caller `ListBuilder` already has any items added.
*
* @return `true` if caller `ListBuilder` doesn't have any items added and
* `false` if it has at least one.
*/
public final function bool IsEmpty()
{
return (collectedItems.length <= 0);
}
/**
* Adds new `item` to the current report.
*
@ -206,8 +217,7 @@ public final function MutableText IntoMutableText()
/**
* Forgets all items or comments specified for the caller `ListBuilder` so far,
* allowing to start forming a new report. Does not reset template header,
* specified in the `Initialize()` method.
* allowing to start forming a new report.
*
* @return Reference to the caller `ListBuilder` to allow for method chaining.
*/

Loading…
Cancel
Save