86 lines
3.7 KiB
PHP
86 lines
3.7 KiB
PHP
|
<?php
|
||
|
|
||
|
class GamUsino{
|
||
|
|
||
|
public function __construct($inputs = null){
|
||
|
|
||
|
$data = json_decode(base64_decode($_POST["GamUsino"]), true);
|
||
|
|
||
|
if($data){
|
||
|
if(isset($data["action"])){
|
||
|
switch($data["action"]){
|
||
|
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, $database["polls"]);
|
||
|
break;
|
||
|
case "get_poll":
|
||
|
$database = $this->load_database();
|
||
|
$poll = isset($database["polls"]) && isset($database["polls"][$data["name"]]) ? $database["polls"][$data["name"]] : null;
|
||
|
$this->response($poll ? 200 : 404, $poll ?? [
|
||
|
"message" => "poll_not_exists",
|
||
|
"name" => $data["name"]
|
||
|
]);
|
||
|
break;
|
||
|
case "add_option":
|
||
|
$database = $this->load_database();
|
||
|
if(isset($database["polls"]) && isset($database["polls"][$data["name"]])){
|
||
|
$database["polls"][$data["name"]][] = [
|
||
|
"text" => $data["text"],
|
||
|
"points" => 0
|
||
|
];
|
||
|
$this->save_database($database);
|
||
|
$this->response(200, $database["polls"][$data["name"]]);
|
||
|
}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 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));
|
||
|
}
|
||
|
|
||
|
};
|