magwo / elevatorsaga

The elevator programming game!

Home Page:http://play.elevatorsaga.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

elevator.stop passengers do not exit

kalderman opened this issue · comments

Challenge #1

The intent:

  • Stopped at floor 0, travel to floor 2.
  • Stopped at floor 2, travel to floor 0.
  • Passing floor 1, stop if any passenger wants to exit.

Bugs:

  • Passengers do not exit at floor 1.
  • Given the code, I expect no more than one destination, however, elevator.destinationQueue occasionally has two entries with the first being nearly 1 causing the elevator to get stuck at floor 1.

Code to reproduce:

{
    init: function(elevators, floors) {
        Array.prototype.contains = function (item) {
            for (var i = 0; i < this.length; i++)
                if (this[i] === item)
                    return true;
            return false;
        };

        var elevator = elevators[0];

        elevator.on("passing_floor", function(floorNum, direction) {
            console.log("Passing floor: " + floorNum + " with destinationQueue: " + elevator.destinationQueue);
            if(elevator.getPressedFloors().contains(floorNum)){
                /*note:
                    Sometimes destinationQueue has two entries, I assume it shouldn't.
                    When this happens the invalid entry is very close to but not 1 (the floor we are passing).
                */
                //var originalDestination = elevator.destinationQueue[0]; //eventually get's stuck at floor 1
                var originalDestination = elevator.destinationQueue.filter(function(x){ return x === 0 || x === 2; })[0]; //workaround

                elevator.stop(); //clears destinationQueue but passengers do not exit

                elevator.destinationQueue.push(originalDestination); //continue to originalDestination
            }
        });

        elevator.on("stopped_at_floor", function(floorNum) {
            if(floorNum === 0)
                elevator.destinationQueue.push(2);
            else if(floorNum === 2)
                elevator.destinationQueue.push(0);
        });
    },
    update: function(dt, elevators, floors) {
        elevators[0].checkDestinationQueue();
    }
}

When you call stop(), it only means that the elevator will decelerate to a halt - it will not stop at any specific floor. It can be called at any time. Typically if you call stop when "passing", for example floor 3, the elevator will stop at floor 3.01 (meaning slightly above floor 3, meaning that users will not get off) or something like that. Maybe stop() should actually stop at the next floor. What do you think?

The correct solution in your case would be something like this I think:

elevator.on("passing_floor", function(floorNum, direction) {
    // Logic and stuff...
    elevator.stop();
    elevator.goToFloor(floorNum);
}

Interesting... I did not realize that was your intent for stop.
With that in mind, I'm not sure I'd want stop to go to the next floor.

Right, well it is equivalent to clearing the queue and adding a floor to the queue. The stop call is immediate, but the deceleration of the elevator happens over time, so if you modify the queue the elevator will start moving. I think it's a little confusing. Maybe the docs should be clarified.

For reference, the docs have been clarified in 9217902.