Skip to content

Instantly share code, notes, and snippets.

@diego-mi
Last active July 6, 2022 19:08
Show Gist options
  • Save diego-mi/4e0318c60f96852a35565a3623b66664 to your computer and use it in GitHub Desktop.
Save diego-mi/4e0318c60f96852a35565a3623b66664 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name TurtRacing - Auto Run
// @namespace https://gist.github.com/diego-mi
// @version 0.16
// @description TurtRacing - Auto Run
// @author DiegoMi
// @match https://play.turtleracing.io
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const showLogs = true; // coloque false caso nao queira ver logs no developer tools do navegador
const turtItemsElements = null;
let turtListItemsElements = null;
const QuerySelector_StartElement = `#myBox2 > div:nth-child(4) > div:nth-child(1)`;
const QuerySelector_TurtItem = `#myBox2 > div:nth-child(4) > div`;
const QuerySelector_TurtItemPlayBtn = `div.contentBx > a`;
const QuerySelector_StartBtn = `#btnPlay`;
const QuerySelector_RecoveryBtn = `#div_buttons > a.buttonClock`;
const QuerySelector_ClockBtn = `#btnClock`;
const QuerySelector_Card_Races = `#div_turtle > div:nth-child(1) > span:nth-child(23)`;
const waitTimeForAbortAnimationRace = 1000; // Alterar este valor em milisegundos para aumentar ou diminuir o tempo de espera para abortar a animacao da corrida
async function WaitForElement(selector) {
while (document.querySelector(selector) === null) {
await new Promise( resolve => requestAnimationFrame(resolve) )
}
await new Promise(resolve => setTimeout(resolve, 100));
return document.querySelector(selector);
}
async function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
async function GetTurts () {
await WaitForElement(QuerySelector_TurtItem);
turtListItemsElements = document.querySelectorAll(QuerySelector_TurtItem);
if (turtListItemsElements.length === 0) return turtleLog("Turts Items not found");
turtleLog(`Turts Items found: ${turtListItemsElements.length}`);
}
async function DoRaces() {
for(let turtIndex = 0; turtIndex < turtListItemsElements.length; turtIndex++) {
turtleLog(`Turt-${turtIndex + 1} verificando`);
if (canRun(turtIndex)) {
//turtleLog(`Turt-${turtIndex + 1} vai correr`);
await selectCarListItemByIndex(turtIndex);
await startRace(turtIndex);
//document.querySelector('#app-root > div > div > nav > ul > li:nth-child(2) > a').click();
} else {
//turtleLog(`Turt-${turtIndex + 1} ja correu`);
}
//turtleLog(`Turt-${turtIndex + 1} verificado, verificando proxima...`);
}
}
async function selectCarListItemByIndex(turtIndex) {
document.querySelector(`#myBox2 > div:nth-child(4) > div:nth-child(${turtIndex + 1}) > div.contentBx > a`).click();
await delay(1000);
}
function canRun(turtIndex) {
try {
return document.querySelector(`#myBox2 > div:nth-child(4) > div:nth-child(${turtIndex + 1}) > div:nth-child(1) > div:nth-child(2)`).textContent.indexOf('/') > -1;
} catch (e) {
return false
}
}
async function startRace() {
await doRecoveryIfNeeded();
if (isFirstRun()) {
firstRace();
}
await doRecoveryIfNeeded();
if (isSecondRun()) {
secondRace();
}
}
function isFirstRun() {
turtleLog(document.querySelector(QuerySelector_Card_Races).textContent);
return document.querySelector(QuerySelector_Card_Races).textContent.indexOf('2/2') > -1;
}
function isSecondRun() {
turtleLog(document.querySelector(QuerySelector_Card_Races).textContent);
return document.querySelector(QuerySelector_Card_Races).textContent.indexOf('1/2') > -1;
}
async function firstRace() {
try {
turtleLog('firstRace');
document.querySelector(QuerySelector_StartBtn).click();
delay(waitTimeForAbortAnimationRace);
window.location.reload(true);
} catch (error) {
}
}
async function secondRace() {
try {
await doRecoveryIfNeeded();
turtleLog('secondRace');
document.querySelector(QuerySelector_StartBtn).click();
delay(waitTimeForAbortAnimationRace);
history.go('');
} catch (error) {
}
}
async function doRecoveryIfNeeded() {
if (recoveryNeeded()) {
doRecovery();
await delay(1000);
}
}
function recoveryNeeded() {
return document.querySelector(QuerySelector_RecoveryBtn) !== null;
}
function doRecovery() {
document.querySelector(QuerySelector_RecoveryBtn).click();
}
function turtleLog(...message) {
if (showLogs) console.log(message);
}
async function initializationFromCollection() {
await GetTurts();
await DoRaces();
turtleLog("Finalizado");
}
/**
* INICIALIZACAO
*/
async function Init() {
turtleLog("TurtleBot");
await WaitForElement(QuerySelector_StartElement);
await delay(1000);
await doRecoveryIfNeeded();
if (document.querySelector(QuerySelector_StartBtn) == null && document.querySelector(QuerySelector_ClockBtn) == null) {
await initializationFromCollection();
} else if (document.querySelector(QuerySelector_ClockBtn) !== null) {
document.querySelector('#app-root > div > div > nav > ul > li:nth-child(2) > a').click();
history.go('');
} else if (document.querySelector(QuerySelector_StartBtn) !== null) {
await doRecoveryIfNeeded();
await firstRace();
document.querySelector('#app-root > div > div > nav > ul > li:nth-child(2) > a').click();
history.go('');
await WaitForElement(QuerySelector_StartElement);
await initializationFromCollection();
}
}
Init();
})();
@diego-mi
Copy link
Author

diego-mi commented May 9, 2022

0.15 - fix crash when run second race and stop in clock button

@diego-mi
Copy link
Author

diego-mi commented May 9, 2022

0.16 - fix when need recovery on after first race

@diego-mi
Copy link
Author

0.17 - fixado paradas repentinas do recovery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment