/// <summary> /// Callback that sends our name to the server /// </summary> /// <param name="state"></param> public void First_Contact_Callback(PreservedState state) { pstate = state; NetworkingCode.Send(pstate.State_Socket, Name_Textbox.Text + "\n"); state.State_Callback = Received_Player_Callback; NetworkingCode.i_want_more_data(state); }
/// <summary> /// Sends our mouse postion to the server /// </summary> public void SendMousePosition() { int xpos, ypos; MousePos(out xpos, out ypos); NetworkingCode.Send(pstate.State_Socket, "(move, " + xpos + ", " + ypos + ")\n"); //do we have to add the "\n" or not? Invalidate(); }
/// <summary> /// sending a single cube to update the world of either a player or a dead cube /// </summary> /// <param name="cube"></param> private void SendCube(Cube cube) { foreach (Socket socket in AllSockets.Keys) { string message = JsonConvert.SerializeObject(cube); NetworkingCode.Send(socket, message + "\n"); } }
/// <summary> /// Sends out split request to the server /// </summary> public void SendSplit() { int xpos, ypos; MousePos(out xpos, out ypos); if (!(SignInPanel.Visible == true)) { NetworkingCode.Send(pstate.State_Socket, "(split, " + xpos + ", " + ypos + ")\n"); } }
/// <summary> /// Sends all the cubes in the world dictionary to the client /// </summary> /// <param name="state"></param> //private void UpdateWorld(PreservedState) private void UpdateWorld() { foreach (Cube cube in world.cubeList.Values) { foreach (Socket socket in AllSockets.Keys) { string message = JsonConvert.SerializeObject(cube); NetworkingCode.Send(socket, message + "\n"); } } }
/// <summary> /// this handles every client connections /// </summary> /// <param name="state"></param> public void Handle_New_Client_Connections(PreservedState state) { //set up a message to let us know that a client has connected to the server Console.WriteLine("A new client has connected to the server."); //set up a callback to recieve the player name state.State_Callback = Recieve_Player_Name; //Request more data NetworkingCode.i_want_more_data(state); }
/// <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> ///Process requests from the Web Browser /// </summary> /// <param name="state"></param> public void Stat_Request(PreservedState state) { int RequestEnd = state.data.ToString().IndexOf('\n'); string page = ""; string message = state.data.ToString().Substring(0, RequestEnd - 1); state.data.Remove(0, RequestEnd + 2); if (message == "GET /players HTTP/1.1") { string commandText = "SELECT * from Players"; page = SQLDatabase.ReadFromDatabase(commandText); } else if (message.Substring(0, 18) == "GET /games?player=") { string[] GetNameArray = message.Substring(18).Split(' '); string name = GetNameArray[0]; string commandText = "SELECT * FROM Players WHERE Name = '" + name + "'"; page = SQLDatabase.ReadFromDatabase(commandText); } else if (message.Substring(0, 14) == "GET /eaten?id=") { string[] GetNameArray = message.Substring(14).Split(' '); string StringGameID = GetNameArray[0]; int GameID; bool BoolGameID = int.TryParse(StringGameID, out GameID); string commandText = "SELECT * FROM EatenNames WHERE ID = " + StringGameID; page = SQLDatabase.ReadFromEatenNames(commandText, GameID); } else { page = "<!DOCTYPE html><html><title> Error </title><body><h5>Error:</h5><p> Cannot connect to server," + " check URL</p></body></html> "; } NetworkingCode.Send(state.State_Socket, page); }
/// <summary> /// Callback that sets our player that we get from the server /// </summary> /// <param name="state"></param> public void Received_Player_Callback(PreservedState state) { string stringData = state.data.ToString(); int EndPlayer = stringData.IndexOf('\n'); string message = stringData.Substring(0, EndPlayer); state.data.Remove(0, EndPlayer + 1); player = JsonConvert.DeserializeObject <Cube>(message); world.cubeList.Add(player.uid, player); this.Paint += AgCubioGUI_Paint; state.State_Callback = Data_Received_Callback; NetworkingCode.i_want_more_data(state); }
/// <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> /// Callback that goes through all of the data from the server and turns it into cubes. /// </summary> /// <param name="state"></param> public void Data_Received_Callback(PreservedState state) { lock (world) { int End; while ((End = state.data.ToString().IndexOf('\n')) >= 0) { string message = state.data.ToString().Substring(0, End); state.data.Remove(0, End + 1); Cube rebuilt = JsonConvert.DeserializeObject <Cube>(message); if (rebuilt.Mass == 0 && rebuilt.uid == player.uid) { player = rebuilt; world.cubeList.Remove(player.uid); MessageBox.Show("You Died!"); } else if (rebuilt.Mass == 0) { world.cubeList.Remove(rebuilt.uid); } else { if (rebuilt.uid == player.uid) { player = rebuilt; world.cubeList[player.uid] = rebuilt; ScaleWidth(player); } else { world.cubeList[rebuilt.uid] = rebuilt; } } } } NetworkingCode.i_want_more_data(state); }
/// <summary> /// Connects to the server /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void connectButton_Click(object sender, EventArgs e) { SignInPanel.Hide(); NetworkingCode.Connect_to_Server(First_Contact_Callback, Server_Name.Text); }
/// <summary> ///Creating a first contact with the Web browser /// </summary> /// <param name="state"></param> public void Web_Browser_Connection(PreservedState state) { state.State_Callback = Stat_Request; NetworkingCode.i_want_more_data(state); }
/// <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); } } } }