response(200, $this->get_polls($this->load_database())); break; case "add_poll": $database = $this->load_database(); isset($database["polls"]) || ($database["polls"] = []); $database["polls"][] = [ "name" => $data["name"], "options" => [] ]; $this->save_database($database); $this->response(200, $this->get_polls($database)); break; case "get_poll": $database = $this->load_database(); $poll = isset($database["polls"]) && isset($database["polls"][$data["i"]]) ? $database["polls"][$data["i"]] : null; $this->response($poll ? 200 : 404, $poll ?? [ "message" => "poll_not_exists", "i" => $data["i"] ]); break; case "add_option": $database = $this->load_database(); if(isset($database["polls"]) && isset($database["polls"][$data["i"]])){ $database["polls"][$data["i"]]["options"][] = [ "text" => $data["text"], "points" => 0 ]; $this->save_database($database); $this->response(200, $database["polls"][$data["i"]]["options"]); }else $this->response(404, [ "message" => "poll_not_exists", "name" => $data["name"] ]); break; case "set_option": break; default: $database = $this->load_database(); if(isset($database["polls"]) && isset($database["polls"][$data["name"]])){ $database["polls"][$data["name"]][$data["i"]]["points"] ++; $this->save_database($database); $this->response(200, null); }else $this->response(404, [ "message" => "poll_not_exists", "name" => $data["name"] ]); break; }; }else $this->response(405, ["message" => "no_action"]); }else $this->response(405, ["message" => "no_data"]); } private function get_polls($database){ return isset($database["polls"]) ? array_map(fn($poll) => ["name" => $poll["name"]], $database["polls"]) : []; } private function response($code, $data){ echo json_encode([ "ok" => $code == 200, "code" => $code, "content" => $data ]); exit(0); } private function load_database(){ return file_exists(\GamUsino\Secrets::settings["database_path"]) ? json_decode(file_get_contents(\GamUsino\Secrets::settings["database_path"]), true) : []; } private function save_database($database){ file_put_contents(\GamUsino\Secrets::settings["database_path"], json_encode($database)); } };