Skip to content

Instantly share code, notes, and snippets.

@gourytch
Last active September 12, 2018 00:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gourytch/0b3144f58a5406ef26d515dd1d079fef to your computer and use it in GitHub Desktop.
Save gourytch/0b3144f58a5406ef26d515dd1d079fef to your computer and use it in GitHub Desktop.
simple automation for https://www.coinbrawl.com/
// ==UserScript==
// @name CoinBrawlAutomaton
// @description Вкачиваем натыренное золото по статам
// @author Gourytch
// @license WTFPL
// @version 1.1
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @include https://coinbrawl.com/*
// @include https://www.coinbrawl.com/*
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function (window, undefined) {
/// constants
const MAX_TOKENS = 100; // 1000;
const MAX_STAMINA = 100; // 1000;
const MAX_ATTACK = -1;
const MAX_DEFENSE = -1;
const MAX_BANK_CAP = -1;
const UPGRADE_USE_GROUPS = true;
const UPGRADE_CHEAPEST_FIRST = true;
// for priority list
const TASK_STAMINA = "stamina";
const TASK_TOKEN = "token";
const TASK_BANK = "bank";
const TASK_ATTACK = "attack";
const TASK_DEFENSE = "defense";
// priority list
var UPGRADE_GROUPS = [[TASK_BANK],
[TASK_STAMINA, TASK_TOKEN],
[TASK_ATTACK, TASK_DEFENSE]];
///
var w;
if (typeof unsafeWindow !== undefined) {
w = unsafeWindow;
} else {
w = window;
}
if (w.self != w.top) {
return;
}
if (!/https:\/\/(www\.|)coinbrawl\.com\/.*/.test(w.location.href)) {
console.log("CoinBrawlAutomaton detected wrong href='" + w.location.href + "'");
return;
}
console.log("CoinBrawlAutomaton");
///////////// FUNCTIONS ///////////
function getActivePage() {
return $("div.panel-navigation ul.nav-pills li.active a.active").attr("href");
}
function go(url) {
top.location.href = "https://www.coinbrawl.com/" + url;
}
function getUpgradeCost(nth) {
return parseInt($("table.stats-table tr:nth-child("+nth+") td:nth-child(3) a:nth-child(1)").text().match(/(\d+)/)[1]);
}
function groupId(s) {
for (let i = 0; i < UPGRADE_GROUPS.length; i++) {
if (UPGRADE_GROUPS[i].indexOf(s) >= 0) {
return i;
}
}
return -1;
}
function nextUpgradeTask() {
let upgrade_list = [];
if (((MAX_BANK_CAP == -1) || (STATE.bank_cap < MAX_BANK_CAP)) && (STATE.price_bank <= STATE.gold)) {
upgrade_list.push({
name: TASK_BANK,
price: STATE.price_bank,
action: function(){
console.log("INCREASE BANK CAPACITY FOR A " + this.price);
go("bank/upgrade");
}
});
}
if (((MAX_TOKENS == -1) || (STATE.tokens_max < MAX_TOKENS)) && (STATE.price_token <= STATE.gold)) {
upgrade_list.push({
name: TASK_TOKEN,
price: STATE.price_token,
action: function(){
console.log("BUY TOKEN FOR A " + this.price);
go("upgrades/maximum_tokens");
}
});
}
if (((MAX_STAMINA == -1) || (STATE.stamina_max < MAX_STAMINA)) && (STATE.price_stamina <= STATE.gold)) {
upgrade_list.push({
name: TASK_STAMINA,
price: STATE.price_stamina,
action: function(){
console.log("BUY STAMINA FOR A " + this.price);
go("upgrades/maximum_stamina");
}
});
}
if (((MAX_ATTACK == -1) || (STATE.attack < MAX_ATTACK)) && (STATE.price_attack <= STATE.gold)) {
upgrade_list.push({
name: TASK_ATTACK,
price: STATE.price_attack,
action: function(){
console.log("BUY ATTACK POWER FOR A " + this.price);
go("upgrades/attack");
}
});
}
if (((MAX_DEFENSE == -1) || (STATE.defense < MAX_DEFENSE)) && (STATE.price_defense <= STATE.gold)) {
upgrade_list.push({
name: TASK_DEFENSE,
price: STATE.price_defense,
action: function(){
console.log("BUY DEFENSE FOR A " + this.price);
go("upgrades/defense");
}
});
}
if (upgrade_list.length === 0) {
return null;
}
upgrade_list.sort(function(a,b){
if (UPGRADE_USE_GROUPS) {
a_id = groupId(a.name);
b_id = groupId(b.name);
console.log("compare by group: " + a.name + "=" + a_id + " vs " + b.name + "=" + b_id);
if (a_id != b_id) {
return (a_id > b_id);
}
}
if (UPGRADE_CHEAPEST_FIRST) {
return b.price < a.price;
} else {
return a.price < b.price;
}
});
console.log("UPGRADE LIST:" + upgrade_list.map(function(item){
return "(" + item.name + ":" + item.price + ")";
}));
return upgrade_list[0];
}
function tryUpgradeTask() {
let obj = nextUpgradeTask();
if (obj === null) {
return false;
}
console.log("choose to upgrade " + obj.name + " for " + obj.price);
setTimeout(function(){
obj.action();
}, 1000, obj);
return true;
}
var STATE = {
bad: null,
username: GM_getValue("username"),
bank_capacity: GM_getValue("bank_capacity"),
bank_nextcap: GM_getValue("bank_nextcap"),
price_bank: GM_getValue("price_bank"),
price_attack: GM_getValue("price_attack"),
price_defense: GM_getValue("price_defense"),
price_stamina: GM_getValue("price_stamina"),
price_token: GM_getValue("price_token"),
level: -1,
levelup: -1,
class: '?',
gems: -1,
attack: -1,
defense: -1,
stamina_cur: -1,
stamina_max: -1,
tokens_cur: -1,
tokens_max: -1,
gold: -1,
satoshi: -1,
safe_gold: -1,
points: -1,
safetime: -1
}; // state object
function parseState() {
var p = $("#quick-stats");
if (p.length === 0) {
STATE.bad = true;
return;
}
STATE.bad = false;
STATE.levelup = parseInt(p.find("li[data-reactid='.0.0.0.0']").attr('data-original-title').match(/(\d+)/)[1]);
STATE.level = parseInt(p.find("li[data-reactid='.0.0.0.0.1']").text());
STATE.class = p.find("span[data-reactid='.0.0.0.1.1']").text();
STATE.gems = parseInt(p.find("span[data-reactid='.0.0.0.2.1']").text());
STATE.attack = parseInt(p.find("span[data-reactid='.0.0.0.3.1.1']").text());
STATE.defense = parseInt(p.find("span[data-reactid='.0.0.0.4.1.1']").text());
var v = p.find("span[data-reactid='.0.0.0.5.1.1']").text().split('/');
STATE.stamina_cur = parseInt(v[0]);
STATE.stamina_max = parseInt(v[1]);
v = p.find("span[data-reactid='.0.0.0.6.1.1']").text().split('/');
STATE.tokens_cur = parseInt(v[0]);
STATE.tokens_max = parseInt(v[1]);
STATE.defense = parseInt(p.find("span[data-reactid='.0.0.0.4.1.1']").text());
STATE.gold = parseInt(p.find("span[data-reactid='.0.0.0.7.1.1']").text());
STATE.satoshi = parseInt(p.find("span[data-reactid='.0.0.0.8.1.1']").text());
STATE.safe_gold = parseInt(p.find("span[data-reactid='.0.0.0.9.1.1']").text());
STATE.points = parseInt(p.find("span[data-reactid='.0.0.0.a.1.1']").text());
}
function dumpState() {
console.log(STATE);
}
parseState();
dumpState();
if (STATE.bad) {
console.log("something went wrong! reload page");
setTimeout(function(){top.location.reload();}, 10000);
return;
}
//ads hiding borrowed from https://greasyfork.org/users/25633
$('div.panel-game-content:first').hide();
$('div.col-sm-9 > center').hide();
$('div.col-sm-3').hide();
$('div.panel-body > p').hide();
document.body.style.fontSize = "10px";
var page = getActivePage();
console.log("active page is '" + page + "'");
if (page == "/character") { // On the character page
STATE.price_stamina = getUpgradeCost(4);
STATE.price_token = getUpgradeCost(5);
STATE.price_attack = getUpgradeCost(6);
STATE.price_defense = getUpgradeCost(7);
GM_setValue("price_attack", STATE.price_attack);
GM_setValue("price_defense", STATE.price_defense);
GM_setValue("price_stamina", STATE.price_stamina);
GM_setValue("price_token", STATE.price_token);
let upgrade_list = [];
let NO_UPGRADE = false;
if (NO_UPGRADE) {
console.log("upgrade disabled. stop.");
return;
}
if (tryUpgradeTask()) {
return;
} else {
console.log("no upgrades available");
let a_stamina = $("table.stats-table a[data-target='#regenerate-stamina-modal']");
if (STATE.stamina_cur === 0 && a_stamina.length > 0) {
console.log("can pop stamina regenerator");
a_stamina[0].click();
return;
}
let a_tokens = $("table.stats-table a[data-target='#regenerate-tokens-modal']");
if (STATE.tokens_cur === 0 && a_tokens.length > 0) {
console.log("can pop tokens regenerator");
a_tokens[0].click();
return;
}
console.log("We're gonna make some gold!");
setTimeout(function(){go("arena");}, 1000);
return;
}
return;
} else if (page == "/bank") { // on the Bank page
var V = $("div.col-sm-6 h5").map(function() { return $.trim($(this).text());}).get();
STATE.bank_capacity = parseInt(V[0].match(/\d+/));
GM_setValue("bank_capacity", STATE.bank_capacity);
STATE.bank_nextcap = parseInt(V[2].match(/\d+/));
GM_setValue("bank_nextcap", STATE.bank_nextcap);
STATE.price_bank = parseInt(V[2].match(/\((\d+)/)[1]);
GM_setValue("price_bank", STATE.price_bank);
if ((MAX_BANK_CAP == -1) || (STATE.bank_capacity < MAX_BANK_CAP)) {
if (STATE.price_bank <= STATE.gold) {
console.log("upgrade bank capacity...");
setInterval(function(){go("bank/upgrade");}, 1000);
return;
}
}
} else if (page == "/arena") { // on the Arena page
if (tryUpgradeTask()) {
console.log("task assigned");
}
if (STATE.stamina_cur === 0 && STATE.tokens_cur === 0) {
console.log("there is noting to do. go and restore something");
setTimeout(function(){go("character");}, 1000);
}
}
})(window);
// var t = ;
// console.log("t='" + t + "'");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment