Lee Morgan преди 6 месеца
родител
ревизия
2ab402b0bc
променени са 9 файла, в които са добавени 148 реда и са изтрити 11 реда
  1. BIN
      .app.js.swp
  2. 2 1
      build.html
  3. 2 2
      createHtml.js
  4. 2 1
      package.json
  5. BIN
      views/.index.js.swp
  6. 3 0
      views/build.html
  7. 47 0
      views/index.css
  8. 19 1
      views/index.html
  9. 73 6
      views/index.js

BIN
.app.js.swp


+ 2 - 1
build.html

@@ -1,2 +1,3 @@
-<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Donut Bingo</title> </head> <body> <h1>Hello</h1> <script>(()=>{var s=new WebSocket("ws://localhost:8000/ws"),o={message:"create",p:"12345"};s.onopen=()=>{s.send(JSON.stringify(o))};s.onmessage=e=>{console.log(e.data)};})();
+<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Donut Bingo</title> <style>*{margin:0;padding:0;box-sizing:border-box}body,html{height:100%;width:100%}body{display:flex;flex-direction:column;justify-content:center;align-items:center}#createModal{display:flex;flex-direction:column;width:50%;position:fixed;left:25%;background:#fff;z-index:2}
+</style> </head> <body> <h1>Donut Bingo</h1> <button onclick="createModal()">Create Game</button> <form id="createModal" style="display:none"> <label>Game Name: <input type="text" id="createName" required> </label> <label>Password: <input type="password" id="createPass" required> </label> <label>Bingo options (comma seperated): <input type="text" id="createOptions" required> </label></form> <script>(()=>{var e=new WebSocket("ws://localhost:8000/ws"),s={message:"create",p:"12345"};e.onopen=()=>{e.send(JSON.stringify(s))};e.onmessage=o=>{console.log(o.data)};})();
 </script> </body> </html> 

+ 2 - 2
createHtml.js

@@ -36,10 +36,10 @@ export default async ()=>{
     });
 
     data = data.replace('<script></script>', `<script>${js}</script>`);
-    data = data.replace('<style></stryle>', `<style>${css}</style>`);
+    data = data.replace('<style></style>', `<style>${css}</style>`);
 
     if(process.env.NODE_ENV !== "production"){
-        fs.writeFile(`${import.meta.dirname}/build.html`, data);
+        fs.writeFile(`${import.meta.dirname}/views/build.html`, data);
     }
 
     return data;

+ 2 - 1
package.json

@@ -15,7 +15,8 @@
   "type": "module",
   "main": "app.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "dev": "nodemon app.js --ignore views/build.html --ext js,css,html"
   },
   "dependencies": {
     "compression": "^1.8.1",

BIN
views/.index.js.swp


+ 3 - 0
views/build.html

@@ -0,0 +1,3 @@
+<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Donut Bingo</title> <style>*{margin:0;padding:0;box-sizing:border-box}body,html{height:100%;width:100%}body{display:flex;flex-direction:column;justify-content:center;align-items:center}.contents{display:flex;justify-content:center;width:100%;position:relative}#inputs{display:flex;flex-direction:column;position:absolute;left:35px;top:35px}table{border-collapse:collapse}td{height:125px;width:125px;border:1px solid black;position:relative}.cross{height:100%;width:100%;position:absolute;top:0;left:0}
+</style> </head> <body> <h1>Donut Bingo</h1> <div class="contents"> <div id="inputs"> <button id="saveInputs">Save</button> </div> <button id="start">Start</button> <div class="bingo"> <table> <tbody id="tbody"></tbody> </table> </div> </div> <svg class="cross" id="cross" style="display:none" width="24px" height="24px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="#000000"> <path d="M6.75827 17.2426L12.0009 12M17.2435 6.75736L12.0009 12M12.0009 12L6.75827 6.75736M12.0009 12L17.2435 17.2426" stroke="#ff0000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path> </svg> <script>(()=>{var c=document.getElementById("inputs"),s=document.getElementById("tbody"),o=[],i=localStorage.getItem("inputs");i?i=i.split(","):i=[];for(let t=0;t<25;t++){let e=document.createElement("input");e.type="text",i[t]&&(e.value=i[t]),c.appendChild(e)}for(let t=0;t<5;t++){let e=document.createElement("tr");for(let n=0;n<5;n++){let l=document.createElement("td");t===2&&n==2&&(l.textContent="Free"),e.appendChild(l),o.push(l)}s.appendChild(e)}var d=t=>{for(let e=t.length-1;e>0;e--){let n=Math.floor(Math.random()*(e+1));[t[e],t[n]]=[t[n],t[e]]}};document.getElementById("saveInputs").addEventListener("click",()=>{let t=document.querySelectorAll("#inputs input"),e="";for(let n=0;n<t.length;n++)t[n].value!==""&&(e+=`${t[n].value},`);e[e.length-1]===","&&(e=e.slice(0,-1)),localStorage.setItem("inputs",e)});document.getElementById("start").addEventListener("click",()=>{let t=document.querySelectorAll("#inputs input"),e=[];for(let l=0;l<t.length;l++)e.push(t[l].value);d(e);let n=document.querySelectorAll("#tbody td");for(let l=0;l<n.length;l++)n[l].textContent=e[l]});for(let t=0;t<o.length;t++)o[t].addEventListener("click",()=>{let e=o[t].querySelector("svg");if(e)o[t].removeChild(e);else{let n=document.getElementById("cross");n=n.cloneNode(!0),n.removeAttribute("id"),n.style.display="block",o[t].appendChild(n)}});})();
+</script> </body> </html> 

+ 47 - 0
views/index.css

@@ -0,0 +1,47 @@
+*{margin:0;padding:0;box-sizing:border-box;}
+
+body, html{
+    height: 100%;
+    width: 100%;
+}
+
+body{
+    display: flex;
+    flex-direction: column;
+    justify-content: center;
+    align-items: center;
+}
+
+.contents{
+    display: flex;
+    justify-content: center;
+    width: 100%;
+    position: relative;
+}
+
+#inputs{
+    display: flex;
+    flex-direction: column;
+    position: absolute;
+    left: 35px;
+    top: 35px;
+}
+
+table{
+    border-collapse: collapse;
+}
+
+td{
+    height: 125px;
+    width: 125px;
+    border: 1px solid black;
+    position: relative;
+}
+
+.cross{
+    height: 100%;
+    width: 100%;
+    position: absolute;
+    top: 0;
+    left: 0;
+}

+ 19 - 1
views/index.html

@@ -3,9 +3,27 @@
     <head>
         <meta charset="utf-8">
         <title>Donut Bingo</title>
+        <style></style>
     </head>
     <body>
-        <h1>Hello</h1>
+        <h1>Donut Bingo</h1>
+
+        <div class="contents">
+            <div id="inputs">
+                <button id="saveInputs">Save</button>
+            </div>
+
+            <button id="start">Start</button>
+            <div class="bingo">
+                <table>
+                    <tbody id="tbody"></tbody>
+                </table>
+            </div>
+        </div>
+
+        <svg class="cross" id="cross" style="display:none" width="24px" height="24px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="#000000">
+            <path d="M6.75827 17.2426L12.0009 12M17.2435 6.75736L12.0009 12M12.0009 12L6.75827 6.75736M12.0009 12L17.2435 17.2426" stroke="#ff0000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
+        </svg>
 
         <script></script>
     </body>

+ 73 - 6
views/index.js

@@ -1,7 +1,74 @@
-const ws = new WebSocket("ws://localhost:8000/ws");
-const data = {
-    message: "create",
-    p: "12345",
+const inputDiv = document.getElementById("inputs");
+const tbody = document.getElementById("tbody");
+const blocks = [];
+let inputs = localStorage.getItem("inputs");
+
+if(inputs){
+    inputs = inputs.split(",");
+}else{
+    inputs = [];
+}
+
+for(let i = 0; i < 25; i++){
+    const input = document.createElement("input");
+    input.type = "text";
+    if(inputs[i]) input.value = inputs[i];
+    inputDiv.appendChild(input);
+}
+
+for(let i = 0; i < 5; i++){
+    const tr = document.createElement("tr");
+    for(let j = 0; j < 5; j++){
+        const td = document.createElement("td");
+        if(i === 2 && j == 2) td.textContent = "Free";
+        tr.appendChild(td);
+        blocks.push(td);
+    }
+    tbody.appendChild(tr);
+}
+
+const shuffle = (arr)=>{
+    for (let i = arr.length - 1; i > 0; i--) {
+        const j = Math.floor(Math.random() * (i + 1));
+        [arr[i], arr[j]] = [arr[j], arr[i]];
+    }
 };
-ws.onopen = ()=>{ws.send(JSON.stringify(data))};
-ws.onmessage = (m)=>{console.log(m.data)};
+
+document.getElementById("saveInputs").addEventListener("click", ()=>{
+    const inputs = document.querySelectorAll("#inputs input");
+    let inputString = "";
+    for(let i = 0; i < inputs.length; i++){
+        if(inputs[i].value === "") continue;
+        inputString += `${inputs[i].value},`;
+    }
+    if(inputString[inputString.length-1] === ",") inputString = inputString.slice(0, -1);
+    localStorage.setItem("inputs", inputString);
+});
+
+document.getElementById("start").addEventListener("click", ()=>{
+    const inputElems = document.querySelectorAll("#inputs input");
+    const inputs = [];
+    for(let i = 0; i < inputElems.length; i++){
+        inputs.push(inputElems[i].value);
+    }
+    shuffle(inputs);
+    const tds = document.querySelectorAll("#tbody td");
+    for(let i = 0; i < tds.length; i++){
+        tds[i].textContent = inputs[i];
+    }
+});
+
+for(let i = 0; i < blocks.length; i++){
+    blocks[i].addEventListener("click", ()=>{
+        let svg = blocks[i].querySelector("svg");
+        if(svg){
+            blocks[i].removeChild(svg);
+        }else{
+            let cross = document.getElementById("cross");
+            cross = cross.cloneNode(true);
+            cross.removeAttribute("id");
+            cross.style.display = "block";
+            blocks[i].appendChild(cross);
+        }
+    });
+}