Advertisement

Internet Highscore - how to?

Started by June 17, 2004 09:51 AM
15 comments, last by FireNet 20 years, 7 months ago
Has anyone of you implemented an internet highscore feature? I would like to know which approaches are feasible. I'm currently working on a C++/OpenGL game and looking forward to store the highscores within a MySql-Database behind a PHP-driven web-page. I want the game to be able to send the highscores automatically to the PHP script which will insert them into the database if a connection exists. Otherwise the game could create a specifically encrypted file the user has to upload manually on the web-page. What's your opinion about these two approaches? Concerning the first one: how will I be able to send the highscores to my PHP-script using C++? Thanks for advice! Greetings, STORM! [Edited by - STORM76 on June 17, 2004 10:59:35 AM]
Greetings,STORM!
This is quite easy to do! (Not as an insult, but for the encouragement)

If you have a PHP script to read the high scores on a webpage, and that script has access to a database, like MySQL, you can simply connect to the MySQL database server directly (like your script does). This is, however, not very secure.

To be more secure, you most likely have to create a custom server of some form, which will be able to write to your database, and reject false high scores, invalid data, and avoid corruption. I dont know if you can create a server in PHP to do this, but relying on a PHP/CGI script, AFAIK, would requite to submit the data over HTTP, which requires to connect to a web server first, etc.

It would probably be easier (and faster) to have a C++ program that acts as a server to receive high scores and maintain your database. I believe there are libraries to connect to MySQL, so you wouldnt have to code that. Your server could run on a specific port and accept TCP connections from clients. This is, of course, assuming you can run this on a server (you may not want to run a server outside of your web host).

Personally, I wouldnt use a database. I would simply code a C++ program that receives connection on a specific port, verifies the high scores in some way, and write them to a simple, custom data base system. The system would then write the scores to a file, and every 10 minutes, it would generate a HTML file containing the top scores, which would be visible from the web. I can afford to do this because I run my own web server and I can run the programs I want on it.

Looking for a serious game project?
www.xgameproject.com
Advertisement
Thanks for your answer!

My environment is as follows: my provider has the MySQL database running behind a firewall so that I only can access it via PHP. I also do not have the possiblity to install a custom C++ programm to act as a server listening on a specific TCP port for receiving the highscores. Therefore the only way to go for me is to build a PHP script inserting the highscores into MySQL.

The question I have is how do I execute a certain PHP script out of my C++ game over the internet? Is there a way to set up a HTTP request (like http://www.mygame.com/insert_highscore.php?player=STORM&value=1000000) without making use of the MFC (if possible)? Or are there other ways to make this work?

Greetings,
STORM!

Greetings,STORM!
A HTTP request is simply a socket connected to port 80 on a web server. You'd go through your normal steps to open up the port 80 socket and send some data down it. What you will need to do though is observe how the HTTP portocol itself works. What I htink you'll need to do is have a PHP page that handles HTTP POST events - this would be exactly the same as a page that you direct your form data to.

In case you didn't know, data is sent to to a HTTP server using POST and GET messages. GET is sent as a string in the page request (eg: www.mygame.com/page.php?id=50&score=100) but personally, I find this method to be easy to abuse.

So that leaves us with POST, as I said before, this is the same mechanism used when sending form data. Now, if you look over on Code Project, they have a short tutorial in using the a custom wrapper for these methods. Failing that, you could look up the RFC for HTML (RFC RFC1945) and learn how to use the raw socket version of the protocol. For your purposes though, you'd probably be best with this wrapper.

Hope this helps?
Great, thanks! I will take a look at the codeproject wrapper.

Greetings,
STORM
Greetings,STORM!
evolutional is quite right.

I will add a little more and state the steps.

1.You open a connection to the server on port 80 (like a browser)
2.You pass the http protocol commands (like get [here will be the page address without any server ip/web address]/http 1.1/)
3.The server will recive this info an pass an html page to you as if you were a browser.

What you do with the html page you recive is yours to decide.You can even show it to the user.So basiaclly you program can do virtually anything a browser can do over the internet.

Get some info on the http protocol and look at the source for some simple browsers and expiriment
______________________________________________________________________________________________________
[AirBash.com]
Advertisement
Quote:
Original post by FireNet
What you do with the html page you recive is yours to decide.You can even show it to the user.So basiaclly you program can do virtually anything a browser can do over the internet.




Whilst this is completely true, I think for your hiscore program you probably won't need to respond to the form POST (eg: send anything back from the Web server)

I don't know PHP but in ASP, a simple hiscore.asp script is easy to knock up:

<%  sPlayerName = Request.Form("gpPlayerName")  sPlayerScore = Request.Form("gpPlayerScore")  set ado = Server.CreateObject("ADODB.Connection")  ado.open "my db connection string or dsn"  ' NOTE: Database assumed to be CREATE TABLE (scoreID int AUTO_INCREMENT, playerName varchar(100), playerScore int, dateEntered datetime, KEY (scoreID) );  sql = "INSERT INTO tPlayerScores(playerName, playerScore, dateEntered) VALUES '[@myvarpname]', [@myvarpscore], NOW());"  sql = Replace(sql, "[@myvarpname]", sPlayerName)  sql = Replace(sql, "[@myvarpscore]", sPlayerScore)  ado.Execute sql  ado.close  set ado = nothing%>


Obviously that code is short and sweet, you'd probably want to do a bunch of error checking (catching rogue " ' " characters, nulls, etc) and even encrypt the player's name and score. A hash/checksum value would be good too. This would stop people cheating and such. Every time the game is completed, it'd send the data in a HTTP POST method and update the database. Naturally you'd probably want to do some culling of the database server-side, making sure that the new score is valid (as I said before) and that it's worth entering in the database to begin with - a score of 50 won't be worth anything if the top 20 players have scores over 10,000.

[Edited by - evolutional on June 18, 2004 5:37:03 AM]
Actually, you should return a page, even 200 OK, to remain within the HTTP spec and to let the program know it has succeeded.

I think scripts that don't produce output can be kept around by the
webserver, which thinks they're still running :)
-- Jonathan
Thinking about it, you're right. The web server would execute the script and return a 200 OK. All you'd need to do Client-side is check for this and then close off the connection. The script itself doesn't need to return an error (unless you want to relay something to the client, like "You've got the 13th highscore!"). If that's the case, then you'd need to parse the returned page as FireNet said.
I had a similar problem where I wanted to create a master server for my game (online pong see sig) and didnt have my own server only access to a php script that had file access.

I used method get because it was extremly easy.

In order to connect to the internet through c++ I used WinInet, which is extremly to use and comes with the windows API (No MFC required).

clicky



Keep googling and you should find tutorials on it.

This topic is closed to new replies.

Advertisement