Jump to content


Photo

Add online highscores to your HTML5 games


  • Please log in to reply
58 replies to this topic

#1 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 29 November 2011 - 04:59 AM

Before you start: I highly recommend you use my newer tutorial for more secure highscores: http://gmc.yoyogames...howtopic=538699

Dave's HTML5 Highscore Tutorial

Due to so many people asking how it's done, I've decided to make a tutorial so you can make your own highscores in your HTML5 games!

What you need:
-GameMaker HTML5
-This resources pack (updated 4/12/11) (note that this tutorial is included in a text file in this pack)
-Half decent GML knowledge

What you might want:
-A webhost that supports PHP and MySQL (these instructions assume a PHPMyAdmin interface)
-Some PHP knowledge if you want to alter anything

If you don't have your own web host, you may run into some problems. Notably, if your high score server and your game are located at different domains, the game wont work in IE or Opera.

Steps 1 and 2 talk you through setting up your own server. If you don't have one, then let me know and I'll set you up on my database. Then skip to step 3.

Step 1 - The database

Due to each host having a different method, you'll need to create the database yourself (Table structure is included in this tutorial, just not creating the initial database). Hosts have many different ways of doing this, so I can't include all options. You might want to start somewhere near a "MySQL" button.

Once you've got your database created, you need to create the tables by importing the SQL file from the resource pack. Open PHPMyAdmin, and click "Import". Upload the file in the appropriate box and click "Go".

This will set up the tables used for storing the highscores and the game info. You need to set up a game in the gamet01 table. To do this, choose the html5_db database from the list on the left (it may have done this automatically depending on your host and PHPMyAdmin version). Next, click gamet01 in the same area.
Next, click the "Insert" tab in the right hand panel.

In the namet01 box, put the name of your game, say, "Game 1".
In the keyd01 box, put an encryption key. This needs to be exactly 15 characters long, letters only (preferably capitals), and non-repeating (you can't have the same letter occuring twice in the string).
Leave the other boxes blank (they will auto-fill) and click "Go".

That's the database set up. Next, time to work on interfacing with the database.

Step 2 - PHP scripts

In the resources pack are three PHP scripts, mysql.php (more on this later) getscores.php and submitscore.php. The latter two should be uploaded to your web server, and be sure to remember the URL as you'll need this later. mysql.php needs some changes before you upload it, as outlined below. Be sure all three scripts are in the same directory.

The mysql.php file contains the details required to connect to your MySQL server. The required details can be provided by your host. The details you need are the MySQL server location (host), your user name, and your password.

I haven't gone into details with the PHP scripts, but if you have any questions then let me know (unless your question is about the encryption, because even though I wrote it, I don't understand it, it was all done through trial and error).

Step 3 - The game

Included in the resources package is an example game, Davescores.gmz. Import this game into GMHTML5 by clicking Import GM81 on the welcome screen. Choose "*.gmz" from the dropdown box, and select the Davescores.gmz file.

I've found when I import a game the extension plays up. To make sure it works, right click on it and delete it. Then right click, add existing extension, click the Install button, then Install, then choose Davejax.gex. Click OK, choose the Davejax extension and click the arrow to add it to the project. It should work fine from there.

The GM file has a fully working example, showing how to submit scores and retrieve highscores. By default, it's set to link to my database. You can update it to work with your database by changing the following settings:

Scripts:

getscores
-change URL on line 7 to the location of your getscores.php file.

submitscores
-change key on line 3 to your encryption key
-change URL on line 7 to the location of your submitscores.php file.

Objects:

viewscores
-create event: Change first argument of getscores() function to the ID number of your game (The value of idd01 in your gamet01 table entry, probably 1 if it's your first entry)

drawsubmit
-press <enter>: Change first argument of submitscores() function to the ID number of your game


And that's it! After changing those values, test out the game and see if it loads and submits to your database OK. It may have lots of 0's or say "undefined" until you get 10 entries in the database for your game. Make sure you always submit at least 10 scores before going public with the game.

Once you've got the example working, you can work on adding it to your own game. I'll leave that one up to you.

Cross domain requests

It's important to note that some browsers do not allow cross-domain AJAX calls. What this means is that if you are in certain browsers, then it wont work unless the game is hosted on the same domain as the PHP scripts you are calling with AJAX. So if you had your game on your site, but it was sending the highscores to my database, most browsers would block this activity by default.

The reason is that if you log into a website, say facebook, then visit my malicious site, I could make an AJAX call that brings up facebook and posts a status. I wouldn't need to make my malicious site log in, because the browser already has a cookie that says I'm logged in, so facebook would let my (potentially) malicious script in to wreak havoc.

All browsers stop this activity by default, but you can send a header on the server that tells the browser to allow access. In the above example, the site sending the header would be facebook, which would say "let in requests from other domains". Obviously facebook doesn't allow this so they don't send that header, but for some sites this is needed, so they do send the header. It's possible to select which source domains are allowed to connect, too. The PHP scripts above both have this header being sent. If you will only ever have your games on the same site as the PHP files, then it may be a good idea to remove the "header(...)" line from the PHP scripts.

Anyway, these headers only work on certain browsers. In my experience, Chrome, Safari, and Firefox do work (if the header is inluded in the PHP file, all major browsers block it without the header), Opera and IE do not work with or without the header.

So if you want to avoid problems, host your PHP scripts on the same site as the game will be played on.

It's also possible to use a PHP proxy script, that tricks the browser into thinking the request came from the same domain, but that's outside the scope of this tutorial.

PLEASE NOTE: http://www.example.com and http://example.com are *different* URLs. Always be consistent in which one you use (I suggest always starting with www).

My webhost has a setting it calls hard canonical, which means that if someone tries to go to http://example.com it forces the site to switch to http://www.example.com. Your host might have it too. If it does, I'd consider switching it on.

Final words:

Hopefully this tutorial has been enough for you to set up highscores for your game. If you have any questions, let me know!

Thanks for reading!

-Dave

PS: I'm very interested in suggestion on improving the tutorial or my code (especially the encryption code). So if you have a question, suggestion or complaint then let me know!

PPS: I'm looking for some more games to add to my website http://www.html5haven.com so if you think your game is at least as good as the others on there, let me know and I'll add yours too :D.

Edited by Dangerous_Dave, 02 May 2012 - 06:24 AM.

  • 4

#2 Desert Dog

Desert Dog

    GMC Member

  • GMC Elder
  • 6409 posts
  • Version:Unknown

Posted 29 November 2011 - 05:25 AM

Sweet!

First!

Pow!

Now I can make my own highscores, and disown you! :D
  • 0
HTML5 games for mobile:
HexDogs Bugz Burn! Captain George Golfing Block Memory

Games for Androids
*NEW* Word Dog - Published by Dangerous_Dave


Code: General Array Functions - GM-S friendly. sorting, shuffling. Includes a quicksort.
Use the quicksort to sort ds_lists 10-18 times faster than ds_list_sort()!

#3 Psalms23

Psalms23

    GMC Member

  • New Member
  • 163 posts

Posted 29 November 2011 - 07:08 AM

Thanks for posting this, I was wondering how this is done. Seems a little complicated for up novices but I'm sure with a careful following of the steps we'll get it
  • 0
The LORD is my shepherd; I shall not want. He makes me lie down in green pastures.
He leads me beside still waters.
He restores my soul. He leads me in paths of righteousness
for his name’s sake. Even though I walk through the valley of the shadow of death,I will fear no evil,
for you are with me;... - Psalms 23

#4 haloflooder

haloflooder

    GMC Member

  • GMC Member
  • 35 posts

Posted 02 December 2011 - 06:17 AM

Wow, this is the best online score tutorial for HTML5 atm.

But I have a problem. phpmyadmin says this whenever I try to import the database file
Spoiler

Edited by haloflooder, 02 December 2011 - 06:18 AM.

  • 0

#5 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 02 December 2011 - 03:25 PM

Wow, this is the best online score tutorial for HTML5 atm.

But I have a problem. phpmyadmin says this whenever I try to import the database file

Spoiler


I'm getting the exact same error. I tried to change the default character set of the file(to import it) to latin1, since that is in the error message, and I get the same thing.

EDIT** I'm noticing that when I use WinRAR to extract/view the files, it appears that the "endline" character is not working right. For example the mysql.php file comes across like this if I copy/paste into this quote box.

<?
//X Y and Z are details you need to get from your host
$dbHost = "X";
$dbUser = "Y";
$dbPass = "Z";
$dbDatabase = "html5_db";
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db($dbDatabase);
?>


But if I copy/paste or just leave it alone in notepad(I'm on windows 7) the lines are all bunched together.

<?
//X Y and Z are details you need to get from your host$dbHost = "X";$dbUser = "Y";$dbPass = "Z";$dbDatabase = "html5_db";$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die "Error connecting to database."); mysql_select_db($dbDatabase);?>


It may stupidly be some issue with text editors and character sets. But I don't know enough about that to figure it out at this time.

I would have thought that using PHPMyAdmin would have it automatically connect and select the database before sending the queries to create the tables, but apparently it didn't in our case.

Assuming that the lack of endline symbols in notepad were not the issue, a solution would be to figure out why the database isn't getting selected. Using the actual intended query, we could run a PHP file(that actually selects the DB) to do it manually instead of using PHPMyAdmin, but with the endline errors, I'm not sure what I need to move where to get this to happen.

EDIT AGAIN***

I was able to get PHPMyAdmin to create the tables for me, using that same .sql file. Originally I was importing the file on the left side, and(at least in firefox) it was bringing up a little window to import the file. But, if you click on the name of the database on the left side, it brings up the structure/etc... on the right side, and then if you click on the "SQL" tab on that is on the top, and follow the same procedure(like you were trying to do in the little window) importing the file, it works, and suddenly you now have the two tables you need.

Edited by kburkhart84, 02 December 2011 - 08:03 PM.

  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#6 haloflooder

haloflooder

    GMC Member

  • GMC Member
  • 35 posts

Posted 02 December 2011 - 10:02 PM

ok, I also imported the tables into the database. I just created a database "html5db" and just import the file from there. :)
  • 0

#7 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 02 December 2011 - 10:21 PM

EDIT** I'm noticing that when I use WinRAR to extract/view the files, it appears that the "endline" character is not working right. For example the mysql.php file comes across like this if I copy/paste into this quote box.
...
It may stupidly be some issue with text editors and character sets. But I don't know enough about that to figure it out at this time.

It appears like that because it isn't using Windows line breaks. Once uploaded to your server it will be fine.

I was able to get PHPMyAdmin to create the tables for me, using that same .sql file. Originally I was importing the file on the left side, and(at least in firefox) it was bringing up a little window to import the file. But, if you click on the name of the database on the left side, it brings up the structure/etc... on the right side, and then if you click on the "SQL" tab on that is on the top, and follow the same procedure(like you were trying to do in the little window) importing the file, it works, and suddenly you now have the two tables you need.

My PHPMyAdmin doesn't appear to have a "Import" button on the left, only on the right, further along from where the "SQL" tab is.

ok, I also imported the tables into the database. I just created a database "html5db" and just import the file from there. :)

Ah, so you need to create a database before you import? I didn't realise, I'll add that into the tutorial.

Thanks for testing it out guys :)

Edit: On further testing, my SQL file is slightly wrong. I've uploaded a new version of the resources folder, so any future people wont have to create the database separately.

Edited by Dangerous_Dave, 02 December 2011 - 10:37 PM.

  • 0

#8 haloflooder

haloflooder

    GMC Member

  • GMC Member
  • 35 posts

Posted 02 December 2011 - 10:47 PM

I have a question about the GML arguments
Lets say I named the game in my database GBRA as my first game
Do I put use
getscores("GBRA", 5, true); and submitscores("GBRA", 5, true);
or
getscores(1 , 5, true); and submitscores(1 , 5, true);

EDIT*
oh, and another question.
What does the score/name look like in the highscore table?
I want to manually import a score to see if it actually works on my game

Edit**
lol... and another question
Lets say I want to load the lowest score (lets say the 100th score) without having to load the first 99 scores. How do I do that?

Edited by haloflooder, 02 December 2011 - 10:57 PM.

  • 0

#9 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 02 December 2011 - 11:18 PM

The game name in the database is not actually used, it's just there so when you open up the database, you can tell which ID is for which game. You use the idd01 of the game, the second option you listed.

The score in the database only has three important values.
gamed01 is the ID of the game. This is used so we only need one table to store highscores for an unlimited number of different games.
scored01 is the score the player got.
named01 is the player's name

The other columns either auto-fill, or are not strictly necessary.


The scores are not capped, every score is kept indefinitely. If you want to load the lowest score ever, you can change the order of the highscores. Open up the viewscores object in the example, and change the arguments of getscores. Currently it looks like this:

getscores(2, 5, true)

The first argument is the game ID, so you probably want to change this to 1 (if you followed the tutorial, you've done this). The second argument is how many scores to return. If you just want the lowest score (rather than the 5 lowest scores), put 1. The third argument is the order. true gives you the highest scores, false gives you the lowest.

It's currently not possible to retrieve just the nth score, only a certain number of the highest or lowest. If you wanted the 100th, you'd have to load 100 scores, then pick the last one.

Edited by Dangerous_Dave, 02 December 2011 - 11:56 PM.

  • 0

#10 haloflooder

haloflooder

    GMC Member

  • GMC Member
  • 35 posts

Posted 03 December 2011 - 12:29 AM

Thanks! but you didn't answer one of my question.
Q1:
Is it possible to manually put in the score via PHPmyAdmin? I would like to do this to see if my game is displaying the scores or not when I'm trying to get it to work.

Q2:
Also, in the mysql.php file. Do I just put in "$dbHost = "sub.domain.com"" or "$dbHost = "sub.domain.com:port""?

I'm having trouble getting this to work :/ whenever I push submit, it doesn't execute the codes in the script for some reason. There is nothing wrong with the GML coding either.
This is what I have in the mouse left pressed event
Spoiler

  • 0

#11 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 03 December 2011 - 12:37 AM

1: Sorry, yes it's possible, you open the table and click the "insert" button, then fill it out as I described in my previous post.

2: It varies from host to host. For example, with my host it's "username.db", some hosts it's "localhost" and in others it's a domain, sometimes with a port. The information should be somewhere in your host's control panel, probably somewhere near where you set up mysql.

Try visiting the mysql.php file in your browser (ie yourdomain.com/mysql.php ). This should show as blank (as it is just connecting, and doing nothing else). It may display an error instead.
  • 0

#12 haloflooder

haloflooder

    GMC Member

  • GMC Member
  • 35 posts

Posted 03 December 2011 - 01:09 AM

Oh, I'm blind. haha. I didn't see that in your previous post for some reason. I'm sorry. haha

Well, I'm hosting it myself and I put my ip in the subdomain and the port number is 3306. So do i use <sub>.hhcraft.info:3306 for my host? and if the php page is blank with no error, it's good to go right? and what is hitst01 used for?

Even though I put the highscore in the database manually. The scores still don't show up on my game. I don't know what's going on. I checked everything and they look fine... I'll go make my game grab some scores from your database. If that doesn't work then I guess I have to find another way to fix it.


EDIT***
I got it working!

I'm sorry that I'm asking A LOT of questions. I just really want to add a highscore feature in a game I'm developing and I'm just getting some trouble setting it up even though I use mySQL and GM everyday :/

Thank you very much for helping me! You have shown me the light.

Edited by haloflooder, 03 December 2011 - 02:22 AM.

  • 0

#13 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 03 December 2011 - 02:34 AM


EDIT** I'm noticing that when I use WinRAR to extract/view the files, it appears that the "endline" character is not working right. For example the mysql.php file comes across like this if I copy/paste into this quote box.
...
It may stupidly be some issue with text editors and character sets. But I don't know enough about that to figure it out at this time.

It appears like that because it isn't using Windows line breaks. Once uploaded to your server it will be fine.

I was able to get PHPMyAdmin to create the tables for me, using that same .sql file. Originally I was importing the file on the left side, and(at least in firefox) it was bringing up a little window to import the file. But, if you click on the name of the database on the left side, it brings up the structure/etc... on the right side, and then if you click on the "SQL" tab on that is on the top, and follow the same procedure(like you were trying to do in the little window) importing the file, it works, and suddenly you now have the two tables you need.

My PHPMyAdmin doesn't appear to have a "Import" button on the left, only on the right, further along from where the "SQL" tab is.

ok, I also imported the tables into the database. I just created a database "html5db" and just import the file from there. :)

Ah, so you need to create a database before you import? I didn't realise, I'll add that into the tutorial.

Thanks for testing it out guys :)

Edit: On further testing, my SQL file is slightly wrong. I've uploaded a new version of the resources folder, so any future people wont have to create the database separately.


As far as an import button on the left, there isn't one directly. Right next to the little "house" button, there is an SQL button, which opens up a little window, which then has the import option which tries to execute the SQL query without having the database selected first. But if I do it on the right side, it works fine.

About the line breaks, I figured that was the case. One, I remember the original kit you offered to me through PM had been zipped on MAC OS, and two, when I copy/pasted it to put it on the forum here in a quote, the line endings worked, though they didn't if I pasted to another text document.

About the need to create the database...I understand that php can do that through code, but it seems that I read somewhere that some hosts don't let that happen. For example 1and1 is supposed to be bad about that, and I happen to have 1and1 for my webhost, though it hasn't given any problems yet. Through the 1and1 administration site, I can access PHPMyAdmin through the MySQL database admin section. But, the button is only there if you actually have created a database. In their FAQs they mention a procedure to manually install PHPMyAdmin, but it seemed easier to me just to create the database through 1and1(though they gave it some crap name like st0000000) and then just use that. So, you may want to explain those details if/when you add the database creation to the tutorial because I'm sure other webhosts have some of the same issues.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#14 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 03 December 2011 - 06:44 AM

I've updated the SQL file so that when you import it via PHP, it automatically creates the database. This doesn't happen through PHP. If you haven't got it working yet, download the updated resources folder then follow the tutorial, it should work from there.
  • 0

#15 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 03 December 2011 - 08:12 PM

I've updated the SQL file so that when you import it via PHP, it automatically creates the database. This doesn't happen through PHP. If you haven't got it working yet, download the updated resources folder then follow the tutorial, it should work from there.


I don't think it would work, in my case at least, because I can't access MyPHPAdmin without having actually created a database first. That's why I mention maybe if you update the tutorial you might want to mention that because 1and1 probably isn't the only host like that.

At the least, for personal use, I'm thinking of taking the gml functions for this and sticking them into an extension, and simply adding the encryption key as an argument. I doubt you'd have a problem with me doing it for personal use, but would you mind if I released it here. I'd be glad to give it to you so you could keep all of it together, or I wouldn't mind putting it up myself either way. Of if you'd rather me not, I'll just keep it for personal usage. What do you think??
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#16 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 04 December 2011 - 01:55 AM

I don't think it would work, in my case at least, because I can't access MyPHPAdmin without having actually created a database first. That's why I mention maybe if you update the tutorial you might want to mention that because 1and1 probably isn't the only host like that.

Ah, I see. Do you know what happens if you try to load the SQL file I uploaded? Does it create a second database? Does it fail with an error?

At the least, for personal use, I'm thinking of taking the gml functions for this and sticking them into an extension, and simply adding the encryption key as an argument. I doubt you'd have a problem with me doing it for personal use, but would you mind if I released it here. I'd be glad to give it to you so you could keep all of it together, or I wouldn't mind putting it up myself either way. Of if you'd rather me not, I'll just keep it for personal usage. What do you think??

Go nuts, mate. You can change, improve and even deprove if you want. Release away.
  • 0

#17 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 04 December 2011 - 03:56 AM

I get this error.

#1044 - Access denied for user 'dbo393690342'@'%' to database 'html5_db'


To even try this, I created a "test" database. Then I opened up PHPMyAdmin with that database. Then I tried to import that SQL file, and it spit that error. Remember I can't open PHPMyAdmin without having created a database through 1and1's administration site, so the error pretty much makes sense.

About my creating an extension out of the gml functions, I think I'll probably just leave it alone as is. I'll likely save out the functions with my changes(dbname, encryption key, url, etc...) so I don't have to import and change that everytime I create a new game. Plus most games won't be using the objects that you already have set up in your test project, rather they will only need the gml scripts. Thanks for the permission though.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#18 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 04 December 2011 - 04:11 AM

Thanks for testing that, I think it might be better to change the tutorial to include creating the database, then remove that part from the SQL file. That way it covers both situations.
  • 0

#19 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 04 December 2011 - 04:15 AM

Thanks for testing that, I think it might be better to change the tutorial to include creating the database, then remove that part from the SQL file. That way it covers both situations.


That makes sense...but I think if you explain it, you might be fine having both versions. That way, the ones who can do it the easy way(like you originally intended) can do it. And the ones who can't can do it the "harder" way. You might even create a list of confirmed hosts that have to be the harder way, with 1and1 being the first.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#20 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 04 December 2011 - 05:19 AM

To be honest, I have no idea how common it is that you can just load the SQL How I exported (obviously it works with my host, but I don't know about others). I think getting people to setup their own database, and just having the tables created via import is a better option. I used a few free hosts a few years ago, and I seem to remember them each having a different way to create a database.

I think I'll just get people to set up their own database, and then they can load the SQL from there. It seems like the best option to incorporate all hosts.
  • 0

#21 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 04 December 2011 - 06:31 AM

I see the point. Indeed there may be too many different things.

One thing I'd recommend is maybe having that SQL query(for creating the tables) be also in the form of a php script executing the query. The reason is because some hosts may not even have PHPMyAdmin, and/or it may actually be easier to upload a php script to the server and navigate a browser to it.

The other problem I see is that some hosts may not have an SQL Database Administration tool, rather may be set up to only allow modification through actual code directly, including even DB creation. Hence my above suggestion may be even more necessary. The issue is that there are too many different possibly situations and hosts, and even different hosting contracts within the same host, and so it is difficult for you(as the person providing the tutorial/kit) to accomodate all of that.

Thanks again for the help. I've never learned "internet programming" having only worked with traditional programming from QBasic to C++, and now GM. I understand some concepts of how internet programming works, but I'm not capable at this time of doing what you have done here(as simple as it actually is) so I appreciate it.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#22 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 04 December 2011 - 08:31 AM

Running the query through PHP is as simple as putting it in a mysql_query("query here"); function. I feel like the situations in which the tutorial doesn't fit are so few, that I'd rather have people ask questions for me to answer than make the tutorial more complicated.

It's good to see you get help out of it, if you have any more questions, be sure to ask :)
  • 0

#23 haloflooder

haloflooder

    GMC Member

  • GMC Member
  • 35 posts

Posted 06 December 2011 - 12:43 AM

Sorry about bothering you again but I have one more question.

Is it possible to pull out dates? So I can do Top Monthly score, Top Daily Score, etc
  • 0

#24 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 06 December 2011 - 02:46 AM

Sorry about bothering you again but I have one more question.

Is it possible to pull out dates? So I can do Top Monthly score, Top Daily Score, etc


This tutorial doesn't have anything directly on it. I don't know enough to explain how, but I'm sure if you study up on some SQL, specifically how queries work, you could make a php script that does it. Then you could use the same function that is in the ajax extension(part of the tutorial) to call that php script.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#25 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 06 December 2011 - 06:19 AM

Is it possible to pull out dates? So I can do Top Monthly score, Top Daily Score, etc

Not by default. If you want to do that, you'll need to add an extra mysql query (or a new PHP script to pull the value).

The SQL would look something like this:
$result = mysql_query('select * from highscorest01 WHERE gamed01=1 and timestampd01 >= DATE_ADD(CURDATE(), INTERVAL -1 MONTH) order by scored01 desc limit 1');

That will grab the top score over the past month.
  • 1

#26 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 07 December 2011 - 12:31 AM

You might want to know...my two HTML5 games on www.firehammergames.com are now high score enabled. The system so far works great, and I will be using it for all of my HTML5 games coming up, unless GM suddenly gets some internal capability rivaling the ease of use of this one.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#27 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 07 December 2011 - 03:22 AM

On Hungry (whose page title says "Interstellar cleanup") it's got a top highscore thing when if first loads, but it doesn't load the same scores as when you submit. Other than that, it works good :)
  • 0

#28 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 07 December 2011 - 03:37 AM

On Hungry (whose page title says "Interstellar cleanup") it's got a top highscore thing when if first loads, but it doesn't load the same scores as when you submit. Other than that, it works good :)


About the title, ouchie, I copied the page so it would be easier to get the HTML5 working with my editor, and forgot to change the name.

About the scores, that is kind of remnants of what was done before I actually added your high score system. It would show the highest score in a players "session" but had no connection with the high score database because it didn't exist at the time. Normally you should be taken there after seeing the high score table, but since the objects detect keys being down it ends up "skipping" that room and going back to the game again. I will fix this, but I was in a hurry to get the games working with the high score database since I had a little time today. That is kind of what happens when I do high scores as an "add-on" after having made the game. At least in this case it was only two games. When I rework them, I will have a better title screen, and have where you actually click a button so from the title screen you can see the high score table. I think I would also make it not force you to input a name, and instead only ask for it if you score above the 10th spot.

Did you notice that I included a backspace event, so you could go fix it if you made a mistake typing. I'm using the same "quick" solution to grab keyboard input as you used in your tutorial of this topic. But it has some problems, for example didn't originally include the backspace thing. It also seems like only letters work right, but I haven't fully tested it.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#29 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 07 December 2011 - 03:50 AM

The example file was actually created really early on, when GMHTML5 didn't support keyboard_string. When I make games, I make the interface separately, then just copy the couple of scripts over, so I forgot it was using the workaround way to accept keyboard typing.

When I wrote a highscore system while porting a friend's game to Mac, I actually had the PHP script return your place in the highscores (whether it be 3rd or 300th). I didn't do it for my HTML5 one because I had it return error numbers instead while I was trying to get it going. It's really just a small tweak in one of the PHP scripts to have it return your place, so I might do that at some stage.
  • 0

#30 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 07 December 2011 - 04:09 AM

The example file was actually created really early on, when GMHTML5 didn't support keyboard_string. When I make games, I make the interface separately, then just copy the couple of scripts over, so I forgot it was using the workaround way to accept keyboard typing.

When I wrote a highscore system while porting a friend's game to Mac, I actually had the PHP script return your place in the highscores (whether it be 3rd or 300th). I didn't do it for my HTML5 one because I had it return error numbers instead while I was trying to get it going. It's really just a small tweak in one of the PHP scripts to have it return your place, so I might do that at some stage.


Sounds like a winner. I was actually considering a few extra things myself. For example, I think I should modify/recreate the submission script so that the score is provided in an argument instead of directly coded as "score." Some people don't use that built-in variable, and would have to modify the script to do it. Also, the PHP side could use a method to return a given span of scores(say from 10 to 20th), and/or things like dates/times(like the other poster mentioned). The only thing that gets me is I'm wondering if/when Yoyo implements high scores into GM internally, how would they work, and what capability would they include. It just feels like we may be inventing the wheel they already have in the works. Who knows??
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#31 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 07 December 2011 - 04:32 AM

Some browsers just down support cross domain requests, so as much as YoYo may want to have a database set up for everyone to use, it's just not going to work. I doubt they are working on HTML5 online highscores, and I would rather their efforts were placed elsewhere.

As for the PHP scripts, yes, there's heaps of additional things you could get it to do, this tutorial is just to get you started ;)
  • 0

#32 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 07 December 2011 - 02:52 PM

Some browsers just down support cross domain requests, so as much as YoYo may want to have a database set up for everyone to use, it's just not going to work. I doubt they are working on HTML5 online highscores, and I would rather their efforts were placed elsewhere.

As for the PHP scripts, yes, there's heaps of additional things you could get it to do, this tutorial is just to get you started ;)


Actually, I was figuring Yoyo could use SQLite if they wanted to implement the thing. It wouldn't work cross-domain either most likely, but it doesn't require a database, rather creates a file in the server space, so it would make sense for them to do it that way. I agree that there are better things they can and should be doing though.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#33 True Valhalla

True Valhalla

    ಠ_ಠ

  • GMC Member
  • 5277 posts
  • Version:Unknown

Posted 11 December 2011 - 01:41 AM

I've never really understood the appeal of stand-alone high score systems like this, because they are too vulnerable to exploitation. Are there any counter-measures which stop clients sending fake scores..? Is that even possible without server side verification of their game session along their way?

I'd love to use this, but a flaw like that can make high score systems pointless.
  • 0

book_forum.png


#34 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 11 December 2011 - 02:33 AM

The highscore itself is encrypted to some extent. The name is sent in plain text. To be able to submit a score, the user needs to know the security key, which GM does a good job of hiding with it's obfuscation.

One thing that could be added is a system to check that the score hasn't already been submitted, which is kinda half set up. Currently the system stores the code used for submission, but something would need to be added to stop the same score having the same code. I'd probably add the linux time stamp (or some other date) to the score, so you would submit string(score)+"|"+timestamp. The system already has the ability to include the | in the string being encrypted. You would then take the output and split off the score in the PHP script, and also check that the same code hasn't already been submitted.

That would stop the same code being used to submit the same score multiple times, but not stop the same score being submitted twice if it were earned. It would also make the encryption slightly harder to hack, as it introduces something which the player has no knowledge of it's value.

That's about as good as I know, but I'm not sure if much else can be done without moving everything client side (which would probably be too slow for an HTML5 game).

Personally, I haven't had any trouble with hackers (that I know of :whistle:). I'd look into further options if it became an issue.

In reality, it's risk free. If people hack it, I'll delete the cheat scores. If lots of people hack it, I'll just remove the online high scores. But why not have online high scores if it encourages more people to play? There's no money at stake, the most I have to lose is the online highscores, which I otherwise wouldn't have added. I really don't understand why you would shy away from them simply because of the potential actions of a few?
  • 0

#35 True Valhalla

True Valhalla

    ಠ_ಠ

  • GMC Member
  • 5277 posts
  • Version:Unknown

Posted 11 December 2011 - 02:51 AM

I really don't understand why you would shy away from them simply because of the potential actions of a few?


They're the ones that you need to watch out for though ;) Of course, for high scores it's not really a big issue.
  • 0

book_forum.png


#36 metaaldetectie

metaaldetectie

    GMC Member

  • New Member
  • 4 posts

Posted 01 January 2012 - 02:14 PM

The high score works fine only I find it annoying that if you don't fill in a name you have an empty field.

Is there a possibility with gml or php to require filling out a name, so you have to fill in something.
  • 0

#37 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 01 January 2012 - 06:09 PM

The high score works fine only I find it annoying that if you don't fill in a name you have an empty field.

Is there a possibility with gml or php to require filling out a name, so you have to fill in something.


Why don't you do it the easy way? In the script where you are receiving the name, when enter is pressed, make sure it is not empty. If it is, either do nothing(expecting a name), display a message that they need to type a name, or simply put a substitute. You do this in the gml, not in the PHP, simply because it is easier than modifying the PHP, unless you know what you are doing.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#38 metaaldetectie

metaaldetectie

    GMC Member

  • New Member
  • 4 posts

Posted 01 January 2012 - 08:24 PM

when enter is pressed, make sure it is not empty. If it is, either do nothing(expecting a name), display a message that they need to type a name, or simply put a substitute.


Can you give me an example in gml?
  • 0

#39 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 01 January 2012 - 11:03 PM


if (global.name != "")

{

  submit();

}

else

{

  show_message("Please enter your name");

}


  • 0

#40 mikebio

mikebio

    GMC Member

  • New Member
  • 18 posts
  • Version:GM:HTML5

Posted 28 January 2012 - 08:36 PM

This all seems a bit too complicated. I just purchased this with hopes that the game I am trying to make would be less complicated but now I am not too sure.

I want a homescreen that the user can enter his/her name and then hit go. If the name exists in a database, then it will pull up the users previous score. If not, then it will create the user with whatever name was entered.

This is being done as a closed study, and that is why I do not have a password.

Can you help with this Dave? You seem to know what you are talking about! I have the DB and table created (with 2 columns: name, score). I am just confused on the whole GM extensions and scripts and like I said, it all seems too complicated. I read your code but just couldn't understand a lot of it (such as strip_tags(mysql_real_escape_string))

Hope you can help,

Thanks,
  • 0

#41 kburkhart84

kburkhart84

    Firehammer Games

  • GMC Member
  • 1980 posts
  • Version:GM:Studio

Posted 28 January 2012 - 09:18 PM

I'm not Dave :), but I can tell you that this extension was not made for what it looks like you are looking for.

My recommendation would be to learn a little PHP and MySQL. I don't know enough myself, mainly because I don't really need to learn any, but I DO know that what you want isn't that complicated once you have the basics of PHP and MySQL down.

Visit w3schools.com for a bunch of tutorials on HTML, PHP, MySQL, and a few other things.
  • 0

My KBInput system is now on the marketplace here.  It wraps up nice and tight GMStudio's input system into a few function calls making a user configurable input system that works the same regardless of what inputs the player has chosen including keyboard, mouse buttons, and gamepad/joysticks using DInput/XInput.  The support forum topic for it is here.


#42 mikebio

mikebio

    GMC Member

  • New Member
  • 18 posts
  • Version:GM:HTML5

Posted 28 January 2012 - 10:58 PM

I'm not Dave :), but I can tell you that this extension was not made for what it looks like you are looking for.

My recommendation would be to learn a little PHP and MySQL. I don't know enough myself, mainly because I don't really need to learn any, but I DO know that what you want isn't that complicated once you have the basics of PHP and MySQL down.

Visit w3schools.com for a bunch of tutorials on HTML, PHP, MySQL, and a few other things.


Hey thanks for the reply! I do know PHP and MySQL quite well... but the problem is I am new to Game Maker. I just can't seem to get everything talking to each other! Actually, I have to go read some GM tutorials because I don't even know how to get text from a user! I have the "Please Enter Your Name" done but that's about it!
  • 0

#43 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 29 January 2012 - 01:21 AM

The download contains a "Davejax" extension, this can be used to make the AJAX call from GM to your PHP script. You say you know PHP/MySQL quite well, have you set up scripts to retrieve and send the name and score and have it do what you want? If you have that side sorted, the GM side is not hard to set up.

To get text from a user, use the variable "keyboard_string". When the player types something, whatever they type is stored in this variable. Use draw_text to draw it's value on the screen so the player can see what they are typing.

mysql_real_escape_string() is a sanitising function to stop hacking via SQL injection. It just adds escape characters before things like ' and " so if the player tries to trick your query into doing something it shouldn't then this helps to stop it.

string_tags() stops players from entering HTML code as a name, causing the page to display differently, or injecting javascript, etc.
  • 0

#44 mikebio

mikebio

    GMC Member

  • New Member
  • 18 posts
  • Version:GM:HTML5

Posted 29 January 2012 - 06:18 PM

The download contains a "Davejax" extension, this can be used to make the AJAX call from GM to your PHP script. You say you know PHP/MySQL quite well, have you set up scripts to retrieve and send the name and score and have it do what you want? If you have that side sorted, the GM side is not hard to set up.

To get text from a user, use the variable "keyboard_string". When the player types something, whatever they type is stored in this variable. Use draw_text to draw it's value on the screen so the player can see what they are typing.

mysql_real_escape_string() is a sanitising function to stop hacking via SQL injection. It just adds escape characters before things like ' and " so if the player tries to trick your query into doing something it shouldn't then this helps to stop it.

string_tags() stops players from entering HTML code as a name, causing the page to display differently, or injecting javascript, etc.


Dave do you think we can exchange email address and get this sorted out? I have tried following this tutorial as well (http://gmc.yoyogames...howtopic=520198) but still no luck. I don't need anything too complicated (as described) but I do need this for my PhD research.

I have the main page setup so that a user can enter a name and it is saved as a global variable called name (global.name). I have the database with the table called game, 2 columns (name, qc).

Basically once the user enters his/her name, I just want to get the qc value from that user name. If the name doesn't exist, then insert the name into the table. It is not that complicated, but I can't solve this with GM.

I am confused on what I need. I have a php file called dbcon.php that has
<?
   // database variables
   $dbHost = "localhost";
   $dbName = "<name here>";
   $dbUser = "<user here>";
   $dbPass = "<password here>";

   // database connection functions
   function dbConnect() {
      global $dbCon, $dbHost, $dbName, $dbUser, $dbPass;
      $dbCon = mysql_connect($dbHost,$dbUser,$dbPass);

      return mysql_select_db($dbName);
   }
   
   function dbClose() {
      global $dbCon, $dbHost, $dbName, $dbUser, $dbPass;
      mysql_close($dbCon);
      unset($dbCon);
   }
?>

What should my javascript file look like? Honestly, at this point I'll give anyone access to my DB and everything just to get this working. Thanks,

Mike
  • 0

#45 holgersson1988

holgersson1988

    GMC Member

  • GMC Member
  • 155 posts
  • Version:GM:Studio

Posted 22 March 2012 - 09:33 AM

This was really helpful. Is it possible to make it work from the Windows runner? Still using GM:HTML5 but running in windows mode.

Thanks for the post!

/Holgersson Entertainment¨
@HolgEntertain
  • 0
Holgersson Entertainment's projects!
Fortify: Special Edition 100% done - (2013)
Turtle Termination 100% done - (GMC Jam #8 2012)
Terraform 100% done - (2012)
Off piste 100% done - (2010)
Fortify 100% done - (2010)

#46 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 22 March 2012 - 07:13 PM

If you can use DLLs in GMHTML5 (haven't tried) then use 39DLL and check out the HTTP tutorial.

It's definitely doable in GM8. 1.
  • 0

#47 GerardoLegend

GerardoLegend

    GMC Member

  • GMC Member
  • 9 posts
  • Version:GM:Studio

Posted 26 March 2012 - 12:29 PM

Hi Dave!

I don't want to be of any trouble but I followed step by step your tutorial and I hit a dead end. Just to inform you before - I also have 1and1 hosting, so things are a tad bit tricky.

My issue is after I plug my ip address in with the scripts, in which are easy to follow, I'm thinking it would work out but the highscores aren't showing. Everything is in the same domain, yet I wonder if I did something wrong? The php files are in their own directory withing the same domain. As for my html5 game, even though the game is in the same domain with the scripts in its own directory, the high scores don't draw.

I did check to see if the tables had any data submitted by my test, and it looks like it does but somehow the game doesn't draw it.

again, I followed your tutorial step by step. the database is there, the tables are there, the key is there, the php with my db info is there. . . ugh.

here is your example live on my domain: My link (which is in the same directory of the php files)

I just get nothing. :confused:
  • 0

#48 GerardoLegend

GerardoLegend

    GMC Member

  • GMC Member
  • 9 posts
  • Version:GM:Studio

Posted 26 March 2012 - 09:13 PM

Also, When I tried to clear the php header, I didn't know exactly what should I replace it with.

I'm confused if I should replace it with header("Content-type: text/plain"); or header("location: www.darkzeta.com");

I've never programmed in PHP, and I don't know what is what even if I read about the basics, I need examples to see what does what. I see that headers have lots of definitions. which one is the right one for your highscore example if I need to replace the header with something else?
  • 0

#49 GerardoLegend

GerardoLegend

    GMC Member

  • GMC Member
  • 9 posts
  • Version:GM:Studio

Posted 27 March 2012 - 02:34 AM

update---

Well I finally have the database being updated by the html5/ajax/php combo! But I still don't see the results in my game.

I went in to redo step by step of the tutorial. What I messed up in the submit part is the gamed01. I got confused with with it, but now I understand it better.

update02-----

FINALLY I got it going ::lmao:: . damn. I derp really bad. Like super retarded derp that lasted 48 hours. ugh. It was a god damn typo on the address part of the getscores(); script. highscore isn't plural. lol

but I learned a valuable lesson doing this with php and game maker.

Thank you dave for the tutorial and ignoring me. lol Be on the look out for your name on the credits page/NES manual/ingame.
  • 0

#50 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9421 posts
  • Version:Unknown

Posted 27 March 2012 - 03:50 AM

Haha you posted at 1:30AM my time, then I was at work all day, and have only just got home! :P

Great to see you got it working, I probably shouldn't mention that if you want more secure high scores, you can use the Playtomic API with a wrapper extension... :whistle:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users