Code:
/*!
* Auto Farm for Grepolis
* Relaxeaza, 2011
*/
var timeOuts = timeOuts || {};
(function() {
// abandonedId: [ haulType, fromVillageId ]
var targets = {
256: [ 1, 275 ]
},
// haulType's
timers = {
1: [ 3E5, 300 ], // 5 min
2: [ 12E5, 1200 ], // 10 min
3: [ 72E5, 7200 ], // 120 min
4: [ 18E6, 18000 ] // 300 min
},
re = {
moral: /Moral\s(\d+)\%/,
object: /({.*})/,
timer: /(\d+:\d+:\d+)/,
res: /"resources":{"wood":(\d+),"stone":(\d+),"iron":(\d+)}/,
pop: /"population":(\d+)/,
favor: /"favor":(\d+)/
};
function url( type, town ) {
return '/game/farm_town_info?action=' +
( type ? 'pillage_info' : 'claim_load' ) + '&town_id=' +
town + '&h=' + Game.csrfToken;
}
function prepare( target, settings ) {
var moral, next;
$.ajax({
url: url( 1, settings[ 1 ] ),
type: 'post',
data: {
json: '{"id":"' + target + '"}'
},
error: function() {
prepare( target, settings );
},
success: function( data ) {
data = re.object.exec( data )[ 1 ];
data = JSON.parse( data );
moral = Number( re.moral.exec( data.html )[ 1 ] );
next = re.timer.exec( data.html );
complete( target, settings, moral, next );
}
});
return false;
}
function complete( target, settings, moral, next ) {
var args = arguments;
$.ajax({
url: url( 0, settings[ 1 ] ),
type: 'post',
data: {
json: JSON.stringify({
target_id: target,
claim_type: moral && moral > 91 ? 'double' : 'normal',
town_id: Game.townId,
time: timers[ settings[ 0 ] ][ 1 ],
nlreq_id: 0
})
},
error: function() {
complete.apply( null, args );
},
success: function( data ) {
var res = re.res.exec( data );
if ( res ) {
var curr = ITowns.getResources( Game.townId );
if ( curr.wood < res[ 1 ] ) {
ITowns.setResources({
wood: res[ 1 ],
stone: res[ 2 ],
iron: res[ 3 ]
}, Game.townId);
}
if ( !timeOuts[ target ] ) {
setTimeout(function() {
timeOuts[ target ] = false;
prepare( target, settings );
}, timers[ settings[ 0 ] ][ 0 ]);
timeOuts[ target ] = true;
}
} else if ( next && !timeOuts[ target ] ) {
next = next[ 1 ].split( ':' );
next = next[ 0 ] * 36E5 + next[ 1 ] * 6E4 + next[ 2 ] * 1E3;
setTimeout(function() {
timeOuts[ target ] = false;
prepare( target, type );
}, next);
timeOuts[ target ] = true;
}
}
});
}
for ( var id in targets ) {
if ( typeof timeOuts[ id ] === 'undefined' ) {
timeOuts[ id ] = false;
}
prepare( id, targets[ id ] );
}
})();