// ==UserScript==
// @name           street-fights
// @namespace      http://apps.facebook.com/street-fights/
// @include        http://apps.facebook.com/street-fights/*
// ==/UserScript==
// AutoStreeFights V1.1 by 蛋製品 20090509
// http://bg.9sweb.com/2009/05/blog-post.html
//20090727 新增判斷載入頁面錯誤
//20090724 修復不能自動攻擊

var page_details = 'http://apps.facebook.com/street-fights/details.php';
var page_job     = 'http://apps.facebook.com/street-fights/job.php';
var page_bank    = 'http://apps.facebook.com/street-fights/bank.php';
var page_fight   = 'http://apps.facebook.com/street-fights/fight.php';
var backurl = 'http://9sweb.com/facebook/street-fight/?v1.1&';
var info = Array();
var timer_sec = 500;
var item = Array();
var thishtml = document.body.innerHTML;
var tools_html = document.createElement('div');

tools_html.id               = 'hack_tools';
tools_html.style.top        = '300px';
tools_html.style.right      = '2px';
tools_html.style.position   = 'absolute';
tools_html.style.color      = '#FFFFFF';
tools_html.style.background = '#000000';
tools_html.style.width      = '200px';
tools_html.style.zIndex      = 100;
window.parent.document.body.appendChild(tools_html);
tools_html.innerHTML = '';


page_error();
bot_action();
window.setTimeout(function() {location.href = backurl}, 20000);

//window.setTimeout(function() {timer();},500);


function timer()
{
	tools_html.innerHTML = location.href;
	timer_sec--;
	//tools_html.innerHTML = timer_sec;
	//window.setTimeout(function() {timer();},1000);
}


function bot_action()
{
	var u = location.href;
	var re = /.*\/.*\/(.*)\.php/;
	var newstr = u.match(re);
	tools_html.innerHTML = 'action:'+newstr[1]+'<br>';
	
	switch(newstr[1])
	{
		case 'details':
			back2main();
		break;
		
		case 'bank':
			save_money();
		break;
		
		case 'job':
			do_job();
		break;
		
		case 'jail':
			jail();
		break;
		
		case 'fight':
			var pagenum = '';
			var b = u.match(/.*\/.*\/fight\.php(\?b\=[0-9]{0,3})?/);
			if(b[1])
			{
				pagenum = b[1].match(/\?b\=([0-9]{0,3})/);
				pagenum = pagenum[1];
			}
			check_fight(pagenum);
		break;
	}
}

function jail()
{
	var jail_btn = document.getElementsByClassName('bonus_button');
	var evt = document.createEvent("MouseEvents");
	evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	jail_btn[1].dispatchEvent(evt);	
}

function save_money()
{
	var money = parseInt(document.getElementsByName("amount")[1].value,10);
	if(money == 0)
	{
		back2main();
		return;
	}
	document.getElementsByName("deposit")[0].click();
}

function do_job()
{
	var info = thishtml.match(/體力: ([0-9]{0,10})\//);
	if(info[1] == 10)
	{
		var job_div = document.getElementById('app17326627347_mod_action-5');
		var action_link = job_div.getElementsByTagName('a')[0];
		var evt = document.createEvent("MouseEvents");
		evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	  action_link.dispatchEvent(evt);	
	  return;
	}
	back2main();
}

function check_fight(b)
{
	var chk1 = thishtml.match(/精神: ([0-9]{0,10})\/[0-9]{0,10}/);
	var chk2 = thishtml.match(/生命: ([0-9]{0,10})\/[0-9]{0,10}/);
	var chk3 = thishtml.match(/等級: ([0-9]{1,3}) /);
	var lv = thishtml.match(/等級: \<span class=\".*\"\>([0-9]{0,10})\<\/span\>/g);
	var na = thishtml.match(/\<div class=\"mod_info\"\>\<a.*\>(.*)\<\/a\>/g);
	var st = thishtml.match(/狀態: \<span class=\".*\"\>(.*)\<\/span\>/g);
	var chk_lv = new Array();
	var chk_na = new Array();
	var chk_st = new Array();
	var fight_user = chk3[1];
	var now_fight = '';
	for(var i = 0;i<5;i++)
	{
		chk_lv[i] = lv[i].match(/等級: \<span class=\".*\"\>([0-9]{0,10})\<\/span\>/);
		chk_na[i] = na[i].match(/\<div class=\"mod_info\"\>\<a.*\>(.*)\<\/a\>/);
		chk_st[i] = st[i].match(/狀態: \<span class=\".*\"\>(.*)\<\/span\>/);
		//chk_lv[i][1] = parseInt(chk_lv[i][1])+1;
		tools_html.innerHTML += '<br>'+i+':'+chk_lv[i][1]+' '+chk_st[i][1]+' '+chk_na[i][1];
		
		if(fight_user >= chk_lv[i][1] && chk_st[i][1] != '重傷' && chk_st[i][1] != '昏迷')
		{
			fight_user = chk_lv[i][1];
			now_fight = i;
		}
	}

	if(now_fight == '' || now_fight > 4)
	{
		if(b == '') b = 0;
		tools_html.innerHTML += '<br>本頁都是強者..換下頁';
		fight_next_page(b);
		return;
	}
	
	tools_html.innerHTML += '<br>攻擊前檢查';
	tools_html.innerHTML += '<br>精神:'+chk1[1]+' 生命:'+chk2[1]+' 等級:'+chk3[1];
	tools_html.innerHTML += '<br>攻擊:'+now_fight+':'+chk_lv[now_fight][1]+' '+chk_st[now_fight][1]+' '+chk_na[now_fight][1];

	if(chk1[1] <1 )
	{
		tools_html.innerHTML += '<br>精神不足';
		back2main();
		return;
	}
	
	if(chk2[1] < 20)
	{
		tools_html.innerHTML += '<br>生命不足';
		back2main();
		return;
	}	
		var fight_div = document.getElementsByClassName('mod_listing')[now_fight];
		var fight_act = fight_div.getElementsByClassName('mod_item')[0].getElementsByTagName('a')[0];
		var evt = document.createEvent("MouseEvents");
		evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	  fight_act.dispatchEvent(evt);	
}

function fight_next_page(b)
{
	b = parseInt(b)+5;
	location.href = page_fight+'?b='+b;
}


function get_detials()
{
	info[1] = thishtml.match(/等級: ([0-9]{1,3}) /);
	info[2] = thishtml.match(/等級: [0-9]{1,3} \(([0-9]{0,10}\/[0-9]{0,10})\)/);
	info[3] = thishtml.match(/生命: ([0-9]{0,10}\/[0-9]{0,10})/);
	info[4] = thishtml.match(/體力: ([0-9]{0,10}\/[0-9]{0,10})/);
	info[5] = thishtml.match(/精神: ([0-9]{0,10}\/[0-9]{0,10})/);
	info[6] = thishtml.match(/現金: \<span class\=\"green\"\>\$(.*)\<\/span\>/);
	//var info7 = thishtml.match(/攻擊力：(.*)\<br \/\>/);
	//var info8 = thishtml.match(/防禦力：(.*)\<br \/\>/);
	
	var ret = true;
	for(var i = 1; i <= 6 ;i++)
	{
		if(info[i][1] == null)
		{
			tools_html.innerHTML += '<br><b>info'+i+' is null</b>';
			ret = false;
		}
		else
		{
			tools_html.innerHTML += '<br>info'+i+':'+info[i][1];
		}
	}
	return ret;
}

function back2main()
{
	if(!get_detials())
	{
		tools_html.innerHTML += 'back2main error';
		return;
	}
	
	for(var i = 1; i <= 6 ;i++)
	{
		backurl += 'info'+i+'='+info[i][1]+'&';
	}
	window.setTimeout(function() {location.href = backurl}, 1000);

}

function page_error()
{
	var title_str = document.title;
	var chk = title_str.match(/.*載入頁面錯誤$/);
	if(chk != null)
	{
		window.setTimeout(function() {location.href = backurl}, 1000);
	}
}