Skip to content

Instantly share code, notes, and snippets.

@arsane
Last active August 29, 2015 14:15
Show Gist options
  • Save arsane/0f60930e1d35618d08f9 to your computer and use it in GitHub Desktop.
Save arsane/0f60930e1d35618d08f9 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name ganster
// @namespace [email protected]
// @include http://www.sexgangsters.com/*
// @version 1
// @grant none
// ==/UserScript==
var api_call = function(api_method, api_args, api_callback) {
return function() {
$.ajax({
url: "/api/", cache: false, type: "POST",
beforeSend: function (xhr) {
var token = getCookie("csrftoken");
if (token) { xhr.setRequestHeader("X-CSRFToken", token); }
},
data: {data: JSON.stringify({ method: api_method, args: api_args })},
success: function(response) {
if (api_callback != undefined) {
var obj = JSON.parse(response);
api_callback(obj);
}}
});
}
};
var onOkJsonResponse = function(func) { return function(obj) { if (obj.result == "ok") { func(obj); } }};
var topList = function(start_id) { api_call("rating.top", {start: start_id, limit:10, prev_week:false})(); };
var doJobIfTooManyEnergy = function() {
if ((userData.resources.energy + 1) >= userData.resources.energy_lim) {
api_call("city.job.do", {cityId: userData.cities.length})();
}
};
var collectAllMoney = api_call("city.business.collectAll", {});
var getRandomInt = function (min, max) { return Math.floor(Math.random() * (max - min)) + min; }
var beats = [14,15,16,17,18,20].map (
function(cid) {
return api_call(
"boss.fight.start", {cid:cid},
onOkJsonResponse(function(obj){
window.setTimeout(api_call("boss.fight.finish", {cid:cid, boosters: null}), getRandomInt(2, 16)*1000);
})
);
}
);
// find job to do. {"method":"city.loc.job.do","args":{"cityId":5,"locId":502}}
var doTask = function() {
//if ((userData.resources.energy + 1) >= userData.resources.energy_lim) {
if (userData.resources.energy >= 12) {
var compareCity = function(a, b) {return a.id - b.id;};
userData.cities.sort(compareCity);
var city = userData.cities.find(function(e, i, a) { return e.id !=1 && e.stage != 4; }); // city 1 only has 2 stages.
var locId = city.id * 100 + city.stage + 1;
api_call("city.loc.job.do", {cityId: city.id, locId: locId})();
}
}
var doFight = function() {
var cSet = userData.collectibles.find(function(e, i, a) { return !e.collected; });
var cItem = cSet && cSet.items.find(function(e, i, a) {return e.qty == 0;});
var cSetId = cSet && cSet.id || 0;
var cItemId = cItem && cItem.id || 0;
var comparePower = function(a, b) {return (a.defence + a.attack) - (b.defence + b.attack);};
api_call(
"pvp.rivals.get", {cSetId: cSetId, cItemId: cItemId},
function (obj) {
obj.response.sort(comparePower);
//console.log("opponent.attack: " + obj.response[0].attack + ", opponent.defence: " + obj.response[0].defence);
//console.log("userData.attack: " + userData.attack + ", userData.defence: " + userData.defence);
if (comparePower(obj.response[0], userData) > 0)
return window.setTimeout(doFight, 60*1000);
window.setTimeout(
api_call(
"pvp.fight.start", {rivalId: obj.response[0].id},
onOkJsonResponse(function(obj){
window.setTimeout(api_call("pvp.fight.finish", {boosters: null}), getRandomInt(2, 16)*1000);
})
), 6*1000);
}
)();
}
var checkMoney = function() {
var collectMoney = function(city_id, business_id) { api_call("city.business.collect", {cityId: city_id, businessId: business_id})(); };
userData.cities.forEach(
function(city) {
city.businesses.forEach(
function(business) {
business.isBought && collectMoney(city.id, business.id);
}
);
});
};
window.addEventListener('load', function() {
collectAllMoney();
// fight twice.
if(userData.resources.stamina > 0) doFight();
window.setTimeout(doFight, 10*60*1000);
// do Task
doTask();
// beat.
beats.forEach(function(func, idx){ window.setTimeout(func, (idx+1)*30*1000);});
//reload page every 15 minutes 5 seconds
window.setTimeout(function() {window.location.reload();}, 15*60*1000 + 5*1000);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment