Results 1 to 5 of 5

Thread: How to do simple high-score tables

  1. #1

    How to do simple high-score tables

    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/Ga...-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


    Here is the code for my scores.php:

    Code:
    <?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
    Code:
    <?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):

    [pascal]
    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;
    [/pascal]

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

    [pascal]
    Procedure TGame.AddHighScore(Const AHighScore : THighScore);
    Const
    cURL = '<MYURL>';
    cData = 'filename=<YOUR SCORES FILENAME>&scoresize=%d&action=INSERT&winname=%s&wi nscore=%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;
    [/pascal]

    I hope this helps someone

    cheers,
    Paul

  2. #2

    Re: How to do simple high-score tables

    Wow.. Thanks paul. This is very helpfull. Now I can just start implementing online highscores without having to figure everything out.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Re: How to do simple high-score tables

    Nice . I think now everyone is just copy-past it in his code.
    NecroSOFT - End of line -

  4. #4

    Re: How to do simple high-score tables

    You forgot to show structure of THighScore

  5. #5

    Re: How to do simple high-score tables

    Quote Originally Posted by User137
    You forgot to show structure of THighScore
    LOL!

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

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

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

    cheers,
    Paul

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •