Free javascript Hosting


JMafiaMikes.js

Uploaded on Sep 17 2009 09:37 by dudge669

/* Changes:
1.03j Modified to work with new travel menu - Josh Miller http://facebook.com/Dudge669
1.03 Check for new "Take" which prevents buying until Take is collected
1.02 Check for MW error page
Check to make sure we're in NY
1.01 Slight change to the url to include ajax=1 (gives a simpler
page on return)
*/
javascript:(function() {
var version='MafiaMikes 1.03 -=Josh Miller Mod=-',
m, s, d,
xmlHTTP,
last_url = null,
retries = 0,
buy_prop = 'http://apps.facebook.com/inthemafia/remote/html_server.php?xw_controller=property&xw_action=buy&ajax=1&amount=',
mike_url = buy_prop + '1&property=13&tmp=',
lots_url = buy_prop + '10&property=1&tmp=',
mikes_bought = 0,
money_spent = 0,
tmp = "",
resume_msg,
content = $('app10979261223_content_row'),
mwtools_div = $("mwtools");
if (!mwtools_div) {
mwtools_div=document.createElement("div");
mwtools_div.setAttribute('id','mwtools');
mwtools_div.innerHTML='<table class="messages"></table>';
content.insertBefore(mwtools_div,content.firstChild);
}
mwtools_div.innerHTML=
'<style type="text/css">'+
'.messages img{margin:0 3px;}'+
'#pl{display:none}'+
'</style>'+
'<table class="messages">'+
'<tr>'+
'<td><a href="http://vern.com/mwtools/">Vern\'s MW Tools</a></td>'+
'<td class="title">'+version+'</td>'+
'<td style="text-align:right">'+
'<a href="#" id="pa"><img src="http://vern.com/mwtools/pa.gif" title="Pause" width="14" height="14"></a>'+
'<a href="#" id="pl"><img src="http://vern.com/mwtools/pl.gif" title="Play" width="14" height="14"></a>'+
'<a href="#" id="cl"><img src="http://vern.com/mwtools/cl.gif" title="Close" width="14" height="14"></a>'+
'<form name="mwtform">'+
'<input type="hidden" name="go" id="go" value="1">'+
'</form>'+
'</td>'+
'</tr>'+
'<tr>'+
'<td width="20%">Mikes bought:</td>'+
'<td id="mikes_bought"></td>'+
'</tr>'+
'<tr>'+
'<td>Money spent:</td>'+
'<td id="money_spent"></td>'+
'</tr>'+
'<tr>'+
'<td>Status:</td>'+
'<td id="status" colspan="2"></td>'+
'</tr>'+
'</table>';

// if there's a Fly to NY button, we're not in NY
//if ($("app10979261223_button_travel_nyc").style.display == "block") {
if($("app10979261223_nav_link_properties").style.display != "inline") {
msg("You must first travel to New York before running this bookmarklet");
alert("You must first travel to New York before running this bookmarklet");
return;
}

function get_xmlHTTP () {
// Method 1: Works in all new browsers
if (window.XMLHttpRequest)
return new XMLHttpRequest();
// Method 2: Works in IE 5 & 6
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
return null;
}

function request(url) {
if (document.mwtform.go.value == 1) {
xmlHTTP.onreadystatechange = state_change;
xmlHTTP.open("GET", url, true);
xmlHTTP.send(null);
} else {
resume_msg=$('status').innerHTML;
msg('Paused');
}
last_url = url;
}

function retry(s) {
if (retries > 9) {
msg(s + '; not retrying any more.');
} else {
setTimeout(function(){
retries++;
msg(s + '; retry #' + retries + '...');
request(last_url);
}, 3000);
}
}

$("cl").onclick=function(e) { /* close */
$("app10979261223_content_row").removeChild($("mwtools"));
}

$("pl").style.display = 'none';
$("pl").onclick=function(e) { /* play */
document.mwtform.go.value = 1;
$("pl").style.display = 'none';
$("pa").style.display = 'inline';
msg('Resuming: '+resume_msg);
request(last_url);
}

$("pa").onclick=function(e) { /* pause */
document.mwtform.go.value = 0;
$("pl").style.display = 'inline';
$("pa").style.display = 'none';
}


function commas(s) {
while (d=/(\d+)(\d{3}.*)/.exec(s)) {
s = d[1] + ',' + d[2];
}
return s;
}


function msg(s) {
$('mikes_bought').innerHTML = commas(mikes_bought);
$('money_spent').innerHTML = '$' + commas(money_spent);
$('status').innerHTML = s;
}


function p_int(s) {
return parseInt(s.replace(/,/g, ''));
}

function state_change() {
if (xmlHTTP.readyState == 4) { // 4 = "loaded"
if (xmlHTTP.status == 200) { // 200 = OK
s = xmlHTTP.responseText;
delete xmlHTTP['onreadystatechange'];
if (m = /You just bought 1 Mafia Mike.s for \$([\d,]+)/.exec(s)) {
mikes_bought++;
money_spent += p_int(m[1]);
msg('Bought Mafia Mike for $' + m[1] + '; trying one more...');
request(mike_url+tmp);
} else if (m = /You just bought 10 Abandoned Lots for \$([\d,]+)/.exec(s)) {
money_spent += p_int(m[1]);
msg('Bought 10 Abandonded Lots for $' + m[1] + '; let us buy more Mikes...');
request(mike_url+tmp);
} else if (/You need an Abandoned Lot/.test(s)) {
msg('Need Abandoned Lots, buying ten...');
request(lots_url+tmp);
} else if (/You have not recruited/.test(s)) {
msg('Recruit more mafia, then rerun the bookmarklet.');
} else if (/You need more cash/.test(s)) {
msg('Get more cash, then rerun the bookmarklet.');
} else if (/Make sure to collect your daily take/.test(s)) {
msg('You need to <a href="http://apps.facebook.com/inthemafia/remote/html_server.php?xw_controller=property&xw_action=view">collect your take</a> before buying new property.');
} else if ((tmp == "") && (m=/action=buy.*tmp=([^\&]*)\&/.exec(s))) {
tmp=m[1];
msg('Retrieved new tmp key, buying Mafia Mikes...');
request(mike_url+tmp);
} else if (/Error while loading page/.test(s)) {
msg('Mafia Wars is having problems, pausing...');
setTimeout(function(){
request(last_url);
},20000);
return;
} else {
retry('Unknown response <a href="'+last_url+'">last_url</a>');
return;
}
retries = 0;
}
else {
retry("Problem retrieving data");
return;
}
}
}


xmlHTTP = get_xmlHTTP();
if (!xmlHTTP) {
alert("Your browser does not support XMLHTTP.");
return;
}

request(mike_url);
msg('Buying Mafia Mike...');

} ());

Back to list