Exemplo n.º 1
0
        /// <summary>
        /// This method receives the beginning data for the world
        /// and sets up the main framework
        /// </summary>
        /// <param name="ss"></param>
        private void ReceiveStartup(SocketState ss)
        {
            //Check for error
            if (ss.ErrorOccured)
            {
                Error(ss.ErrorMessage);
            }

            //Change network action to receive normal flow of data method
            ss.OnNetworkAction = ReceiveMessage;

            //Extract the ID data and world data for setup
            string startUp = ss.GetData();

            string[] elements = startUp.Split('\n');
            playerID = int.Parse(elements[0]);
            int worldSize = int.Parse(elements[1]);

            //Setup the world
            theWorld = new World(worldSize);

            //Clear startup data. We have what is needed
            ss.ClearData();

            //Call the receive message method indirectly
            Networking.GetData(ss);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Process the data in current state buffer, the method will find all tokens end with \n, and make sure all data being processed(ToDo) is complete.
        /// It also saves incomplete data in the SocketState buffer and waits for the next incoming receive the complete the partial data
        /// </summary>
        /// <param name="state"></param>
        /// <param name="ToDo"> A action that will be taken for each token(end with \n) in the data</param>>
        private static string[] ProcessData(SocketState state)
        {
            string data = state.GetData();

            string[] jArray = data.Split('\n');

            if (data.Last() != '\n') // Meaning there is incomplete data in current buffer
            {
                // Keep partial data
                state.RemoveData(0, data.LastIndexOf('\n') + 1);
                // Set the last element to empty string
                jArray[jArray.Length - 1] = "";
            }
            else
            {
                //All data are complete, clear the whole buffer
                state.ClearData();
            }

            return(jArray);
        }
Exemplo n.º 3
0
        /// <summary>
        /// We will receive the player's name and assign an ID number
        /// At this time we will also send to the client world size, the ID number
        /// and the walls
        /// </summary>
        /// <param name="client"></param>
        private void GetPlayerInfo(SocketState client)
        {
            //Check if error occured and write message to console
            if (client.ErrorOccured)
            {
                Console.WriteLine(client.ErrorMessage);
            }

            //Set the new callback action
            client.OnNetworkAction = GetActionDataFromClient;

            //Get the player name
            string playerName = client.GetData().Trim('\n');

            //Create a new tank representing the player at a random location
            Tank newPlayer = new Tank(playerNumber, playerName, RandomLocationGenerator())
            {
                //Allows the tank to fire upon spawn
                FrameCount = framesBetweenShot + 1
            };

            //We don't spawn on walls or powerups
            while (CheckForCollision(newPlayer, 1))
            {
                newPlayer.Location = new Vector2D(RandomLocationGenerator());
            }

            //Add player to our connections
            lock (connections)
            {
                //Send ID and worldsize info
                Networking.Send(client.TheSocket, playerNumber.ToString() + "\n" + serverWorld.Size.ToString() + "\n");
                //Add socket state to the collection of players with their ID number
                connections.Add(client, playerNumber);
            }

            Console.WriteLine("Player " + playerNumber.ToString() + ": " + playerName + " has connected.");

            //Add player to server world
            lock (serverWorld.Tanks)
            {
                serverWorld.Tanks.Add(newPlayer.GetID(), newPlayer);
                //Increase player ID number
                playerNumber++;
            }

            //Create a string builder info to serialize and send all the walls
            StringBuilder wallinfo = new StringBuilder();

            foreach (Wall wall in serverWorld.Walls.Values)
            {
                wallinfo.Append(JsonConvert.SerializeObject(wall) + "\n");
            }

            //Send walls to the client
            Networking.Send(client.TheSocket, wallinfo.ToString());

            //Empty the socket state of data
            client.ClearData();

            //Begin receive loop
            Networking.GetData(client);
        }