Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name FEH QHB Auto
- // @version 0.2
- // @description FEH auto-battle, but for a browser minigame
- // @match https://events.fire-emblem-heroes.com/color/play
- // @grant none
- // ==/UserScript==
- const $ = document.querySelector.bind(document); // keep `this` as `document`
- const $$ = document.querySelectorAll.bind(document); // keep `this` as `document`
- const matchupMap = new Map([ // enemy -> Hero with WTA
- [`green`, `Roy`],
- [`red`, `Lucina`],
- [`blue`, `Ike`],
- [`white`, `Lyn`],
- ]);
- const coordinateMap = new Map([ // Hero -> touch coordinates for Hero's button
- [`Roy`, [160, 701]],
- [`Lucina`, [251, 701]],
- [`Ike`, [368, 701]],
- [`Lyn`, [546, 701]],
- ]);
- // generate fake touch events
- function sendTouchEvent (x, y, element, eventType) {
- const touch = new Touch({
- identifier: Date.now(),
- target: element,
- clientX: x,
- clientY: y,
- });
- const touchEvent = new TouchEvent(eventType, {
- changedTouches: [touch],
- });
- element.dispatchEvent(touchEvent);
- }
- // skip sound options, difficulty select, tweet and play
- function checkForModal() {
- if ($(`.btn-sound-off`)) {
- console.log(`Turning off sound...`);
- sendTouchEvent(0, 0, $(`.btn-sound-off`), `touchend`);
- } else if ($(`.btn-battle-lunatic`)) {
- console.log(`Selecting lunatic difficulty...`);
- sendTouchEvent(0, 0, $(`.btn-battle-lunatic`), `touchend`);
- console.log(`Clicking "Tweet and Play" button...`);
- sendTouchEvent(0, 0, [...$$(`.modal-container-connect > .btn-general-link`)][3], `touchend`);
- } else if($(".modal-container-result") && $(".modal-container-result").parentNode.parentNode.style.display==="") { // game over popup is visible
- if ($(`.btn-result-tweet`)) {
- console.log(`Clicking "Tweet and Try Again" button...`);
- sendTouchEvent(0, 0, $(`.btn-result-tweet`), 'touchend');
- }
- if ($(`.btn-result-retry`)) {
- console.log(`Clicking "Try Again" button...`);
- sendTouchEvent(0, 0, $(`.btn-result-retry`), 'touchend');
- }
- }
- }
- // return Hero with WTA against enemy
- function getHero(color, flying) {
- return flying ? `Lyn` : matchupMap.get(color); // Lyn gives two combo points against all flying enemies
- }
- // determine which Hero icon to press given enemy properties
- function processNewEnemy(color, flying) {
- const hero = getHero(color, flying);
- console.log(`Selected ${hero} against ${flying ? `flying ` : ``}${color} enemy.`);
- setTimeout(function() {
- sendTouchEvent(...coordinateMap.get(hero), $(`canvas`), `touchstart`);
- }, 300); // slight delay to ensure button is pressed when pressable
- }
- // wrap Console.log() to intercept data logged by the game
- const oldLog = window.console.log;
- window.console.log = function(...args) {
- if (typeof args[0] === `number` && args[1].color) {
- processNewEnemy(args[1].color, args[1].flying);
- }
- oldLog(...args);
- };
- setTimeout(function() { // refresh every 10 minutes just in case the game breaks
- console.log(`Reloading page...`);
- location.reload();
- }, 600000);
- setInterval(checkForModal, 1000); // check every second
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement