PDA

View Full Version : How to do simple high-score tables



paul_nicholls
07-10-2009, 04:19 AM
Hi all,
in case if anyone is interested, I found out how to make a simple high-score table for any game you are making, I have the details below:

1. get a web host site (free or paid)
a. use a free hosted site like I am http://www.000webhost.com/207343.html (includes referal link) which allows PHP programs.
b. create a web host on your local machine using WAMP (http://www.wampserver.com/) (free windows software) and enable internet access to it.
c. use a paid web host of some sort.

2. read this flash tutorial http://www.flashkit.com/tutorials/Games/High-sco-Glen_Rho-657/index.php

I modified my scores.php file (from tutorial above) so I could pass parameters to it via my game as well as via the URL.

This was done by adding a GetValue() function at the beginning and calling this for each variable being read.

I also made a dedicated script (dodscores.php) that only reads the table and returns it as the HTML code so I could easily use that for people to see the high-scores :)

In dodscores.php, substitute the string

<YOUR SCORES FILENAME>

with the dedicated filename of the appropriate high-scores table file...

With my high-score table, you can go here to test it out:

http://paulsbitsandbytes.webatu.com/dodscores.php
(http://paulsbitsandbytes.webatu.com/dodscores.php)

Here is the code for my scores.php:



<?php
function GetValue($name)
{
$value = $_POST[$name];
if (!isset($_POST[$name]))
$value = $_GET[$name];

return $value;
}

$filename = "scores/" . GetValue("filename");
$action = GetValue("action");
$viewtype = GetValue("viewtype");
$winscore = GetValue("winscore");
$winname = GetValue("winname");
$scoresize = GetValue("scoresize");

$winscore = (int)$winscore;

// Create a Blank File if it doesn't already exist
if (!file_exists($filename))
{
$file=fopen($filename, "w");
fclose ($file);
}

// Read the file in
$oscores = file ($filename);
$numreadin = count($oscores);

// Break out the data into a new 2-d array called $tscores
for ($i = 0; $i < $numreadin; $i++)
{
$g = unserialize($oscores[$i]);
$tscores[$i][0] = $g[0];
$tscores[$i][1] = $g[1];
}

// Fill in any missing data with none/0
for ($i = $numreadin; $i < $scoresize; $i++)
{
$tscores[$i][0] = 0;
$tscores[$i][1] = "none";
}

// Process the actions

// Insert a score/name
if ($action == "INSERT")
{

// Add name to end of list, and sort
$tscores[$scoresize + 1][0] = $winscore;
$tscores[$scoresize + 1][1] = $winname;
rsort ($tscores);

$file=fopen($filename, "w");

// Write them out
for ($i = 0; $i < $scoresize; $i++)
{
$st = serialize($tscores[$i]) . "\n";
fputs($file, $st);
}

fclose($file);
}

// Clear the list
if ($action == "CLEAR")
{

$k[0] = 0;
$k[1] = "none";
$ser = serialize($k);

$file=fopen($filename, "w");

for ($i = 0; $i < $scoresize; $i++)
{
$st = $ser . "\n";
fputs($file, $st);
}

fclose($file);
}

// Process the OUTPUT options
if ($viewtype == "HTML")
{
// HTML PAGE CREATED HERE
?>


<table cellpadding=2 cellspacing=2 border=0 width="152">
<tr align=center>
<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">#</font></th>
<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Name</font></th>
<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Score</font></th>
</tr>

<?

for ($i = 0; $i < $scoresize; $i++)
{
echo ("<tr bgcolor='#666666' align='center'><td><font size='2' face='Arial, Helvetica, sans-serif'>");
echo ($i + 1);
echo ("</font></td><td><font size='2' face='Arial, Helvetica, sans-serif'>");
echo ($tscores[$i][1]);
echo ("</font></td><td><font size='2' face='Arial, Helvetica, sans-serif'>");
echo ($tscores[$i][0]);
echo ("</font></td></tr>");
}

?>
</table>
<?

}

// FLASH DATA CREATED HERE
if ($viewtype == "FLASH")
{
for ($i = 0; $i < $scoresize; $i++)
{
echo ("NAME" . $i . "=");
echo ($tscores[$i][1]);
echo ("&SCORE" . $i . "=");
echo ($tscores[$i][0]);
echo ("&");
}
}

?>


here is the code for dodscores.php


<?php

$filename = "scores/<YOUR SCORES FILENAME>";
$scoresize = 10;

$winscore = (int)$winscore;

// Create a Blank File if it doesn't already exist
if (!file_exists($filename))
{
$file=fopen($filename, "w");
fclose ($file);
}

// Read the file in
$oscores = file ($filename);
$numreadin = count($oscores);

// Break out the data into a new 2-d array called $tscores
for ($i = 0; $i < $numreadin; $i++)
{
$g = unserialize($oscores[$i]);
$tscores[$i][0] = $g[0];
$tscores[$i][1] = $g[1];
}

// Fill in any missing data with none/0
for ($i = $numreadin; $i < $scoresize; $i++)
{
$tscores[$i][0] = 0;
$tscores[$i][1] = "none";
}

// Process the actions

// HTML PAGE CREATED HERE
?>


<table cellpadding=2 cellspacing=2 border=0 width="152">
<tr align=center>
<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">#</font></th>
<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Name</font></th>
<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Score</font></th>
</tr>

<?

for ($i = 0; $i < $scoresize; $i++)
{
echo ("<tr bgcolor='#666666' align='center'><td><font size='2' face='Arial, Helvetica, sans-serif'>");
echo ($i + 1);
echo ("</font></td><td><font size='2' face='Arial, Helvetica, sans-serif'>");
echo ($tscores[$i][1]);
echo ("</font></td><td><font size='2' face='Arial, Helvetica, sans-serif'>");
echo ($tscores[$i][0]);
echo ("</font></td></tr>");
}

?>
</table>
<?

?>


This is how I read the high-score table from within my Delphi code with the Synapse HttpPostURL routine (using generic scores.php with FLASH output):


Procedure TGame.ReadOnlineHightScores;
Const
cURL = '<MYURL>';
cData = 'filename=<YOUR SCORES FILENAME>&scoresize=%d&action=VIEW&viewtype=FLASH';
Var
i : Integer;
Data : AnsiString;
Stream : TMemoryStream;
Line : AnsiString;
Scores : TStringList;
Name : AnsiString;
Score : AnsiString;
Begin
Stream := TMemoryStream.Create;
Try
Data := Format(cData,
[cHighScoreTableLen]);
If HttpPostURL(cURL,
Data,Stream) Then
Begin
Stream.Position := 0;
SetLength(Line,Stream.Size);
Stream.Read(PChar(Line)^,Stream.Size);
Line := StringReplace(Line,'&',',',[rfReplaceAll]);
Scores := TStringList.Create;
Try
SetLength(FHighScoreTable,cHighScoreTableLen);
Scores.CommaText := Line;
i := 0;
While i < cHighScoreTableLen * 2 Do
Begin
Name := Scores.Strings[i + 0];
Score := Scores.Strings[i + 1];
Name := Copy(Name ,Pos('=',Name) + 1,Length(Name));
Score := Copy(Score,Pos('=',Score) + 1,Length(Score));

FHighScoreTable[i Div 2].TimeDateStamp := Now;
FHighScoreTable[i Div 2].Name := Name;
FHighScoreTable[i Div 2].Score := StrToInt(Score);
Inc(i,2);
End;
Finally
Scores.Free;
End;
End;
Finally
Stream.Free;
End;
End;


This is how I add to the high-score table for my game using Delphi code with the Synapse HttpPostURL routine :


Procedure TGame.AddHighScore(Const AHighScore : THighScore);
Const
cURL = '<MYURL>';
cData = 'filename=<YOUR SCORES FILENAME>&scoresize=%d&action=INSERT&winname=%s&winscore=%d&viewtype=FLASH';
Var
i : Integer;
Data : AnsiString;
Stream : TMemoryStream;
Begin
i := Length(FHighScoreTable);
SetLength(FHighScoreTable,i + 1);
FHighScoreTable[i] := AHighScore;
SortHighScoreTable;
If Length(FHighScoreTable) > cHighScoreTableLen Then
SetLength(FHighScoreTable,cHighScoreTableLen);

Stream := TMemoryStream.Create;
Try
Data := Format(cData,
[cHighScoreTableLen,EncodeURLElement(AHighScore.Nam e),AHighScore.Score]);
If HttpPostURL(cURL,
Data,Stream) Then
Begin
ReadOnlineHightScores;
End;
Finally
Stream.Free;
End;
End;


I hope this helps someone :)

cheers,
Paul

chronozphere
07-10-2009, 06:47 AM
Wow.. Thanks paul. ;) This is very helpfull. Now I can just start implementing online highscores without having to figure everything out. ;D

NecroDOME
07-10-2009, 07:25 AM
Nice :). I think now everyone is just copy-past it in his code.

User137
07-10-2009, 05:57 PM
You forgot to show structure of THighScore ;)

paul_nicholls
07-10-2009, 07:38 PM
You forgot to show structure of THighScore ;)


LOL!

I thought it was obvious from the code, but that wasn't the "important" bit ;)

FHighScoreTable[i Div 2].Name := Name;
FHighScoreTable[i Div 2].Score := StrToInt(Score);

Besides, I'm not going to do ALL the work for you! hahaha!

cheers,
Paul