-
Notifications
You must be signed in to change notification settings - Fork 2
/
req.php
78 lines (72 loc) · 2.17 KB
/
req.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
session_start();
$filename="leaderboard";
/* which command is it? */
if(!isset($_GET['command'])) {die("no command specified, aborting."); }
$command = $_GET['command'];
/* debug */
if($command == 'debug') {
die(phpversion());
}
/* set user name in SESSION */
if($command == 'setuser') {
if(isset($_POST['user'])) {
$_SESSION['user'] = trim(trim($_POST['user']), ':');
$user = $_SESSION['user'];
die("ok, user changed to '$user'");
} else {
die("error, username unreadable");
}
}
/* read score file */
if($command == 'get_highscore' || $command == 'get_scores') {
$f=fopen($filename, "r") or die("failed to open file for reading");
$board=array();
while ($line = fgets($f)) {
$line=rtrim($line);
$v = explode(':', $line);
$score = $v[0];
$user = $v[1]; if(!$user) $user="anon";
$moves = $v[2]; if(!$moves) $moves="";
//$score = preg_replace('/^([^:]*):\s*.*$/', '\1', $line);
//$remaining = preg_replace('/^[^:]*:\s*/', '', $line);
//$user = preg_replace('/^([^:]*):\s*.*$/', '\1', $remaining);
//$moves = preg_replace('/^[^:]*:\s*/', '', $remaining);
$board[]=array($score,$user,$moves);
}
$users=array();
fclose($f);
rsort($board);
$first=json_encode(array('user'=>$board[0][1], 'score'=>$board[0][0], 'moves'=>$board[0][2]));
if($command == 'get_highscore') echo $first;
if($command == 'get_scores') {
foreach($board as $b) {
$user = $b[1];
$score = $b[0];
if(!in_array($user, $users)) {
$users[] = $user;
if(strlen($b[2])<=2) {
echo "$b[1] : $b[0]\n";
} else {
echo "<a href='#' onclick=\"javascript:replay('$b[2]');\">$b[1] : $b[0]</a>\n";
}
}
}
};
}
/* writing score */
if($command == 'send_score' && isset($_POST['score'])) {
$score = 1 * $_POST['score'];
$user = $_SESSION['user'];
if(isset($_POST['username'])) {
$user = $_POST['username'];
$_SESSION['user'] = $user;
}
$moves = $_POST['moves'];
$f = fopen($filename, "a") or die("failed to open file for writing");
fwrite($f, "$score:$user:$moves\n");
fclose($f);
var_dump($_POST);
die("added score $score for $user");// with moves $moves");
}
?>