0

im working on a single player game in java and want to add a high scores table. what im thinking is when a player finishes a session my script will contact my server like this "mywebsite.com/sethighscore.php?secretkey=23234234&user=bob&itemscollected=45&timeplayed=1hour" <-- this would add a new user to database

i have very little knowledge of php and mysql so anything helps

the secretkey will make it so players cant cheat- there is only one secret key
"user" is the players name
itemscollected is the number of items that player collected that round
timeplayed will be calculated with java's timer
.
.
the script should also know if a user is already in the mysql database and not overwrite it, unless its to update a score.
.
.
the items collected wouldn't be reset. so if bob collected 45 in game session 1 itemscollected=45
but then in game session 2 bob collected 30 then itemscollected=75
.
.
.
then i want to make it so that i can get a players info like this:
"mywebsite.com/gethighscore.php?user=bob&info=itemscollected"
this would display bob's collected items "45"
or
"mywebsite.com/gethighscore.php?user=bob&info=timeplayed"
this would display"1hour"
or
"mywebsite.com/gethighscore.php?user=bob&info=name"
this would display bob's name "bob"
or
"mywebsite.com/gethighscore.php?user=bob&info=all"
this would display "Items collected:45 Time Played:1 hour"

each players info would be stored in a mysql database.

then if the url = "mywebsite.com/gethighscore.php?stat=collected"
then it would display all the users items collected combined
or
if the url = "mywebsite.com/gethighscore.php?stat=time"
then it would display all the users playing time combined

thank you

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by veedeoo
0

Hi,
!WARNING! EXAMPLE script below, does not have any security protection of any kind. Please read or search more about security measures in PHP. Most importantly, if mysql is involved. DO NOT USE example below in production server.

I consider myself as a newbie in coding java . The last time I wrote an application using this language was more than one and half years ago. It was about donwloading youtube videos using java, because there is no way it can be done in php alone. So, I am pretty rusty. However, I can probably help you with the php .

So, I am in the assumption here that you are extremely familiar on how to instantiate your java class within the php script, and your server is also equiped with the PHP/Java bridge or whatever dll files are needed. I am not sure about this though, because of the reasons I already stated above.

Assuming that we have this as given,

mywebsite.com/sethighscore.php?secretkey=23234234&user=bob&itemscollected=45&timeplayed=1hour

then our php code to grab those pertinent information can be as easy as

## let's start as session for the time reference
session_start();

## let us assume that in the beginning of the play, the time is when the user landed on the play page, or for whatever method we can derive from the java application.

## the time of the page visit is held constant in the session
$_SESSION['begin'] = time();



if((isset($_GET) && ((!empty($_GET['user']) && (!empty($_GET['itemscollected'])){

## we know the secretkey is wrapped within the session 

$secretkey = $_GET['secretkey'];
$user = $_GET['user'];
$itemCollected = $_GET['itemscollected'];

## make sure that the session begin is set
if(isset($_SESSION['begin'])){

$timeBegin = $_SESSION['begin'];
}
else{
$timeBegin = time();

}
## put your database connection below

## after establishing a database connection, do your INSERT function to mysql
## sample insert query in mysql
mysql_query("INSERT INTO player (user, time, items) VALUES('".$user."', '".$timeBegin."','".$itemsCollected."' ) ") or die(mysql_error());  


}

else{

## do something if validation above fails

}

The above sample script is the minified example of mysql insert function. For your other question. In order to be able to the data needed for this,

mywebsite.com/gethighscore.php?user=bob&info=itemscollected

you need to get those info. from the database. Once again, your script can be something like this.

## put your database connection credentials here
## WARNING! you must invent your own variable naming. DO NOT follow the most common method as shown below.
$dbHost = "localhost"; 
$dbUser = "someUser"; 
$dbPass = "somePassword"; 
$dbName = "player";

## we connect to our database using the credentials above
mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error());
echo "Connected to MySQL<br />";

## we select our database named player
mysql_select_db($dbName) or die(mysql_error());
echo "Connected to Database";

## we attempt to check if our user "bob" ixist in our database user

## WARNING! this is a very simple query, there are other effecient ways of doing this. Just for the sake of clarification, I must use this method.

$result = mysql_query("SELECT * FROM player WHERE user = '".$user."' ") or die(mysql_error());  

$row = mysql_fetch_array( $result );

## we use the result anyway you want it.

$url = "mywebsite.com/gethighscore.php?user=".$row['user']."&info=".$row['items'];

## show the length of time played using the current time as comparator.

$timePlayed = (time() - $row['time']);

## you must convert the $timePlayed into human readable time from unix timestamp

That's pretty much it.... good luck

Edited by veedeoo: more info. added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.