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( protected function ExecutedFor(
EPlayer player, EPlayer target,
CallData result, CallData arguments,
EPlayer callerPlayer) EPlayer instigator)
{ {
local InventoryTool tool; local InventoryTool tool;
local DynamicArray itemsArray, specifiedLists; local DynamicArray itemsArray, specifiedLists;
LoadUserFlags(result.options); LoadUserFlags(arguments.options);
tool = class'InventoryTool'.static.CreateFor(player); tool = class'InventoryTool'.static.CreateFor(target);
if (tool == none) { if (tool == none) {
return; return;
} }
itemsArray = result.parameters.GetDynamicArray(T(TITEMS)); itemsArray = arguments.parameters.GetDynamicArray(T(TITEMS));
specifiedLists = result.options.GetDynamicArrayBy(P("/list/lists names")); specifiedLists = arguments.options
if (result.subCommandName.IsEmpty()) { .GetDynamicArrayBy(P("/list/lists names"));
if (arguments.subCommandName.IsEmpty()) {
tool.ReportInventory(callerConsole, flagHidden); tool.ReportInventory(callerConsole, flagHidden);
} }
else if (result.subCommandName.Compare(T(TADD))) { else if (arguments.subCommandName.Compare(T(TADD))) {
SubCommandAdd(tool, itemsArray, specifiedLists); SubCommandAdd(tool, itemsArray, specifiedLists);
} }
else if (result.subCommandName.Compare(T(TREMOVE))) { else if (arguments.subCommandName.Compare(T(TREMOVE))) {
SubCommandRemove(tool, itemsArray, specifiedLists); SubCommandRemove(tool, itemsArray, specifiedLists);
} }
else if (result.subCommandName.Compare(T(TSET))) else if (arguments.subCommandName.Compare(T(TSET)))
{ {
tool.RemoveAllItems(flagKeep, flagForce, flagHidden); tool.RemoveAllItems(flagKeep, flagForce, flagHidden);
SubCommandAdd(tool, itemsArray, specifiedLists); SubCommandAdd(tool, itemsArray, specifiedLists);
} }
if (!callerPlayer.SameAs(player)) { tool.SetupReportInstigator(instigator);
tool.ReportChanges(callerPlayer, targetConsole, IRT_Target); if (!instigator.SameAs(target)) {
tool.ReportChanges(instigator, targetConsole, IRT_Target);
} }
tool.ReportChanges(callerPlayer, callerConsole, IRT_Instigator); tool.ReportChanges(instigator, callerConsole, IRT_Instigator);
tool.ReportChanges(callerPlayer, othersConsole, IRT_Others); tool.ReportChanges(instigator, othersConsole, IRT_Others);
_.memory.Free(tool); _.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 templateItemsAddedVerbose, templateItemsRemovedVerbose;
var private TextTemplate templateAdditionFailed, templateRemovalFailed; var private TextTemplate templateAdditionFailed, templateRemovalFailed;
var const int TINSTIGATOR, TTARGET; var const int TINSTIGATOR, TTARGET, TRESOLVED_INTO, TTILDE_QUOTE;
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 TITEM_MISSING, TITEM_NOT_REMOVABLE, TUNKNOWN, TVISIBLE; var const int TITEM_MISSING, TITEM_NOT_REMOVABLE, TUNKNOWN, TVISIBLE;
var const int TDISPLAYING_INVENTORY, THEADER_COLON, TDOT_SPACE, TCOLON_SPACE; 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 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() public static function StaticConstructor()
{ {
@ -85,10 +82,10 @@ public static function StaticConstructor()
"%%instigator%% {$TextPositive added} following weapons to" "%%instigator%% {$TextPositive added} following weapons to"
@ "%%target%%: "); @ "%%target%%: ");
default.templateItemsRemoved = __().text.MakeTemplate_S( default.templateItemsRemoved = __().text.MakeTemplate_S(
"Weapons {$TextPositive added} to %%target%%: ");
default.templateItemsAddedVerbose = __().text.MakeTemplate_S(
"%%instigator%% {$TextNegative removed} following weapons from" "%%instigator%% {$TextNegative removed} following weapons from"
@ "%%target%%: "); @ "%%target%%: ");
default.templateItemsAddedVerbose = __().text.MakeTemplate_S(
"Weapons {$TextPositive added} to %%target%%: ");
default.templateItemsRemovedVerbose = __().text.MakeTemplate_S( default.templateItemsRemovedVerbose = __().text.MakeTemplate_S(
"Weapons {$TextNegative removed} from %%target%%: "); "Weapons {$TextNegative removed} from %%target%%: ");
default.templateAdditionFailed = __().text.MakeTemplate_S( default.templateAdditionFailed = __().text.MakeTemplate_S(
@ -490,6 +487,52 @@ public function RemoveEquippedItems(
_.memory.FreeMany(equippedItems); _.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. * Reports changes made to the player's inventory so far.
* *
@ -505,79 +548,58 @@ public function RemoveEquippedItems(
* be reported. * be reported.
*/ */
public final function ReportChanges( public final function ReportChanges(
EPlayer blamedPlayer, EPlayer instigator,
ConsoleWriter writer, ConsoleWriter writer,
InventoryReportTarget reportTarget) InventoryReportTarget reportTarget)
{ {
local Text blamedName, targetName; if (TargetPlayerIsInvalid()) {
return;
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();
}
} }
default.templateItemsAdded if (reportTarget != IRT_Instigator)
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
default.templateItemsRemoved
.TextArg(T(TINSTIGATOR), blamedName)
.TextArg(T(TTARGET), targetName);
if (reportTarget == IRT_Others)
{ {
ReportWeaponList(writer, default.templateItemsAdded, itemsAdded); SwapTargetNameInTemplates(instigator, reportTarget);
ReportWeaponList(writer, default.templateItemsRemoved, itemsRemoved); ReportWeaponList(writer, default.templateItemsRemoved, itemsRemoved);
}
else if (reportTarget == IRT_Target)
{
ReportWeaponList(writer, default.templateItemsAdded, itemsAdded); 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( ReportWeaponList(
writer, writer,
default.templateItemsRemovedVerbose, default.templateItemsRemovedVerbose,
itemsRemovedPrivate); itemsRemovedPrivate);
ReportWeaponList( ReportWeaponList(
writer, writer,
default.templateAdditionFailed, default.templateRemovalFailed,
itemsAdditionFailed); itemsRemovalFailed);
ReportWeaponList( ReportWeaponList(
writer, writer,
default.templateremovalFailed, default.templateItemsAddedVerbose,
itemsRemovalFailed); itemsAddedPrivate);
ReportWeaponList(
writer,
default.templateAdditionFailed,
itemsAdditionFailed);
} }
_.memory.Free(blamedName);
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();
}
default.templateItemsAdded.TextArg(T(TTARGET), targetName);
default.templateItemsRemoved.TextArg(T(TTARGET), targetName);
_.memory.Free(targetName); _.memory.Free(targetName);
} }
@ -588,9 +610,10 @@ private final function ReportWeaponList(
{ {
local MutableText output; local MutableText output;
if (writer == none) { if (writer == none) return;
return; if (builder == none) return;
} if (builder.IsEmpty()) return;
if (header != none) if (header != none)
{ {
output = header.CollectFormattedM(); output = header.CollectFormattedM();
@ -598,20 +621,12 @@ private final function ReportWeaponList(
_.memory.Free(output); _.memory.Free(output);
output = none; output = none;
} }
if (builder != none) {
output = builder.GetMutable(); output = builder.GetMutable();
}
writer.WriteLine(output); writer.WriteLine(output);
_.memory.Free(output); _.memory.Free(output);
} }
/* itemsAdded.Initialize(T(TITEMS_ADDED_MESSAGE)); // TODO: Use `ListBuilder` for the below method?
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)); */
/** /**
* Command that outputs summary of the player's inventory. * Command that outputs summary of the player's inventory.
* *
@ -738,56 +753,44 @@ private final function AppendAmmoInstanceInfo(MutableText builder, EAmmo ammo)
defaultproperties defaultproperties
{ {
// TODO remove old constants
// TODO refactor `ReportChanges()`
TINSTIGATOR = 0 TINSTIGATOR = 0
stringConstants(0) = "%%instigator%%" stringConstants(0) = "instigator"
TTARGET = 1 TTARGET = 1
stringConstants(1) = "%%target%%" stringConstants(1) = "target"
TITEMS_REMOVED_MESSAGE = 2 TRESOLVED_INTO = 2
stringConstants(2) = "%%instigator%% {$TextNegative removed} following weapons from %%target%%:" stringConstants(2) = "` resolved into `"
TITEMS_REMOVED_VERBOSE_MESSAGE = 3 TTILDE_QUOTE = 3
stringConstants(3) = "Weapons {$TextNegative removed} from %%target%%:" stringConstants(3) = "`"
TITEMS_ADDITION_FAILED_MESSAGE = 4 TFAULTY_INVENTORY_IMPLEMENTATION = 4
stringConstants(4) = "Weapons we've {$TextFailure failed} to add to %%target%%:" stringConstants(4) = "faulty inventory implementation"
TITEMS_REMOVAL_FAILED_MESSAGE = 5 TITEM_MISSING = 5
stringConstants(5) = "Weapons we've {$TextFailure failed} to remove from %%target%%:" stringConstants(5) = "item missing"
TRESOLVED_INTO = 6 TITEM_NOT_REMOVABLE = 6
stringConstants(6) = "` resolved into `" stringConstants(6) = "item not removable"
TTILDE_QUOTE = 7 TUNKNOWN = 7
stringConstants(7) = "`" stringConstants(7) = "unknown"
TFAULTY_INVENTORY_IMPLEMENTATION = 8 TVISIBLE = 8
stringConstants(8) = "faulty inventory implementation" stringConstants(8) = "visible"
TITEM_MISSING = 9 TDISPLAYING_INVENTORY = 9
stringConstants(9) = "item missing" stringConstants(9) = "{$TextHeader Displaying inventory for player }"
TITEM_NOT_REMOVABLE = 10 THEADER_COLON = 10
stringConstants(10) = "item not removable" stringConstants(10) = "{$TextHeader :}"
TUNKNOWN = 11 TDOT_SPACE = 11
stringConstants(11) = "unknown" stringConstants(11) = ". "
TVISIBLE = 12 TCOLON_SPACE = 12
stringConstants(12) = "visible" stringConstants(12) = ": "
TDISPLAYING_INVENTORY = 13 TCOMMA_SPACE = 13
stringConstants(13) = "{$TextHeader Displaying inventory for player }" stringConstants(13) = ", "
THEADER_COLON = 14 TSPACE = 14
stringConstants(14) = "{$TextHeader :}" stringConstants(14) = " "
TDOT_SPACE = 15 TOUT_OF = 15
stringConstants(15) = ". " stringConstants(15) = " out of "
TCOLON_SPACE = 16 THIDDEN_ITEMS = 16
stringConstants(16) = ": " stringConstants(16) = "{$TextSubHeader Hidden items:}"
TCOMMA_SPACE = 17 TDOLLAR = 17
stringConstants(17) = ", " stringConstants(17) = "$"
TSPACE = 18 TYOU = 18
stringConstants(18) = " " stringConstants(18) = "you"
TOUT_OF = 19 TTHEMSELVES = 19
stringConstants(19) = " out of " stringConstants(19) = "themselves"
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"
} }

14
sources/Tools/ListBuilder.uc

@ -62,6 +62,17 @@ protected function Finalizer()
Reset(); 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. * 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, * Forgets all items or comments specified for the caller `ListBuilder` so far,
* allowing to start forming a new report. Does not reset template header, * allowing to start forming a new report.
* specified in the `Initialize()` method.
* *
* @return Reference to the caller `ListBuilder` to allow for method chaining. * @return Reference to the caller `ListBuilder` to allow for method chaining.
*/ */

Loading…
Cancel
Save