Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Online HighScore Module
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ecliptical




PostPosted: Mon Jan 02, 2006 10:18 pm   Post subject: [Tutorial] Online HighScore Module

Before we start coding you need to ask yourself if it would be better to use just local storage for scores if thats the case then follow this tutorial by hacker dan
http://www.compsci.ca/v2/viewtopic.php?t=5340

Otherwise lets get started.

Ok first things first lets plan out how this is going to work

Step 1.
Make a fun game. (This is hard)

Step 2.
Make it challenging enough that the player dies (easier then step 1)

Step 3.

Send his score, name and e-mail to the server (all encrypted).

Step 4.
Make the server save,handle, sort and then send the results back to Turing.

Step 5.
Check to see how many people are playing your game (or at least how many sent their highscores).


    What you'll need for this to work:
    -PHP 4x on any server
    -Domain Name/WebSpace (or possibly some free hosting subdomain service haven't tried it)
    -Turing 4.04d
    -A Game (The game i've included if it isn't obvious is a pseudo code compacted game")


The Game: This is just an example of how this works.
code:

procedure RunMyAwsomeGame
if PlayerDied then
gameServer.SendScore(playerId,finalScore,emailAddress:string)
else
 Run.TheGame
end RunMyAwsomeGame

RunMyAwsomeGame



So it's pretty simply so far, once the player dies we send his/her info to the server.

Not My Job: Create the required code to check when the player has died and store the playerID and finalScore in a variable.


code:

module gameServer
    export SendScore
    procedure SendScore (playerID, score, emailAddress : string)
        var netStream : int
        var netLine : string (1)
        netStream := Net.OpenURLConnection ("")
        if netStream <= 0 then
            put "Error could not send", score, "!"
            return
        end if
        loop
            exit when eof (netStream)
        end loop
    end SendScore
end gameServer


What do we have?
We've created a module that opens a connection to URL and reads the file of that URL but doesn't display anything yet.

Net.OpenURLConnection: This is pretty much the same as opening a file, except that we're reading a file located remotely on a server somewhere instead of on our computer.

Ok now lets make it actually do something!



code:

const gameServerURL : string := "http://www.yourwebsiteaddresshere.com/highScoreHandler.php?"
module gameServer
    export SendScore
    procedure SendScore (playerID, finalScore : string)
        var netStream : int :=
            var netLine : string (1)
        netStream := Net.OpenURLConnection (gameServerURL + "playerID="+playerID + "&finalScore=" + finalScore)
        if netStream <= 0 then
            put "Error could not send", score, "!"
            return
        end if
        loop
            exit when eof (netStream)
            get : netStream, net_line : 1
            put netLine ..
        end loop
    end SendScore
end gameServer



So all we added is the location of the file on our server that will handle our highscore as well as the code that will output our results (the player's score with other peoples scores) back to turing.

Now for where the real magic happens ONLINE!

Ok so in your code editor of choice (NotePad++ if you're cool) create the following simple php script.

code:

<?echo ("Thank you $playerID your score of $finalScore was added!");?>

Upload the file to your server and name it highScoreHandler.php

Ok so we lied to the played we haven't actually added his score to the server but this will let us know if our code works so far.

code:

<html><head><title><title></head><body>
<?
$filename = 'scores.txt';
$fp = fopen($filename, "a");
$addScore =$playedID."........".$finalScore;
$write = fputs($fp, $addScore);

fclose($fp);
include ($filename);
?>

</body>
</html>



Ohh and the usage of course is
code:

gameServer.SendScore("JohnSmith","3423")


There I guess that wasn't so hard we've actually added the players score to our database (just a normal text file) and included that file so his result will be displayed.


Turing seems to like properly formated HTML pages so the next part of this
tutorial will include how to strip out that html and sort the results.

Also the next part of the tutorial will include how you encrypt the code. Because as if you are distributing your game with the source then someone could just easily send whatever score they wanted to your highScoreHandler.php

Currently my server just upped its security so I wasn't able to test this code but a couple of weeks ago before the upgrade it worked fine so I"m going to assume it's still working however while rewriting it I may have messed some stuff up let me know if anything looks out of place or if you have any questions in general.

Future gameServer Tutorials
gameServer.GetMove
gameServer.SendMove
gameServer.BanUser
gameServer.Login
gameServer.Logout
gameServer.GameUpdate :Does the player have the latest build of your game? If not download an update.
-this isn't working totally yet.
gameServer.getHighScore


That's pretty much all I've worked on but development has been put off until my server stuff is resolved.

Anyway this works pretty great for Turn based games...and with some AJAX hotsause you might even get some decent proformance for real time games...but thats a whole other Tutorial enjoy.
Sponsor
Sponsor
Sponsor
sponsor
ecliptical




PostPosted: Mon Jan 02, 2006 10:32 pm   Post subject: (No subject)

scratch that I just remembed AJAX includes javascript (duh!) which is executed on the client and in this case the client is Turing which does NOT have a javascript engine.

So realistically the only "real" time game would be one where part of the game play has a time limit so you can send moves back and forth without lag.
Taur




PostPosted: Wed Jan 04, 2006 7:02 pm   Post subject: (No subject)

okay but I got a bit confused with the web part. Confused Where do we put the "usage"

could you just attach the files we have to upload to our server? and what if we want to send a string, is that aldready converted to a string and we're not sending an interger variable?
ecliptical




PostPosted: Thu Jan 05, 2006 12:11 pm   Post subject: (No subject)

opps I had the in an earlier version that caugh int's must of got lost in the rewrite - anyway once my issues with my server are resolved I'll post all of the gameServer modules
Taur




PostPosted: Thu Jan 05, 2006 9:07 pm   Post subject: (No subject)

k thx
ecliptical




PostPosted: Thu Jan 05, 2006 9:20 pm   Post subject: (No subject)

Taur do you have a host that runs php scripts?
Taur




PostPosted: Fri Jan 06, 2006 9:20 am   Post subject: (No subject)

I think so, try

www.freewebs.com
unknowngiver




PostPosted: Fri May 12, 2006 10:28 am   Post subject: (No subject)

hey
now since you can submit stuff from turing to php...can you make a Program that sends stuff from turing to PHP which inputs it to a database...and then when user wants to recieve the information thats in the database..the program runs another php script which gets the stuff from the database and puts it on the turing screen?
can i do something like that in another way? i really need this! thanks
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: