Jump to content

Ajax And Server Handles


Redroest

Recommended Posts

Hey people as some of you know I am busy with a A* pathfinding script in php. I discovered that A* is the best way for my purpose to calculate a route from point A to point B trough obstacles, but I also discovered that this is asks much of a servers processor especially on large grids. Since I am planning to run a server of my own, I have to make sure that my scripts are efficient to reduce pressure on my server. php is a serverside language and that means that when a user sends a request to calculate a path it happens on the server and thus it requires processing on the server. So here's my question: Would it be better to make a javascript version so that the path will be calculated client-side and after the path array is made send the array back to the server? In this case the path will be calculated on the client-pc witch saves valuable processing speed and page loading time.Am I talking crap right now or is this really a good idea to use javascript for calculating?

Link to comment
Share on other sites

Why not just make the whole game in Javascript? PHP isn't really designed to run videogames. Is there anything that actually requires communication with the server?

Link to comment
Share on other sites

Well, you can see my game as a map. Every player has an army and a base. The army's can be send and as you go to the map you can see all of the army's walking to or from eachother. This has to be up to date because people have to be able to intercept an army etc etc. So when the paths and the map is calculated I use AJAX to update the army's positions every 5 seconds to make sure that the client-side part is up to date (depending on how much traffic there will be)So the server times everything (using unixtime) and the clients calculate everything. Thats my goal

Link to comment
Share on other sites

I have no idea what A* is, but I do a lot of AJAX, and I generally go with the philosophy outlined in your first post: process on the client, post data to the server. When possible, reserve PHP for pre-processing and post-processing. I find that javaScript can process a lot of abstract data pretty quickly, usually faster than transfer times. JavaScript (the DOM, really) gets slow when you have to update a lot of page elements.

Link to comment
Share on other sites

Ideally, you would have the server doing most of the work and then sending the information through a socket connection. Of course, PHP is an interpretted language so it would go much slower than a compiled program executing on the server.Javascript doesn't have sockets (a constant open connection between server and client), so you're simulating sockets by sending repeated requests to the server. This would be troublesome if too many people connect. If there are 20 people online, the server is receiving and respnding to 20 HTTP requests every 5 seconds. Generally, it takes longer to process an HTTP request than to just send the data, so a lot of time is wasted just opening and closing requests.The choice of PHP and Javascript for an online multiplayer game means sacrificing efficiency. To summarise, the processing time for the pathfinding algorithm is not what's going to cause most of the stress on the server.But I suppose it would be good to let each client doing the processing for their own army. Just think of this: Since the game is in Javascript, people can cheat and send their own information to the server with whichever position on the map that they like (given that they know a bit of Javascript).

Link to comment
Share on other sites

I am now trying to do this:1-User combines army in javascript2-User presses submit3-The army will be stored in the server-database with a current-position and a destination (witch is the same as start) using php/ajax4-User loads the map/grid5-php checks the current-positions using unixtime as reference6-php sends al of the army's, there current-positions/destinations possible path-array's and the army's speed to the user7-javascript kicks in and checks if the current-positions and destinations are the same8-If the current-position is un-equal to the destination, make the army walk using the path-array's9-Else do nothing10-The current-position/destination, possible path-array's and the army's speed etc will be reloaded every 5 seconds using AJAX to make sure the clientinfo is up to date11-Javascript allows the user to select an army and click a destination12-Javascript calculates path using the defined algorithm13-AJAX sends the path-array's to the server. 14-The server will update every client browser (automatic using the 5 seconds) to show that the users army has a new destination and is walkingStep 5 to 10 will be repeated every 5 seconds. Steps 1 to 4 and 11 to 14 are users input to update the server when needed

Link to comment
Share on other sites

I have no idea what A* is
The A* ("A star") search algorithm is a pathfinding algorithm, it's one of the classic AI problems.
Since the game is in Javascript, people can cheat and send their own information to the server with whichever position on the map that they like (given that they know a bit of Javascript).
That's right, the only question you need to ask yourself is whether or not it matters if the data is accurate. If that matters, then do it on the server to make sure the user isn't going to send whatever they want to send.
Link to comment
Share on other sites

Ok didn't read the last post. So basicly what i'm doing is not the way. I have heard about actionscript do you recommend me to learn this before I'm planning to make a game? This means for me that I have to start all over. And since I already know how to make and javascript and php with ajax its so much extra work

Link to comment
Share on other sites

Javascript doesn't have sockets (a constant open connection between server and client), so you're simulating sockets by sending repeated requests to the server. This would be troublesome if too many people connect. If there are 20 people online, the server is receiving and respnding to 20 HTTP requests every 5 seconds. Generally, it takes longer to process an HTTP request than to just send the data, so a lot of time is wasted just opening and closing requests.
What programming language should I use to send data without using those requests? I try not to use any languages that require a download first to be able to run
Link to comment
Share on other sites

If you're sending a request at all it's not going to matter what language you use, all requests have the same security implications regardless of how you send them. A request is just a request, an ajax request isn't any different from a request from Flash, the server sees the same thing. Someone could use a tool like Fiddler to craft a request to the server with exactly the data they want to send, and the server wouldn't really be able to tell how the request got there (at least not in a way that you couldn't fake).If that's an issue, then I would recommend encrypting the request data yourself. If you did that, it wouldn't be a good idea to use Javascript because someone would be able to look at the Javascript code and see the encryption key, and then encrypt their own data the same way. Flash would be a better tool to use there, so you could create your request in Flash and encrypt it using a certain encryption algorithm and key, and then send the data to the server. PHP would decrypt the data using the same algorithm and key. So that way you can send the requests through Flash and it wouldn't matter if someone intercepted the request and looked at the data, they wouldn't be able to tell how it was encrypted to make their own request.I set up an API for one my applications to work like that, so the API receives encrypted data and decrypts it to do what it needs to do. That basically means that the only people who are authorized to send requests to the API are the people who know the encryption key. In that case, the API sends and receives an entire JSON structure that got encrypted. So to send a request someone would pack all of the data into an array, then convert the array to a JSON string, then base64-encode the JSON string, then encrypt the base64 data and send that to the server. The server decrypts the data, decodes the base64 data, decodes the JSON string back into an array, and has all of the data in the array. It sounds a little complicated, but no one would be able to send any data to the API without knowing all of that, plus the encryption key. In my case, the encryption algorithm is the Rijndael 256 cypher. PHP has the mcrypt extension to handle encryption and decryption with a variety of algorithms. This is what the start of the API code for the application looks like, where it receives and decrypts the data:

<?phprequire_once '../include/global.init.php';ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');$data = form_var('data');$response = array();$response['success'] = false;$response['data'] = '';$response['errors'] = array();if ($data == ''){  $response['errors']['reason'] = 'No data supplied';  finish();}# decrypt the data$json = trim(  mcrypt_decrypt(	MCRYPT_RIJNDAEL_256,	$config['api_enc_key'],	base64_decode($data),	MCRYPT_MODE_ECB,	mcrypt_create_iv(	  mcrypt_get_iv_size(		MCRYPT_RIJNDAEL_256,		MCRYPT_MODE_ECB	  ),	  MCRYPT_RAND	)  ));$data = json_decode($json, $assoc = true);if ($data === null || $data === false){  $response['errors']['reason'] = 'The data was not able to be decrypted';  finish();}switch ($data['action']){  case 'login':	/*	Log a user in and redirect to the main page.  This function does not return data, it redirects or quits.	data:	  username	- the username to authenticate	  password	- the plain-text password	*/	if ($sess->login(trim($data['data']['username']), sha1(trim($data['data']['password']))))	{	  $sess->redirect('../index.php');	}	else	{	  exit('Login error: ' . $sess->error . '<br><a href="../index.php">Log In</a>');	}  break;  case 'auth_user':	/*	Authenticate username and password.  Will only authenticate non-admin users.	data:	  username	- the username to authenticate	  password	- the plain-text password	return:	  success	 - boolean; true if the username and password matched, false otherwise	  id		  - int; the user ID, 0 if unsuccessful	*/	$response['data']['success'] = $sess->login(trim($data['data']['username']), sha1(trim($data['data']['password'])));	$response['data']['id'] = 0;	if ($response['data']['success'])	{	  if ($sess->any_admin)		$response['data']['success'] = false;	  else		$response['data']['id'] = $sess->userid;	  $sess->logout();	}	$response['success'] = true;  break;...function finish(){  global $response, $config;  $json = json_encode($response);  $encrypted_data = trim(	base64_encode(	  mcrypt_encrypt(		MCRYPT_RIJNDAEL_256,		$config['api_enc_key'],		$json,		MCRYPT_MODE_ECB,		mcrypt_create_iv(		  mcrypt_get_iv_size(			MCRYPT_RIJNDAEL_256,			MCRYPT_MODE_ECB		  ),		  MCRYPT_RAND		)	  )	)  );  //echo $json;  echo $encrypted_data;  exit();}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...