elevatorsaga
elevatorsaga copied to clipboard
Bug: User doesn't enter the elevator when it stops enroute
Steps
- elevator is on the way to
0
, - elevator has detected that there's someone waiting on
1
- downIndicator is
ON
since that's the direction the elevator is going - elevator stops
Observation
- user doesn't climb aboard.
Expectation
- since the elevator is going in the right direction the expectation is that the user climb aboard
Code
Click to view code!
{
init: function(elevators, floors) {
// hasWaitingPeople :: Int -> "up" | "down" | undefined -> Boolean
const hasWaitingPeople = (floorNum, direction) => {
const source = floors.find(fl => fl.floorNum() === floorNum)
if(source === undefined)
throw new Error(`Could not find floor ${floorNum}`)
const {up, down} = source.buttonStates
return direction === undefined
? up === "activated" || down === "activated" : direction === "down"
? down === "activated" : up === "activated"
}
// setIndicator :: El -> Int -> void
const setIndicator = (el, destination) => {
const currentFloor = el.currentFloor()
if(destination > currentFloor) {
el.goingUpIndicator(true)
el.goingDownIndicator(false)
}
else {
el.goingUpIndicator(false)
el.goingDownIndicator(true)
}
}
// Elevator Listeners
for(el of elevators) {
el.on("floor_button_pressed", function(destination) {
console.log("Go To: ", destination)
setIndicator(el, destination)
// insert based on direction
el.goToFloor(destination, true)
});
el.on("passing_floor", function(floorNum, direction) {
if(el.loadFactor() < 1 && hasWaitingPeople(floorNum, direction)) {
console.log('Stopped:', floorNum, direction)
el.stop()
console.log("indicators:", el.goingUpIndicator(), el.goingDownIndicator())
}
});
el.on("stopped_at_floor", function(floorNum) {
console.log("STOPPED at: ", floorNum)
if(el.destinationQueue.length === 0) {
el.goingUpIndicator(true)
el.goingDownIndicator(true)
}
})
el.on("idle", function() {
const allStates = floors.filter(fl => hasWaitingPeople(fl.floorNum())).map(fl => fl.floorNum())
console.log('Idle: ', allStates)
const target = Math.max(...allStates)
setIndicator(el, target)
el.goToFloor(target)
});
}
},
update: function(dt, elevators, floors) {
}
}
Related way to reproduce: Turn off indicators, but turn on when someone presses the button. People at floor 0 never enter the elevator.
{
init: function(elevators, floors) {
var elevator = elevators[0]; // Let's use the first elevator
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(false);
for (let floor of floors) {
floor.on("up_button_pressed", function() {
if (floor.floorNum() == elevator.currentFloor())
elevator.goingUpIndicator(true);
});
}
},
update: function(dt, elevators, floors) {
}
}