/// <summary> /// This method acts as the callback from Accepting_A_New_Client method. It takes in the state object and updates the callback to be /// RecievePlayerName and then it requests more data from the server to actually recieve the player name. /// </summary> /// <param name="state">The state obj of the about to about to be added player.</param> static void HandleNewClient(PreservedState state) { // Update the call back and request more data, aka the player name. state.callBack = ReceivePlayerName; Network.i_want_more_data(state); }
/// <summary> /// Called whenever we receive a move or split request from a client. /// </summary> /// <param name="state">The state object of aplayer, it contains its socket, callback and str builder.</param> static void HandleClientGameRequests(PreservedState state) { // Declare these variable to add in the decoding of the string from the network. String requests; String[] requestArray; int team_id; String command; // Find out what the client's message is requests = state.strBuilder.ToString(); // Pull out and process the first valid request from the network request string. String request; // Ensure the the request is in a valid format to be processed. if (requests[0] == '(') { request = requests.Substring(1, requests.IndexOf('\n') - 2); } // If it is a partial message, look at the next request. else { String temp = requests.Substring(requests.IndexOf('\n') + 1); if (temp.Length > 0) { request = temp.Substring(1, temp.IndexOf('\n') - 2); } else { return; } } // Clear the remaining duplicate requests. state.strBuilder.Clear(); // Get the team_id of the player from the ClientSockets Set. team_id = ClientSockets[state.socket]; // Split of the request to process all its individual peices. requestArray = request.Split(','); // Pull out the main command of the request - move or split command = requestArray[0]; // Try and parse the request into the desired double destination values. try { double destX = Convert.ToDouble(requestArray[1].Trim()); double destY = Convert.ToDouble(requestArray[2].Trim()); lock (world) { if (command == "move") { world.MovePlayer(destX, destY, world.Teams[team_id]); } else // command == "split" { world.Split(destX, destY, team_id); lock (mergeTimerLocker) { StartMergeTimer(team_id); } } } } catch (Exception e) { Console.WriteLine("Bad Data: " + request); } // Continue listening for more requests Network.i_want_more_data(state); }
/// <summary> /// A Callback that's called when a new client connects. /// Gets the client's desired player name, sends the client /// their player and the world, and then starts listening /// for move and split requests. /// </summary> /// <param name="state">The state object of the newly added player, it contains its socket, callback and str builder.</param> static void ReceivePlayerName(PreservedState state) { // Pull the player name from the state obj str builder and then clear it. String playerName = state.strBuilder.ToString(); state.strBuilder.Clear(); // Remove the \n from the end of the player name. playerName = playerName.Substring(0, playerName.Length - 1); // Declare team id and player cube out here so it can be used in all the locks. int team_id; Cube playerCube; // Lock the world in order to generate information to build the player cube. lock (world) { // Create the player's cube atributes Point playerXY = world.GetPlayerSpawnLoc(); int argb_color = world.SetCubeColor(); double mass = world.INITIAL_PLAYER_MASS; int playerUid = world.GetNextUID(); team_id = playerUid; // Generate the player cube and add it to the world, new players list, and teams list. playerCube = new Cube(playerXY.X, playerXY.Y, argb_color, playerUid, team_id, false, playerName, mass); world.AddOrUpdateCube(playerCube); // Create a team for the player world.Teams.Add(team_id, new List <Cube>() { playerCube }); // Start tracking the player's team's stats PlayerSessionStats session = new PlayerSessionStats(); session.MaximumMass = world.INITIAL_PLAYER_MASS; session.CurrentMass = world.INITIAL_PLAYER_MASS; world.TeamStatistics.Add(team_id, session); } // Lock the client sockets set inorder to add the newly created players socket to it. lock (clientSocketsLocker) { ClientSockets.Add(state.socket, team_id); } // Serialize the player cube to send it to the client. String playerCubeStr = JsonConvert.SerializeObject(playerCube) + "\n"; // Send player their cube Network.Send(state.socket, playerCubeStr); // Update the callback in order to handle more players potentially adding. state.callBack = HandleClientGameRequests; // Since there are now players in the game, set this to false so updates can happen. noPlayersJoined = false; // Send player the current state of the world lock (world) { StringBuilder worldCubes = new StringBuilder(); foreach (Cube cube in world.Cubes) { worldCubes.Append(JsonConvert.SerializeObject(cube) + '\n'); } Network.Send(state.socket, worldCubes.ToString()); } // Request more data from the server. Network.i_want_more_data(state); }
/// <summary> /// Uses AgCubio's Network class to begin receiving the web request. Changes the /// callback fcn of the state object so that HandleWebRequest will be called to process /// the request info. /// </summary> static void GetWebRequest(PreservedState state) { state.callBack = HandleWebRequest; Network.i_want_more_data(state); }