Skip to content

Instantly share code, notes, and snippets.

@adammw
Created February 6, 2011 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammw/813368 to your computer and use it in GitHub Desktop.
Save adammw/813368 to your computer and use it in GitHub Desktop.
Mafia Wars Mobile Fight Bot
var MafiaWarsMobileFightBot = function() {
var fightUrl = 'mobileweb?xw_controller=fight&xw_action=view_mobile';
var estimateStrengthWeights = {mafia: 1.5, level: 1.0};
var strengthDifferenceThreshold = 0.15;
//TODO: add UI
//TODO: handle hospital case, if $('.hospital-block').length . heal-text and heal-button will describe action and cost
//TODO: implement lower end of strength threshold (e.g. don't fight too weak opponents)
function is_fights_page() {
return Boolean($('.fight-opp').length);
}
function load_page(url,callback) {
if (!ajaxLoad) {
ajaxLoad = true;
if ($('#main').is('.scroll-to-top')) {
$('#ajax-container').scrollTop = 0;
window.scrollTo(0,0);
showLoader(true);
} else {
showLoader(false);
}
$('#ajax-container').load(url + ' #content', function(rt, ts, r){
hijackLinks(rt,ts,r);
if (typeof callback == 'function') { callback(rt,ts,r); }
});
}
}
function load_fights_page(refresh, callback) {
if (refresh || !$('.fight-opp').length) {
console.log('Loading opponents...');
load_page(fightUrl,function(rt,ts,r) {
if ($('.hospital-block').length) {
console.log('Not enough health to continue fighting.');
hospital_callback();
return;
}
if (typeof callback == 'function') { callback(rt,ts,r); }
});
return true;
}
if (typeof callback == 'function') { callback(); }
return false;
}
function find_fight() {
if (!is_fights_page()) { load_fights_page(false, find_fight); return; }
console.log('Finding opponent to fight...');
var userData = get_user_data();
var fightData = get_fights_data()[0];
if (should_fight(userData,fightData)) {
console.log('Fighting',fightData.name,', level',fightData.level,', mafia size',fightData.mafia,fightData);
load_page(fightData.fightAction,fight_callback);
} else {
console.log('No opponents weak enough to fight.');
load_fights_page(true, find_fight);
}
}
function fight_callback() {
if ($('.hospital-block').length) {
console.log('Not enough health to continue fighting.');
hospital_callback();
return;
}
var fightResult = get_fight_result();
console.log('Fight finished. Result:',fightResult);
}
function hospital_callback() {
var healButton = $('.hospital-block .heal-button');
var healCost = parseInt(/([0-9,]+)/.exec(healButton.text())[0].replace(',',''))
console.log('Healing costs V$',healCost);
}
function get_user_data() {
var avatarStats = $('.avatar-info span');
var fightStats = $('#main .ind-stat');
var userData = {name: $(avatarStats[0]).text().trim(), level: parseInt($(avatarStats[1]).text().trim()), attack: parseInt($(fightStats[0]).text().trim()), defense: parseInt($(fightStats[1]).text().trim()), mafia: parseInt($(fightStats[2]).text().trim())};
userData.estimatedStrength = get_estimated_strength(userData);
return userData;
}
function get_fights_data() {
var fights = $('.fight-opp');
var fightData = [];
for (var i=0;i<fights.length;i++) {
var fightStats = $(fights[i]).find('.fight-opp-stats div');
fightData[i] = {name: $(fightStats[0]).text().trim(), level: parseInt($(fightStats[1]).text().trim()), mafia: parseInt($(fightStats[2]).text().trim()), fightAction: $(fights[i]).find('.fight-action a[href]').attr('href')}
fightData[i].estimatedStrength = get_estimated_strength(fightData[i]);
}
fightData.sort(function(a,b) {
if (!a.estimatedStrength || a.estimatedStrength > b.estimatedStrength) return 1;
if (a.estimatedStrength < b.estimatedStrength) return -1;
return 0;
});
return fightData;
}
function get_fight_result() {
var imageSrc = $('.fight-results-stat div div img').attr('src');
if (imageSrc.indexOf('lost') !== -1) { return 'lost'; }
if (imageSrc.indexOf('won') !== -1) { return 'won'; }
if (imageSrc.indexOf('iced') !== -1) { return 'iced'; }
return null;
}
function get_estimated_strength(fightData) {
return (fightData.mafia * estimateStrengthWeights.mafia) + (fightData.level * estimateStrengthWeights.level);
}
function should_fight(userData,opponentData) {
return ((userData.estimatedStrength - opponentData.estimatedStrength) / userData.estimatedStrength > strengthDifferenceThreshold);
}
this.find_fight = find_fight;
};
MafiaWarsMobileFightBot = new MafiaWarsMobileFightBot();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment