2048 grid edit

2048 level editor

  1. // ==UserScript==
  2. // @name 2048 grid edit
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 2048 level editor
  6. // @author pigPen
  7. // @match http*://play2048.co/*
  8. // @icon https://www.google.com/s2/favicons?domain=play2048.co
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. function c(n){
  14. let s = n;
  15. let x = 0
  16. while (n > 2){
  17. s+=n*2;
  18. n/=2;
  19. x+=1
  20. }
  21. s+=x*2.4;
  22. return s;
  23. }
  24. 'use strict';
  25. if (location.pathname == "/edit"){
  26. let grid = JSON.parse( localStorage.getItem("gameState")).grid.cells;
  27.  
  28.  
  29.  
  30. document.querySelector(".container").remove();
  31. let center = document.createElement("center");
  32.  
  33. let header = document.createElement("h1");
  34. header.innerHTML = "?";
  35. header.setAttribute("id", "score");
  36. center.appendChild(header);
  37. let table = document.createElement("table");
  38. for (let x = 0; x < 4; x++){
  39. let row = document.createElement("tr");
  40. for (let y = 0; y < 4; y++){
  41. let thisslot = grid[y][x];
  42. let slot = document.createElement("td");
  43. let text = document.createElement("textarea");
  44. if (thisslot == undefined)
  45. text.value = "";
  46. else
  47. text.value = thisslot.value
  48. text.style.width = "100px";
  49. text.style.height = "100px";
  50. text.style.resize = "none";
  51. text.style.fontSize = "20px";
  52. text.style.textAlign = "center";
  53. //text.setAttribute("maxlength", 6);
  54. text.setAttribute("id", y+"x"+x)
  55. text.oninput = ()=>{
  56. let score = 0;
  57. for (let tarr of document.querySelectorAll("textarea"))
  58. if (!isNaN(parseInt(tarr.value)))
  59. score+=c(parseInt(tarr.value));
  60.  
  61.  
  62. document.querySelector("#score").innerHTML = "Score: " + score;
  63. };
  64. //text.setAttribute("readonly", true);
  65. slot.appendChild(text);
  66. row.appendChild(slot);
  67. }
  68. table.appendChild(row);
  69. }
  70. center.appendChild(table);
  71.  
  72. let booleanAttrs = [["Is won", "won"], ["Is over", "over"], ["Keep Playing", "keepPlaying"]];
  73. for (let i = 0; i<booleanAttrs.length; i++){
  74. center.appendChild(document.createElement("br"));
  75. let label = document.createElement("label");
  76. label.setAttribute("for", booleanAttrs[i][1]);
  77. label.innerHTML = booleanAttrs[i][0];
  78. center.appendChild(label);
  79. let check = document.createElement("input");
  80. check.setAttribute("id", booleanAttrs[i][1]);
  81. check.setAttribute("type", "checkbox");
  82. check.checked=JSON.parse( localStorage.getItem("gameState"))[booleanAttrs[i][1]];
  83. center.appendChild(check);
  84. document.querySelector("body").appendChild(center);
  85. document.querySelector("textarea").oninput();
  86. }
  87. center.appendChild(document.createElement("br"));
  88.  
  89.  
  90. let btn = document.createElement("button");
  91. btn.classList.add("btn");
  92. btn.classList.add("btn-primary");
  93. btn.innerHTML = "Save";
  94. btn.style.fontSize = "20px";
  95. center.appendChild(btn);
  96. btn.onclick = ()=>{
  97. let ngrid = [];
  98. let newfile = JSON.parse( localStorage.getItem("gameState"))
  99. for (let y = 0; y < 4; y++){
  100. let colum = [];
  101. for (let x = 0; x < 4; x++){
  102. let score = parseInt(document.getElementById(""+y+"x"+x).value);
  103. if (isNaN(score))
  104. colum.push(undefined);
  105. else
  106. colum.push({position:{x:y, y:x}, value:score});
  107. }ngrid.push(colum);
  108.  
  109. }
  110. newfile.grid = {size: 4,cells: ngrid};
  111. newfile.score = parseInt(document.querySelector("h1").innerHTML.split(" ")[1]);
  112. console.log(JSON.stringify(newfile));
  113. for (let attr of booleanAttrs){
  114. newfile[attr[1]] = document.getElementById(attr[1]).checked;
  115. }
  116. console.log(JSON.stringify(newfile));
  117. localStorage.setItem("gameState", JSON.stringify(newfile));
  118. };
  119. btn = document.createElement("button");
  120. btn.classList.add("btn");
  121. btn.classList.add("btn-primary");
  122. btn.innerHTML = "Reset";
  123. btn.style.fontSize = "20px";
  124. center.appendChild(btn);
  125. btn.onclick = ()=>{
  126. if (confirm("Are you sure you wish to reset?")){
  127. localStorage.removeItem("gameState");
  128. history.back()
  129. // location.reload();
  130. }
  131. }
  132.  
  133. }
  134. if (location.pathname == "/"){
  135. localStorage.removeItem = (r)=>{};
  136.  
  137.  
  138. let center = document.createElement("center");
  139. let span = document.createElement("button");
  140. span.classList.add("pp-donate");
  141. span.style.fontSize = "25px";
  142. span.innerHTML = "<a href='/edit'>Save editor</a>";
  143. center.appendChild(span);
  144.  
  145. document.querySelector("footer.links").appendChild(document.createElement("br"));
  146. document.querySelector("footer.links").appendChild(document.createElement("br"));
  147. document.querySelector("footer.links").appendChild(document.createElement("br"));
  148. document.querySelector("footer.links").appendChild(document.createElement("br"));
  149. document.querySelector("footer.links").appendChild(center);
  150. `
  151. span = document.createElement("button");
  152. span.classList.add("pp-donate");
  153. span.style.fontSize = "25px";
  154. span.innerHTML = "Save";
  155. document.querySelector(".game-message").appendChild(span);
  156. `
  157.  
  158. }
  159. })();