Two same objects show different result



  • @mrfaul I was able to fix the post



  • Ah here we go, much better.

    OK don't use const since that is a constant and as such definitive (at least it should be) use instead let.
    The benefit of let is, that it is only available inside the block it was declared in so it can be easily collected when the block is done.

    Don't use var since those are global.
    Also put at the beginning of your files: "use strict"; this will throw errors if you have wired variable declarations.

    But are you sure you don't have any creeps that get counted?



  • @mrfaul said in Two same objects show different result:

    "But are you sure you don't have any creeps that get counted?";

    of course there are no creeps you see in cycle 0 itself, but when outside the cycle this field shows 1



  • It doesn't work because of this: for (const i in rolesRoom.rooms)

    In roles.js, which is where that comes from, it's defined as: rooms: [...stuff...];

    Meaning roleRoom.rooms is an array, not an object. Sure, the overall container it's in is an object... but rooms is an array. So you use the of iterator, like: for (const i of rolesRoom.rooms)

    To be honest? I could feel myself descending into madness just trying to figure out what that code does. It could probably be a lot simpler. Not that I'm judging... I've certainly written code I've looked at later and wondered what was going on.



  • @smokeman said in Two same objects show different result:

    Meaning roleRoom.rooms is an array, not an object. Sure, the overall container it's in is an object... but rooms is an array. So you use the of iterator, like: for (const i of rolesRoom.rooms)

    I did so and the result is the same

    for (const obj of rolesRoom.rooms) {
          createRoles[obj.name] = rolesRoom.levels[obj.level];
    }
    

    To be honest? I could feel myself descending into madness just trying to figure out what that code does. It could probably be a lot simpler. Not that I'm judging... I've certainly written code I've looked at later and wondered what was going on.

    sorry but I'm just learning)) I think it's better than nothing



  • I'm not seeing anything else wrong. I think I see what you're trying to do? And it should work?

    I would start putting in console.logs all over to check the assumptions as the algo goes along. Somewhere it must be going off the rails.





  • maybe another ideas ?



  • Wanna know why programming is so hard? It's because it relies almost exclusively on a single skill: Problem solving.

    The core of problem solving is to isolate the problem in the first place. Don't overly assume unless those assumptions are proven. Once an algo / section of code is nailed down and known to work, THEN you can compress it with assumptions and terse syntax. Not before.

    From the screen shot, I can see that both bits of code (That produce different results.) are executing in the same tick. Ok, that's a set of assumptions that can be removed (The required array isn't changing mid stream.)

    The lines of code that produce the results are different. Either the result was changed twice, or the line of code inside the loop is treated differently because it has an if statement wrapping it. Are there more than one source of: "roles['E59S58'].upgrader" ? If the answer is no, then the results should be the same and the error is you're overwriting the resulting value.

    Start by losing the if around the first result test. If it's still different, then you have to work back in the loop, stretching out the code and logging out the intermediate results so they can be verified. Use "JSON.stringify(object);" to log out the contents of an object.



  • OK if you want to isolate problems, spam your console.
    So replace your script with this and tell us what happens:

    "use strict";
    
    let rolesRoom = require('roles');
    
    let createRoles = {}; // here I am creating a new object with which it does not work
    for (let i in rolesRoom.rooms) {
        createRoles[rolesRoom.rooms[i].name] = rolesRoom.levels[rolesRoom.rooms[i].level];
    }
    
    // let roles = createRoles; // if uncomment it will be new code
    let roles = rolesRoom.role; // Here i import old code
    
    // подсчитываем количество крипов в руме
    for (let spawnRoom in roles) {
        for (let roleName in roles[spawnRoom]) {
            roles[spawnRoom][roleName].currentQuantity = _.sum(Game.creeps, (c) => {
                let test = c.memory.role === roleName && c.memory.spawnRoom === spawnRoom;
                console.log("Test for creep: ",c.name," is ",toString(test));
                return test;
            });
    
            if (spawnRoom === 'E59S58' && roleName === 'upgrader') {
                console.log("Inside loop: ",roles['E59S58'].upgrader.currentQuantity);
                // old code - 0
                // new code - 0
                // if here old code it will be 0, and if will be new code it will be 0
            }
        }
    }
    
    console.log("Outside loop: ", roles['E59S58'].upgrader.currentQuantity)
    // old code - 0
    // new code - 1  (why????????????)
    // if here old code it will be 0, but if will be new code it will 1 why is this so ???
    

    All I did is named your logs and added a additional inside the sum, will spam your console but should add clarity what is happening.



  • @mrfaul Here: alt text

    When I comment this "console.log("Test for creep: ",c.name," is ",toString(test)); "

    alt text



  • @smokeman alt text here you can see the objects are completely identical, but show different results

    I was thinking if you have the opportunity, maybe you could look with teamviawer?



  • It is quite obvious that something isn't working, object Undefined isn't a option, it should be either true or false.
    This means the boolian check above fails, replace the log with this one:

    console.log("role: ",c.memory.role," spawn: ", c.memory.spawnRoom," test: ",toString(test));
    

    But honestly the way you are counting your creeps is very very inefficient.
    You are iterating for each role in each room through your entire rooster of creeps: rooms * roles * creeps = CPU waste
    You should instead iterate through your creeps once and count what they do in a fresh temporary object and use that to update your roles,
    or better leave that temporary since it is a snapshot of the current tick anyway don't bother to save that.



  • @mrfaul ok then i try to change my code ! thank you



  • @mrfaul I started to count differently and the problem is the same, with the old way everything is fine, with the new problem)))))))) can say why?)))))

    alt text

    const createRoles = {};
    
    for (const i in rolesRoom.rooms) {
    createRoles[rolesRoom.rooms[i].name] = rolesRoom.levels[rolesRoom.rooms[i].level];
    }
    
     const roles = createRoles;
    // const roles = rolesRoom.role;
    // console.log(JSON.stringify(roles))
    // подсчитываем количество крипов в руме
     for (const spawnRoom in roles) {
    for (const roleName in roles[spawnRoom]) {
      roles[spawnRoom][roleName].currentQuantity = 0;
    }
    }
    
     for (const i in Game.creeps) {
    const creep = Game.creeps[i];
    roles[creep.memory.spawnRoom][creep.memory.role].currentQuantity += 1;
    if (creep.memory.spawnRoom === 'E59S58' && creep.memory.role === 'upgrader') {
      // console.log(creep.memory.spawnRoom)
      // console.log(creep.memory.role)
      console.log(creep) // we only come here once
    }
    }
    console.log(roles['E59S58']['upgrader'].currentQuantity) // here 4 why ?????????????


  • alt text alt text



  • You might want to check if your room/role combination is valid and don't blindly trust the incremental operators, sometimes they behave wired for no apparent reason:

    if( roles[creep.memory.spawnRoom] && roles[creep.memory.spawnRoom][creep.memory.role] ){
    roles[creep.memory.spawnRoom][creep.memory.role].currentQuantity = 
    roles[creep.memory.spawnRoom][creep.memory.role].currentQuantity + 1;
    }
    

    But if that still dosn't work please dump your entire main somewhere like pastbin etc.



  • @mrfaul no it doesn't work )))))))))))))))) https://pastebin.com/AgCJZ4y6



  • Sorry for the late answser I was busy

    hmm indeed I see nothing in there why it shouldn't work, but I have a idea why it could be buggy:
    You only set the roles quantity to "0" but that doesn't guaranties that there are no wild "undefined" variables when you loop through your creeps.

    Try this:

    for (const i in Game.creeps) {
            const creep = Game.creeps[i];
            let currentQuantity = roles[creep.memory.spawnRoom][creep.memory.role].currentQuantity || 0; // use the saved variable or "0"
            ++currentQuantity; // increment
            roles[creep.memory.spawnRoom][creep.memory.role].currentQuantity = currentQuantity; // save
    
            if (creep.memory.spawnRoom === 'E59S58' && creep.memory.role === 'upgrader') {
                 console.log(roles['E59S58']['upgrader'].currentQuantity)
            }
        }
    

    Using this method you can also get rid of the additional for loop that only sets it to zero.
    One thing less that takes up time.



  • @kepamuk @o4kapuk jo apparently the last picture:

    http://dl4.joxi.net/drive/2020/01/31/0011/2711/772759/59/69625b05ca.jpg

    Is contaminated with some malicious software and got flagged by our GData Enterprise Suite... So it could be that the host server or your private machine is compromised pls check that.

    Update: Or GData is just beeing GData 😑 since I double checked it and only 2 out of 73 scanner flagged it.
    Man that software is annoying, but better than nothing. At least it is doing it's job... sorta.