Browse Source

Add `ConsoleWriter` ability to pick receivers

Before this patch `ConsoleWriter` could only send messages to all
players or to none of them. Now it posseses methods to configure a
sprecise set of players to send messages to.
pull/8/head
Anton Tarasenko 3 years ago
parent
commit
d71f74db91
  1. 142
      sources/Console/ConsoleWriter.uc

142
sources/Console/ConsoleWriter.uc

@ -36,15 +36,15 @@ enum ConsoleWriterTarget
{ {
// No one. Can happed if our target disconnects. // No one. Can happed if our target disconnects.
CWTARGET_None, CWTARGET_None,
// A certain player. // A certain set of players.
CWTARGET_Player, CWTARGET_Players,
// All players. // All players.
CWTARGET_All CWTARGET_All
}; };
var private ConsoleWriterTarget targetType; var private ConsoleWriterTarget targetType;
// Player that will receive output passed to this `ConsoleWriter`. // Players that will receive output passed to this `ConsoleWriter`.
// Only used when `targetType == CWTARGET_Player` // Only used when `targetType == CWTARGET_Players`
var private APlayer outputTarget; var private array<APlayer> outputTargets;
var private ConsoleBuffer outputBuffer; var private ConsoleBuffer outputBuffer;
var private bool needToResetColor; var private bool needToResetColor;
@ -302,37 +302,117 @@ public final function ConsoleWriter SetTotalLineLength(int newMaxTotalLineWidth)
/** /**
* Configures caller `ConsoleWriter` to output to all players. * Configures caller `ConsoleWriter` to output to all players.
* `Flush()` will be automatically called between target change. * `Flush()` will be automatically called if target actually has to switch
* (before the switch occurs).
* *
* @return Returns caller `ConsoleWriter` to allow for method chaining. * @return Returns caller `ConsoleWriter` to allow for method chaining.
*/ */
public final function ConsoleWriter ForAll() public final function ConsoleWriter ForAll()
{ {
if (targetType != CWTARGET_All) {
Flush(); Flush();
}
targetType = CWTARGET_All; targetType = CWTARGET_All;
return self; return self;
} }
/** /**
* Configures caller `ConsoleWriter` to output only to the given player. * Configures caller `ConsoleWriter` to output only to the given player.
* `Flush()` will be automatically called between target change. * `Flush()` will be automatically called if target actually has to switch
* (before the switch occurs).
* *
* @param targetPlayer Player, to whom console we want to write. * @param targetPlayer Player, to whom console we want to write.
* If `none` - caller `ConsoleWriter` would be configured to * If `none` - caller `ConsoleWriter` would be configured to
* throw messages away. * throw messages away.
* @return ConsoleWriter Returns caller `ConsoleWriter` to allow for * @return Returns caller `ConsoleWriter` to allow for method chaining.
* method chaining.
*/ */
public final function ConsoleWriter ForPlayer(APlayer targetPlayer) public final function ConsoleWriter ForPlayer(APlayer targetPlayer)
{ {
if (targetPlayer == none)
{
Flush();
targetType = CWTARGET_None;
return self;
}
if (targetType != CWTARGET_Players) {
Flush(); Flush();
if (targetPlayer != none) }
outputTargets.length = 0;
targetType = CWTARGET_Players;
outputTargets[0] = targetPlayer;
return self;
}
/**
* Configures caller `ConsoleWriter` to output to one more player,
* given by `targetPlayer`.
* `Flush()` will be automatically called if target actually has to switch
* (before the switch occurs).
*
* @param targetPlayer Player, to whom console we want to write.
* If `none` - this method will do nothing.
* @return Returns caller `ConsoleWriter` to allow for method chaining.
*/
public final function ConsoleWriter AndPlayer(APlayer targetPlayer)
{
local int i;
if (targetPlayer == none) return self;
if (!targetPlayer.IsConnected()) return self;
if (targetType != CWTARGET_Players)
{ {
targetType = CWTARGET_Player; Flush();
outputTarget = targetPlayer; if (targetType == CWTARGET_None) {
outputTargets.length = 0;
} }
else { else {
targetType = CWTARGET_None; outputTargets.length = _.players.GetAll();
}
}
targetType = CWTARGET_Players;
for (i = 0; i < outputTargets.length; i += 1)
{
if (outputTargets[i] == targetPlayer) {
return self;
}
}
Flush();
outputTargets[outputTargets.length] = targetPlayer;
return self;
}
/**
* Configures caller `ConsoleWriter` to output to one less player,
* given by `targetPlayer`.
* `Flush()` will be automatically called if target actually has to switch
* (before the switch occurs).
*
* @param targetPlayer Player, to whom console we no longer want to write.
* If `none` - this method will do nothing.
* @return Returns caller `ConsoleWriter` to allow for method chaining.
*/
public final function ConsoleWriter ButPlayer(APlayer playerToRemove)
{
local int i;
if (targetType == CWTARGET_None) return self;
if (playerToRemove == none) return self;
if (!playerToRemove.IsConnected()) return self;
if (targetType == CWTARGET_All)
{
Flush();
outputTargets = _.players.GetAll();
}
targetType = CWTARGET_Players;
while (i < outputTargets.length)
{
if (outputTargets[i] == playerToRemove)
{
Flush();
outputTargets.Remove(i, 1);
break;
}
i += 1;
} }
return self; return self;
} }
@ -345,7 +425,7 @@ public final function ConsoleWriter ForPlayer(APlayer targetPlayer)
*/ */
public final function ConsoleWriterTarget CurrentTarget() public final function ConsoleWriterTarget CurrentTarget()
{ {
if (targetType == CWTARGET_Player && outputTarget == none) { if (targetType == CWTARGET_Players && outputTargets.length == 0) {
targetType = CWTARGET_None; targetType = CWTARGET_None;
} }
return targetType; return targetType;
@ -354,6 +434,8 @@ public final function ConsoleWriterTarget CurrentTarget()
/** /**
* Returns `APlayer` to whom console caller `ConsoleWriter` is * Returns `APlayer` to whom console caller `ConsoleWriter` is
* outputting messages. * outputting messages.
* If caller `ConsoleWriter` is setup to message several different players,
* returns an arbitrary one of them.
* *
* @return Player (`APlayer` class) to whom console caller `ConsoleWriter` is * @return Player (`APlayer` class) to whom console caller `ConsoleWriter` is
* outputting messages. Returns `none` iff it currently outputs to * outputting messages. Returns `none` iff it currently outputs to
@ -362,7 +444,27 @@ public final function ConsoleWriterTarget CurrentTarget()
public final function APlayer GetTargetPlayer() public final function APlayer GetTargetPlayer()
{ {
if (targetType == CWTARGET_All) return none; if (targetType == CWTARGET_All) return none;
return outputTarget; if (outputTargets.length <= 0) return none;
return outputTargets[0];
}
/**
* Returns `APlayer` to whom console caller `ConsoleWriter` is
* outputting messages.
* If caller `ConsoleWriter` is setup to message several different players,
* returns an arbitrary one of them.
*
* @return Player (`APlayer` class) to whom console caller `ConsoleWriter` is
* outputting messages. Returns `none` iff it currently outputs to
* every player or to no one.
*/
public final function array<APlayer> GetTargetPlayers()
{
local array<APlayer> emptyArray;
if (targetType == CWTARGET_None) return emptyArray;
if (targetType == CWTARGET_All) return _.players.GetAll();
return outputTargets;
} }
/** /**
@ -514,11 +616,15 @@ private final function array<PlayerController> GetRecipientsControllers()
if (targetType != CWTARGET_All) if (targetType != CWTARGET_All)
{ {
playerService = PlayerService(class'PlayerService'.static.Require()); playerService = PlayerService(class'PlayerService'.static.Require());
if (playerService != none && outputTarget != none) { if (playerService == none) {
nextRecipient = playerService.GetController(outputTarget); return recipients;
} }
for (i = 0; i < outputTargets.length; i += 1)
{
nextRecipient = playerService.GetController(outputTargets[i]);
if (nextRecipient != none) { if (nextRecipient != none) {
recipients[0] = nextRecipient; recipients[recipients.length] = nextRecipient;
}
} }
return recipients; return recipients;
} }

Loading…
Cancel
Save