From dee8bdfb637a5e380502cb2d4a6b3a7f9b7b6e66 Mon Sep 17 00:00:00 2001 From: KyMAN <0kyman0@gmail.com> Date: Fri, 5 Apr 2024 17:10:30 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Creado=20todo=20el=20entorno=20Core=20b?= =?UTF-8?q?=C3=A1sico.=204=20Jams=20con=20un=20total=20de=202=20horas=20y?= =?UTF-8?q?=2038=20minutos.=20=C2=A1Jajaja!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP/GamUsino.php | 104 +++++++++++++++++++++++++---------- Public/ecma/GamUsino.ecma.js | 80 +++++++++++++++++++-------- Public/index.html | 4 +- 3 files changed, 134 insertions(+), 54 deletions(-) diff --git a/PHP/GamUsino.php b/PHP/GamUsino.php index d306157..e19f733 100644 --- a/PHP/GamUsino.php +++ b/PHP/GamUsino.php @@ -10,68 +10,106 @@ if(isset($data["action"])){ switch($data["action"]){ 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; case "add_poll": - $database = $this->load_database(); + $database = self::load_database(); 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"][] = [ "name" => $data["name"], - "options" => [] + "options" => [], + "ip" => self::get_ip(), + "date" => time(), + "deleted" => false ]; - $this->save_database($database); - $this->response(200, $this->get_polls($database)); + self::save_database($database); + self::response(200, self::get_polls($database)); break; 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; - $this->response($poll ? 200 : 404, $poll ?? [ + self::response($poll ? 200 : 404, $poll ?? [ "message" => "poll_not_exists", "i" => $data["i"] ]); break; case "add_option": - $database = $this->load_database(); + $database = self::load_database(); 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"][] = [ "text" => $data["text"], - "points" => 0 + "points" => [], + "ip" => self::get_ip(), + "date" => time(), + "deleted" => false ]; - $this->save_database($database); - $this->response(200, $database["polls"][$data["i"]]["options"]); + self::save_database($database); + self::response(200, $database["polls"][$data["i"]]["options"]); }else - $this->response(404, [ + self::response(404, [ "message" => "poll_not_exists", "name" => $data["name"] ]); break; 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; 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"] - ]); + self::response(404, ["message" => "action_unknown"]); break; }; }else - $this->response(405, ["message" => "no_action"]); + self::response(405, ["message" => "no_action"]); }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"]) : []; } - private function response($code, $data){ + private static function response($code, $data){ echo json_encode([ "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) : []; } - private function save_database($database){ + private static function save_database($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; + } + }; \ No newline at end of file diff --git a/Public/ecma/GamUsino.ecma.js b/Public/ecma/GamUsino.ecma.js index 9e5defa..8728e4a 100644 --- a/Public/ecma/GamUsino.ecma.js +++ b/Public/ecma/GamUsino.ecma.js @@ -5,13 +5,18 @@ GamUsino = function(custom){ nulls : false, default_value : null, autostart : true, - timeout : 2000 + timeout : 2000, + frames_per_second : 24 }, settings = {}, - fragments = {}; + fragments = {}, + filter_cache = {}; let started = false, + my_ip = null, + frames_per_second = 24, ajax_timeout = 2000, - preload_timeout = 2000; + preload_timeout = 2000, + thread_filter = null; const constructor = () => { @@ -31,8 +36,10 @@ GamUsino = function(custom){ ajax_timeout = self.settings(["ajax_timeout", "timeout"]); preload_timeout = self.settings(["preload_timeout", "timeout"]); + frames_per_second = self.settings(["frames_per_second", "fps"]); self.send({action : "load_polls"}, data => { + my_ip = data.content.ip; self.preload(".polls-box>.polls", () => { 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); @@ -213,7 +222,7 @@ GamUsino = function(custom){ self.send({ action : "add_poll", 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)); item.setAttribute("data-selected", true); + document.querySelector(".poll-box textarea").value = ""; self.send({ action : "get_poll", i : i - }, data => { - console.log(data); - document.querySelector(".poll-box>.poll").innerHTML = self.string_variables(fragments.poll_box, { - ...data.content, - i : i, - 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("") - }); - }); + }, data => document.querySelector(".poll-box>.poll").innerHTML = self.string_variables(fragments.poll_box, { + ...data.content, + i : i, + maximum_points : data.content.options.length ? Math.max(...data.content.options.map(option => option.points)) : 0, + options : create_options_html(data.content.options) + })); }; 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, - 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", () => { self.send({ action : "add_option", i : i, 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(); - 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, 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(""); - }; + + 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(); diff --git a/Public/index.html b/Public/index.html index 904d017..542801e 100644 --- a/Public/index.html +++ b/Public/index.html @@ -6,7 +6,7 @@ @@ -45,7 +45,7 @@ {text} - +