/// <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> /// 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); }