Keyboard Display Script

shows keyboard inputs on screen

  1. // ==UserScript==
  2. // @name Keyboard Display Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.25.4.5
  5. // @description shows keyboard inputs on screen
  6. // @author Oki and meppydc
  7. // @match https://*.jstris.jezevec10.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. /**************************
  12. Keyboard Display Script
  13. **************************/
  14. (function() {
  15. window.addEventListener('load', function() {
  16.  
  17. //Only display the keyboard if either in a Game or Replay
  18. if (typeof Game != "undefined" || typeof Replayer != "undefined") {
  19. var nameR = Math.floor(Math.random() * (2 - 0 + 1) + 0)
  20. var names = ['jez', 'oki', 'mep']
  21. //positioning stuff: fiddle around with the positioning
  22.  
  23. //in game
  24. let gameLeft = -300
  25. let gameTop = 100
  26.  
  27. //replays
  28. let replayRight = 200
  29. let replayTop = 200
  30.  
  31. //names[nameR] is replacable with a string
  32.  
  33. //labels are 0 1 2 3
  34. var labels = ['180','SD', 'HD', 'CCW',
  35. //4 5 6 7 8 9
  36. 'HL', 'CW', names[nameR], '<', 'v', '>']
  37.  
  38. let kbdisplay = {left:7,right:9,sd:1,hd:2,ccw:3,cw:5,hold:4,180:0,reset:6,new:8}
  39.  
  40. //each number here corresponds to the label of the corresponding number (ex: 180 is set to 0 so label 0 will be highlighted when 180 key is pressed
  41. //make sure that each number in `kbdisplay` is unique and points to the right label
  42.  
  43.  
  44. //don't scroll to the bottom
  45.  
  46. //nothing important after
  47. // left right sd hd ccw cw hold 180 reset new
  48. //order is: [6, 8, 1, 2, 3, 5, 4, 0] (.22)
  49. //order is: [7, 9, 8, 2, 5, 6, 1, 4] (.23)
  50. //order is: [7, 9, 8, 2, 5, 6, 1, 4] (.24)
  51. //order is: [7, 9, 8, 2, 5, 6, 1, 4, 0 3] (.24.1)
  52. //order is: [7, 9, 8, 3, 5, 6, 2, 4, 0 1] (.24.2)
  53. //0.25 added crap ton of variables, maybe use dictionary instead?
  54. //0.25.1 enum
  55.  
  56.  
  57. //var highlight = [[6,2],[8,2],[6,0],[8,0],[3,2],[5,2],[0,2],[2,2],[1,2],,[4,2]][this['actions'][this['ptr']]["a"]] //reference
  58.  
  59.  
  60.  
  61.  
  62. if (typeof getParams != "function") {
  63. var getParams = a => {
  64. var params = a.slice(a.indexOf("(") + 1);
  65. params = params.substr(0, params.indexOf(")")).split(",");
  66. return params
  67. }
  68. }
  69. if (typeof trim != "function") {
  70. var trim = a => {
  71. a = a.slice(0, -1);
  72. a = a.substr(a.indexOf("{") + 1);
  73. return a
  74. }
  75. }
  76.  
  77. //Create the "keyboard holder". It's a div positioned where the keyboard will be, but it doesnt contain anything yet.
  78. var kbhold = document.createElement("div");
  79. kbhold.id = "keyboardHolder";
  80. kbhold.style.position = "absolute"
  81. //Im trying to position it relative to the main canvas (this doesnt really work well...)
  82. kbhold.style.left = (myCanvas.getBoundingClientRect().left + gameLeft) + "px";
  83. kbhold.style.top = (myCanvas.getBoundingClientRect().top + gameTop) + "px";
  84.  
  85. //Helper method for keyboards in replays
  86. if (typeof Replayer != "undefined" && typeof Game == "undefined") {
  87. Replayer["pressKey"] = function(num, type) {
  88. //console.log(num)
  89. //type: 0=release 1=down 2=press
  90. //Highlights the corresponding key
  91. //gets array of all "kbkey" classes and takes cell "num" and hightlights it
  92. document.getElementsByClassName("kbkey")[num].style.backgroundColor = type ? "lightgoldenrodyellow" : ""
  93. if (type == 2) {
  94. //from replay data you dont really know how long a key has been pressed. im using 100ms as a default
  95. setTimeout(x => {
  96. document.getElementsByClassName("kbkey")[num].style.backgroundColor = ""
  97. }, 20)
  98. }
  99.  
  100. }
  101. //positions the keyboard holder differently for replays (thanks to meppydc)
  102. kbhold.style.left = (myCanvas.getBoundingClientRect().right + replayRight) + "px";
  103. kbhold.style.top = (myCanvas.getBoundingClientRect().top + replayTop) + "px";
  104. }
  105.  
  106. document.body.appendChild(kbhold);
  107.  
  108.  
  109. //(important)
  110. //this is what is pasted into the keyboard holder and makes up the entire visual keyboard.
  111. //(i decompressed and tidied it up a bit)
  112. //basically it's a table consisting of 2 rows and 6 columns
  113. //maybe read up on css tables if you wanna add new cells
  114.  
  115. //nah
  116.  
  117.  
  118. f = `
  119.  
  120. <style>
  121. #kbo {text-align:center;position: absolute;font-size:15px;}
  122. #kbo .tg {border-collapse:collapse;border-spacing:0;color:red;}
  123. #kbo .tg td{padding:10px 5px;border-style:solid;border-width:2px;}
  124. #kbo .tg th{padding:10px 5px;border-style:solid;border-width:2px;}
  125. #kbo .tg .tg-wp8o{border-color:#000000;border:inherit;}
  126. #kbo .tg .tg-tc3e{border-color:#34ff34;}
  127. #kbo .tg .tg-jy2k{border-color:#f8a102;}
  128. #kbo .tg .tg-p39m{border-color:#f8ff00;}
  129. #kbo .kbkey {background-color:black;}
  130. </style>
  131.  
  132. <div id=\"kbo\"><div id=\"kps\"></div>
  133. <table class=\"tg\">
  134. <tr>
  135. <td class=\"tg-tc3e kbkey\">${labels[0]}</td>
  136. <td class=\"tg-tc3e kbkey\">${labels[1]}</td>
  137. <td class=\"tg-tc3e kbkey\">${labels[2]}</td>
  138. <td class=\"tg-wp8o\"></td>
  139. <td class=\"tg-jy2k kbkey\">${labels[3]}</td>
  140. <td class=\"tg-wp8o\"></td>
  141. </tr>
  142. <tr>
  143. <td class=\"tg-p39m kbkey\">${labels[4]}</td>
  144. <td class=\"tg-p39m kbkey\">${labels[5]}</td>
  145. <td class=\"tg-p39m kbkey\">${labels[6]}</td>
  146. <td class=\"tg-jy2k kbkey\">${labels[7]}</td>
  147. <td class=\"tg-jy2k kbkey\">${labels[8]}</td>
  148. <td class=\"tg-jy2k kbkey\">${labels[9]}</td>
  149. </tr>
  150. </table>
  151. </div>
  152. `
  153. keyboardHolder.innerHTML = f
  154.  
  155. //keyboard if in game (not replays)********************************************************************************************************************************
  156. if (typeof Game != "undefined") {
  157.  
  158. document['addEventListener']('keydown', press);
  159. document['addEventListener']('keyup', press);
  160.  
  161. function press(e) {
  162. if (~Game['set2ings'].indexOf(e.keyCode)) {
  163.  
  164. //lol
  165.  
  166. //console.log(Game['set2ings'])//displays the keycodes of your controls
  167. // left right sd hd ccw cw hold 180
  168. //(important)
  169. //listens to pressed keys and highlights the corresponsing div.
  170. //the magic array converts between the actions and the div that is highlighted.
  171. //If you change the order of the boxes, you might also have to adjust the numbers in this array
  172. // left right sd hd ccw cw hold 180 reset new
  173. //order is: [6, 8, 1, 2, 3, 5, 4, 0] (.22 order)
  174. //order is: [7, 9, 8, 2, 5, 6, 1, 4] (.23 order)
  175. //order is: [7, 9, 8, 2, 5, 6, 1, 4] (.24 order)
  176. //order is: [7, 9, 8, 2, 5, 6, 1, 4, 0 3] (.24.1 order)
  177. //order is: [7, 9, 8, 3, 5, 6, 2, 4, 0 1] (.24.2 order)
  178. var corresponding = [kbdisplay.left, kbdisplay.right, kbdisplay.sd, kbdisplay.hd, kbdisplay.ccw, kbdisplay.cw, kbdisplay.hold, kbdisplay['180'], kbdisplay.reset, kbdisplay.new][Game['set2ings'].indexOf(e.keyCode)]
  179. document.getElementsByClassName("kbkey")[corresponding].style.backgroundColor = ["lightgoldenrodyellow", ""][+(e.type == "keyup")]
  180. }
  181. }
  182.  
  183. //This saves the settings array (which maps all actions in jstris to their keycodes) to a global variable for me to use (called Game['se2ings'])
  184. //resets every time a new game is started
  185. var set2ings = Game['prototype']['readyGo'].toString()
  186. set2ings = "Game['set2ings']=this.Settings.controls;" + trim(set2ings)
  187. Game['prototype']['readyGo'] = new Function(set2ings);
  188.  
  189. //calculates kps from kpp and pps and writes it into the kps element
  190. var updateTextBarFunc = Game['prototype']['updateTextBar'].toString()
  191. updateTextBarFunc = trim(updateTextBarFunc) + ";kps.innerHTML='KPS: '+(this.getKPP()*this.placedBlocks/this.clock).toFixed(2)"
  192. Game['prototype']['updateTextBar'] = new Function(updateTextBarFunc);
  193. } else {
  194.  
  195. //else: we're in a replay********************************************************************************************************************************
  196.  
  197. var website = "jstris.jezevec10.com"
  198. var url = window.location.href
  199. var parts = url.split("/")
  200.  
  201. if (parts[3] == "replay" && parts[2].endsWith(website)) {
  202.  
  203. //making a web request for the sole purpose of getting the DAS that was used for the replay
  204. //(can be done more elegantly)
  205. fetch("https://" + parts[2] + "/replay/data?id=" + (parts.length == 6 ? (parts[5] + "&live=1") : (parts[4])))
  206. .then(function(response) {
  207. return response.json();
  208. })
  209. .then(function(jsonResponse) {
  210. var das = jsonResponse.c.das
  211. var playT = Replayer['prototype']['playUntilTime'].toString()
  212. var playTparams = getParams(playT);
  213.  
  214.  
  215. //going though the list of actions done in the replay, and translating that into the timings for highlighting the divs
  216. //i dont rememer how exactly i did this but it's not pretty
  217. var insert1 = `
  218. kps.innerHTML="KPS: "+(this.getKPP()*this.placedBlocks/(this.clock/1000)).toFixed(2)
  219. this["delayedActions"] = []
  220. for (var i = 0; i < this["actions"].length; i++) {
  221. var action = JSON.parse(JSON.stringify(this["actions"][i]));
  222. if(action.a == 2 || action.a == 3){
  223. action.t = (action.t-` + das + `)>0 ? (action.t-` + das + `) : 0
  224. }
  225. this["delayedActions"].push(action)
  226. }
  227.  
  228. this["delayedActions"].sort(function(a, b) {
  229. return a.t - b.t;
  230. });
  231.  
  232. var oldVals = [this["timer"],this["ptr"]]
  233.  
  234. while (` + playTparams[0] + ` >= this['delayedActions'][this['ptr']]['t']) {
  235. if (this['ptr']) {
  236. this['timer'] += (this['delayedActions'][this['ptr']]['t'] - this['delayedActions'][this['ptr'] - 1]['t']) / 1000
  237. };
  238. if(this['delayedActions'][this['ptr']]["a"] == 2){
  239. Replayer["pressKey"](${kbdisplay.left},1)
  240. }
  241. if(this['delayedActions'][this['ptr']]["a"] == 3){
  242. Replayer["pressKey"](${kbdisplay.right},1)
  243. }
  244.  
  245. this['ptr']++;
  246. if (this['delayedActions']['length'] === this['ptr']) {
  247. this['reachedEnd'] = true;
  248. break
  249. }
  250. };
  251.  
  252. this["timer"] = oldVals[0]
  253. this["ptr"] = oldVals[1]`
  254.  
  255.  
  256. //this maps a specific action (this['actions'][this['ptr']]["a"]) to what to do with the divs
  257. //the list of what action code equals what action is in that one harddrop forum post
  258. //for example, if you DAS_LEFT then left will be held down.
  259. //You might also adjust the numbers here (the first number in the pairs. remap them the same as in the magic array)
  260. var insert2 = `
  261. var highlight = [[${kbdisplay.left},2],[${kbdisplay.right},2],[${kbdisplay.left},0],[${kbdisplay.right},0],[${kbdisplay.ccw},2],[${kbdisplay.cw},2],[${kbdisplay['180']},2],[${kbdisplay.hd},2],[${kbdisplay.sd},2],,[${kbdisplay.hold},2]][this['actions'][this['ptr']]["a"]]
  262. if(highlight){
  263. Replayer["pressKey"](...highlight)
  264. };
  265. `
  266.  
  267. playT = playT.replace(";", insert1 + ";")
  268. playT = playT.replace("1000};", "1000};" + insert2)
  269. Replayer['prototype']['playUntilTime'] = new Function(...playTparams, trim(playT));
  270.  
  271. //i hate myself - Oki
  272. //I heard jez whips Oki - meppydc
  273. //I hate myself as well once it stopped working when I did basically nothing - meppydc
  274. //typing var names sucks - meppydc
  275. //typos make me sad - meppydc
  276. });
  277. }
  278. }
  279. }
  280. });
  281. })();