-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostscore.php
36 lines (35 loc) · 927 Bytes
/
postscore.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
<?php
/*
* Falldown canvas game
* Copyright 2011 Viktor Barsk
* Licenced under CC BY-SA 2.5
* http://creativecommons.org/licenses/by-sa/2.5/
* Source code: https://github.com/Ahtenus/falldown
*/
if(isset($_GET['n']) && is_numeric($_GET['s']) && isset($_GET['_'])){
$name = strip_tags(substr($_GET['n'],0,20));
$arr = json_decode(file_get_contents('score.json'),true);
$newarr;
$yourscore = array('n' =>$name, 's' => $_GET['s']);
$ad = 0;
$add = 0;
$n = count($arr) < 10 ? count($arr) : 10;
for($i = 0;$i < $n - $ad;$i++)
{
if($add == 0 && $arr[$i]['s'] < $_GET['s'])
{
$ad = count($arr) < 10 ? 0 : 1;
$add = 1;
$newarr[] = $yourscore;
}
$newarr[$i + $add] = $arr[$i];
}
if($add == 0 && count($arr) < 10){
$newarr[] = $yourscore;
}
$json = json_encode($newarr);
$file = fopen("score.json", 'w') or exit('Filen kunde inte öppnas.');
fwrite($file,$json);
fclose($file);
echo $json;
}