-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddLeaderboard.php
93 lines (80 loc) · 2.89 KB
/
AddLeaderboard.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
session_start();
if(isset($_POST["name"]) && !empty($_POST["name"])){
$_SESSION["name"] = $_POST["name"];
header("Location: index.php");
}
if(isset($_POST["score"]) && !empty($_POST["score"])){
$_SESSION["score"] = $_POST["score"];
echo "score";
var_dump($_SESSION);
}
if(isset($_SESSION["score"]) && !empty($_SESSION["score"]) && isset($_SESSION["name"]) && !empty($_SESSION["name"])){
AddPlayer($_SESSION["name"], $_SESSION["score"]);
unset($_SESSION["score"]);
var_dump($_SESSION);
}
function AddPlayer($name, $score){
if(sizeof(explode(' ', $name)) != 1){
$newName = explode(' ', $name);
$name = join('-', $newName);
}
$actualPlayer = $name. ' : '.$score. ' - '. date("d/F/Y")."\n";
$lignes = [];
$i = 0;
$players = [];
$newPlayers = [];
$added = false;
$open = fopen("leaderboard.txt", "r") or die ("Problème d'ouverture du fichier liste");
while(!feof($open)){
$lignes[$i] = fgets($open);
$i++;
}
fclose($open);
echo strlen($lignes[0]);
var_dump($lignes);
echo $lignes[0];
if(strlen($lignes[0]) == 1){
$open = fopen("leaderboard.txt", "w") or die ("Problème d'ouverture du fichier liste");
$ligne = $actualPlayer;
echo "ok";
fwrite($open, $ligne);
fclose($open);
return;
}
for($i = 0; $i < sizeof($lignes); $i++){
if(!strpos("-", $lignes[$i]) || !strpos(" ", $lignes[$i])){
array_push($players, $lignes[$i]);
}
}
for($j = 0; $j < sizeof($players)-1; $j++){
if(intval(explode(' ', $players[$j])[2]) >= $score && !$added){
array_push($newPlayers, $actualPlayer);
for($k = $j; $k < sizeof($players)-1; $k++){
array_push($newPlayers, $players[$k]);
}
$open = fopen("leaderboard.txt", "w") or die ("Problème d'ouverture du fichier liste");
for($x = 0; $x < sizeof($newPlayers); $x++){
$line2write = $newPlayers[$x];
fwrite($open, $line2write);
}
fclose($open);
$added = true;
return;
}
else{
array_push($newPlayers, $players[$j]);
}
}
if(!$added){
array_push($newPlayers, $actualPlayer);
}
$open = fopen("leaderboard.txt", "w") or die ("Problème d'ouverture du fichier liste");
for($x = 0; $x < sizeof($newPlayers); $x++){
$line2write = $newPlayers[$x];
fwrite($open, $line2write);
}
var_dump($newPlayers);
fclose($open);
}
?>