Finding a specific name from a list using a Foreach check

10/05/2013 01:22 hacksaw#1
I've been working on a new event idea which involves 2 people being paired up and then parts of each char being checked against the other, I have everything working fine apart from 1 of the checks that selects the players opponent from the list by checking the names in the list, It only selects the first person added to the list and then returns a fail rather than checking the whole list and returning the correct match if they are still there, I have checked and players are being added fine and all player names are in the list when it reaches the "foreach" check, & matchopponent name is correct & in the list.
Anyone know why it only selects the first person who was added to the list rather than searching the list for the correct player and selecting them?

Code:
public static List<Game.Entity> SignedUp = new List<Game.Entity>()

if (client.Entity.ItemEquipped == true)
 {
    foreach (var c in Kernel.SignedUp)   //All players needed are in the list but Only pulls the 1st player who was added to the list rather than the player it should, Opponent confirmed as in list
     {
       if (c.Name == client.Entity.MatchOpponent) //Selects first person added to the list & Calls a fail even tho the opponents name is in the list
         {
10/05/2013 01:51 pro4never#2
It's very difficult for us to say why it only iterates once without seeing the actual code.

When doing a foreach loop (and if you've verified that the collection DOES contain more than one entry) then whats inside that loop controls its logic.

It's possible you break out or return from the loop prematurely.


It would help us if you actually explained what it is you wanted to do. How are you wanting to match up players? Finding two signed up players who are the closest in level/bp/same class/etc?

You're best using a LINQ query that fits your parameters and pulling the two closest matches from that collection to start a match (and if it fails you wait for more people to enter. You could even slowly make it less demanding if no one new signs up)
10/05/2013 03:07 hacksaw#3
Below is the current code im using which contains the foreach loop, Its currently running it from an npc, I can confirm all characters needed are in the Collection, The opponent is already determined before the below code is activated & "client.Entity.MatchOpponent" contains the clients opponents name which the foreach loop is supposed to search for, The names here are also checked and definatly correct.

Code:
Kernel.CardMatch.Add(client.Entity); //players added to the collection prior to the next code

case 1:
                                                {
                                                   if (client.Entity.CardEquipped == true)
                                                    {
                                                    try
                                                    {
                                                       foreach (var c in Kernel.CardMatch)
                                                       {
                                                       if (c.Name == client.Entity.CardMatchOpponent) //This is where its failing and returns "Your opponent has resigned.", Client names needed are def in the list, & the clients opponents name is def fine n correct
                                                           {
                                                             if (c.CardEquipped == true)
                                                               {
                                                                   dialog.Text("Choose your move.");
                                                                   dialog.Option("1.", 1);
                                                                   dialog.Option("2.", 2);
                                                                   dialog.Option("3.", 3);
                                                                   dialog.Option("4.", 4);
                                                                   dialog.Option("5.", 5);
                                                                   dialog.Send();
                                                                   break;
                                                               }
                                                               else
                                                               {
                                                                   dialog.Text("Your opponent hasent selected a card.");
                                                                   dialog.Option("Ok.", 255);
                                                                   dialog.Send();
                                                                   break;
                                                               }
                                                            }
                                                            else
                                                            {
                                                                dialog.Text("Your opponent has resigned.");
                                                                dialog.Option("Okies.", 255);
                                                                dialog.Send();
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    catch { }
                                                    }
                                                    else
                                                    {
                                                        dialog.Text("You havent equipped your battle card.");
                                                        dialog.Option("Okies.", 255);
                                                        dialog.Send();
                                                        break;
                                                    }
                                                }
                                            break;
10/05/2013 03:16 _Emme_#4
The client.Entity.CardMatchOpponent is NOT the first value in the list Kernel.CardMatch.

Whenever it falls to the else, it breaks the loop and thus only iterates once.
10/05/2013 03:17 Aceking#5
Well your code gets the first name in the collection, if it doesnt match then it returns resigned and breaks from the loop.

You need to insert your opponent has resigned AFTER the for each loop, if the name was not found.
So you could insert a success boolean, and when the name matches set it to true.

After the foreach loop, check if success is true, if it isnt, then you would execute the opponent has resigned.

Hope you can understand that.
10/05/2013 06:57 -Shunsui-#6
LINQ
10/05/2013 11:15 shadowman123#7
i understood Nothing to be Honest
10/05/2013 13:52 drakejoe67#8
Well, like they said, in the npc dialogue. You're using foreach and it will only check out the first character only. This is because if the name does does match, it will give you the controls and so on, and break out. Ending the npc dialogue. but this is happening even if the first character conditions is not matching the name. Which is where the resigned opponent begins. And you also had a break there. That itself is stopping the whole loop so it will only check the very first character of your list.

What you could've done is make a boolean, and if name matches, just 1 is enough. Then set bool to true, and add the dialogues afterwards using if (bool) //true


Code:
Kernel.CardMatch.Add(client.Entity); //players added to the collection prior to the next code

case 1:
                                                {
                                                   if (client.Entity.CardEquipped == true)
                                                    {
                                                    try
                                                    {
							 //change the object to whatever var c is
							object cc = null;
                                                        foreach (var c in Kernel.CardMatch)
                                                        {
                                                            if (c.Name == client.Entity.CardMatchOpponent)
                                                            {
								 cc = c;
                                                            }
                                                        }
							if (cc != null)
                                                        {
							    if (cc.Name == client.Entity.CardMatchOpponent)
							    {
                                                                if (cc.CardEquipped == true)
                                                                {
                                                                   dialog.Text("Choose your move.");
                                                                   dialog.Option("1.", 1);
                                                                   dialog.Option("2.", 2);
                                                                   dialog.Option("3.", 3);
                                                                   dialog.Option("4.", 4);
                                                                   dialog.Option("5.", 5);
                                                                   dialog.Send();
                                                                   break;
                                                                }
                                                                else
                                                                {
                                                                   dialog.Text("Your opponent hasent selected a card.");
                                                                   dialog.Option("Ok.", 255);
                                                                   dialog.Send();
                                                                   break;
                                                                }
                                                              }
							else
                                                            {
                                                                dialog.Text("Your opponent has resigned.");
                                                                dialog.Option("Okies.", 255);
                                                                dialog.Send();
                                                                break;
                                                            }
                                                        }
                                                        else
                                                        {
                                                                dialog.Text("Your opponent has logged off.");
                                                                dialog.Option("Okies.", 255);
                                                                dialog.Send();
                                                                break;
                                                        }
                                                    }
                                                    catch { }
                                                    }
                                                    else
                                                    {
                                                        dialog.Text("You havent equipped your battle card.");
                                                        dialog.Option("Okies.", 255);
                                                        dialog.Send();
                                                        break;
                                                    }
                                                }
                                            break;
10/05/2013 15:02 hacksaw#9
Thanks for all the info and suggestions guys, Can see where i was going wrong now, All sorted :handsdown: