feat: Creado todo el entorno Core básico. 4 Jams con un total de 2 horas y 38 minutos. ¡Jajaja!
This commit is contained in:
parent
edbc609272
commit
dee8bdfb63
104
PHP/GamUsino.php
104
PHP/GamUsino.php
@ -10,68 +10,106 @@
|
|||||||
if(isset($data["action"])){
|
if(isset($data["action"])){
|
||||||
switch($data["action"]){
|
switch($data["action"]){
|
||||||
case "load_polls":
|
case "load_polls":
|
||||||
$this->response(200, $this->get_polls($this->load_database()));
|
self::response(200, [
|
||||||
|
"polls" => self::get_polls(self::load_database()),
|
||||||
|
"ip" => self::get_ip()
|
||||||
|
]);
|
||||||
break;
|
break;
|
||||||
case "add_poll":
|
case "add_poll":
|
||||||
$database = $this->load_database();
|
$database = self::load_database();
|
||||||
isset($database["polls"]) || ($database["polls"] = []);
|
isset($database["polls"]) || ($database["polls"] = []);
|
||||||
|
foreach($database["polls"] as $poll)
|
||||||
|
$poll["name"] == $data["name"] && self::response(400, [
|
||||||
|
"message" => "poll_already_exists",
|
||||||
|
"name" => $data["name"]
|
||||||
|
]);
|
||||||
$database["polls"][] = [
|
$database["polls"][] = [
|
||||||
"name" => $data["name"],
|
"name" => $data["name"],
|
||||||
"options" => []
|
"options" => [],
|
||||||
|
"ip" => self::get_ip(),
|
||||||
|
"date" => time(),
|
||||||
|
"deleted" => false
|
||||||
];
|
];
|
||||||
$this->save_database($database);
|
self::save_database($database);
|
||||||
$this->response(200, $this->get_polls($database));
|
self::response(200, self::get_polls($database));
|
||||||
break;
|
break;
|
||||||
case "get_poll":
|
case "get_poll":
|
||||||
$database = $this->load_database();
|
$database = self::load_database();
|
||||||
$poll = isset($database["polls"]) && isset($database["polls"][$data["i"]]) ? $database["polls"][$data["i"]] : null;
|
$poll = isset($database["polls"]) && isset($database["polls"][$data["i"]]) ? $database["polls"][$data["i"]] : null;
|
||||||
$this->response($poll ? 200 : 404, $poll ?? [
|
self::response($poll ? 200 : 404, $poll ?? [
|
||||||
"message" => "poll_not_exists",
|
"message" => "poll_not_exists",
|
||||||
"i" => $data["i"]
|
"i" => $data["i"]
|
||||||
]);
|
]);
|
||||||
break;
|
break;
|
||||||
case "add_option":
|
case "add_option":
|
||||||
$database = $this->load_database();
|
$database = self::load_database();
|
||||||
if(isset($database["polls"]) && isset($database["polls"][$data["i"]])){
|
if(isset($database["polls"]) && isset($database["polls"][$data["i"]])){
|
||||||
|
foreach($database["polls"][$data["i"]]["options"] as $option)
|
||||||
|
$option["text"] == $data["text"] && self::response(400, [
|
||||||
|
"message" => "poll_option_already_exists",
|
||||||
|
"text" => $data["text"]
|
||||||
|
]);
|
||||||
$database["polls"][$data["i"]]["options"][] = [
|
$database["polls"][$data["i"]]["options"][] = [
|
||||||
"text" => $data["text"],
|
"text" => $data["text"],
|
||||||
"points" => 0
|
"points" => [],
|
||||||
|
"ip" => self::get_ip(),
|
||||||
|
"date" => time(),
|
||||||
|
"deleted" => false
|
||||||
];
|
];
|
||||||
$this->save_database($database);
|
self::save_database($database);
|
||||||
$this->response(200, $database["polls"][$data["i"]]["options"]);
|
self::response(200, $database["polls"][$data["i"]]["options"]);
|
||||||
}else
|
}else
|
||||||
$this->response(404, [
|
self::response(404, [
|
||||||
"message" => "poll_not_exists",
|
"message" => "poll_not_exists",
|
||||||
"name" => $data["name"]
|
"name" => $data["name"]
|
||||||
]);
|
]);
|
||||||
break;
|
break;
|
||||||
case "set_option":
|
case "set_option":
|
||||||
|
$database = self::load_database();
|
||||||
|
if(isset($database["polls"]) && isset($database["polls"][$data["i"]])){
|
||||||
|
$ip = self::get_ip();
|
||||||
|
$done = false;
|
||||||
|
$date = time();
|
||||||
|
foreach($database["polls"][$data["i"]]["options"][$data["j"]]["points"] as $i => $option)
|
||||||
|
if($done = $option["ip"] == $ip){
|
||||||
|
$database["polls"][$data["i"]]["options"][$data["j"]]["points"][$i]["selected"] = $data["selected"];
|
||||||
|
$database["polls"][$data["i"]]["options"][$data["j"]]["points"][$i]["changes"] ++;
|
||||||
|
$database["polls"][$data["i"]]["options"][$data["j"]]["points"][$i]["date_last"] = $date;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
!$done && ($database["polls"][$data["i"]]["options"][$data["j"]]["points"][] = [
|
||||||
|
"ip" => $ip,
|
||||||
|
"selected" => $data["selected"],
|
||||||
|
"date_in" => $date,
|
||||||
|
"date_last" => $date,
|
||||||
|
"changes" => 1,
|
||||||
|
"deleted" => false
|
||||||
|
]);
|
||||||
|
self::save_database($database);
|
||||||
|
self::response(200, $database["polls"][$data["i"]]["options"]);
|
||||||
|
}else
|
||||||
|
self::response(404, [
|
||||||
|
"message" => "poll_not_exists",
|
||||||
|
"i" => $data["i"],
|
||||||
|
"j" => $data["j"]
|
||||||
|
]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$database = $this->load_database();
|
self::response(404, ["message" => "action_unknown"]);
|
||||||
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;
|
break;
|
||||||
};
|
};
|
||||||
}else
|
}else
|
||||||
$this->response(405, ["message" => "no_action"]);
|
self::response(405, ["message" => "no_action"]);
|
||||||
}else
|
}else
|
||||||
$this->response(405, ["message" => "no_data"]);
|
self::response(405, ["message" => "no_data"]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function get_polls($database){
|
private static function get_polls($database){
|
||||||
return isset($database["polls"]) ? array_map(fn($poll) => ["name" => $poll["name"]], $database["polls"]) : [];
|
return isset($database["polls"]) ? array_map(fn($poll) => ["name" => $poll["name"]], $database["polls"]) : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function response($code, $data){
|
private static function response($code, $data){
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
"ok" => $code == 200,
|
"ok" => $code == 200,
|
||||||
@ -82,12 +120,22 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function load_database(){
|
private static function load_database(){
|
||||||
return file_exists(\GamUsino\Secrets::settings["database_path"]) ? json_decode(file_get_contents(\GamUsino\Secrets::settings["database_path"]), true) : [];
|
return file_exists(\GamUsino\Secrets::settings["database_path"]) ? json_decode(file_get_contents(\GamUsino\Secrets::settings["database_path"]), true) : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function save_database($database){
|
private static function save_database($database){
|
||||||
file_put_contents(\GamUsino\Secrets::settings["database_path"], json_encode($database));
|
file_put_contents(\GamUsino\Secrets::settings["database_path"], json_encode($database));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function get_ip(){
|
||||||
|
foreach(["HTTP_X_REAL_IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR", "REMOTE_ADDR"] as $key){
|
||||||
|
if(!empty($_SERVER[$key]))
|
||||||
|
return explode(",", $_SERVER[$key])[0];
|
||||||
|
if($ips = getenv($key))
|
||||||
|
return explode(",", $ips)[0];
|
||||||
|
};
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
@ -5,13 +5,18 @@ GamUsino = function(custom){
|
|||||||
nulls : false,
|
nulls : false,
|
||||||
default_value : null,
|
default_value : null,
|
||||||
autostart : true,
|
autostart : true,
|
||||||
timeout : 2000
|
timeout : 2000,
|
||||||
|
frames_per_second : 24
|
||||||
},
|
},
|
||||||
settings = {},
|
settings = {},
|
||||||
fragments = {};
|
fragments = {},
|
||||||
|
filter_cache = {};
|
||||||
let started = false,
|
let started = false,
|
||||||
|
my_ip = null,
|
||||||
|
frames_per_second = 24,
|
||||||
ajax_timeout = 2000,
|
ajax_timeout = 2000,
|
||||||
preload_timeout = 2000;
|
preload_timeout = 2000,
|
||||||
|
thread_filter = null;
|
||||||
|
|
||||||
const constructor = () => {
|
const constructor = () => {
|
||||||
|
|
||||||
@ -31,8 +36,10 @@ GamUsino = function(custom){
|
|||||||
|
|
||||||
ajax_timeout = self.settings(["ajax_timeout", "timeout"]);
|
ajax_timeout = self.settings(["ajax_timeout", "timeout"]);
|
||||||
preload_timeout = self.settings(["preload_timeout", "timeout"]);
|
preload_timeout = self.settings(["preload_timeout", "timeout"]);
|
||||||
|
frames_per_second = self.settings(["frames_per_second", "fps"]);
|
||||||
|
|
||||||
self.send({action : "load_polls"}, data => {
|
self.send({action : "load_polls"}, data => {
|
||||||
|
my_ip = data.content.ip;
|
||||||
self.preload(".polls-box>.polls", () => {
|
self.preload(".polls-box>.polls", () => {
|
||||||
|
|
||||||
let html = document.querySelector(".html-structure-fragments").innerHTML,
|
let html = document.querySelector(".html-structure-fragments").innerHTML,
|
||||||
@ -54,7 +61,9 @@ GamUsino = function(custom){
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
build_polls(data.content);
|
build_polls(data.content.polls);
|
||||||
|
|
||||||
|
setTimeout(() => thread_filter = setInterval(thread_filter_method, 1000 / frames_per_second), 1000);
|
||||||
|
|
||||||
});
|
});
|
||||||
end(true);
|
end(true);
|
||||||
@ -213,7 +222,7 @@ GamUsino = function(custom){
|
|||||||
self.send({
|
self.send({
|
||||||
action : "add_poll",
|
action : "add_poll",
|
||||||
name : poll_name
|
name : poll_name
|
||||||
}, data => build_polls(data.content));
|
}, data => data.content.message ? alert(data.content.message) : build_polls(data.content));
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -224,52 +233,75 @@ GamUsino = function(custom){
|
|||||||
|
|
||||||
document.querySelectorAll(".polls-box>.polls [data-seleted=true]").forEach(item => item.setAttribute("data-selected", false));
|
document.querySelectorAll(".polls-box>.polls [data-seleted=true]").forEach(item => item.setAttribute("data-selected", false));
|
||||||
item.setAttribute("data-selected", true);
|
item.setAttribute("data-selected", true);
|
||||||
|
document.querySelector(".poll-box textarea").value = "";
|
||||||
|
|
||||||
self.send({
|
self.send({
|
||||||
action : "get_poll",
|
action : "get_poll",
|
||||||
i : i
|
i : i
|
||||||
}, data => {
|
}, data => document.querySelector(".poll-box>.poll").innerHTML = self.string_variables(fragments.poll_box, {
|
||||||
console.log(data);
|
...data.content,
|
||||||
document.querySelector(".poll-box>.poll").innerHTML = self.string_variables(fragments.poll_box, {
|
i : i,
|
||||||
...data.content,
|
maximum_points : data.content.options.length ? Math.max(...data.content.options.map(option => option.points)) : 0,
|
||||||
i : i,
|
options : create_options_html(data.content.options)
|
||||||
maximum_points : data.content.options.length ? Math.max(...data.content.options.map(option => option.points)) : 0,
|
}));
|
||||||
options : data.content.options.map((option, j) => self.string_variables(fragments.option_item, {
|
|
||||||
...option,
|
|
||||||
i : j
|
|
||||||
})).join("")
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.add_option = (item, event) => {
|
this.add_option = (item, event) => {
|
||||||
|
|
||||||
|
const poll_selected = document.querySelector(".polls-box>.polls [data-selected=true]");
|
||||||
|
|
||||||
|
if(!poll_selected){
|
||||||
|
alert("poll_no_selected");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
const text = item.parentNode.parentNode.querySelector("textarea").value,
|
const text = item.parentNode.parentNode.querySelector("textarea").value,
|
||||||
i = Number(document.querySelector(".polls-box>.polls [data-selected=true]").getAttribute("data-i"));
|
i = Number(poll_selected.getAttribute("data-i"));
|
||||||
|
|
||||||
!isNaN(i) && text && self.confirm("add_option_sure", () => {
|
!isNaN(i) && text && self.confirm("add_option_sure", () => {
|
||||||
self.send({
|
self.send({
|
||||||
action : "add_option",
|
action : "add_option",
|
||||||
i : i,
|
i : i,
|
||||||
text : text
|
text : text
|
||||||
}, data => build_options(data.content));
|
}, data => data.content.message ? alert(data.content.message) : build_options(data.content));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const build_options = options => {
|
const create_options_html = options => {
|
||||||
|
|
||||||
const text = document.querySelector(".poll-box textarea").value.trim();
|
const text = document.querySelector(".poll-box textarea").value.trim();
|
||||||
|
|
||||||
document.querySelector(".poll-box>.poll .options>ul").innerHTML = options.map((option, k) => self.string_variables(fragments.option_item, {
|
return options.map((option, k) => self.string_variables(fragments.option_item, {
|
||||||
...option,
|
...option,
|
||||||
i : k,
|
i : k,
|
||||||
visible : !text || option.text.includes(text)
|
visible : !text || option.text.includes(text),
|
||||||
|
selected : option.points.some(point => my_ip == point.ip && point.selected) ? " checked" : ""
|
||||||
})).join("");
|
})).join("");
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const build_options = options => document.querySelector(".poll-box>.poll .options>ul").innerHTML = create_options_html(options);
|
||||||
|
|
||||||
this.select_option = (item, event) => {};
|
this.select_option = (item, event) => self.send({
|
||||||
|
action : "set_option",
|
||||||
|
i : Number(document.querySelector(".polls-box>.polls [data-selected=true]").getAttribute("data-i")),
|
||||||
|
j : Number(item.parentNode.parentNode.getAttribute("data-i")),
|
||||||
|
selected : item.checked
|
||||||
|
}, data => build_options(data.content));
|
||||||
|
|
||||||
|
const thread_filter_method = () => [
|
||||||
|
["polls", ".polls-box [type=text]", ".polls-box>.polls li"],
|
||||||
|
["poll", ".poll-box textarea", ".poll-box>.poll li"]
|
||||||
|
].forEach(([key, box, items]) => {
|
||||||
|
|
||||||
|
const text = document.querySelector(box).value.trim();
|
||||||
|
|
||||||
|
if(filter_cache[key] != text){
|
||||||
|
filter_cache[key] = text;
|
||||||
|
document.querySelectorAll(items).forEach(item => item.setAttribute("data-visible", !text || item.getAttribute("title").includes(text)));
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
constructor();
|
constructor();
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
|
|
||||||
<style data-type="text/css" data-language="CSS2" data-rel="stylesheet" charset="utf-8">
|
<style data-type="text/css" data-language="CSS2" data-rel="stylesheet" charset="utf-8">
|
||||||
.html-structure-fragments{display : none;}
|
.html-structure-fragments,[data-visible=false]{display : none;}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script data-type="text/javascript" data-lang="ECMAScript 2015" src="ecma/GamUsino.ecma.js" data-crossorigin="anonymous" charset="utf-8"></script>
|
<script data-type="text/javascript" data-lang="ECMAScript 2015" src="ecma/GamUsino.ecma.js" data-crossorigin="anonymous" charset="utf-8"></script>
|
||||||
@ -45,7 +45,7 @@
|
|||||||
<span data-i18n="{text}">{text}</span>
|
<span data-i18n="{text}">{text}</span>
|
||||||
<span class="bar" data-points="0"></span>
|
<span class="bar" data-points="0"></span>
|
||||||
<span class="select checkbox">
|
<span class="select checkbox">
|
||||||
<input type="checkbox" name="option_{i}" onchange="gamusino.select_option(this, event);" />
|
<input type="checkbox" name="option_{i}" onchange="gamusino.select_option(this, event);"{selected} />
|
||||||
<span data-icon="chckbox"></span>
|
<span data-icon="chckbox"></span>
|
||||||
</span>
|
</span>
|
||||||
</li>
|
</li>
|
||||||
|
Loading…
Reference in New Issue
Block a user