Browse Source

Change nick command to use announcer

feature_improvement
Anton Tarasenko 2 years ago
parent
commit
e77de68fb1
  1. 118
      sources/Commands/ACommandNick.uc
  2. 96
      sources/Commands/ACommandNick_Announcer.uc

118
sources/Commands/ACommandNick.uc

@ -19,6 +19,19 @@
*/ */
class ACommandNick extends Command; class ACommandNick extends Command;
var private bool foundErrors;
var private MutableText newName;
var private ACommandNick_Announcer announcer;
protected function Finalizer()
{
_.memory.Free(announcer);
_.memory.Free(newName);
newName = none;
super.Finalizer();
}
protected function BuildData(CommandDataBuilder builder) protected function BuildData(CommandDataBuilder builder)
{ {
builder.Name(P("nick")).Summary(P("Changes nickname.")); builder.Name(P("nick")).Summary(P("Changes nickname."));
@ -28,7 +41,7 @@ protected function BuildData(CommandDataBuilder builder)
builder.Option(P("plain")) builder.Option(P("plain"))
.Describe(P("Take nickname exactly as typed, without attempting to" .Describe(P("Take nickname exactly as typed, without attempting to"
@ "treat it like formatted string.")); @ "treat it like formatted string."));
builder.Option(P("fix"), P("F")) builder.Option(P("fix"), P("f"))
.Describe(P("In case of a nickname with erroroneous formatting or" .Describe(P("In case of a nickname with erroroneous formatting or"
@ "invalid default color (specified with `--color`)," @ "invalid default color (specified with `--color`),"
@ "try to fix/ignore it instead of simply rejecting it.")); @ "try to fix/ignore it instead of simply rejecting it."));
@ -36,62 +49,74 @@ protected function BuildData(CommandDataBuilder builder)
.Describe(P("Color to use for the nickname. In case nickname is already" .Describe(P("Color to use for the nickname. In case nickname is already"
@ "colored, this flag will only affects uncolored parts.")) @ "colored, this flag will only affects uncolored parts."))
.ParamText(P("default_color")); .ParamText(P("default_color"));
announcer = ACommandNick_Announcer(
_.memory.Allocate(class'ACommandNick_Announcer'));
} }
protected function ExecutedFor( protected function Executed(
EPlayer player,
CallData result, CallData result,
EPlayer callerPlayer) EPlayer callerPlayer)
{ {
local bool foundErrors, selfChange; local Text givenName;
local Text givenName, oldName, callerName;
local MutableText newName;
local array<FormattingErrorsReport.FormattedStringError> errors; local array<FormattingErrorsReport.FormattedStringError> errors;
oldName = player.GetName();
givenName = result.parameters.GetText(P("nick")); givenName = result.parameters.GetText(P("nick"));
callerName = callerPlayer.GetName(); // `newName`'s reference persists between different command calls and
selfChange = callerPlayer.SameAs(player); // only deallocated when we need this variable for the next execution.
if (result.options.HasKey(P("plain"))) // "Leaking" a single `Text` like that is insignificant.
{ _.memory.Free(newName);
player.SetName(givenName);
AnnounceNicknameChange(callerName, oldName, givenName, selfChange);
_.memory.Free(oldName);
_.memory.Free(callerName);
return;
}
newName = _.text.Empty(); newName = _.text.Empty();
if (result.options.HasKey(P("plain"))) {
newName = givenName.MutableCopy();
}
else
{
errors = class'FormattingStringParser'.static errors = class'FormattingStringParser'.static
.ParseFormatted(givenName, newName, true); .ParseFormatted(givenName, newName, true);
}
foundErrors = false;
if (result.options.HasKey(P("color"))) if (result.options.HasKey(P("color")))
{ {
foundErrors = !TryChangeDefaultColor( foundErrors = !TryChangeDefaultColor(
newName, result.options.GetTextBy(P("/color/default_color"))); result.options.GetTextBy(P("/color/default_color")));
} }
foundErrors = foundErrors || (errors.length > 0); foundErrors = foundErrors || (errors.length > 0);
class'FormattingReportTool'.static.Report(callerConsole, errors);
class'FormattingReportTool'.static.FreeErrors(errors);
}
protected function ExecutedFor(
EPlayer target,
CallData result,
EPlayer instigator)
{
local Text alteredVersion;
if (!foundErrors || result.options.HasKey(P("fix"))) if (!foundErrors || result.options.HasKey(P("fix")))
{ {
player.SetName(newName); announcer.Setup(target, instigator, othersConsole);
AnnounceNicknameChange(callerName, oldName, newName, selfChange); target.SetName(newName);
alteredVersion = target.GetName();
if (newName.Compare(alteredVersion, SCASE_SENSITIVE, SFORM_SENSITIVE)) {
announcer.AnnounceChangedNickname(newName);
}
else {
announcer.AnnounceChangedAlteredNickname(newName, alteredVersion);
}
_.memory.Free(alteredVersion);
} }
class'FormattingReportTool'.static.Report(callerConsole, errors);
class'FormattingReportTool'.static.FreeErrors(errors);
_.memory.Free(newName);
_.memory.Free(oldName);
_.memory.Free(callerName);
} }
protected function bool TryChangeDefaultColor( protected function bool TryChangeDefaultColor(BaseText specifiedColor)
MutableText newName,
BaseText specifiedColor)
{ {
local Color defaultColor; local Color defaultColor;
if (newName == none) return false; if (newName == none) return false;
if (specifiedColor == none) return false; if (specifiedColor == none) return false;
if (_.color.Parse(specifiedColor, defaultColor)) if (_.color.Parse(specifiedColor, defaultColor))
{ {
newName.ChangeDefaultFormatting( newName.ChangeDefaultColor(defaultColor);
_.text.FormattingFromColor(defaultColor));
return true; return true;
} }
callerConsole callerConsole
@ -100,39 +125,6 @@ protected function bool TryChangeDefaultColor(
return false; return false;
} }
protected function AnnounceNicknameChange(
BaseText callerName,
BaseText oldName,
BaseText newName,
bool selfChange)
{
if (selfChange)
{
callerConsole
.Write(F("Your nickname was {$TextPositive changed} to "))
.WriteLine(newName);
othersConsole
.Write(callerName)
.Write(F(" {$TextEmphasis changed} thier own nickname to "))
.WriteLine(newName);
return;
}
callerConsole
.Write(P("Nickname for player ")).Write(oldName)
.Write(F(" was {$TextPositive changed} to ")).WriteLine(newName);
targetConsole
.Write(F("Your nickname was {$TextEmphasis changed} to "))
.Write(newName)
.Write(P(" by "))
.WriteLine(callerName);
othersConsole
.Write(callerName)
.Write(F(" {$TextEmphasis changed} nickname for player "))
.Write(oldName)
.Write(P(" to "))
.WriteLine(newName);
}
defaultproperties defaultproperties
{ {
} }

96
sources/Commands/ACommandNick_Announcer.uc

@ -0,0 +1,96 @@
/**
* Announcer for `ACommandNick`.
* Copyright 2022 Anton Tarasenko
*------------------------------------------------------------------------------
* This file is part of Acedia.
*
* Acedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License, or
* (at your option) any later version.
*
* Acedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/
class ACommandNick_Announcer extends CommandAnnouncer;
var private AnnouncementVariations changedNickname, changedAlteredNickname;
protected function Finalizer()
{
FreeVariations(changedNickname);
FreeVariations(changedAlteredNickname);
super.Finalizer();
}
public final function AnnounceChangedNickname(BaseText newNickname)
{
local int i;
local array<TextTemplate> templates;
if (!changedNickname.initialized)
{
changedNickname.initialized = true;
changedNickname.toSelfReport = _.text.MakeTemplate_S(
"Your nickname {$TextNeutral changed} to \"%1\"");
changedNickname.toSelfPublic = _.text.MakeTemplate_S(
"%%instigator {$TextNeutral changed} their own nickname to"
@ "\"%1\"");
changedNickname.toOtherReport = _.text.MakeTemplate_S(
"Nickname for %%instigator%% {$TextNeutral changed} to"
@ "\"%1\"");
changedNickname.toOtherPrivate = _.text.MakeTemplate_S(
"%%instigator%% {$TextNeutral changed} your nickname to \"%1\"");
changedNickname.toOtherPublic = _.text.MakeTemplate_S(
"%%instigator%% {$TextNeutral changed} nickname for player"
@ "%%target%% to \"%1\"");
}
templates = MakeArray(changedNickname);
for (i = 0; i < templates.length; i += 1) {
templates[i].Reset().Arg(newNickname);
}
MakeAnnouncement(changedNickname);
}
public final function AnnounceChangedAlteredNickname(
BaseText newNickname,
BaseText alteredVersion)
{
local int i;
local array<TextTemplate> templates;
if (!changedAlteredNickname.initialized)
{
changedAlteredNickname.initialized = true;
changedAlteredNickname.toSelfReport = _.text.MakeTemplate_S(
"Your nickname was {$TextNeutral changed} to \"%1\", but then"
@ "got altered by the game into \"%2\"");
changedAlteredNickname.toSelfPublic = _.text.MakeTemplate_S(
"%%instigator has {$TextNeutral changed} their own nickname to"
@ "\"%1\", but it was altered by the game into \"%2\"");
changedAlteredNickname.toOtherReport = _.text.MakeTemplate_S(
"Nickname for %%instigator%% was {$TextNeutral changed} to"
@ "\"%1\", but then got altered by the game into \"%2\"");
changedAlteredNickname.toOtherPrivate = _.text.MakeTemplate_S(
"%%instigator%% has {$TextNeutral changed} your nickname to \"%1\","
@ "but then it got altered by the game into \"%2\"");
changedAlteredNickname.toOtherPublic = _.text.MakeTemplate_S(
"%%instigator%% has {$TextNeutral changed} nickname for player"
@ "%%target%% to \"%1\", but then it got altered by the game"
@ "into \"%2\"");
}
templates = MakeArray(changedAlteredNickname);
for (i = 0; i < templates.length; i += 1) {
templates[i].Reset().Arg(newNickname).Arg(alteredVersion);
}
MakeAnnouncement(changedAlteredNickname);
}
defaultproperties
{
}
Loading…
Cancel
Save