/// <summary> /// sends out the player name after the first contact /// </summary> /// <param name="state"></param> public void Recieve_Player_Name(PreservedState state) { if (NetworkingCode.IsConnected(state.State_Socket)) { //recieve player name from view string stringData = state.data.ToString(); int EndName = stringData.IndexOf('\n'); string name = stringData.Substring(0, EndName); state.data.Remove(0, EndName + 1); //create player cube and send to client Cube PlayerCube = world.CreatePlayer(name); //change callback state.State_Callback = Handle_Data_from_Client; lock (world) { //Add to world and player dictionary world.cubeList.Add(PlayerCube.uid, PlayerCube); PlayerDictionary.Add(PlayerCube.uid, PlayerCube); //adds the socket and the socket's player to a dictionary to keep track of the player for each socket AllSockets.Add(state.State_Socket, PlayerCube); //Seriliaze player cube string message = JsonConvert.SerializeObject(PlayerCube); NetworkingCode.Send(state.State_Socket, message + "\n"); //update World UpdateWorld(); } //starts the timer for the server to begin Updating //timer.Start(); //ask for more data NetworkingCode.i_want_more_data(state); } else { lock (world) { AllSockets.Remove(state.State_Socket); if (AllSockets.Count == 0) { //Awaiting Network client connections Start(); } } } }
/// <summary> /// Handles every request from the client /// </summary> /// <param name="state"></param> public void Handle_Data_from_Client(PreservedState state) { if (NetworkingCode.IsConnected(state.State_Socket)) { int MessageEnd; lock (world) { //getting message from the client and desecting the message to respond accordingly while ((MessageEnd = state.data.ToString().IndexOf('\n')) >= 0) { //message recived string message = state.data.ToString().Substring(0, MessageEnd); state.data.Remove(0, MessageEnd + 1); string[] positionArray = message.Split(' '); string xpos = positionArray[1]; string ypos = positionArray[2]; //geting mouse location xpos = xpos.Remove(xpos.Length - 1); ypos = ypos.Remove(ypos.Length - 1); double mouse_x; double mouse_y; bool Double_x = double.TryParse(xpos, out mouse_x); bool Double_y = double.TryParse(ypos, out mouse_y); mouseLocX = mouse_x; mouseLocY = mouse_y; Cube Updated_player = AllSockets[state.State_Socket]; //handles move requests if (message.Substring(1, 4) == "move") { //will decrease the speed as the mass gets bigger, speed starts at 50 double playerSpeed = 1000 / (Updated_player.Mass * 0.5); //double playerSpeed = 1; if (!(Updated_player.team_id == 0)) { foreach (Cube SplitCubes in SplitDictionary[Updated_player.team_id]) { MoveCube(mouse_x, mouse_y, SplitCubes, playerSpeed); } } else { MoveCube(mouse_x, mouse_y, Updated_player, playerSpeed); AllSockets[state.State_Socket] = Updated_player; } } //handling split request else if (message.Substring(1, 5) == "split") { //loop through to see get split 20 times if (splitCount > world.MaxSplit) { splitCount = 0; } else { //splitting and ensuring team id is assigned splitCount++; if ((Updated_player.Mass > world.MinSplitMass) && splitCount <= world.MaxSplit) { if (Updated_player.team_id == 0) { TeamIDCount++; Updated_player.team_id = TeamIDCount; Updated_player.Mass = Updated_player.Mass / 2; Cube SplittedCube = world.Split(Updated_player); SplitTheCube(mouse_x, mouse_y, SplittedCube); SplitDictionary.Add(Updated_player.team_id, new HashSet <Cube>() { SplittedCube, Updated_player }); } //else if player already splitted else { HashSet <Cube> NewSplitCubes = new HashSet <Cube>(); foreach (Cube OldCube in SplitDictionary[Updated_player.team_id]) { OldCube.Mass = OldCube.Mass / 2; Cube SplittedCube = world.Split(OldCube); SplitTheCube(mouse_x, mouse_y, SplittedCube); NewSplitCubes.Add(SplittedCube); NewSplitCubes.Add(OldCube); } SplitDictionary[Updated_player.team_id] = NewSplitCubes; } // end else } //end if } //end else } //end if } // end while } //end lock NetworkingCode.i_want_more_data(state); } else { //Removes socket from list if no information is coming in lock (world) { PlayerDictionary.Remove(AllSockets[state.State_Socket].uid); world.cubeList.Remove(AllSockets[state.State_Socket].uid); if (AllSockets[state.State_Socket].team_id != 0) { SplitDictionary.Remove(AllSockets[state.State_Socket].team_id); } //adding dead player to database SQLDatabase.AddPlayerToDatabase(AllSockets[state.State_Socket]); AllSockets.Remove(state.State_Socket); if (AllSockets.Count == 0) { //Awaiting Network client connections NetworkingCode.Server_Awaiting_Client_Loop(Handle_New_Client_Connections, 11000); } } } }