From 3d79c2cf16ffb4eb907da136891ffdaf6bdb2976 Mon Sep 17 00:00:00 2001 From: KyMAN <0kyman0@gmail.com> Date: Fri, 5 Apr 2024 15:19:43 +0200 Subject: [PATCH] =?UTF-8?q?#wip:=20Segunda=20round=20de=20la=20Jam.=20Carg?= =?UTF-8?q?a=20de=20Polls=20casi=20completada.=20Queda=20selecci=C3=B3n.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP/GamUsino.php | 13 +++- Public/ecma/GamUsino.ecma.js | 134 +++++++++++++++++++++++++++++++++-- Public/index.html | 19 ++++- 3 files changed, 154 insertions(+), 12 deletions(-) diff --git a/PHP/GamUsino.php b/PHP/GamUsino.php index d0fc418..b891c4c 100644 --- a/PHP/GamUsino.php +++ b/PHP/GamUsino.php @@ -9,6 +9,9 @@ if($data){ if(isset($data["action"])){ switch($data["action"]){ + case "load_polls": + $this->response(200, $this->get_polls($this->load_database())); + break; case "add_poll": $database = $this->load_database(); isset($database["polls"]) || ($database["polls"] = []); @@ -17,14 +20,14 @@ "options" => [] ]; $this->save_database($database); - $this->response(200, $database["polls"]); + $this->response(200, $this->get_polls($database)); break; case "get_poll": $database = $this->load_database(); - $poll = isset($database["polls"]) && isset($database["polls"][$data["name"]]) ? $database["polls"][$data["name"]] : null; + $poll = isset($database["polls"]) && isset($database["polls"][$data["i"]]) ? $database["polls"][$data["i"]] : null; $this->response($poll ? 200 : 404, $poll ?? [ "message" => "poll_not_exists", - "name" => $data["name"] + "i" => $data["i"] ]); break; case "add_option": @@ -64,6 +67,10 @@ } + 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([ diff --git a/Public/ecma/GamUsino.ecma.js b/Public/ecma/GamUsino.ecma.js index 9195f91..5507dea 100644 --- a/Public/ecma/GamUsino.ecma.js +++ b/Public/ecma/GamUsino.ecma.js @@ -7,11 +7,17 @@ GamUsino = function(custom){ autostart : true, timeout : 2000 }, - settings = {}; + settings = {}, + fragments = {}; let started = false, - ajax_timeout = 2000; + ajax_timeout = 2000, + preload_timeout = 2000; - const constructor = () => {}; + const constructor = () => { + + self.settings("autostart") && self.start(); + + }; this.start = callback => { @@ -24,8 +30,35 @@ GamUsino = function(custom){ started = true; ajax_timeout = self.settings(["ajax_timeout", "timeout"]); + preload_timeout = self.settings(["preload_timeout", "timeout"]); - end(true); + self.send({action : "load_polls"}, data => { + self.preload(".polls-box>.polls", () => { + + let html = document.querySelector(".html-structure-fragments").innerHTML, + matches; + + while(matches = html.match(/<\!\-{2}\s*\[{2}([^\[\]]+)\]{2}\s*\-{2}>/)){ + + const key = matches[1]; + let l; + + html = html.substring(matches.index + matches[0].length); + + if((l = html.indexOf(matches[0])) == -1) + continue; + + fragments[key] = html.substring(0, l); + + html = html.substring(l + matches[0].length); + + }; + + build_polls(data.content); + + }); + end(true); + }); return true; }; @@ -36,7 +69,7 @@ GamUsino = function(custom){ this.settings = (keys, inputs, _default, nulls) => { - const m = (keys = (typeof keys == "object" && keys instanceof Array ? keys : keys).filter(key => key && typeof key == "string")).length; + const m = (keys = (typeof keys == "object" && keys instanceof Array ? keys : [keys]).filter(key => key && typeof key == "string")).length; if(m){ @@ -58,7 +91,24 @@ GamUsino = function(custom){ let ended = false; const ajax = new XMLHttpRequest(), date = Date.now(), - end = error => !ended && (ended = true) && typeof callback == "function" && callback(ajax.responseText && JSON.parse(ajax.responseText) || ajax.responseText, ajax.status, ajax.readyState, error == "OK", error); + end = error => { + if(ended) + return; + ended = true; + if(typeof callback != "function") + return; + + let data = ajax.responseText; + + try{ + data = JSON.parse(data); + }catch(exception){ + data = ajax.responseText; + }; + + callback(data, ajax.status, ajax.readyState, error == "OK", error); + + }; ajax.open("post", "/api.php", true); ajax.timeout = ajax_timeout; @@ -104,6 +154,78 @@ GamUsino = function(custom){ }; + this.preload = (selector, callback) => { + if(!selector){ + callback(null, false); + return; + }; + if(selector.nodeName || selector.tagName){ + callback(selector, false); + return; + }; + if(typeof selector != "string"){ + callback(false, false); + return; + }; + + let item; + + try{ + if(item = document.querySelector(selector)){ + callback(item, false); + return; + }; + }catch(exception){ + callback(null, false); + return; + }; + + const date = Date.now(); + let preload = setInterval(() => { + if(item = document.querySelector(selector)){ + clearInterval(preload); + callback(item, true); + return; + }else if(Date.now() - date > preload_timeout){ + clearInterval(preload); + callback(null, true); + return; + }; + }, 1000 / frames_per_second); + + }; + + this.string_variables = (string, variables, _default) => { + + const l = (variables = ( + typeof variables == "object" ? variables instanceof Array ? variables : [variables] : [] + ).filter(subset => subset && typeof subset == "object")).length; + + return string.replace(/\{([^\{\}]+)\}/g, (all, key) => { + for(let i = 0; i < l; i ++) + if(variables[i][key] !== undefined) + return variables[i][key]; + return _default !== undefined ? _default : all; + }); + }; + + const build_polls = polls => document.querySelector(".polls-box>.polls").innerHTML = polls.map((poll, i) => self.string_variables(fragments.poll_menu_fragment, { + ...poll, + i : i + })).join(""); + + this.show_poll = (item, event) => { + document.querySelectorAll(".polls-box>.polls [data-seleted=true]").forEach(item => item.setAttribute("data-selected", false)); + item.setAttribute("data-selected", true); + self.send({ + action : "get_poll", + i : Number(item.getAttribute("data-i")) + }, data => { + console.log(data); + document.querySelector(".poll-box>.poll").innerHTML = data.content.name; + }); + }; + constructor(); }; \ No newline at end of file diff --git a/Public/index.html b/Public/index.html index 5f2c486..6d14cf7 100644 --- a/Public/index.html +++ b/Public/index.html @@ -5,6 +5,10 @@ + +