var Rma = Rma || {};
Rma.debug = Rma.debug || false;
Rma.settings = Rma.settings || {};
(function($) {
Rma.obj = function() {
function getGameSettings(callback) {
function Callback() {
if(Rma.debug) console.info('Rma.settings.game : ',Rma.settings.game);
callback();
};
if(Rma.settings.game != null) {
Callback();
return;
}
$.ajax({
type: 'GET',
url: '/interface.php?func=get_config',
dataType: 'xml',
success: function(xml){
var $game = $(xml).find('game');
var archer = $game.find('archer').text();
var knight = $game.find('knight').text();
Rma.settings.game = {
archer:archer == 1,
knight:knight > 0
};
Callback();
},
error: function() {
UI.ErrorMessage('An error occurred while processing XML file.');
}
});
}
function getDefaultSettings() {
return '[1,[["***Noble*** ({coords})",[["snob","gte","1"]]],["===OFF=== ({coords})",[["ram","gt","200"]]],["===OFF=== ({coords})",[["axe","gt","2000"],["light","gt","500"]]],["Fakes ({coords})",[["ram","e","1"]]],["Fakes ({coords})",[["catapult","e","1"]]],["Cata ({coords})",[["catapult","gte","1"],["axe","lt","200"],["light","lt","100"]]],["Pillage ({coords})",[["spy","gte","1"],["light","gte","1"]]],["Scout ({coords})",[["spy","gte","1"],["ram","e","0"],["axe","e","0"],["light","e","0"]]]]]';
}
function generateRmaSettings() {
if(typeof(rmaSettings) == 'undefined' || rmaSettings == null || rmaSettings == '')
rmaSettings = getDefaultSettings();
Rma.settings.rma = null;
try {
Rma.settings.rma = JSON.parse(rmaSettings);
}
catch(e){}
if(Rma.debug) {
console.info('Rma.settings.rma : ',Rma.settings.rma);
}
}
function getValues($row) {
var units = ['spear','sword','axe','archer','spy','light','marcher','heavy','ram','catapult','knight','snob'];
var $cells = $row.find('td:gt(2)');
var values = {};
var diff = 0;
$.each(units,function(index,unit) {
if((unit == 'archer' || unit == 'marcher') && !Rma.settings.game.archer)
diff ++;
else if((unit == 'knight') && !Rma.settings.game.knight)
diff ++;
else
values[unit] = parseInt($($cells[index - diff]).text());
});
if(Rma.debug) {
console.info('$row : ',$row,' | values : ',values);
}
return values;
}
function getCoordinates(text) {
var matches = text.match(/([0-9]{1,3}\|[0-9]{1,3})/g);
if(matches.length >= 1) {
return matches[0];
}
return '';
}
function getAttackName($row) {
return $row.find('.quickedit-label').text();
}
function editAttackName($row) {
var $button = $row.find('.rename-icon');
$button.click();
}
function renameAttackName($row,name) {
var $input = $row.find('.quickedit-edit input[type="text"]');
$input.val(name);
}
function submitAttackName($row) {
var $button = $row.find('.quickedit-edit input[type="button"]');
$button.click();
}
function findRule(values) {
var searchRule = null;
var rules = Rma.settings.rma[1];
for(var i=0;i<rules.length;i++) {
if(Rma.debug) {
console.info('Test rule : ',rules[i]);
}
var conds = rules[i][1];
var allCondsOk = true;
for(var j=0;j<conds.length;j++) {
if(Rma.debug) {
console.info('Test cond : ',conds[j]);
}
var unit = conds[j][0];
var operator = conds[j][1];
var nb = conds[j][2];
var condOk = false;
if(operator == 'e' && values[unit] == nb) {
condOk = true;
}
else if(operator == 'lt' && values[unit] < nb) {
condOk = true;
}
else if(operator == 'lte' && values[unit] <= nb) {
condOk = true;
}
else if(operator == 'gt' && values[unit] > nb) {
condOk = true;
}
else if(operator == 'gte' && values[unit] >= nb) {
condOk = true;
}
if(!condOk) {
allCondsOk = false;
if(Rma.debug) {
console.info('cond pas ok');
}
break;
}
else {
if(Rma.debug) {
console.info('cond ok');
}
}
}
if(allCondsOk) {
searchRule = rules[i];
break;
}
}
if(Rma.debug) {
console.info('Rule found : ',searchRule);
}
return searchRule;
}
function process() {
var $rows = $('tr[class*="nowrap"]');
$rows.each(function() {
var $row = $(this);
var name = getAttackName($row);
if(name.indexOf('Attaque sur') > -1) {
var values = getValues($row);
var coords = getCoordinates(name);
/////////////////////////////////////
// Search correct rule
var rule = findRule(values);
if(rule != null) {
var newName = rule[0];
newName = newName.replace('{coords}',coords);
editAttackName($row);
renameAttackName($row,newName);
submitAttackName($row);
}
}
});
}
function init() {
if(game_data.screen == 'overview_villages' && game_data.mode == 'commands') {
generateRmaSettings();
if(Rma.settings.rma != null)
getGameSettings(process);
else
UI.ErrorMessage('La configuration utilisée est incorrecte');
}
else {
UI.ErrorMessage('Ce script doit être lancé depuis l\'aperçu ordres');
}
}
///////////////////////////////////////////////////
// Return
return {
init: init
};
};
})(jQuery);
var rma = new Rma.obj();