Picture of problem: http://puu.sh/5spfQ.png
My first question here. I hope I can explain it to a decent extent, to give you an idea of what I'm trying to do.
I am working on a small javascript game, where the only objective is to make money. One of the methods to make money is by hiring workers. However, there's a problem. For instance we have Worker A and Worker B. For some odd reason, whenever Worker B mines something, Worker A also receives the mined ore (so both seem to always have the same amount of ore). I can't seem to figure out why this is; they should not be receiving each others ore.
Note: >hired< workers are stored in the "employed" variable.
The fun part, the code:
*This function fills in placeholders to the employed variable for each possible worker for hire. *
//add "placeholder data" to prevent future undefined/errors
//and makes future modification easier, specially in workerMain()
var o = [];
for(ore in ores){o[ore] = 0;}
for(w in workers){
employed[w] = [0, o];
}
So our employed variable may look something along the lines of:
('miner a' => [0, ['coal_ore' => 0,'diamond_ore' => 0]], 'miner b' => [0, ['coal_ore' => 0, 'diamond_ore' => 0]])
Now once they are hired, a loop that is ran every second will determine how much ore they have mined, and it should put any worker's mined ore in their respective spot.
function workerMain(){
setTimeout(function(){
for(e in employed){
var wObj = workers[e];
var orePerSecond = Math.ceil(wObj.opm/60)*employed[e][0];
var oresMined = generateOres(orePerSecond, wObj.pickaxe);
//add newly mined ore count to worker's ore
for(ore in oresMined){
employed[e][1][ore] += oresMined[ore];
//we aren't storing the ore, so let's go ahead and sell it
money += ores[ore].worth*oresMined[ore];
}
}
updateValues();
workerMain();
}, 1000);
}
But again, somehow if Miner A mines a coal ore, somehow Miner B will also get that coal ore.
Hope I've explained my problem thoroughly. If you can't seem to find the problem within this code, it may lie within these sibling functions: