예제 #1
0
        // Add the client to the lists
        public static void AddUser(Gamers gamer)

        {
            Set printData         = new Set();
            Get closingOrNotLobby = new Get();

            // Number which I will assign to the new player
            int gamerNumber;

            // Add to the list of players and assign a number

            Data.GamersList.gamer.Add(gamer);
            gamerNumber = Data.GamersList.gamer.Count - 1;
            Data.GamersList.gamer[gamerNumber].Number = gamerNumber;

            // The player becomes the last in the lobby
            Data.LastNumber = gamerNumber;

            // Add tables from another stream to the collection
            Data.Inf.Dispatcher.Invoke(new Action(delegate()
            {
                Data.gamerList.Add(Data.GamersList.gamer[gamerNumber]);
            }));


            // Showing who joined
            printData.SetMassageAddLeave(true, Data.LastNumber);

            // Send a new list to all players
            Data.GamersList.Type = 0;
            SendAllThread(Data.GamersList);

            // Close the Ready button until ping appears
            Data.LobbyLink.Dispatcher.Invoke(new Action(delegate()
            {
                Data.LobbyLink.CheckPing();
                Data.LobbyLink.FlashWin();
            }));

            // Close the lobby or not
            closingOrNotLobby.GetClosingLobby();

            // Control the start button
            Data.LobbyLink.Dispatcher.Invoke(new Action(delegate()
            {
                Data.LobbyLink.Go();
            }));
        }
예제 #2
0
        // Removing a player over TCP (basic removal)
        public static void RemoveUser(TcpClient tcpUser)
        {
            Set print     = new Set();
            Set printData = new Set();

            for (int i = 1; i < Data.GamersList.gamer.Count; i++)
            {
                // If the participant is in the tcp list of clients, then delete it by index
                if (tcpClients[i] == tcpUser)
                {
                    //  First show that the participant has left the chat

                    printData.SetMassageAddLeave(false, i);
                    GamerList gamerListAdd = new GamerList();
                    gamerListAdd.Type    = 9;
                    gamerListAdd.Number  = i;
                    gamerListAdd.Message = "leave";

                    Host.SendAllThread(gamerListAdd);

                    try
                    {
                        // Delete all information from the list
                        Data.GamersList.gamer.RemoveAt(i);

                        // If not the last one in the list has left, then I recount the numbers after it
                        if (i != Data.GamersList.gamer.Count)
                        {
                            for (int j = i; j < Data.GamersList.gamer.Count; j++)
                            {
                                Data.GamersList.gamer[j].Number = j;
                            }
                        }


                        // Delete from the table collection
                        Data.ChatStatic.Dispatcher.Invoke(new Action(delegate()
                        {
                            Data.gamerList.Clear();
                            Set addToDataGrid = new Set();
                            // Host sending
                            for (int gamer = 0; gamer < Data.GamersList.gamer.Count; gamer++)
                            {
                                addToDataGrid.AddToGrid(gamer);
                            }
                        }));
                    }
                    catch (Exception ex) { }

                    if (tcpUser != null)
                    {
                        tcpUser.Close();
                    }

                    if (tcpClients[i] != null)
                    {
                        tcpClients[i].Close();
                        tcpClients.RemoveAt(i);
                    }

                    Get closingOrNotLobby = new Get();

                    // Open the lobby if the player leaves the lobby himself, if he doesn’t open, the lobby will not open
                    closingOrNotLobby.GetClosingLobby();

                    Data.GamersList.Type = 0;
                    Host.SendAllThread(Data.GamersList);

                    // Control the start button
                    Data.LobbyLink.Dispatcher.Invoke(new Action(delegate()
                    {
                        Data.LobbyLink.Go();
                    }));
                    Data.LobbyLink.Dispatcher.Invoke(new Action(delegate()
                    {
                        Data.LobbyLink.CheckPing();
                    }));
                }
            }
        }