1
\$\begingroup\$

I have a little problem: I have a list with all the users that have logged in the game and a list that has all the names of the active players and I make a List with all the players as a List<T> (It's a Lists List).

public void CheckNames()
{
    results.Clear();
    for (int i = 0; i < TC.chatters.viewers.Length; i++)
    {
        if (!TotalPlayerNames.Contains(TC.chatters.viewers[i]))
        {
            TotalPlayerNames.Add(TC.chatters.viewers[i]);
            TotalPlayers.Add(new PlayerReal(TC.chatters.viewers[i]));
        }
        if (TotalPlayerNames.Contains(TC.chatters.viewers[i]))
        {
            results.AddRange(TotalPlayers.FindAll(x => x.displayName == TC.chatters.viewers[i]));
            for (int j = 0; j < results.Count; j++)
            {
                results[j].active = true;
            }

        }
    }

    ActivePlayers = results;
    for (int k = 0; k < TotalPlayers.Count; k++)
    {
        bool check = false;
        for (int i = 0; i < TC.chatters.viewers.Length; i++)
        {

            if (TotalPlayers[k].displayName == TC.chatters.viewers[i] && !check)
            {
                TotalPlayers[k].active = true;
                check = true;
            }
            else if (TotalPlayers[k].displayName != TC.chatters.viewers[i] && !check && ((TC.chatters.viewers.Length-1)==i))
            {
                TotalPlayers[k].active = false;
                check = true;
            }

        }
    }
}

There's no problem if I have 100-300 players playing but I DO have a huge problem if 1000-10000 players are playing... any help on this? This method executes each 20 seconds.

\$\endgroup\$
3
\$\begingroup\$

Please use conventional naming of properties. Chatters instead of chatters for example.

Also, why do you use for instead of foreach if you use index only to get an element of collection? Use foreach and your code will be much clearer.

First thing that should come into your mind when you encounter many Contains and !Contains calls which need to determine if an element should be added into a collection is to use HashSet. Use HashSet<string> for TotalPlayerNames and TC.Chatters.Viewers.

Main problem of your code is a lot of repeating loops and duplications like TC.chatters.viewers[i]. Please learn programming basics and particularly C# basics.

All your code can be rewritten like:

var viewers = TC.Chatters.Viewers;

TotalPlayers.AddRange(viewers.Where(v => TotalPlayerNames.Add(v))
                             .Select(v => new PlayerReal(v)));

var activePlayers = new List<PlayerReal>();
foreach (var player in TotalPlayers)
{
    player.Active = viewers.Contains(player.DisplayName);
    if (player.Active)
        activePlayers.Add(player);
}

ActivePlayers = activePlayers;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.