OSDN Git Service

辛夷祭当日の変更を含む最終コミット
[h58pcdgame/GameScriptCoreLibrary.git] / www / ranking.php
diff --git a/www/ranking.php b/www/ranking.php
new file mode 100755 (executable)
index 0000000..99e1094
--- /dev/null
@@ -0,0 +1,104 @@
+<?php\r
+echo("<html>");\r
+echo("<head><meta charset='UTF-8'><title>ランキング</title></head>");\r
+echo("<body>");\r
+$scores = array();\r
+$names = array();\r
+$max_score = -9999;\r
+$min_score = 0;\r
+$newUser = true;\r
+\r
+//パラメーターの読み込み\r
+if(isset($_GET['name']))\r
+{\r
+       if(isset($_GET['score']))\r
+       {\r
+               //データを追加\r
+               $postName = urldecode($_GET['name']);\r
+               $postScore = intval($_GET['score']);\r
+       }\r
+}\r
+\r
+//データの読み込み\r
+$fp = fopen("./data.txt", "r");\r
+if(!$fp){\r
+       echo "データを開けません";\r
+       exit;\r
+}\r
+for($i = 0; !feof($fp);)\r
+{\r
+       $line = fgets($fp, 4096);\r
+       if($line == "") continue;\r
+       $line = rtrim($line);\r
+       $tkns = split("\t", $line);\r
+       if(isset($tkns[1])){\r
+               $name = $tkns[1];                               //これに改行がはいってるかも\r
+               $score = intval($tkns[0]);\r
+               $names[$i] = $name;\r
+               if(isset($postName) && strcmp($name, $postName) == 0){\r
+                       $newUser = false;\r
+                       if($postScore > $score){\r
+                               $scores[$i] = $postScore;\r
+                       } else{\r
+                               $scores[$i] = $score;\r
+                       }\r
+               } else{\r
+                       $scores[$i] = $score;\r
+               }\r
+               if($scores[$i] > $max_score){\r
+                       $max_score = $scores[$i];\r
+               }\r
+               if($scores[$i] < $min_score){\r
+                       $min_score = $scores[$i];\r
+               }\r
+               $i++;\r
+       }\r
+}\r
+//新規ユーザーの登録\r
+if(isset($postName) && isset($postScore) && $newUser){\r
+       $names[$i] = $postName;\r
+       $scores[$i] = $postScore;\r
+       if($scores[$i] > $max_score){\r
+               $max_score = $scores[$i];\r
+       }\r
+       if($scores[$i] < $min_score){\r
+               $min_score = $scores[$i];\r
+       }\r
+       $i++;\r
+}\r
+//ファイルへ書き戻す\r
+$fp = fopen("./data.txt", "w");\r
+$k = $i;\r
+for($i = 0; $i < $k; $i++){\r
+       fputs($fp, $scores[$i] . "\t" . $names[$i] . "\n");\r
+}\r
+\r
+//自分の得点表示\r
+if(isset($postName) && isset($postScore)){\r
+       echo($postName . "さんの点数は". $postScore . "点でした!<br>ランキングTOP7:\n");\r
+}\r
+\r
+$rankct = 0;\r
+$rankmax = 7;\r
+for($now_score = $max_score; $now_score >= $min_score; $now_score--){\r
+       for($i = 0; $i < $k; $i++){\r
+               if($scores[$i] == $now_score){\r
+                       $rankct++;\r
+                       echo("<div style='font-size: 32px;");\r
+                       if(strcmp($postName, $names[$i]) == 0){\r
+                               echo("color: red;");\r
+                       }\r
+                       echo("'>");\r
+                       echo($names[$i] . "\t" . $scores[$i] . "<br></div>");\r
+                       $rankct++;\r
+                       echo "<hr>\n";\r
+               }\r
+       }\r
+       \r
+       if($rankct > $rankmax){\r
+               break;\r
+       }\r
+}\r
+\r
+echo("</body></html>");\r
+?>
\ No newline at end of file