My HTML has three divs, each id tagged with the logic behind my object (ma/shadow/traps) which contain 10 div each (every icons), also ID-tagged according to the object. On click, I'm gathering the ID and the parent ID to traverse the object and find the value.
It's only possible to raise the value if the pre-requirement is not 0, or if the pre-requirement is false (no pre-req). My problem comes when there are 2 pre-requirements which need to be checked. I check the first one, and if the first meets the pre-req, I check if a second one exists, and check it.
There has to be a better way of checking on variables than this nightmare of if
s. It works, but I don't think it's good code at ALL. I'm not looking for a full copy/paste code here, but more for a more efficient logic on how to do what I'm already doing.
$(function () {
var basemin = 0;
var basemax = 20;
var skill = {
ma:{
//sample object
},
shadow:{
clawMastery:{
base:0,
preReq:false
}, psychicHammer:{
base:0,
preReq:false
}, burstOfSpeed:{
base:0,
preReq:'clawMastery'
}, weaponBlock:{
base:0,
preReq:'clawMastery'
}, cloakOfShadows:{
base:0,
preReq:'psychicHammer'
}, fade:{
base:0,
preReq:'burstOfSpeed'
}, shadowWarrior:{
base:0,
preReq:'cloakOfShadows',
preReq2:'weaponBlock'
}, mindBlast:{
base:0,
preReq:'cloakOfShadows'
}, venom:{
base:0,
preReq:'fade'
}, shadowMaster:{
base:0,
preReq:'shadowWarrior'
}
},
traps:{
//sample object
}
};
$('#tree div').bind("contextmenu", function (e) {
e.preventDefault();
});
$('#tree .tab div').mousedown(function (e) {
var $this = $(this);
$this.attr('unselectable', 'on').css('UserSelect', 'none').css('MozUserSelect', 'none');
var $tab = $this.parent().attr("id");
var $skill = $this.attr("id");
function checkPreReq(e) {
var $preReq = skill[$tab][e]['preReq'];
var $preReq2 = skill[$tab][e]['preReq2'];
alert($preReq);
if ($preReq === false) {
alert('no pre-req');
return true;
} else if ($preReq !== false) {
alert('pre-req1 exists');
if(skill[$tab][$preReq]['base'] > 0) {
alert('pre-req1 higher than 0');
if ($preReq2 === undefined) {
alert('no second pre-req, go on');
return true;
} else {
alert('check second pre-req value');
if (skill[$tab][$preReq2]['base'] > 0) {
alert('pre-req2 higher than 0');
return true;
} else {
alert('pre-req2 not met (0)');
return false;
}
}
} else {
alert('pre-req1 not met (0)');
return false;
}
} else {
alert('else');
return false
}
}
if (e.which == 1) {
//leftclick
if (skill[$tab][$skill]['base'] < basemax && checkPreReq($skill)) {
skill[$tab][$skill]['base'] += 1;
//$rem_skills -= 1;
}
} else if (e.which == 3) {
//rightclick
if (skill[$tab][$skill]['base'] > basemin) {
skill[$tab][$skill]['base'] -= 1;
//$rem_skills += 1;
}
}
$this.find(".lvl").text(skill[$tab][$skill]['base']);
//$("#output").find(".rem_skills").text($rem_skills);
});
});