Exemplo n.º 1
0
        /// <summary>
        /// Returns a list of all available clients
        /// </summary>
        /// <param name="clientSocket">A System.Net.Sockets Socket</param>
        public void ReturnClientList(Socket clientSocket)
        {
            try
            {
                #region Sort what Clients is not currently in game

                List <AClientConnection> AllSortedClients = new List <AClientConnection>();     // placeholder for sorted data

                if (AllClients != null)                                                         // Checks if the AllClients list is null
                {
                    #region find my own instance of 'AClientConnection'
                    AClientConnection me = new AClientConnection();

                    foreach (var client in AllClients)
                    {
                        if (client.ClientSocket == clientSocket)                // find myself
                        {
                            me = client;                                        // set my client object = AClientConnection me
                        }
                    }
                    #endregion

                    #region Filters the list of 'AllClients' befor sending it back

                    foreach (var Client in AllClients)                                          // Sort data by InGameStatus
                    {
                        if (Client.InGameStatus == false || Client.ClientId == me.ClientId)
                        {
                            AllSortedClients.Add(Client);
                        }
                    }

                    #endregion

                    #region Sends all sorted Client list back to the Client as one big string

                    string allClientConnections = "";
                    foreach (var Client in AllSortedClients)                                            // Runs thrugh all sorted clients
                    {
                        string aClientAndConnection;
                        string clientId          = Client.ClientId.ToString();
                        string ClientDisplayName = Client.ClientDisplayName;

                        // Connects the clientId and ClientDisplayName into one string (~ = end of every 'aClientAndConnection')
                        aClientAndConnection = clientId + ";" + ClientDisplayName + "#";

                        allClientConnections = (allClientConnections += aClientAndConnection);         // Append 'aClientAndConnection' to the string 'allClientConnections'
                    }
                    #endregion

                    #region Calles back to the client with a methosd id of '0'
                    // Adds a methodId at the frone of the list of sorted clients
                    string methidIdAndDataString = '0' + "~" + allClientConnections;

                    // Send data back to client
                    clientSocket.Send(ConvertDataToByteArray(methidIdAndDataString));                   // Sendsa list of AllClients in form of one big string 'allClientConnections'

                    #endregion
                }
                else
                {
                }

                #endregion
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Uses the first index in the data from the client to Call methods
        /// </summary>
        /// <param name="clientSocket">A System.Net.Sockets Socket</param>
        /// <param name="resivedData">Data send from the client</param>
        public void ClientMethodCalls(Socket clientSocket, byte[] resivedData)
        {
            try
            {
                bool keepRunning = true;
                while (keepRunning == true)
                {
                    string   clientDataString = ConvertByteArrayToData(resivedData);                    // Deserializees the data the client has send. | returns one big data string
                    string[] resultData       = clientDataString.Split('~');                            // Splits up the clientDataString
                    switch (resultData[0])
                    {
                        #region case 0: kill connection
                    case "0":      // kill connection

                        clientSocket.Disconnect(false);
                        clientSocket.Dispose();

                        // Let the current thread run out. by breaking out of the while loop
                        keepRunning = false;
                        break;
                        #endregion

                        #region case 1: Make a new client
                    case "1":       // Make a new client
                        AClientConnection newClient = new AClientConnection();

                        #region Data from the client

                        newClient.ClientDisplayName = resultData[1].ToString();
                        newClient.HasGameEnded      = Convert.ToBoolean(resultData[2]);
                        newClient.IsPlayer1Turn     = Convert.ToBoolean(resultData[3]);

                        // Splits up all the game field values
                        string[] gameboardValues = resultData[4].ToString().Split('#');

                        // Asigns all gameboardValues to a GameSymbolTypes[] inside 'newClient'.
                        for (int i = 0; i < gameboardValues.Length; i++)
                        {
                            newClient.GameboardFildsArray[i] = (GameSymbolTypes)Enum.Parse(typeof(GameSymbolTypes), gameboardValues[i]);
                        }

                        #endregion

                        newClient.ClientId     = RandomString(64);                                      // Generates a new 'ClientId' that is a random numeric string with 64 characters in it
                        newClient.ClientSocket = clientSocket;                                          // Asign the acceptet socked to the client
                        newClient.InGameStatus = false;                                                 // Not curently in game

                        #region makes sure ClientId is uniqe
                        bool IdCheck = false;
                        while (IdCheck == true)
                        {
                            IdCheck = false;
                            foreach (var AclientId in AllClients)
                            {
                                if (newClient.ClientId == AclientId.ClientId)
                                {
                                    IdCheck = true;
                                }
                            }
                            if (IdCheck == true)
                            {
                                newClient.ClientId = RandomString(64);
                            }
                        }
                        #endregion

                        ReturnClientList(newClient.ClientSocket);                                       // Sends a list of clients back to the new client

                        AllClients.Add(newClient);                                                      // Adds the new client to the list of all clients
                        break;
                        #endregion

                        #region case 2: Connect two players
                    case "2":
                        // Takes two values:
                        // 1, oponentId

                        AClientConnection me      = new AClientConnection();
                        AClientConnection oponent = new AClientConnection();            // my oponent

                        foreach (var client in AllClients)
                        {
                            if (client.ClientId == clientDataString[1].ToString())      // find oponent
                            {
                                oponent = client;                                       // set oponent client = AClientConnection oponent
                            }

                            if (client.ClientSocket == clientSocket)                    // find myself
                            {
                                me = client;                                            // set my client object = AClientConnection me
                            }
                        }

                        // Connect two players.
                        oponent.Oponent = me;           // I am his
                        me.Oponent      = oponent;      // he is mine

                        // updates InGameStatus
                        oponent.InGameStatus = true;
                        me.InGameStatus      = true;

                        // piggyback data: returns an updated InGameStatus for both players
                        oponent.ClientSocket.Send(Encoding.ASCII.GetBytes(ConvertDataToByteArray(oponent.InGameStatus).ToString()));
                        me.ClientSocket.Send(Encoding.ASCII.GetBytes(ConvertDataToByteArray(me.InGameStatus).ToString()));

                        break;
                        #endregion

                        #region case 3: Update oponent HasGameEnded status
                    case "3":                                               // Update oponent HasGameEnded status

                        AClientConnection myself = new AClientConnection(); // A placholder for my own client object

                        // Finding my own client object in the AllClients list
                        AllClients.ForEach(o =>
                        {
                            if (o.ClientId == myself.ClientId)
                            {
                                myself = o;
                            }
                        });

                        myself.Oponent.HasGameEnded = true;                                     // Sets the oponent HasGameEnded to true
                        // Send the HasGameEnded status to the oponent
                        myself.Oponent.ClientSocket.Send(Encoding.ASCII.GetBytes(ConvertDataToByteArray(myself.Oponent.HasGameEnded).ToString()));
                        break;
                        #endregion

                        #region Update Oponent Gameboard
                    case "4":       // Update Oponent Gameboard

                        break;
                        #endregion

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }