Misc changes

* Use Pair instead of KeyValuePair
* double -> var
* Butcher XML comments
* Change WinState default to Undefined and use it instead of the new GameOutcome
* Other changes
This commit is contained in:
Pavlos Touboulidis
2014-05-20 17:45:33 +03:00
parent fe1eb1f3e0
commit b8bbd55598
8 changed files with 74 additions and 134 deletions

View File

@@ -16,62 +16,30 @@ using OpenRA.Network;
namespace OpenRA
{
/// <summary>
/// Contains information about a finished game
/// </summary>
public class GameInformation
{
/// <summary>The map identifier.</summary>
public string MapUid;
/// <summary>The map title.</summary>
public string MapTitle;
/// <summary>Game start timestamp.</summary>
public DateTime StartTimeUtc;
/// <summary>Game end timestamp (when the recoding stopped).</summary>
// Game end timestamp (when the recoding stopped).
public DateTime EndTimeUtc;
/// <summary>
/// Gets the game's duration, from the time the game started until the
/// replay recording stopped.
/// </summary>
/// <value>The game's duration.</value>
// Gets the game's duration, from the time the game started until the
// replay recording stopped.
public TimeSpan Duration { get { return EndTimeUtc > StartTimeUtc ? EndTimeUtc - StartTimeUtc : TimeSpan.Zero; } }
/// <summary>
/// Gets the list of players.
/// </summary>
/// <value>The players.</value>
public IList<Player> Players { get; private set; }
/// <summary>
/// Gets the map preview, using <see cref="Game.modData.MapCache"/> and the <see cref="MapUid"/>.
/// </summary>
/// <value>The map preview.</value>
public MapPreview MapPreview { get { return Game.modData.MapCache[MapUid]; } }
/// <summary>
/// Gets the human players.
/// </summary>
/// <value>The human players.</value>
public IEnumerable<Player> HumanPlayers { get { return Players.Where(p => p.IsHuman); } }
/// <summary>
/// Gets a value indicating whether this instance has just one human player.
/// </summary>
/// <value><c>true</c> if this instance has just one human player; otherwise, <c>false</c>.</value>
public bool IsSinglePlayer { get { return HumanPlayers.Count() == 1; } }
Dictionary<OpenRA.Player, Player> playersByRuntime;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
public GameInformation()
{
Players = new List<Player>();
playersByRuntime = new Dictionary<OpenRA.Player, Player>();
}
/// <summary>
/// Deserialize the specified data into a new instance.
/// </summary>
/// <param name="data">Data.</param>
public static GameInformation Deserialize(string data)
{
try
@@ -104,9 +72,6 @@ namespace OpenRA
}
}
/// <summary>
/// Serialize this instance.
/// </summary>
public string Serialize()
{
var nodes = new List<MiniYamlNode>();
@@ -119,11 +84,7 @@ namespace OpenRA
return nodes.WriteToString();
}
/// <summary>
/// Adds the start-up player information.
/// </summary>
/// <param name="runtimePlayer">Runtime player.</param>
/// <param name="lobbyInfo">Lobby info.</param>
// Adds the player information at start-up.
public void AddPlayer(OpenRA.Player runtimePlayer, Session lobbyInfo)
{
if (runtimePlayer == null)
@@ -160,11 +121,7 @@ namespace OpenRA
Players.Add(player);
}
/// <summary>
/// Gets the player information for the specified runtime player instance.
/// </summary>
/// <returns>The player, or <c>null</c>.</returns>
/// <param name="runtimePlayer">Runtime player.</param>
// Gets the player information for the specified runtime player instance.
public Player GetPlayer(OpenRA.Player runtimePlayer)
{
Player player;
@@ -174,56 +131,37 @@ namespace OpenRA
return player;
}
/// <summary>Specifies whether the player was defeated, victorious, or there was no outcome defined.</summary>
public enum GameOutcome
{
/// <summary>Unknown outcome.</summary>
Undefined,
/// <summary>The player was defeated</summary>
Defeat,
/// <summary>The player was victorious</summary>
Victory
}
///<summary>
/// Information about a player
/// </summary>
public class Player
{
//
// Start-up information
//
/// <summary>The client index.</summary>
public int ClientIndex;
/// <summary>The player name, not guaranteed to be unique.</summary>
// The player name, not guaranteed to be unique.
public string Name;
/// <summary><c>true</c> if the player is a human player; otherwise, <c>false</c>.</summary>
public bool IsHuman;
/// <summary><c>true</c> if the player is a bot; otherwise, <c>false</c>.</summary>
public bool IsBot;
/// <summary>The faction name (aka Country).</summary>
// The faction name (aka Country)
public string FactionName;
/// <summary>The faction id (aka Country, aka Race).</summary>
// The faction id (aka Country, aka Race)
public string FactionId;
/// <summary>The color used by the player in the game.</summary>
public HSLColor Color;
/// <summary>The team id on start-up, or 0 if the player is not part of the team.</summary>
// The team id on start-up, or 0 if the player is not part of the team.
public int Team;
/// <summary>The index of the spawn point on the map, or 0 if the player is not part of the team.</summary>
public int SpawnPoint;
/// <summary><c>true</c> if the faction was chosen at random; otherwise, <c>false</c>.</summary>
// True if the faction was chosen at random; otherwise, false
public bool IsRandomFaction;
/// <summary><c>true</c> if the spawn point was chosen at random; otherwise, <c>false</c>.</summary>
// True if the spawn point was chosen at random; otherwise, false.</summary>
public bool IsRandomSpawnPoint;
//
// Information gathered at a later stage
//
/// <summary>The game outcome for this player.</summary>
public GameOutcome Outcome;
/// <summary>The time when this player won or lost the game.</summary>
// The game outcome for this player
public WinState Outcome;
// The time when this player won or lost the game
public DateTime OutcomeTimestampUtc;
}
}

View File

@@ -23,7 +23,7 @@ using OpenRA.Traits;
namespace OpenRA
{
public enum PowerState { Normal, Low, Critical };
public enum WinState { Won, Lost, Undefined };
public enum WinState { Undefined, Won, Lost };
public class Player : IScriptBindable, IScriptNotifyBind, ILuaTableBinding, ILuaEqualityBinding, ILuaToStringBinding
{

View File

@@ -58,7 +58,7 @@ namespace OpenRA.Widgets
{
SpriteFont font;
if (!Game.Renderer.Fonts.TryGetValue(Font, out font))
throw new ArgumentException("Request font '{0}' was not found.".F(Font));
throw new ArgumentException("Requested font '{0}' was not found.".F(Font));
var text = GetText();
if (text == null)

View File

@@ -308,9 +308,7 @@ namespace OpenRA
var pi = gameInfo.GetPlayer(player);
if (pi != null)
{
pi.Outcome = player.WinState == WinState.Lost ? GameInformation.GameOutcome.Defeat
: player.WinState == WinState.Won ? GameInformation.GameOutcome.Victory
: GameInformation.GameOutcome.Undefined;
pi.Outcome = player.WinState;
pi.OutcomeTimestampUtc = DateTime.UtcNow;
}
}

View File

@@ -42,7 +42,7 @@ namespace OpenRA.Mods.RA
var client = world.LobbyInfo.ClientInSlot(kv.Key);
var spid = (client == null || client.SpawnPoint == 0)
? ChooseSpawnPoint(world, available, taken)
: spawns[client.SpawnPoint-1];
: spawns[client.SpawnPoint-1];
Start.Add(player, spid);

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var preview = available.Get<MapPreviewWidget>("MAP_PREVIEW");
preview.Preview = () => lobby.Map;
preview.OnMouseDown = mi => LobbyUtils.SelectSpawnPoint(orderManager, preview, lobby.Map, mi);
preview.SpawnOccupants = () => LobbyUtils.GetSpawnClients(orderManager.LobbyInfo, lobby.Map);
preview.SpawnOccupants = () => LobbyUtils.GetSpawnOccupants(orderManager.LobbyInfo, lobby.Map);
var title = available.GetOrNull<LabelWidget>("MAP_TITLE");
if (title != null)
@@ -54,7 +54,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var preview = invalid.Get<MapPreviewWidget>("MAP_PREVIEW");
preview.Preview = () => lobby.Map;
preview.OnMouseDown = mi => LobbyUtils.SelectSpawnPoint(orderManager, preview, lobby.Map, mi);
preview.SpawnClients = () => LobbyUtils.GetSpawnClients(orderManager.LobbyInfo, lobby.Map);
preview.SpawnOccupants = () => LobbyUtils.GetSpawnOccupants(orderManager.LobbyInfo, lobby.Map);
var title = invalid.GetOrNull<LabelWidget>("MAP_TITLE");
if (title != null)
@@ -73,7 +73,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var preview = download.Get<MapPreviewWidget>("MAP_PREVIEW");
preview.Preview = () => lobby.Map;
preview.OnMouseDown = mi => LobbyUtils.SelectSpawnPoint(orderManager, preview, lobby.Map, mi);
preview.SpawnOccupants = () => LobbyUtils.GetSpawnClients(orderManager.LobbyInfo, lobby.Map);
preview.SpawnOccupants = () => LobbyUtils.GetSpawnOccupants(orderManager.LobbyInfo, lobby.Map);
var title = download.GetOrNull<LabelWidget>("MAP_TITLE");
if (title != null)
@@ -100,7 +100,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var preview = progress.Get<MapPreviewWidget>("MAP_PREVIEW");
preview.Preview = () => lobby.Map;
preview.OnMouseDown = mi => LobbyUtils.SelectSpawnPoint(orderManager, preview, lobby.Map, mi);
preview.SpawnOccupants = () => LobbyUtils.GetSpawnClients(orderManager.LobbyInfo, lobby.Map);
preview.SpawnOccupants = () => LobbyUtils.GetSpawnOccupants(orderManager.LobbyInfo, lobby.Map);
var title = progress.GetOrNull<LabelWidget>("MAP_TITLE");
if (title != null)

View File

@@ -149,14 +149,14 @@ namespace OpenRA.Mods.RA.Widgets.Logic
color.AttachPanel(colorChooser, onExit);
}
public static Dictionary<CPos, SpawnOccupant> GetSpawnClients(Session lobbyInfo, MapPreview preview)
public static Dictionary<CPos, SpawnOccupant> GetSpawnOccupants(Session lobbyInfo, MapPreview preview)
{
var spawns = preview.SpawnPoints;
return lobbyInfo.Clients
.Where(c => c.SpawnPoint != 0)
.ToDictionary(c => spawns[c.SpawnPoint - 1], c => new SpawnOccupant(c));
}
public static Dictionary<CPos, SpawnOccupant> GetSpawnClients(IEnumerable<GameInformation.Player> players, MapPreview preview)
public static Dictionary<CPos, SpawnOccupant> GetSpawnOccupants(IEnumerable<GameInformation.Player> players, MapPreview preview)
{
var spawns = preview.SpawnPoints;
return players

View File

@@ -104,25 +104,25 @@ namespace OpenRA.Mods.RA.Widgets.Logic
if (ddb != null)
{
// Using list to maintain the order
var options = new List<KeyValuePair<GameType, string>>
var options = new List<Pair<GameType, string>>
{
new KeyValuePair<GameType, string>(GameType.Any, ddb.GetText()),
new KeyValuePair<GameType, string>(GameType.Singleplayer, "Singleplayer"),
new KeyValuePair<GameType, string>(GameType.Multiplayer, "Multiplayer")
Pair.New(GameType.Any, ddb.GetText()),
Pair.New(GameType.Singleplayer, "Singleplayer"),
Pair.New(GameType.Multiplayer, "Multiplayer")
};
var lookup = options.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var lookup = options.ToDictionary(kvp => kvp.First, kvp => kvp.Second);
ddb.GetText = () => lookup[filter.Type];
ddb.OnMouseDown = _ =>
{
Func<KeyValuePair<GameType, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
Func<Pair<GameType, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
{
var item = ScrollItemWidget.Setup(
tpl,
() => filter.Type == option.Key,
() => { filter.Type = option.Key; ApplyFilter(); }
() => filter.Type == option.First,
() => { filter.Type = option.First; ApplyFilter(); }
);
item.Get<LabelWidget>("LABEL").GetText = () => option.Value;
item.Get<LabelWidget>("LABEL").GetText = () => option.Second;
return item;
};
@@ -139,27 +139,27 @@ namespace OpenRA.Mods.RA.Widgets.Logic
if (ddb != null)
{
// Using list to maintain the order
var options = new List<KeyValuePair<DateType, string>>
var options = new List<Pair<DateType, string>>
{
new KeyValuePair<DateType, string>(DateType.Any, ddb.GetText()),
new KeyValuePair<DateType, string>(DateType.Today, "Today"),
new KeyValuePair<DateType, string>(DateType.LastWeek, "Last 7 days"),
new KeyValuePair<DateType, string>(DateType.LastFortnight, "Last 14 days"),
new KeyValuePair<DateType, string>(DateType.LastMonth, "Last 30 days")
Pair.New(DateType.Any, ddb.GetText()),
Pair.New(DateType.Today, "Today"),
Pair.New(DateType.LastWeek, "Last 7 days"),
Pair.New(DateType.LastFortnight, "Last 14 days"),
Pair.New(DateType.LastMonth, "Last 30 days")
};
var lookup = options.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var lookup = options.ToDictionary(kvp => kvp.First, kvp => kvp.Second);
ddb.GetText = () => lookup[filter.Date];
ddb.OnMouseDown = _ =>
{
Func<KeyValuePair<DateType, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
Func<Pair<DateType, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
{
var item = ScrollItemWidget.Setup(
tpl,
() => filter.Date == option.Key,
() => { filter.Date = option.Key; ApplyFilter(); }
() => filter.Date == option.First,
() => { filter.Date = option.First; ApplyFilter(); }
);
item.Get<LabelWidget>("LABEL").GetText = () => option.Value;
item.Get<LabelWidget>("LABEL").GetText = () => option.Second;
return item;
};
@@ -176,27 +176,27 @@ namespace OpenRA.Mods.RA.Widgets.Logic
if (ddb != null)
{
// Using list to maintain the order
var options = new List<KeyValuePair<DurationType, string>>
var options = new List<Pair<DurationType, string>>
{
new KeyValuePair<DurationType, string>(DurationType.Any, ddb.GetText()),
new KeyValuePair<DurationType, string>(DurationType.VeryShort, "Under 5 min"),
new KeyValuePair<DurationType, string>(DurationType.Short, "Short (10 min)"),
new KeyValuePair<DurationType, string>(DurationType.Medium, "Medium (30 min)"),
new KeyValuePair<DurationType, string>(DurationType.Long, "Long (60+ min)")
Pair.New(DurationType.Any, ddb.GetText()),
Pair.New(DurationType.VeryShort, "Under 5 min"),
Pair.New(DurationType.Short, "Short (10 min)"),
Pair.New(DurationType.Medium, "Medium (30 min)"),
Pair.New(DurationType.Long, "Long (60+ min)")
};
var lookup = options.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var lookup = options.ToDictionary(kvp => kvp.First, kvp => kvp.Second);
ddb.GetText = () => lookup[filter.Duration];
ddb.OnMouseDown = _ =>
{
Func<KeyValuePair<DurationType, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
Func<Pair<DurationType, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
{
var item = ScrollItemWidget.Setup(
tpl,
() => filter.Duration == option.Key,
() => { filter.Duration = option.Key; ApplyFilter(); }
() => filter.Duration == option.First,
() => { filter.Duration = option.First; ApplyFilter(); }
);
item.Get<LabelWidget>("LABEL").GetText = () => option.Value;
item.Get<LabelWidget>("LABEL").GetText = () => option.Second;
return item;
};
@@ -277,25 +277,25 @@ namespace OpenRA.Mods.RA.Widgets.Logic
ddb.IsDisabled = () => string.IsNullOrEmpty(filter.PlayerName);
// Using list to maintain the order
var options = new List<KeyValuePair<GameInformation.GameOutcome, string>>
var options = new List<Pair<WinState, string>>
{
new KeyValuePair<GameInformation.GameOutcome, string>(GameInformation.GameOutcome.Undefined, ddb.GetText()),
new KeyValuePair<GameInformation.GameOutcome, string>(GameInformation.GameOutcome.Defeat, "Defeat"),
new KeyValuePair<GameInformation.GameOutcome, string>(GameInformation.GameOutcome.Victory, "Victory")
Pair.New(WinState.Undefined, ddb.GetText()),
Pair.New(WinState.Lost, "Defeat"),
Pair.New(WinState.Won, "Victory")
};
var lookup = options.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var lookup = options.ToDictionary(kvp => kvp.First, kvp => kvp.Second);
ddb.GetText = () => lookup[filter.Outcome];
ddb.OnMouseDown = _ =>
{
Func<KeyValuePair<GameInformation.GameOutcome, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
Func<Pair<WinState, string>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, tpl) =>
{
var item = ScrollItemWidget.Setup(
tpl,
() => filter.Outcome == option.Key,
() => { filter.Outcome = option.Key; ApplyFilter(); }
() => filter.Outcome == option.First,
() => { filter.Outcome = option.First; ApplyFilter(); }
);
item.Get<LabelWidget>("LABEL").GetText = () => option.Value;
item.Get<LabelWidget>("LABEL").GetText = () => option.Second;
return item;
};
@@ -409,7 +409,11 @@ namespace OpenRA.Mods.RA.Widgets.Logic
button.IsDisabled = () => selectedReplay == null;
button.OnClick = () =>
{
onDeleteReplay(selectedReplay, () => { if (selectedReplay == null) SelectFirstVisibleReplay(); });
onDeleteReplay(selectedReplay, () =>
{
if (selectedReplay == null)
SelectFirstVisibleReplay();
});
};
}
@@ -514,7 +518,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
// Duration
if (filter.Duration != DurationType.Any)
{
double minutes = replay.GameInfo.Duration.TotalMinutes;
var minutes = replay.GameInfo.Duration.TotalMinutes;
switch (filter.Duration)
{
case DurationType.VeryShort:
@@ -551,7 +555,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
return false;
// Outcome
if (filter.Outcome != GameInformation.GameOutcome.Undefined && filter.Outcome != player.Outcome)
if (filter.Outcome != WinState.Undefined && filter.Outcome != player.Outcome)
return false;
// Faction
@@ -582,7 +586,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
{
selectedReplay = replay;
selectedSpawns = (selectedReplay != null)
? LobbyUtils.GetSpawnClients(selectedReplay.GameInfo.Players, selectedReplay.GameInfo.MapPreview)
? LobbyUtils.GetSpawnOccupants(selectedReplay.GameInfo.Players, selectedReplay.GameInfo.MapPreview)
: new Dictionary<CPos, SpawnOccupant>();
if (replay == null)
@@ -680,7 +684,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
public GameType Type;
public DateType Date;
public DurationType Duration;
public GameInformation.GameOutcome Outcome;
public WinState Outcome;
public string PlayerName;
public string MapName;
public string Faction;
@@ -692,7 +696,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
return Type == default(GameType)
&& Date == default(DateType)
&& Duration == default(DurationType)
&& Outcome == default(GameInformation.GameOutcome)
&& Outcome == default(WinState)
&& string.IsNullOrEmpty(PlayerName)
&& string.IsNullOrEmpty(MapName)
&& string.IsNullOrEmpty(Faction);