﻿// ==UserScript==
// @name auto Office War
// @namespace hk.orz
// @include http://apps.facebook.com/office-wars/job.php
// @include http://apps.facebook.com/office-wars/bank.php
// Version 1.0
// ==/UserScript==


// Things you can change
//---------------------------------------------------------------------------------------------
const jobid = 6;  //做第幾件工作
const jobspirit = 2;  //工作所需精神
const relaxid = 5;  //減壓工作
const relaxspirit = 1;  //減壓工作所需精神
const maxpressure = 5;  //到達壓力上限會做減壓工作
//---------------------------------------------------------------------------------------------

var last_check = new Date();
var check_time = (last_check.getMinutes() % 5)

// setup a count down box
var htmlElement = document.createElement('div');
htmlElement.id = 'neotimer';
htmlElement.style.font = "11px arial"
htmlElement.style.color = 'black';
htmlElement.style.Top = '3px';
htmlElement.style.Left = '2px';
htmlElement.style.position = 'absolute';
htmlElement.style.width = '200px';
htmlElement.style.height = '42px';
htmlElement.style.paddingTop = '3px';
htmlElement.style.paddingLeft = '2px';
htmlElement.style.background = '#CCFFCC';
htmlElement.style.zIndex = 100;
window.parent.document.body.appendChild(htmlElement);

// start up check
window.setTimeout(function() { tick() }, 300);

function check_and_run() {
  // check spirit
  spirit_tag = document.getElementById('app21745179649_main');
  if ( spirit_tag == null) return;
  str = (new RegExp('精神:[^v]+')).exec(spirit_tag.innerHTML);
  if (str == null) return null;
  spirit = parseInt(str.toString().split(':')[1].split('/')[0]);
  if (spirit < jobspirit) return spirit;
  action = document.getElementById('app21745179649_mod_action-' + jobid);
  if (action == null) return spirit;
  action_link = action.getElementsByTagName('a')[0];
  if (action_link == null) return spirit;

  // click
  GM_setValue('reload', 1);
  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 health;

}

function check_pressure() {
  // check pressure
  pressure_tag = document.getElementById('app21745179649_main');
  if ( pressure_tag == null) return;
  str = (new RegExp('壓力:[^v]+')).exec(pressure_tag.innerHTML);
  if (str == null) return null;
  pressure = parseInt(str.toString().split(':')[1].split('/')[0]);
  if (pressure < maxpressure) return pressure;
  if (spirit < relaxspirit) return pressure;
  action = document.getElementById('app21745179649_mod_action-' + relaxid);
  if (action == null) return pressure;
  action_link = action.getElementsByTagName('a')[0];
  if (action_link == null) return pressure;

  // click
  GM_setValue('reload', 1);
  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 pressure;
}

function check_money() {
  // check money
  money_tag = document.getElementById('app21745179649_main');
  if ( money_tag == null) return;
  str = (new RegExp('現金:[^v]+')).exec(money_tag.innerHTML);
  if (str == null) return null;
  money = parseInt(str.toString().split('$')[1].split('<')[0].replace(/,/,""));
  if (money < 101) return money;
  if (location.href!='http://apps.facebook.com/office-wars/bank.php')
    location.href='http://apps.facebook.com/office-wars/bank.php';
  return money;	
}


function tick() {
  current = new Date();
  seconds = (current - last_check) / 1000;
  spirit = check_and_run();
  pressure = check_pressure();
  money = check_money();

  // display 
  htmlElement.innerHTML = '自動辦工存錢機 v1.0 ' 
  htmlElement.innerHTML += '<br />Time: ' + current.getHours()
  htmlElement.innerHTML += ':' + checkTime(current.getMinutes()) + ':' + checkTime(current.getSeconds());
  if (spirit != null)
    htmlElement.innerHTML += ' <br /> 精神: ' + spirit;
  if (pressure != null)
    htmlElement.innerHTML += ' &nbsp; 壓力: ' + pressure;
  if (money != null)
    htmlElement.innerHTML += ' &nbsp; 現金: ' + money;

  // reload on error
  if (document.getElementById('error_message') != null) {
    GM_setValue('reload', 0);
    location.href='http://apps.facebook.com/office-wars/job.php';
    return;
  }

  // reload if a job ran
  if (GM_getValue('reload') == 1) {
    GM_setValue('reload', 0);
    location.href='http://apps.facebook.com/office-wars/job.php';
    return;
  }  
  
  // reload every 10 minutes, so that hourly income will save to bank and reset the script from error
  if (((current.getMinutes() % 5) == check_time ) && (current.getSeconds()<=1)) {
    if (location.href!='http://apps.facebook.com/office-wars/bank.php') {
      GM_setValue('reload', 0);
      location.href='http://apps.facebook.com/office-wars/bank.php';
      return;
	}
  }


  // click the deposit button if there is money to save
  if (location.href=='http://apps.facebook.com/office-wars/bank.php')
    if (document.getElementsByName("amount").length > 1) {
      if (parseInt(document.getElementsByName("amount")[1].value) > 100) {
		document.getElementsByName("deposit")[0].click();
		return;
	  } else {
		GM_setValue('reload', 0);
		location.href='http://apps.facebook.com/office-wars/job.php';
		return;
      }	
	} else {
		GM_setValue('reload', 0);
		location.href='http://apps.facebook.com/office-wars/job.php';
		return;
    }
  
  
  // come back again every second, looks cool
  window.setTimeout(function() { tick()}, 1037);
}

function checkTime(i) {
  if (i<10) i="0" + i;
  return i;
}
