Last active
January 30, 2020 05:14
-
-
Save aluramh/1e3657d6a20ba0571a9a5af8ab2cea76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
init(elevators, floors) { | |
function leastLoadedElevator() { | |
// Find elevator with lowest load factor | |
let elevatorIndex = 0; | |
let minLoad = 9999999; | |
elevators.forEach((elevator, index) => { | |
const currentLoad = elevator.loadFactor(); | |
if (currentLoad < minLoad) { | |
minLoad = currentLoad; | |
elevatorIndex = index; | |
} | |
}); | |
return elevators[elevatorIndex]; | |
} | |
elevators.forEach(function(elevator) { | |
elevator.on("idle", function() { | |
const pressedFloors = elevator.getPressedFloors(); | |
if (pressedFloors && pressedFloors.length > 0) { | |
// You are idle but somebody pressed a floor | |
const nextFloor = pressedFloors[0]; | |
elevator.goToFloor(nextFloor); | |
} else { | |
// You are idle but no one pressed anything | |
elevator.goToFloor(3); | |
} | |
}); | |
}); | |
floors.forEach(function(floor) { | |
floor.on("up_button_pressed", function() { | |
const elevator = leastLoadedElevator(); | |
elevator.goToFloor(floor.level); | |
}); | |
floor.on("down_button_pressed", function() { | |
const elevator = leastLoadedElevator(); | |
elevator.goToFloor(floor.level); | |
}); | |
}); | |
}, | |
update(dt, elevators, floors) { | |
// We normally don't need to do anything here | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment