Guest User

Neomacro (slightly) improved

a guest
Feb 1st, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1. // ==UserScript==
  2. // @name TagPro Neomacro
  3. // @namespace http://tiny.cc/neomacro
  4. // @description Based on https://gist.github.com/steppin/5292526 by http://www.reddit.com/user/contact_lens_linux/
  5. // @include http://tagpro-*.koalabeast.com:*
  6. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  7. // @author Popcorn
  8. // @version 1.3
  9. // ==/UserScript==
  10.  
  11. // Body of this function is injected into the HTML page
  12. function Script()
  13. {
  14. /**************************
  15. * *
  16. * START OF CONFIG SECTION *
  17. * *
  18. **************************/
  19.  
  20. // These are JS keycodes (event.keyCode) with corresponding character
  21. // Find them here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
  22. var buttons = {
  23. 65:"←",
  24. 87:"↑",
  25. 68:"→",
  26. 83:"↓",
  27. 81:"●",
  28. 69:"0"
  29. };
  30.  
  31. // All generated messages are prefixed with this
  32. var messagePrefix = "➜ ";
  33.  
  34. // Maximal interval beteween two keypresses (ms)
  35. var keypressLimit = 1000;
  36.  
  37. // Minimal interval beteween two messages sent (ms)
  38. var messageLimit = 300;
  39.  
  40. // All macros support random messages - for example, you can replace
  41. // "↑↓↑": "Yes"
  42. // with
  43. // "↑↓↑": ["Yes", "Yeah", "Yea", "Whatever..."]
  44.  
  45. // Macros sent to your team only (the T-key chat)
  46. var teamMacros = {
  47. // Yes/No
  48. "←→←": "No",
  49. "↑↓↑": "Yes",
  50.  
  51. // Indicative sentences
  52. "↓●↑": "I'll attack!",
  53. "↓●↓": "I'll defend!",
  54. "↓●←": "I'll trick the enemies!",
  55. "↓●→": "I'll control the button!",
  56. "↑●↑": "Enemy's attacking!",
  57. "↑●↓": "Enemy's defending!",
  58. "↑●←": "Enemy's trying to trick us!",
  59. "↑●→": "Enemy's controlling the button!",
  60.  
  61. // Questions
  62. "●↓" : "Is our flag safe?",
  63. "●↑" : "Where is enemy FC?",
  64. "●←" : "What's our plan? How many defenders?",
  65. "●→" : "Who's controlling the button?",
  66.  
  67. // Commands
  68. "↓●●" : "Defend our flag!",
  69. "↑●●" : "Attack!",
  70. "←●●" : "Trick the enemy!",
  71. "→●●" : "Control the button!",
  72.  
  73. };
  74.  
  75. // Prefixes for giving directions. Two arrow presses indicating the direction comes after them.
  76. // First macro is empty: it's triggered just by two arrow presses
  77. // [Direction], [direction] and [arrow] are wildcards.
  78. // Sent to your team only.
  79. var directionMacros = {
  80. "" : "Enemy FC on [direction]. ([arrow])",
  81. "↑0" : "Enemy on [direction]. ([arrow])",
  82. "↓0" : "I'm coming [direction]. ([arrow])",
  83. "←0" : "[Direction] is safe. ([arrow])",
  84. "→0" : "[Direction] powerup is respawning in 15 seconds. ([arrow])"
  85. };
  86.  
  87. // Macros sent to everyone (Enter-key chat)
  88. var globalMacros = {
  89. // Positive emotions
  90. "●●↑" : [
  91. "Yea!",
  92. "We're good!",
  93. "YES WE CAN",
  94. "Dream team.",
  95. "Well played."
  96. ],
  97.  
  98. // Angry emotions
  99. "●●↓" : [
  100. "Shazbot!",
  101. "Oh noez!",
  102. "I've let you win this time.",
  103. "C'mon team!",
  104. "This made me angry."
  105. ],
  106.  
  107. // Bad jokes
  108. "●●←" : [
  109. "ALL GLORY TO THE HYPNOTOAD",
  110. "So game. Much flag. Many ball. Amaze.",
  111. "I have a UDP joke for you, but I'm afraid you won't get it.",
  112. "I'm a linguist. I love ambiguity more than most people.",
  113. "What's green and eats nuts? Syphilis.", // by /u/EstherHarshom
  114. "A blind man walks into a bar. And a table. And a chair.", // by /u/VinciFox
  115. "Life without women would be a pain in the ass.", // by /u/-Minnow-
  116. "Where did Sally go during the bombing? Everywhere.", // by /u/My_Name_Is_Not_Chris
  117. ],
  118.  
  119. // Use often. Very often.
  120. "●●→" : "Neomacro! TagPro macro system. http://tiny.cc/neomacro",
  121. };
  122.  
  123. // Aliases for some combinations. Alias comes first, real code second.
  124. // You can setup more aliases for a macro.
  125. // If you setup the same alias as the macro itself (for example, "←←":"←←"), the universe will explode
  126. var aliases = {
  127. "→←→":"←→←", // No
  128. "↓↑↓":"↑↓↑", // Yes
  129.  
  130. "●●●":"0" // Backwards compatibility
  131. };
  132.  
  133. /************************
  134. * *
  135. * END OF CONFIG SECTION *
  136. * *
  137. ************************/
  138.  
  139. // Why would you change this?
  140. var directions = {
  141. "↓←": ["lower left", "↙"],
  142. "←←": ["left", "←"],
  143. "↑←": ["upper left", "↖"],
  144. "↑↑": ["top", "↑"],
  145. "↑→": ["upper right", "↗"],
  146. "→→": ["right", "→"],
  147. "↓→": ["lower right", "↘"],
  148. "↓↓": ["bottom", "↓"],
  149. "0": ["middle", "●"]
  150. };
  151.  
  152. directions["←↓"] = directions["↓←"];
  153. directions["←↑"] = directions["↑←"];
  154. directions["→↓"] = directions["↓→"];
  155. directions["→↑"] = directions["↑→"];
  156.  
  157. // Because the game's keypress handlers are more prioritized, they're circumvented using a dummy input
  158. var handlerbtn = document.getElementById("macrohandlerbutton");
  159. handlerbtn.focus();
  160. handlerbtn.addEventListener('keydown', keydownHandler, false);
  161. handlerbtn.addEventListener('keyup', keyupHandler, false);
  162.  
  163.  
  164. document.addEventListener('keydown', documentKeydown, false);
  165. function documentKeydown(event)
  166. {
  167. if(!tagpro.disableControls)
  168. {
  169. handlerbtn.focus(); // The handler button should be always fucused
  170.  
  171. // Disables backspace and all ctrl interactions -- prevents leaving page by accident
  172. if((event.keyCode==8 || event.ctrlKey) && !tagpro.disableControls)
  173. {
  174. event.preventDefault();
  175. event.stopPropagation();
  176. return false;
  177. }
  178. }
  179. }
  180.  
  181. // Relasing arrow key tricks TagPro to think that you relased WASD-key too, even if you didn't
  182. // This code prevents that from happening
  183. function keyupHandler(event)
  184. {
  185. if(event.keyCode in buttons && !tagpro.disableControls)
  186. {
  187. event.preventDefault();
  188. event.stopPropagation();
  189. }
  190. }
  191.  
  192. // Main macro keypresses handler
  193. var lastKey = 0;
  194. var currentMacro = "";
  195. function keydownHandler(event)
  196. {
  197. if(!(event.keyCode in buttons) || tagpro.disableControls)
  198. return;
  199.  
  200. event.preventDefault();
  201. event.stopPropagation();
  202.  
  203. var now = new Date();
  204. if((now - lastKey) > keypressLimit)
  205. {
  206. currentMacro = "";
  207. }
  208. lastKey = now;
  209.  
  210. currentMacro += buttons[event.keyCode];
  211.  
  212. var message = getMacro(currentMacro);
  213. if(message)
  214. {
  215. chat(message);
  216. currentMacro = "";
  217. }
  218. }
  219.  
  220. // Utility function to get corresponding message for macro code
  221. function getMacro(x)
  222. {
  223. function capitalize(s)
  224. {
  225. return s[0].toUpperCase() + s.slice(1);
  226. }
  227.  
  228. function isDirectionMacro(s)
  229. {
  230. return s.substring(0,s.length-2) in directionMacros && s.substring(s.length-2, s.length) in directions;
  231. }
  232.  
  233. function getDirectionMacro(s)
  234. {
  235. var direction = directions[s.substring(s.length-2, s.length)];
  236. return select(directionMacros[s.substring(0,s.length-2)])
  237. .replace(/\[Direction\]/g, capitalize(direction[0]))
  238. .replace(/\[direction\]/g, direction[0])
  239. .replace(/\[arrow\]/g, direction[1]);
  240. }
  241.  
  242. if(isDirectionMacro(x))
  243. return {text:getDirectionMacro(x), global:0};
  244.  
  245. // When array is supplied, return random message - ideal to add variety to your messages
  246. function select(x)
  247. {
  248. if(typeof x == "object")
  249. return x[Math.floor(Math.random()*x.length)];
  250. return x;
  251. }
  252.  
  253. if(x in teamMacros)
  254. return {text:select(teamMacros[x]), global:0};
  255.  
  256. if(x in globalMacros)
  257. return {text:select(globalMacros[x]), global:1};
  258.  
  259. if(x in aliases)
  260. return getMacro(aliases[x]);
  261.  
  262. return false;
  263. }
  264.  
  265. // This functions does what expected - sends a chat message
  266. var lastMessage = 0;
  267. function chat(chatMessage)
  268. {
  269. var now = new Date();
  270. var timeDiff = now - lastMessage;
  271. if (timeDiff > messageLimit)
  272. {
  273. tagpro.socket.emit("chat",
  274. {
  275. message: messagePrefix + chatMessage.text,
  276. toAll: chatMessage.global
  277. });
  278. lastMessage = new Date();
  279. }
  280. else if (timeDiff >= 0)
  281. {
  282. setTimeout(chat, messageLimit - timeDiff, chatMessage);
  283. }
  284. }
  285. }
  286.  
  287. // This dummy input will handle macro keypresses
  288. var btn = document.createElement("input");
  289. btn.style.opacity = 0;
  290. btn.style.position = "absolute";
  291. btn.style.top = "-100px";
  292. btn.style.left = "-100px";
  293. btn.id = "macrohandlerbutton";
  294. document.body.appendChild(btn);
  295.  
  296. // Create a script node holding this source code
  297. var script = document.createElement('script');
  298. script.setAttribute("type", "application/javascript");
  299. script.textContent = '(' + Script + ')();';
  300. document.body.appendChild(script);
Advertisement
Add Comment
Please, Sign In to add comment