Flash memory spil med highscore (PHP)
Hey;Jeg er i gang med et flash memory/vende spil. Spillet fungerer ganske fint. Så vil jeg gerne have sendt mine infomationer ud til mine php-filer og gemme dem (memory_highscore.dat) for derefter at kunne hente dem til en highscoreliste.
Jeg har haft problemer med $_get og $_post. tidligerei scriptet stod der $HTTP_SERVER_GET, men da denne er for gammel i forhold til PHP 5 skiftede jeg til $_POST. Men nu for jeg en masse [type Function] meldinger i min highscore liste, hvor den før hentede resultaterne.
Desforuden kunne jeg godt tænke mig at vide, om man kan lave en slags (td) i flash, så den viser highscoren på pæn og overskuelig vis.
Først har jeg min flash code som gemme og henter highscoren:
scoreMsg = "Your score is " + _root.gamePoints + " points !";
txtScore = "Loading highscore, please wait...";
if(_root.modeHighscore == 1) {
txtTop10 = "Top 10:";
loadVariables("./get_score.php?count=10&" + _root.phpCounter++, "", "POST");
}
if(_root.modeHighscore == 0) {
txtScore = _root.getHighscore();
}
for(var i = 0; i < _root.arrCats[i].length; i++) {
_root.arrCats[i]._visible = true;
_root.arrCards[i]._visible = false;
}
function onBtnApply() {
if(_root.modeHighscore == 0) {
_root.addScoreItem(txtPlayersName, txtPlayersEmail);
_root.saveHighscore();
}
if(_root.modeHighscore == 1) {
var d = new Date();
var strDate = d.getDate() + "." + (d.getMonth() + 1) + "." + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes();
loadVariablesNum("./save_score.php?p=" + _root.gamePoints + "&n=" + escape(txtPlayersName) + "&e=" + escape(txtPlayersEmail) + "&d=" + escape(strDate) + "&l=" + _root.gameLevel + "&t=" + _root.secondsCounter / 1000 + "&a=" + _root.attemptsCounter, "", "POST");
}
btnApply.setEnabled(false);
gotoAndPlay(1);
};
function OnBtnBack() {
gotoAndPlay(1);
};
stop();
Og så har jeg en save_score.php:
<?php
// Append the score to file
$fp = fopen("./memory_highscore.dat", "a");
foreach($_POST as $var) {
fwrite($fp, "$var" . chr(9));
}
fwrite($fp, chr(10));
fclose($fp);
?>
Og så har jeg en get_score.php:
<?php
// Add spaces to string
function formatString($var, $length) {
$ret = $var;
if(strlen($ret) > $length) {
$ret = substr($ret, 0, $length);
}
$n = strlen($ret);
for($i = 0; $i < strlen($ret); $i++) {
if(ord(substr($ret, $i, 1)) > 192) {
$n--; // UNICODE, count only 1x
$i++;
}
}
$n = $length - $n;
if($n > 0) {
$ret = $var.str_repeat("%20", $n);
}
return $ret;
}
// Sort function used by usort
function sortfunc($val1, $val2) {
return ($val1["p"] < $val2["p"]);
}
// Count of highscore entries
$count = 10;
if(isset($ _POST['count'])) {
$count = $_POST['count'];
}
// Header
$s1 = formatString("Points", 15);
$s2 = formatString("Navn", 50);
$s3 = formatString("E-mail", 50);
$s4 = formatString("Tid/Se",25);
$s5 = formatString("Forsoeg", 25);
$s6 = formatString("Level", 25);
$s7 = formatString("Dato", 25);
$score = $s1.$s2.$s3.$s4.$s5.$s6.$s7.chr(10);
// Read entries from file into an array
$i = 0;
$fp = fopen("./memory_highscore.dat", "r");
while($strLine = fgets($fp, 100)) {
$arr = split(chr(9), $strLine);
$arrarr{$i} = array("p" => $arr[0], "n" => $arr[1], "e" => $arr[2], "d" => $arr[3], "l" => $arr[4], "t" => $arr[5], "a" => $arr[6]);
$i++;
}
fclose($fp);
// Sort the array
usort($arrarr, sortfunc);
// Print the array
$i = 1;
while(list($id, $val) = each($arrarr)) {
if($i > $count) {
break;
}
$s1 = formatString($arrarr{$id}{"p"}, 7);
$s2 = formatString($arrarr{$id}{"n"}, 50);
$s3 = formatString($arrarr{$id}{"e"}, 40);
$s4 = formatString($arrarr{$id}{"t"}, 20);
$s5 = formatString($arrarr{$id}{"a"}, 20);
$s6 = formatString($arrarr{$id}{"l"}, 20);
$s7 = formatString($arrarr{$id}{"d"}, 20);
$score = $score.$s1.$s2.$s3.$s4.$s5.$s6.$s7.chr(10);
$i++;
}
echo "&txtScore="."$score"; // Flash variable, attached to the highscore textfield
?>
Og til sidst en show_score.php:
<?php
// Sort function used by usort
function sortfunc($val1, $val2) {
return ($val1["p"] < $val2["p"]);
}
// Read entries from file into an array
$i = 0;
$fp = fopen("./memory_highscore.dat", "r");
while($strLine = fgets($fp, 100)) {
$arr = split(chr(9), $strLine);
$arrarr{$i} = array("p" => $arr[0], "n" => $arr[1], "e" => $arr[2],"d" => $arr[3], "l" => $arr[4], "t" => $arr[5], "a" => $arr[6]);
$i++;
}
fclose($fp);
// Sort the array
usort($arrarr, sortfunc);
// Output as table
echo ' <table width="100%" cellspacing="0" cellpadding="4" border="1">';
echo ' <tr>';
echo ' <th align="left">LL</th>';
echo ' <th align="left">Point</th>';
echo ' <th align="left">Navn</th>';
echo ' <th align="left">Email</th>';
echo ' <th align="left">Dato</th>';
echo ' <th align="left">Level</th>';
echo ' <th align="left">Tid</th>';
echo ' <th align="left">Forsøg</th>';
echo ' </tr>';
$i = 1;
while(list($id, $val) = each($arrarr)) {
$p = $arrarr{$id}{"p"};
$n = $arrarr{$id}{"n"};
$e = $arrarr{$id}{"e"};
$d = $arrarr{$id}{"d"};
$l = $arrarr{$id}{"l"};
$t = $arrarr{$id}{"t"};
$a = $arrarr{$id}{"a"};
echo ' <tr>';
echo ' <td>' . "$i" . '</td>';
echo ' <td>' . "$p" . '</td>';
echo ' <td>' . "$n" . '</td>';
echo ' <td>' . "$e" . '</td>';
echo ' <td>' . "$d" . '</td>';
echo ' <td>' . "$l" . '</td>';
echo ' <td>' . "$t" . '</td>';
echo ' <td>' . "$a" . '</td>';
echo ' </tr>';
$i++;
}
echo ' </table>';
?>
</body>
</html>
