Skip to content

Instantly share code, notes, and snippets.

@sim642
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save sim642/13291ef79ceea89761dd to your computer and use it in GitHub Desktop.

Select an option

Save sim642/13291ef79ceea89761dd to your computer and use it in GitHub Desktop.
Elevatorsaga
{
init: function(elevators, floors) {
var ups = {};
var downs = {};
elevators.forEach(function(elevator, ei) {
/*if (ei > 0) // for move challenges
return;*/
elevator.pressed = {};
elevator.on("idle", function() {
var goFloor = Object.keys(ei % 2 == 0 ? ups : downs)[0] || Object.keys(ei % 2 != 0 ? ups : downs)[0] ||(ei % 4 != 0 ? 0 : floors.length - 1);
elevator.goingUpIndicator(elevator.currentFloor() <= goFloor);
elevator.goingDownIndicator(elevator.currentFloor() >= goFloor);
elevator.goToFloor(goFloor);
});
elevator.on("passing_floor", function(floorNum, direction) {
if (elevator.getPressedFloors().indexOf(floorNum) == -1 && elevator.loadFactor() >= 0.7)
return;
else if (elevator.getPressedFloors().indexOf(floorNum) != -1)
elevator.goToFloor(floorNum, true);
else if (direction == "up" && (floorNum in ups))
elevator.goToFloor(floorNum, true);
else if (direction == "down" && (floorNum in downs))
elevator.goToFloor(floorNum, true);
});
elevator.on("stopped_at_floor", function(floorNum) {
delete elevator.pressed[floorNum];
var goFloor = elevator.destinationQueue[0];
if (goFloor) {
elevator.goingUpIndicator(floorNum <= goFloor);
elevator.goingDownIndicator(floorNum >= goFloor);
}
else {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
}
if (elevator.goingUpIndicator() && (floorNum in ups))
delete ups[floorNum];
else if (elevator.goingDownIndicator() && (floorNum in downs))
delete downs[floorNum];
elevators.forEach(function(elevator2) {
if (elevator2.goingUpIndicator() == elevator.goingUpIndicator() && elevator2.goingDownIndicator() == elevator.goingDownIndicator()) {
elevator2.destinationQueue = elevator2.destinationQueue.filter(function(floorNum2) {
return floorNum != floorNum2 || (floorNum2 in elevator2.pressed);
});
elevator2.checkDestinationQueue();
}
});
});
elevator.on("floor_button_pressed", function(floorNum) {
//console.log(elevator.getFirstPressedFloor());
elevator.pressed[floorNum] = true;
elevator.goingUpIndicator(elevator.currentFloor() < floorNum);
elevator.goingDownIndicator(elevator.currentFloor() > floorNum);
elevator.goToFloor(floorNum, true);
});
});
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
ups[floor.floorNum()] = true;
});
floor.on("down_button_pressed", function() {
downs[floor.floorNum()] = true;
});
});
},
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