Exemplo n.º 1
0
        /// <summary>
        /// Gets initial data from the client, more specifically the player name. Sends initial game info to the client.
        /// </summary>
        /// <param name="state"></param>
        private static void ReceivePlayerName(SocketState state)
        {
            try
            {
                // This block splits up the server data within the StringBuilder, and places it into initialInfo.
                StringBuilder sb          = state.sb;
                char[]        separator   = new char[] { '\n' };
                string[]      initialInfo = sb.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);

                // Assigns the ID sent by the server to the socket state for uniqie identification of connections.
                state.sb.Remove(0, initialInfo[0].Length + 1);
                string name = initialInfo[0];

                Ship newShip;
                lock (theWorld)
                {
                    newShip = theWorld.AddShipRandomPosition(name, settings.GetShotDelay(), settings.GetThrustStrength(), settings.GetTurningRate(), settings.GetStartingHP(), settings.GetShipHitBoxSize());
                }

                state.ClientCallback = HandleClientRequest;
                state.ID             = newShip.GetID();

                Networking.SendSocket(state.theSocket, "" + newShip.GetID() + "\n" + theWorld.GetSize() + "\n");

                lock (clients)
                {
                    clients.AddLast(state);
                }

                Networking.GetData(state);
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: " + e.Message);
            }
        }