#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Network;
namespace OpenRA
{
///
/// Contains information about a finished game
///
public class GameInformation
{
/// The map identifier.
public string MapUid;
/// The map title.
public string MapTitle;
/// Game start timestamp.
public DateTime StartTimeUtc;
/// Game end timestamp (when the recoding stopped).
public DateTime EndTimeUtc;
///
/// Gets the game's duration, from the time the game started until the
/// replay recording stopped.
///
/// The game's duration.
public TimeSpan Duration { get { return EndTimeUtc > StartTimeUtc ? EndTimeUtc - StartTimeUtc : TimeSpan.Zero; } }
///
/// Gets the list of players.
///
/// The players.
public IList Players { get; private set; }
///
/// Gets the map preview, using and the .
///
/// The map preview.
public MapPreview MapPreview { get { return Game.modData.MapCache[MapUid]; } }
///
/// Gets the human players.
///
/// The human players.
public IEnumerable HumanPlayers { get { return Players.Where(p => p.IsHuman); } }
///
/// Gets a value indicating whether this instance has just one human player.
///
/// true if this instance has just one human player; otherwise, false.
public bool IsSinglePlayer { get { return HumanPlayers.Count() == 1; } }
Dictionary playersByRuntime;
///
/// Initializes a new instance of the class.
///
public GameInformation()
{
Players = new List();
playersByRuntime = new Dictionary();
}
///
/// Deserialize the specified data into a new instance.
///
/// Data.
public static GameInformation Deserialize(string data)
{
try
{
var info = new GameInformation();
var nodes = MiniYaml.FromString(data);
foreach (var node in nodes)
{
var keyParts = node.Key.Split('@');
switch (keyParts[0])
{
case "Root":
FieldLoader.Load(info, node.Value);
break;
case "Player":
info.Players.Add(FieldLoader.Load(node.Value));
break;
}
}
return info;
}
catch (InvalidOperationException)
{
Log.Write("debug", "GameInformation deserialized invalid MiniYaml:\n{0}".F(data));
throw;
}
}
///
/// Serialize this instance.
///
public string Serialize()
{
var nodes = new List();
nodes.Add(new MiniYamlNode("Root", FieldSaver.Save(this)));
for (var i=0; i
/// Adds the start-up player information.
///
/// Runtime player.
/// Lobby info.
public void AddPlayer(OpenRA.Player runtimePlayer, Session lobbyInfo)
{
if (runtimePlayer == null)
throw new ArgumentNullException("runtimePlayer");
if (lobbyInfo == null)
throw new ArgumentNullException("lobbyInfo");
// We don't care about spectators and map players
if (runtimePlayer.NonCombatant || !runtimePlayer.Playable)
return;
// Find the lobby client that created the runtime player
var client = lobbyInfo.ClientWithIndex(runtimePlayer.ClientIndex);
if (client == null)
return;
var player = new Player
{
ClientIndex = runtimePlayer.ClientIndex,
Name = runtimePlayer.PlayerName,
IsHuman = !runtimePlayer.IsBot,
IsBot = runtimePlayer.IsBot,
FactionName = runtimePlayer.Country.Name,
FactionId = runtimePlayer.Country.Race,
Color = runtimePlayer.Color,
Team = client.Team,
SpawnPoint = runtimePlayer.SpawnPoint,
IsRandomFaction = runtimePlayer.Country.Race != client.Country,
IsRandomSpawnPoint = runtimePlayer.SpawnPoint != client.SpawnPoint
};
playersByRuntime.Add(runtimePlayer, player);
Players.Add(player);
}
///
/// Gets the player information for the specified runtime player instance.
///
/// The player, or null.
/// Runtime player.
public Player GetPlayer(OpenRA.Player runtimePlayer)
{
Player player;
playersByRuntime.TryGetValue(runtimePlayer, out player);
return player;
}
/// Specifies whether the player was defeated, victorious, or there was no outcome defined.
public enum GameOutcome
{
/// Unknown outcome.
Undefined,
/// The player was defeated
Defeat,
/// The player was victorious
Victory
}
///
/// Information about a player
///
public class Player
{
//
// Start-up information
//
/// The client index.
public int ClientIndex;
/// The player name, not guaranteed to be unique.
public string Name;
/// true if the player is a human player; otherwise, false.
public bool IsHuman;
/// true if the player is a bot; otherwise, false.
public bool IsBot;
/// The faction name (aka Country).
public string FactionName;
/// The faction id (aka Country, aka Race).
public string FactionId;
/// The color used by the player in the game.
public HSLColor Color;
/// The team id on start-up, or 0 if the player is not part of the team.
public int Team;
/// The index of the spawn point on the map, or 0 if the player is not part of the team.
public int SpawnPoint;
/// true if the faction was chosen at random; otherwise, false.
public bool IsRandomFaction;
/// true if the spawn point was chosen at random; otherwise, false.
public bool IsRandomSpawnPoint;
//
// Information gathered at a later stage
//
/// The game outcome for this player.
public GameOutcome Outcome;
/// The time when this player won or lost the game.
public DateTime OutcomeTimestampUtc;
}
}
}