Skip to content

Instantly share code, notes, and snippets.

@radbasa
Created October 26, 2023 15:16
Show Gist options
  • Save radbasa/fb08f42580a0781042dc6af362020314 to your computer and use it in GitHub Desktop.
Save radbasa/fb08f42580a0781042dc6af362020314 to your computer and use it in GitHub Desktop.
Elevator Saga
{
init: function(elevators, floors) {
_.each(elevators, function(elevator) {
elevator.on("idle", function() {
elevator.goToFloor(0);
});
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
})
elevator.on("passing_floor", function(floorNum, direction) {
if (elevator.getPressedFloors().includes(floorNum)) {
elevator.destinationQueue = elevator.destinationQueue.filter((f) => f !== floorNum);
elevator.goToFloor(floorNum, true);
}
})
})
_.each(floors, function(floor) {
floor.on("up_button_pressed down_button_pressed", function() {
in_queue = false;
if (elevators.some((e) => e.destinationQueue.includes(floor.floorNum()))) {
return;
}
this_elevator = elevators[0];
function e_weight(e, requested_floor_num, floors_num) {
direction_weight = 0;
if (e.destinationDirection() == "up") {
if (requested_floor_num - e.currentFloor() > 0) {
direction_weight = (requested_floor_num - e.currentFloor()) / floors_num;
} else {
direction_weight = ((floors_num - e.currentFloor()) + (floors_num - requested_floor_num)) / floors_num;
}
} else {
if (e.currentFloor() - requested_floor_num > 0) {
direction_weight = (e.currentFloor() - requested_floor_num) / floors_num;
} else {
direction_weight = (e.currentFloor() + requested_floor_num) / floors_num;
}
}
//e.currentFloor() - requested_floor_num
return(e.destinationQueue.length * e.loadFactor() * direction_weight);
}
_.each(elevators, function(elevator) {
if(e_weight(elevator, floor.floorNum(), floors.length) < e_weight(elevator, floor.floorNum(), floors.length)) {
this_elevator = elevator;
}
})
this_elevator.goToFloor(floor.floorNum());
})
})
},
update: function(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