Expose player names to localization.
This commit is contained in:
committed by
Gustas
parent
9a46f3053a
commit
4e5556dccc
@@ -19,6 +19,9 @@ namespace OpenRA
|
||||
{
|
||||
public class GameInformation
|
||||
{
|
||||
[TranslationReference("name", "number")]
|
||||
const string EnumeratedBotName = "enumerated-bot-name";
|
||||
|
||||
public string Mod;
|
||||
public string Version;
|
||||
|
||||
@@ -118,6 +121,7 @@ namespace OpenRA
|
||||
Name = runtimePlayer.PlayerName,
|
||||
IsHuman = !runtimePlayer.IsBot,
|
||||
IsBot = runtimePlayer.IsBot,
|
||||
BotType = runtimePlayer.BotType,
|
||||
FactionName = runtimePlayer.Faction.Name,
|
||||
FactionId = runtimePlayer.Faction.InternalName,
|
||||
DisplayFactionName = runtimePlayer.DisplayFaction.Name,
|
||||
@@ -143,6 +147,20 @@ namespace OpenRA
|
||||
return player;
|
||||
}
|
||||
|
||||
public string ResolvedPlayerName(Player player)
|
||||
{
|
||||
if (player.IsBot)
|
||||
{
|
||||
var number = Players.Where(p => p.BotType == player.BotType).ToList().IndexOf(player) + 1;
|
||||
return TranslationProvider.GetString(EnumeratedBotName,
|
||||
Translation.Arguments(
|
||||
"name", TranslationProvider.GetString(player.Name),
|
||||
"number", number));
|
||||
}
|
||||
|
||||
return player.Name;
|
||||
}
|
||||
|
||||
public class Player
|
||||
{
|
||||
#region Start-up information
|
||||
@@ -153,6 +171,7 @@ namespace OpenRA
|
||||
public string Name;
|
||||
public bool IsHuman;
|
||||
public bool IsBot;
|
||||
public string BotType;
|
||||
|
||||
/// <summary>The faction's display name.</summary>
|
||||
public string FactionName;
|
||||
|
||||
@@ -465,7 +465,7 @@ namespace OpenRA
|
||||
public override string ToString()
|
||||
{
|
||||
return $"OrderString: \"{OrderString}\" \n\t Type: \"{Type}\". \n\t Subject: \"{Subject}\". \n\t Target: \"{Target}\"." +
|
||||
$"\n\t TargetString: \"{TargetString}\".\n\t IsImmediate: {IsImmediate}.\n\t Player(PlayerName): {Player?.PlayerName}\n";
|
||||
$"\n\t TargetString: \"{TargetString}\".\n\t IsImmediate: {IsImmediate}.\n\t Player(PlayerName): {Player?.ResolvedPlayerName}\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,9 @@ namespace OpenRA
|
||||
|
||||
public class Player : IScriptBindable, IScriptNotifyBind, ILuaTableBinding, ILuaEqualityBinding, ILuaToStringBinding
|
||||
{
|
||||
[TranslationReference("name", "number")]
|
||||
const string EnumeratedBotName = "enumerated-bot-name";
|
||||
|
||||
public readonly Actor PlayerActor;
|
||||
public readonly string PlayerName;
|
||||
public readonly string InternalName;
|
||||
@@ -80,6 +83,9 @@ namespace OpenRA
|
||||
readonly IUnlocksRenderPlayer[] unlockRenderPlayer;
|
||||
readonly INotifyPlayerDisconnected[] notifyDisconnected;
|
||||
|
||||
readonly IReadOnlyCollection<IBotInfo> botInfos;
|
||||
string resolvedPlayerName;
|
||||
|
||||
// Each player is identified with a unique bit in the set
|
||||
// Cache masks for the player's index and ally/enemy player indices for performance.
|
||||
public LongBitSet<PlayerBitMask> PlayerMask;
|
||||
@@ -97,6 +103,16 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The chosen player name including localized and enumerated bot names.</summary>
|
||||
public string ResolvedPlayerName
|
||||
{
|
||||
get
|
||||
{
|
||||
resolvedPlayerName ??= ResolvePlayerName();
|
||||
return resolvedPlayerName;
|
||||
}
|
||||
}
|
||||
|
||||
public static FactionInfo ResolveFaction(
|
||||
string factionName, IEnumerable<FactionInfo> factionInfos, MersenneTwister playerRandom, bool requireSelectable = true)
|
||||
{
|
||||
@@ -133,18 +149,6 @@ namespace OpenRA
|
||||
return factions.FirstOrDefault(f => f.InternalName == factionName) ?? factions.First();
|
||||
}
|
||||
|
||||
public static string ResolvePlayerName(Session.Client client, IEnumerable<Session.Client> clients, IEnumerable<IBotInfo> botInfos)
|
||||
{
|
||||
if (client.Bot != null)
|
||||
{
|
||||
var botInfo = botInfos.First(b => b.Type == client.Bot);
|
||||
var botsOfSameType = clients.Where(c => c.Bot == client.Bot).ToArray();
|
||||
return botsOfSameType.Length == 1 ? botInfo.Name : $"{botInfo.Name} {botsOfSameType.IndexOf(client) + 1}";
|
||||
}
|
||||
|
||||
return client.Name;
|
||||
}
|
||||
|
||||
public Player(World world, Session.Client client, PlayerReference pr, MersenneTwister playerRandom)
|
||||
{
|
||||
World = world;
|
||||
@@ -152,6 +156,7 @@ namespace OpenRA
|
||||
PlayerReference = pr;
|
||||
|
||||
inMissionMap = world.Map.Visibility.HasFlag(MapVisibility.MissionSelector);
|
||||
botInfos = World.Map.Rules.Actors[SystemActors.Player].TraitInfos<IBotInfo>();
|
||||
|
||||
// Real player or host-created bot
|
||||
if (client != null)
|
||||
@@ -159,7 +164,7 @@ namespace OpenRA
|
||||
ClientIndex = client.Index;
|
||||
color = client.Color;
|
||||
Color = color;
|
||||
PlayerName = ResolvePlayerName(client, world.LobbyInfo.Clients, world.Map.Rules.Actors[SystemActors.Player].TraitInfos<IBotInfo>());
|
||||
PlayerName = client.Name;
|
||||
|
||||
BotType = client.Bot;
|
||||
Faction = ResolveFaction(world, client.Faction, playerRandom, !pr.LockFaction);
|
||||
@@ -224,7 +229,21 @@ namespace OpenRA
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{PlayerName} ({ClientIndex})";
|
||||
return $"{ResolvedPlayerName} ({ClientIndex})";
|
||||
}
|
||||
|
||||
string ResolvePlayerName()
|
||||
{
|
||||
if (IsBot)
|
||||
{
|
||||
var botInfo = botInfos.First(b => b.Type == BotType);
|
||||
var botsOfSameType = World.Players.Where(c => c.BotType == BotType).ToArray();
|
||||
return TranslationProvider.GetString(EnumeratedBotName,
|
||||
Translation.Arguments("name", TranslationProvider.GetString(botInfo.Name),
|
||||
"number", botsOfSameType.IndexOf(this) + 1));
|
||||
}
|
||||
|
||||
return PlayerName;
|
||||
}
|
||||
|
||||
public PlayerRelationship RelationshipWith(Player other)
|
||||
@@ -310,7 +329,7 @@ namespace OpenRA
|
||||
|
||||
public LuaValue ToString(LuaRuntime runtime)
|
||||
{
|
||||
return $"Player ({PlayerName})";
|
||||
return $"Player ({ResolvedPlayerName})";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -16,9 +16,9 @@ namespace OpenRA.Scripting
|
||||
readonly Player player;
|
||||
|
||||
protected override string DuplicateKeyError(string memberName) =>
|
||||
$"Player '{player.PlayerName}' defines the command '{memberName}' on multiple traits";
|
||||
$"Player '{player.ResolvedPlayerName}' defines the command '{memberName}' on multiple traits";
|
||||
protected override string MemberNotFoundError(string memberName) =>
|
||||
$"Player '{player.PlayerName}' does not define a property '{memberName}'";
|
||||
$"Player '{player.ResolvedPlayerName}' does not define a property '{memberName}'";
|
||||
|
||||
public ScriptPlayerInterface(ScriptContext context, Player player)
|
||||
: base(context)
|
||||
|
||||
@@ -1323,7 +1323,7 @@ namespace OpenRA.Server
|
||||
foreach (var cmpi in Map.WorldActorInfo.TraitInfos<ICreatePlayersInfo>())
|
||||
cmpi.CreateServerPlayers(Map, LobbyInfo, worldPlayers, playerRandom);
|
||||
|
||||
gameInfo = new GameInformation
|
||||
gameInfo = new GameInformation()
|
||||
{
|
||||
Mod = Game.ModData.Manifest.Id,
|
||||
Version = Game.ModData.Manifest.Metadata.Version,
|
||||
|
||||
Reference in New Issue
Block a user