Advertisement
Guest User

Silly FEHeroes Minigame script

a guest
Sep 3rd, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. // ==UserScript==
  2. // @name FEH QHB Auto
  3. // @version 0.2
  4. // @description FEH auto-battle, but for a browser minigame
  5. // @match https://events.fire-emblem-heroes.com/color/play
  6. // @grant none
  7. // ==/UserScript==
  8.  
  9. const $ = document.querySelector.bind(document); // keep `this` as `document`
  10. const $$ = document.querySelectorAll.bind(document); // keep `this` as `document`
  11. const matchupMap = new Map([ // enemy -> Hero with WTA
  12. [`green`, `Roy`],
  13. [`red`, `Lucina`],
  14. [`blue`, `Ike`],
  15. [`white`, `Lyn`],
  16. ]);
  17. const coordinateMap = new Map([ // Hero -> touch coordinates for Hero's button
  18. [`Roy`, [160, 701]],
  19. [`Lucina`, [251, 701]],
  20. [`Ike`, [368, 701]],
  21. [`Lyn`, [546, 701]],
  22. ]);
  23.  
  24. // generate fake touch events
  25. function sendTouchEvent (x, y, element, eventType) {
  26. const touch = new Touch({
  27. identifier: Date.now(),
  28. target: element,
  29. clientX: x,
  30. clientY: y,
  31. });
  32. const touchEvent = new TouchEvent(eventType, {
  33. changedTouches: [touch],
  34. });
  35. element.dispatchEvent(touchEvent);
  36. }
  37.  
  38. // skip sound options, difficulty select, tweet and play
  39. function checkForModal() {
  40. if ($(`.btn-sound-off`)) {
  41. console.log(`Turning off sound...`);
  42. sendTouchEvent(0, 0, $(`.btn-sound-off`), `touchend`);
  43. } else if ($(`.btn-battle-lunatic`)) {
  44. console.log(`Selecting lunatic difficulty...`);
  45. sendTouchEvent(0, 0, $(`.btn-battle-lunatic`), `touchend`);
  46. console.log(`Clicking "Tweet and Play" button...`);
  47. sendTouchEvent(0, 0, [...$$(`.modal-container-connect > .btn-general-link`)][3], `touchend`);
  48. } else if($(".modal-container-result") && $(".modal-container-result").parentNode.parentNode.style.display==="") { // game over popup is visible
  49. if ($(`.btn-result-tweet`)) {
  50. console.log(`Clicking "Tweet and Try Again" button...`);
  51. sendTouchEvent(0, 0, $(`.btn-result-tweet`), 'touchend');
  52. }
  53. if ($(`.btn-result-retry`)) {
  54. console.log(`Clicking "Try Again" button...`);
  55. sendTouchEvent(0, 0, $(`.btn-result-retry`), 'touchend');
  56. }
  57. }
  58. }
  59.  
  60. // return Hero with WTA against enemy
  61. function getHero(color, flying) {
  62. return flying ? `Lyn` : matchupMap.get(color); // Lyn gives two combo points against all flying enemies
  63. }
  64.  
  65. // determine which Hero icon to press given enemy properties
  66. function processNewEnemy(color, flying) {
  67. const hero = getHero(color, flying);
  68. console.log(`Selected ${hero} against ${flying ? `flying ` : ``}${color} enemy.`);
  69. setTimeout(function() {
  70. sendTouchEvent(...coordinateMap.get(hero), $(`canvas`), `touchstart`);
  71. }, 300); // slight delay to ensure button is pressed when pressable
  72. }
  73.  
  74. // wrap Console.log() to intercept data logged by the game
  75. const oldLog = window.console.log;
  76. window.console.log = function(...args) {
  77. if (typeof args[0] === `number` && args[1].color) {
  78. processNewEnemy(args[1].color, args[1].flying);
  79. }
  80. oldLog(...args);
  81. };
  82.  
  83.  
  84. setTimeout(function() { // refresh every 10 minutes just in case the game breaks
  85. console.log(`Reloading page...`);
  86. location.reload();
  87. }, 600000);
  88. setInterval(checkForModal, 1000); // check every second
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement