/// <summary> /// starts the world /// </summary> private void Start() { world = new World(); PlayerDictionary = new Dictionary <int, Cube>(); AllSockets = new Dictionary <Socket, Cube>(); SplitDictionary = new Dictionary <int, HashSet <Cube> >(); HighestRanks = new Dictionary <int, Cube>(); foodCount = 0; VirusCount = 0; world = new World(); timer = new Timer(); //(1 second/25 heartbeats) * 1000 = 40 miliseconds timer.Interval = (double)(1.0 / world.Heartbeat * 1000); //the heartbeats of the world new timer and set seconds to update timer.Elapsed += Timer_Elapsed; lock (world) { if (foodCount < world.MaxFood) { for (int i = foodCount; i < world.MaxFood; i++) { Cube food = world.CreateFood(); world.cubeList.Add(food.uid, food); } } if (VirusCount < world.MaxVirus) { for (int i = VirusCount; i < world.MaxVirus; i++) { Cube virus = world.CreateVirus(); world.cubeList.Add(virus.uid, virus); } } } foodCount = world.MaxFood; VirusCount = world.MaxVirus; timer.Start(); //Awaiting Network client connections NetworkingCode.Server_Awaiting_Client_Loop(Handle_New_Client_Connections, 11000); NetworkingCode.Server_Awaiting_Client_Loop(Web_Browser_Connection, 11100); }
/// <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); } } } }