X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=api.php;h=ea390e5abe852a1671356a8a3634d10a8cbf3936;hb=8fce5d0b04cbc93ce56eee3ab937457f5162e02a;hp=3479aaf0521830e02606a7b6f8a307d18ae084ff;hpb=7dbc5032b29febdc34ef802151c48681c23c1deb;p=dragonblocks.git diff --git a/api.php b/api.php index 3479aaf..ea390e5 100644 --- a/api.php +++ b/api.php @@ -1,55 +1,107 @@ /dev/null")); array_pop($base); echo json_encode($base); } - function check_worldname($name){ + + function walk_directory($path, $func) + { + $data = array(); + + $files = scandir($path); + + foreach ($files as $filename) { + if ($filename[0] == ".") + continue; + + $result = $func($filename, $path . "/" . $filename); + + $data[$filename] = $result; + } + + echo json_encode($data); + } + + function check_worldname($name) + { return preg_match("/^[a-zA-Z0-9]+$/", $name); } - function world_exists($name){ + + function world_exists($name) + { return check_worldname($name) && file_exists("worlds/" . $name); } - switch($_POST["call"]){ + + function get_mods($path) + { + walk_directory($path, function($modname, $modpath) { + $dependencies = file_get_contents($modpath . "/dependencies.txt"); + + return array( + "name" => $modname, + "description" => file_get_contents($modpath . "/description.txt"), + "dependencies" => $dependencies ? array_values(array_filter(explode("\n", $dependencies))) : array(), + "path" => $modpath, + ); + }); + } + + switch($_POST["call"]) { case "getGamemods": - get_directory("game"); + get_mods("game"); break; + case "getMods": - get_directory("mods"); + get_mods("mods"); break; + case "getWorlds": - get_directory("worlds"); + walk_directory("worlds", function($worldname, $path) { + return array( + "name" => $worldname, + "owned" => is_loggedin() && get_username() == file_get_contents($path . "/owner.txt"), + ); + }); break; + case "getTextures": - get_directory("textures/* game/*/textures/* mods/*/textures/*"); + get_files("textures/* game/*/textures/* mods/*/textures/*"); break; + case "getSounds": - get_directory("sounds/* game/*/sounds/* mods/*/sounds/*"); + get_files("sounds/* game/*/sounds/* mods/*/sounds/*"); break; + case "isLoggedin": echo json_encode(is_loggedin()); break; + case "getUsername": echo get_username(); break; - case "checkWorldname": - echo json_encode(check_worldname($_POST["name"]) || false); - break; + case "saveWorld": - if(! is_loggedin()) - break; - if(! $_POST["name"]) - break; - if(! check_worldname($_POST["name"])) - break; - if(! world_exists($_POST["name"])) + if (! is_loggedin()) + return; + else if (! $_POST["name"]) + return; + else if (! check_worldname($_POST["name"])) + return; + + if (! world_exists($_POST["name"])) mkdir("worlds/" . $_POST["name"]); - else if(file_get_contents("worlds/" . $_POST["name"] . "/owner.txt") != get_username()) + else if (file_get_contents("worlds/" . $_POST["name"] . "/owner.txt") != get_username()) return; + file_put_contents("worlds/" . $_POST["name"] . "/world.json", $_POST["world"]); file_put_contents("worlds/" . $_POST["name"] . "/owner.txt", get_username()); break; + case "commitID": echo shell_exec("git rev-parse --short HEAD"); break;