Jump to content

Best way to implement a high score list from a database


Brendan
 Share

Recommended Posts

Hi,

 

What is the best way to implement a high score system in Phaser where the scores are stored in a mySQL database? Is my current approach the correct way?

 

I am using a XMLHttpRequest() to request a PHP page on my server. this connects to and returns the scores from the DB see php file on server below:

<?php	include("dblogin.php"); //connects to my db	$sql="SELECT * FROM scores";	$result=mysql_query($sql);	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {	echo $row["name"]." ".$row["score"];	echo '\n ';	}?>

i then use XMLHttpRequest.responseText to store the score info in a local variable on the client side see below:

//xhr is my XMLHttpRequest objectxhr.onreadystatechange = function () {text = xhr.responseText;scoreText.setText(text); //scoreText is my phaser text objectconsole.log(text);};

This kind of works and console output is as expected based on the contents of my DB (Player1 50\n Player2 5\n Player3 15\n Player4 20\n ) but when i update the text in game using setText( ) all of the text is being displayed on a single line (and the \n is being ignored and appearing in the text). please can someone advise on what i am doing wrong and whether this is the best approach.

 

cheers

Brendan

Link to comment
Share on other sites

\n is not considered as newline in HTML.

 

this will replace \n with <br /> witch is the right way to make new lines

scoreText.setText( text.replace(/\n/g, '<br />') );

that being said, a better approach would be to return a JSON string from your server so you can manipulate it easily on the client side.

Link to comment
Share on other sites

Thanks for the reply, i was using \n to set a new line in the string parameter of phasers .setText( ) function. Thanks for the tip on returning a JSON string, i was wondering what was the best format to send my data from PHP to the client side, i have now implemented this :-)

Link to comment
Share on other sites

The format doesn't really matter but JSON is state of the art. By using it you will get an understanding for it and you can read in other projects too.

By the way, you're php code doesn't look very experienced (sorry, no offend). If you encounter any other problems with your current implementation, maybe your whole highscore problem is solved by using an external highscore service provider ? I never used one but it is advertised as plug & play. I know this for example: https://clay.io/docs/leaderboards

 

Good luck!

Link to comment
Share on other sites

  • 2 years later...
On 2/17/2014 at 6:34 PM, Brendan said:

Hi,

 

What is the best way to implement a high score system in Phaser where the scores are stored in a mySQL database? Is my current approach the correct way?

 

I am using a XMLHttpRequest() to request a PHP page on my server. this connects to and returns the scores from the DB see php file on server below:


<?php	include("dblogin.php"); //connects to my db	$sql="SELECT * FROM scores";	$result=mysql_query($sql);	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {	echo $row["name"]." ".$row["score"];	echo '\n ';	}?>

i then use XMLHttpRequest.responseText to store the score info in a local variable on the client side see below:


//xhr is my XMLHttpRequest objectxhr.onreadystatechange = function () {text = xhr.responseText;scoreText.setText(text); //scoreText is my phaser text objectconsole.log(text);};

This kind of works and console output is as expected based on the contents of my DB (Player1 50\n Player2 5\n Player3 15\n Player4 20\n ) but when i update the text in game using setText( ) all of the text is being displayed on a single line (and the \n is being ignored and appearing in the text). please can someone advise on what i am doing wrong and whether this is the best approach.

 

cheers

Brendan

Sir can you show the full source code regarding the storage of high score in a database? thank you!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...