magwo/elevatorsaga

People don’t reconsider entering a stopped elevator when it changes its up/down lights

roryokane opened this issue · 9 comments

Hacker News commenter LoSboccacc mentioned a bug 5 days ago where people don’t reconsider entering the elevator when an elevator changes its up/down indicator lights while on a floor, even though that should tempt some people into entering the elevator.

After that comment was written, #18 “People not getting on elevator” was fixed with 6ce62a1. But I don’t think that 6ce62a1 fixes this issue, because in that change’s code, I don’t see any events triggered by changes in an elevator’s up/down indicator lights (the indicatorstate_change event).

Hmmmm ok..

I can confirm that this issue still exists. Here is a test case that demonstrates this behavior. Try running it on challenge 2.

{
    init: function(elevators, floors) {
        var elevator = elevators[0];

        var isWaitingForDownHallCall = true;

        elevator.on("idle", function() {
            if (isWaitingForDownHallCall) {
                elevator.goingUpIndicator(false);
                elevator.goingDownIndicator(false);
            } else {
                elevator.goingUpIndicator(false);
                elevator.goingDownIndicator(true);
            }
        });

        floors.forEach(function(floor) {
            floor.on("up_button_pressed", function() { /* nothing */ });
            floor.on("down_button_pressed", function() {
                elevator.goToFloor(floor.floorNum());
                isWaitingForDownHallCall = false;
            });
        });

        // if this is added, the user gets on properly
        /*
        elevator.on("stopped_at_floor", function(floorNum) {
            elevator.goingUpIndicator(false);
            elevator.goingDownIndicator(true);
        });
        */
    },
    update: function(dt, elevators, floors) { /* nothing */ }
}

When running this code, the elevator arrives at a floor where someone wants to go down, with its indicator lights turned off. After it becomes idle, the elevator changes its indicator lights to say it is going down, the direction the user wants to go. But the user does not get on, even though they should.

This problem only happens when the lights change during the elevator’s idle event. If the elevator changes its lights during the stopped_at_floor event, which happens earlier, the user gets on properly.

Mmm.. yes... thanks for the detailed description. I understand the problem, but I'm not sure if it's a bug or an enhancement request. It's not obviously broken. Hmm.

I notice this today as well - it seems a lot like a bug. Here's the code when the elevator light changes:

    elevator.on("change:goingUpIndicator", function(value){
        elevator.trigger("indicatorstate_change", {up: elevator.goingUpIndicator, down: elevator.goingDownIndicator});
    });

    elevator.on("change:goingDownIndicator", function(value){
        elevator.trigger("indicatorstate_change", {up: elevator.goingUpIndicator, down: elevator.goingDownIndicator});
    });

This fires an event, presumably allowing the model to listen and have people enter that elevator now that it's changed. It looks like it was forgotten, because nothing (besides the UI for lighting up the light) actually listens to that event.

Hi!

I have encountered a similar problem: If an elevator idle on a floor (up and down indicator lights off), and someone presses a button on that floor, the indicator turn on but the passengers don't step into the elevator.
Here is a simple code to reproduce the problem:

{
    init: function(elevators, floors) {
        var elevator = elevators[0];
        elevator.goingUpIndicator(false);
        elevator.goingDownIndicator(false);
        floors[0].on("up_button_pressed", function() {
            elevator.goingUpIndicator(true);
        });
    },
    update: function(dt, elevators, floors) {
    }
}

With this code on challenge 1, one could expect the passenger enters the elevator at floor 0, but actually it stands still after he pressed the up button and the indicator light turned on.

Yeah hmmm....

Trying to think of an elegant solution.

I've noticed this as well, currently my workaround solution has been-- when stopped at a floor and changing indicator lights, call goToFloor( elevator.currentFloor() );

This causes whatever internal mechanism that triggers AI to walk into elevator to fire a second time after the indicator lights have changed.

Might give you a hint how to fix internally.

Many thanks keless! This bug really bothers me.