private void ListPvPGames()
        {
            int gameCount = sm.GetMatchCount();

            for (int i = 0; i < gameCount; i++)
            {
                MatchStatistics ms = sm.GetMatchByIndex(i);

                int pCount = ms.GetPlayerCount();
                int pTeam  = -1;

                for (int j = 0; j < pCount; j++)
                {
                    PlayerStatistics ps = ms.GetPlayer(j);

                    if (!ps.IsAI && !ps.WasSpectator)
                    {
                        // If we find a single player on a different team than another player,
                        // we'll count the game as a PvP game
                        if (pTeam > -1 && (ps.Team != pTeam || ps.Team == 0))
                        {
                            ListGameIndexIfPrerequisitesMet(i);
                            break;
                        }

                        pTeam = ps.Team;
                    }
                }
            }
        }
        private void ListOnlineGames()
        {
            int gameCount = sm.GetMatchCount();

            for (int i = 0; i < gameCount; i++)
            {
                MatchStatistics ms = sm.GetMatchByIndex(i);

                int pCount  = ms.GetPlayerCount();
                int hpCount = 0;

                for (int j = 0; j < pCount; j++)
                {
                    PlayerStatistics ps = ms.GetPlayer(j);

                    if (!ps.IsAI)
                    {
                        hpCount++;

                        if (hpCount > 1)
                        {
                            ListGameIndexIfPrerequisitesMet(i);
                            break;
                        }
                    }
                }
            }
        }
        private PlayerStatistics FindLocalPlayer(MatchStatistics ms)
        {
            int pCount = ms.GetPlayerCount();

            for (int pId = 0; pId < pCount; pId++)
            {
                PlayerStatistics ps = ms.GetPlayer(pId);

                if (!ps.IsAI && ps.IsLocalPlayer)
                {
                    return(ps);
                }
            }

            return(null);
        }
        private void ListCoOpGames()
        {
            int gameCount = sm.GetMatchCount();

            for (int i = 0; i < gameCount; i++)
            {
                MatchStatistics ms = sm.GetMatchByIndex(i);

                int  pCount  = ms.GetPlayerCount();
                int  hpCount = 0;
                int  pTeam   = -1;
                bool add     = true;

                for (int j = 0; j < pCount; j++)
                {
                    PlayerStatistics ps = ms.GetPlayer(j);

                    if (!ps.IsAI && !ps.WasSpectator)
                    {
                        hpCount++;

                        if (pTeam > -1 && (ps.Team != pTeam || ps.Team == 0))
                        {
                            add = false;
                            break;
                        }

                        pTeam = ps.Team;
                    }
                }

                if (add && hpCount > 1)
                {
                    ListGameIndexIfPrerequisitesMet(i);
                }
            }
        }
        /// <summary>
        /// Adjusts the labels on the "Total statistics" tab.
        /// </summary>
        private void SetTotalStatistics()
        {
            int      gamesStarted  = 0;
            int      gamesFinished = 0;
            int      gamesPlayed   = 0;
            int      wins          = 0;
            int      gameLosses    = 0;
            TimeSpan timePlayed    = TimeSpan.Zero;
            int      numEnemies    = 0;
            int      numAllies     = 0;
            int      totalKills    = 0;
            int      totalLosses   = 0;
            int      totalScore    = 0;
            int      totalEconomy  = 0;

            int[] sideGameCounts = new int[sides.Length];
            int   numEasyAIs     = 0;
            int   numMediumAIs   = 0;
            int   numHardAIs     = 0;

            foreach (int gameIndex in listedGameIndexes)
            {
                MatchStatistics ms = sm.GetMatchByIndex(gameIndex);

                gamesStarted++;

                if (ms.SawCompletion)
                {
                    gamesFinished++;
                }

                timePlayed += TimeSpan.FromSeconds(ms.LengthInSeconds);

                PlayerStatistics localPlayer = FindLocalPlayer(ms);

                if (!localPlayer.WasSpectator)
                {
                    totalKills   += localPlayer.Kills;
                    totalLosses  += localPlayer.Losses;
                    totalScore   += localPlayer.Score;
                    totalEconomy += localPlayer.Economy;

                    if (localPlayer.Side > 0 && localPlayer.Side <= sides.Length)
                    {
                        sideGameCounts[localPlayer.Side - 1]++;
                    }

                    if (!ms.SawCompletion)
                    {
                        continue;
                    }

                    if (localPlayer.Won)
                    {
                        wins++;
                    }
                    else
                    {
                        gameLosses++;
                    }

                    gamesPlayed++;

                    for (int i = 0; i < ms.GetPlayerCount(); i++)
                    {
                        PlayerStatistics ps = ms.GetPlayer(i);

                        if (!ps.WasSpectator && (!ps.IsLocalPlayer || ps.IsAI))
                        {
                            if (ps.Team == 0 || localPlayer.Team != ps.Team)
                            {
                                numEnemies++;
                            }
                            else
                            {
                                numAllies++;
                            }

                            if (ps.IsAI)
                            {
                                if (ps.AILevel == 0)
                                {
                                    numEasyAIs++;
                                }
                                else if (ps.AILevel == 1)
                                {
                                    numMediumAIs++;
                                }
                                else
                                {
                                    numHardAIs++;
                                }
                            }
                        }
                    }
                }
            }

            lblGamesStartedValue.Text  = gamesStarted.ToString();
            lblGamesFinishedValue.Text = gamesFinished.ToString();
            lblWinsValue.Text          = wins.ToString();
            lblLossesValue.Text        = gameLosses.ToString();

            if (gameLosses > 0)
            {
                lblWinLossRatioValue.Text = Math.Round(wins / (double)gameLosses, 2).ToString();
            }
            else
            {
                lblWinLossRatioValue.Text = "-";
            }

            if (gamesStarted > 0)
            {
                lblAverageGameLengthValue.Text = TimeSpan.FromSeconds((int)timePlayed.TotalSeconds / gamesStarted).ToString();
            }
            else
            {
                lblAverageGameLengthValue.Text = "-";
            }

            if (gamesPlayed > 0)
            {
                lblAverageEnemyCountValue.Text = Math.Round(numEnemies / (double)gamesPlayed, 2).ToString();
                lblAverageAllyCountValue.Text  = Math.Round(numAllies / (double)gamesPlayed, 2).ToString();
                lblKillsPerGameValue.Text      = (totalKills / gamesPlayed).ToString();
                lblLossesPerGameValue.Text     = (totalLosses / gamesPlayed).ToString();
                lblAverageEconomyValue.Text    = (totalEconomy / gamesPlayed).ToString();
            }
            else
            {
                lblAverageEnemyCountValue.Text = "-";
                lblAverageAllyCountValue.Text  = "-";
                lblKillsPerGameValue.Text      = "-";
                lblLossesPerGameValue.Text     = "-";
                lblAverageEconomyValue.Text    = "-";
            }

            if (totalLosses > 0)
            {
                lblKillLossRatioValue.Text = Math.Round(totalKills / (double)totalLosses, 2).ToString();
            }
            else
            {
                lblKillLossRatioValue.Text = "-";
            }

            lblTotalTimePlayedValue.Text = timePlayed.ToString();
            lblTotalKillsValue.Text      = totalKills.ToString();
            lblTotalLossesValue.Text     = totalLosses.ToString();
            lblTotalScoreValue.Text      = totalScore.ToString();
            lblFavouriteSideValue.Text   = sides[GetHighestIndex(sideGameCounts)];

            if (numEasyAIs >= numMediumAIs && numEasyAIs >= numHardAIs)
            {
                lblAverageAILevelValue.Text = "Easy";
            }
            else if (numMediumAIs >= numEasyAIs && numMediumAIs >= numHardAIs)
            {
                lblAverageAILevelValue.Text = "Medium";
            }
            else
            {
                lblAverageAILevelValue.Text = "Hard";
            }
        }
        private void LbGameList_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbGameStatistics.ClearItems();

            if (lbGameList.SelectedIndex == -1)
            {
                return;
            }

            MatchStatistics ms = sm.GetMatchByIndex(listedGameIndexes[lbGameList.SelectedIndex]);

            List <PlayerStatistics> players = new List <PlayerStatistics>();

            for (int i = 0; i < ms.GetPlayerCount(); i++)
            {
                players.Add(ms.GetPlayer(i));
            }

            players = players.OrderBy(p => p.Score).Reverse().ToList();

            Color textColor = UISettings.AltColor;

            for (int i = 0; i < ms.GetPlayerCount(); i++)
            {
                PlayerStatistics ps = players[i];

                //List<string> items = new List<string>();
                List <XNAListBoxItem> items = new List <XNAListBoxItem>();

                if (ps.Color > -1 && ps.Color < mpColors.Count)
                {
                    textColor = mpColors[ps.Color].XnaColor;
                }

                if (ps.IsAI)
                {
                    items.Add(new XNAListBoxItem(AILevelToString(ps.AILevel), textColor));
                }
                else
                {
                    items.Add(new XNAListBoxItem(ps.Name, textColor));
                }

                if (ps.WasSpectator)
                {
                    // Player was a spectator
                    items.Add(new XNAListBoxItem("-", textColor));
                    items.Add(new XNAListBoxItem("-", textColor));
                    items.Add(new XNAListBoxItem("-", textColor));
                    items.Add(new XNAListBoxItem("-", textColor));
                    items.Add(new XNAListBoxItem("-", textColor));
                    XNAListBoxItem spectatorItem = new XNAListBoxItem();
                    spectatorItem.Text      = "Spectator";
                    spectatorItem.TextColor = textColor;
                    spectatorItem.Texture   = sideTextures[sideTextures.Length - 1];
                    items.Add(spectatorItem);
                    items.Add(new XNAListBoxItem("-", textColor));
                }
                else
                {
                    if (!ms.SawCompletion)
                    {
                        // The game wasn't completed - we don't know the stats
                        items.Add(new XNAListBoxItem("-", textColor));
                        items.Add(new XNAListBoxItem("-", textColor));
                        items.Add(new XNAListBoxItem("-", textColor));
                        items.Add(new XNAListBoxItem("-", textColor));
                        items.Add(new XNAListBoxItem("-", textColor));
                    }
                    else
                    {
                        // The game was completed and the player was actually playing
                        items.Add(new XNAListBoxItem(ps.Kills.ToString(), textColor));
                        items.Add(new XNAListBoxItem(ps.Losses.ToString(), textColor));
                        items.Add(new XNAListBoxItem(ps.Economy.ToString(), textColor));
                        items.Add(new XNAListBoxItem(ps.Score.ToString(), textColor));
                        items.Add(new XNAListBoxItem(
                                      Conversions.BooleanToString(ps.Won, BooleanStringStyle.YESNO), textColor));
                    }

                    if (ps.Side == 0 || ps.Side > sides.Length)
                    {
                        items.Add(new XNAListBoxItem("Unknown", textColor));
                    }
                    else
                    {
                        XNAListBoxItem sideItem = new XNAListBoxItem();
                        sideItem.Text      = sides[ps.Side - 1];
                        sideItem.TextColor = textColor;
                        sideItem.Texture   = sideTextures[ps.Side - 1];
                        items.Add(sideItem);
                    }

                    items.Add(new XNAListBoxItem(TeamIndexToString(ps.Team), textColor));
                }

                if (!ps.IsLocalPlayer)
                {
                    lbGameStatistics.AddItem(items);

                    items.ForEach(item => item.Selectable = false);
                }
                else
                {
                    lbGameStatistics.AddItem(items);
                    lbGameStatistics.SelectedIndex = i;
                }
            }
        }