Last active
August 11, 2022 18:45
-
-
Save diego-mi/482a3e828c42f896a0929260077aa049 to your computer and use it in GitHub Desktop.
TurtRacing - Auto Run Indexed - Version Fast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name TurtRacing - Auto Run Indexed | |
// @namespace https://gist.github.com/diego-mi | |
// @version 2.2 | |
// @description TurtRacing - Auto Run Indexed | |
// @author DiegoMi | |
// @match https://play.turtleracing.io | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const querySelector_ElementToWaitToStart = '#div_turtle'; | |
const msgAlreadyRun = 'You already done your runs.'; | |
const msgRunning = 'Running'; | |
const msgNeedsEnery = 'Your turtle needs energy drink.'; | |
const msgInsufficient = 'Insufficient balance'; | |
let turtlesBot = []; | |
/** | |
* HELPERS | |
*/ | |
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)); | |
} | |
/** | |
* FIM HELPERS | |
*/ | |
/** | |
* Indexação de turtles | |
*/ | |
const getTurtlesFromApiAndRun = async () => { | |
await main.get("/home/getTurtles", function (data) { | |
formatTurtlesFromApi(data.turtles); | |
run(0); | |
}); | |
} | |
const formatTurtlesFromApi = (turtlesFromApi) => { | |
for(var index = 0; index < turtlesFromApi.length; index++) { | |
turtlesBot.push(buildTurtleItem(turtlesFromApi[index])); | |
} | |
} | |
const buildTurtleItem = (turtleFromApi) => { | |
return new TurtleItemBot(turtleFromApi.id, turtleFromApi.run, turtleFromApi.energy, turtleFromApi.age, turtleFromApi.fisio, turtleFromApi.fisio2); | |
} | |
/** | |
* Classe central para informacoes de uma turtle | |
*/ | |
class TurtleItemBot { | |
constructor(id, run, energy, age, fisio, fisio2) { | |
this.id = id; | |
this.run = run; | |
this.energy = energy; | |
this.age = age; | |
this.fisio = fisio; | |
this.fisio2 = fisio2; | |
} | |
hasRacesToDo = () => { | |
console.log('this.run', this.run); | |
return this.run < 2; | |
} | |
needRecovery = () => this.energy === 0; | |
addOneRaceDone = () => this.run++; | |
needFisio = () => false; | |
} | |
const run = async (index) => { | |
console.log(`running with ${index}`); | |
console.log(`running with ${turtlesBot[index].id}`); | |
if (turtlesBot[index].hasRacesToDo()) { | |
if (turtlesBot[index].needRecovery()) { | |
return execRecovery(index); | |
} | |
if (turtlesBot[index].needFisio()) { | |
return execFisio(index); | |
} | |
console.log(`Turtle-${index + 1} running race ${turtlesBot[index].run + 1}`); | |
await execRace(index); | |
await delay(500); | |
} else { | |
console.log(`${index + 1} already raced`); | |
runNext(index); | |
} | |
} | |
const execRace = async (index) => { | |
await main.get(`/home/play?id=${turtlesBot[index].id}`, function (data) { | |
console.log('data run', data); | |
console.log('data.error', data.error); | |
if (!data.error) { | |
turtlesBot[index].addOneRaceDone(); | |
} | |
if (data.error === msgAlreadyRun) { | |
runNext(index); | |
} else if (data.error === msgRunning) { | |
runNext(index); | |
} else if (data.error === msgNeedsEnery) { | |
execRecovery(index); | |
} else if (turtlesBot[index].run === 2) { | |
runNext(index); | |
} else { | |
console.log(`Turtle-${index + 1} running next race `); | |
run(index); | |
} | |
}); | |
} | |
const execRecovery = (index) => { | |
console.log(`recovery in Turtle-${index + 1}`); | |
main.get(`/home/feed?id=${turtlesBot[index].id}`, function (data) { | |
console.log('data recovery', data); | |
if (data.error && data.error === msgInsufficient) { | |
alert(msgInsufficient); | |
} else if (!!data.error) { | |
runNext(index); | |
} else { | |
turtlesBot[index].energy = 100; | |
run(index); | |
} | |
}); | |
} | |
const execFisio = (index) => { | |
console.log(`fisio in Turtle-${index + 1}`); | |
main.get(`/home/heal?id=${turtlesBot[index].id}`, function (data) { | |
console.log('Data fisio', data); | |
console.log('Data fisio', data.error === msgInsufficient); | |
if (data.error && data.error === msgInsufficient) { | |
alert(msgInsufficient); | |
} else if (!!data.error) { | |
runNext(index); | |
} else { | |
turtlesBot[index].fisio = false; | |
turtlesBot[index].fisio2 = false; | |
run(index); | |
} | |
}); | |
} | |
const runNext = (index) => { | |
if (index < turtlesBot.length) { | |
console.log(`running with next Turtle-${index + 1}`); | |
run(index + 1); | |
} | |
} | |
/** | |
* Metodo responsavel por salvar um item no local storage | |
*/ | |
const setItemToLocalStorage = (name, value) => { | |
localStorage.setItem(name, JSON.stringify(value)) | |
} | |
/** | |
* Metodo responsavel por recuperar um item do local storage | |
*/ | |
const getItemToLocalStorage = (name) => { | |
return JSON.parse( localStorage.getItem(name) ) | |
} | |
/** | |
* FIM Indexação de turtles | |
*/ | |
//---------------------------------------------------------------------------------------------// | |
/** | |
* INICIALIZACAO | |
*/ | |
async function Init() { | |
console.log("TurtleBot v2"); | |
await WaitForElement(querySelector_ElementToWaitToStart); | |
await getTurtlesFromApiAndRun(); | |
} | |
Init(); | |
/** | |
* FIM INICIALIZACAO | |
*/ | |
})(); |
2.2 - disabled auto fisio
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2.1 - fix error with infinite fisio