The online racing simulator
#1 - Vego
Listing Players in textBox (c#)
Hello, at the begining i'm sorry for my bad english.
I'm making a small insim for me and my friend. I have a problem with listing players at server. I'm using lfs_external.
I can add player to list when he connecting, but i can't delete him when he left the server.
Here is a part of my code:
// A new client joined the server.
private void NCN_ClientJoinsHost(Packets.IS_NCN NCN)
{
AddToConnectionsList(NCN); // Update Connections[] list
//message from message.txt
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader("message.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{

InSim.Send_MTC_MessageToConnection(line, NCN.UCID, 0); // Show line

}
}
textBox2.Text += NCN.UName;

}




// A client left the server.
private void CNL_ClientLeavesHost(Packets.IS_CNL CNL)
{
RemoveFromConnectionsList(CNL.UCID); // Update Connections[] list


}



I don't know what to write in this case "// A client left the server." I'm trying a lot, but i'm a begginer at programing and i need help.

Kapsel (vego)
You can remove the name from the string like this.

string a = "John Paul George Ringo"; // List of names.
string b = "Paul"; // Name we want to remove.

// Find the starting index of the name.
int index = a.IndexOf(b);

// Remove the name from the string.
a = a.Remove(index, b.Length);

But really, is a TextBox the best way to store a list of players? The ListBox control sounds like it would be much better suited.

// Add items.
listBox1.Items.Add("John");
listBox1.Items.Add("Paul");
listBox1.Items.Add("George");
listBox1.Items.Add("Ringo");

// Remove one.
listbox1.Items.Remove("Paul");

#3 - Silox
Maybe an easier solution is to make a function called update() that loops through the connections list and puts the results into the textbox... This function would look like this:

'Just some rough code
function PlayersUpdate()
textbox.text = ""
for each name as string in connections()
textbox.text += connections(i) & vbcrlf 'Vbcrlf if you want to begin a new line with each player
i += 1
loop
i =0
end function

And just add PlayersUpdate() whenever someone leaves / joins the server.

For a beginner this is (yes, slower) but also easier to understand.
#4 - Vego
Thanks for reply. I change textbox into listbox. Adding is very simple so i made it. But problem with deleting is in that the clientleaveserver have only cnl.ucid (uniqueID), it dont have UName or PName like ClientJoinHost
now my code look like this:
Into the joining:
listBox1.Items.Add(NCN.UName);

But i dont know how made deleting:
listBox1.Items.Remove(??);
#5 - amp88
Quote from Vego :Thanks for reply. I change textbox into listbox. Adding is very simple so i made it. But problem with deleting is in that the clientleaveserver have only cnl.ucid (uniqueID), it dont have UName or PName like ClientJoinHost
now my code look like this:
Into the joining:
listBox1.Items.Add(NCN.UName);

But i dont know how made deleting:
listBox1.Items.Remove(??);

A list isn't a suitable abstract data type to use if you want to store information on connections and players. A Map (I don't know C# so I don't know what implementations you have to work with) is a better one. With a list you just store the connection name but, as you say, when a connection leaves the server you are only given it's unique connection ID. What a map does is that you put what's called a "key and value pair" into the map. The key is a small piece of information that you use to uniquely identify values. So, for a connection the key would be the unique connectionID and the value would be the actual player information. So, when you want to remove the connection from the map all you do is say to remove the connectionID (the key) and both the key and value are removed from the map. Perhaps someone who knows C# can give you more specific information and/or a code example.
Quote from Vego :Thanks for reply. I change textbox into listbox. Adding is very simple so i made it. But problem with deleting is in that the clientleaveserver have only cnl.ucid (uniqueID), it dont have UName or PName like ClientJoinHost
now my code look like this:
Into the joining:
listBox1.Items.Add(NCN.UName);

But i dont know how made deleting:
listBox1.Items.Remove(??);

I don't know much about LFS_External, but something like this should work. Bare in mind I've not tested this.
// A client left the server.
private void CNL_ClientLeavesHost(Packets.IS_CNL CNL)
{
// Get the connection which is leaving.
clsConnection conn = Connections[GetConnIdx(CNL.UCID)];
// Remove name from the ListBox.
listBox1.Items.Remove(conn.Username);

RemoveFromConnectionsList(CNL.UCID); // Update Connections[] list
}

Quote from amp88 :A list isn't a suitable abstract data type to use if you want to store information on connections and players. A Map (I don't know C# so I don't know what implementations you have to work with) is a better one. With a list you just store the connection name but, as you say, when a connection leaves the server you are only given it's unique connection ID. What a map does is that you put what's called a "key and value pair" into the map. The key is a small piece of information that you use to uniquely identify values. So, for a connection the key would be the unique connectionID and the value would be the actual player information. So, when you want to remove the connection from the map all you do is say to remove the connectionID (the key) and both the key and value are removed from the map. Perhaps someone who knows C# can give you more specific information and/or a code example.

It's called a Dictionary in C#, and yes it's ideal for storing the player/connection lists, but that's not what he's asking. LFS_External already stores those lists (in an incredibly strange and over-complicated way), he's just looking to display them on-screen, for which a ListBox control is what you want.
#8 - Vego
Quote from DarkTimes :I don't know much about LFS_External, but something like this should work. Bare in mind I've not tested this.
// A client left the server.
private void CNL_ClientLeavesHost(Packets.IS_CNL CNL)
{
// Get the connection which is leaving.
clsConnection conn = Connections[GetConnIdx(CNL.UCID)];
// Remove name from the ListBox.
listBox1.Items.Remove(conn.Username);

RemoveFromConnectionsList(CNL.UCID); // Update Connections[] list
}


Yeah. Thats a very good idea. I didn't think in this situation about this metod for checking a username of player which disconnected.
Thanks for everybody which want to help me
#9 - amp88
Quote from DarkTimes :It's called a Dictionary in C#, and yes it's ideal for storing the player/connection lists, but that's not what he's asking. LFS_External already stores those lists (in an incredibly strange and over-complicated way), he's just looking to display them on-screen, for which a ListBox control is what you want.

Ok, sorry. I obviously misunderstood.
#10 - Vego
I have a next problem. I add the button, but i dont know where is a mistake. I think that everything is good.
CODE:
#region ' Packet receive events '
// Admin's message from textBox
private void button1_Click(object sender, System.EventArgs e)
{
textBox2.Text = "dupa"; //this is an example
}

Problem is in that the click on the button nothing make :/
Make sure that the button's click event is bound to that event-handler. If you select the button on the form, then go to Properties and select Events, you can see which event-handler's have been bound. See the screenshot below for more information.
Attached images
Click-Events.jpg
#12 - Vego
Thank you very much. I'm a beginner at c# and i didn't know that i must do that :/
#13 - Vego
I have one more question This is possible to set with insim time which player must to wait on start line (layout)? Lfs set it automaticly but i want to have a control at it. I can make a normal timer, but lap time will start before player start
Quote from Vego :I have one more question This is possible to set with insim time which player must to wait on start line (layout)? Lfs set it automaticly but i want to have a control at it. I can make a normal timer, but lap time will start before player start

So you want a longer countdown to start the race?

If so, I don't think it's possible... Well, you can try to build barriers with a layout to block everyone on the start grid but I don't think you can change that.

FGED GREDG RDFGDR GSFDG