Skip to content

Instantly share code, notes, and snippets.

@tinnet
Created January 23, 2015 06:02
Show Gist options
  • Save tinnet/37b9a237e87d8feaa903 to your computer and use it in GitHub Desktop.
Save tinnet/37b9a237e87d8feaa903 to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
var rotator = 0; // roundrobin var
var minLoad = 0.0; // 0.6 for N moves challenge
var upWaiting = [];
var downWaiting = [];
var randomInt = function (min, max) {
return Math.floor(Math.random()*(max-min+1)+min);
};
_.each(elevators, function(elevator) {
elevator.on("idle", function() {
if (downWaiting.length > 0) {
elevator.goToFloor(downWaiting.pop());
} else if (upWaiting.length > 0) {
elevator.goToFloor(upWaiting.pop());
} else {
elevator.goToFloor(0);
};
});
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
elevator.on("passing_floor", function(floorNum, direction) {
var head;
var tail;
if (direction === "up") {
if (elevator.loadFactor() <= 0.9 && upWaiting.indexOf(floorNum) > -1) {
elevator.destinationQueue.push(floorNum);
_.remove(upWaiting, function(num) { return num == floorNum; });
};
head = _.filter(elevator.destinationQueue, function(num) { return num >= floorNum; });
tail = _.filter(elevator.destinationQueue, function(num) { return num < floorNum; });
head.sort();
tail.sort().reverse();
} else {
if ( elevator.loadFactor() <= 0.9 && downWaiting.indexOf(floorNum) > -1) {
elevator.destinationQueue.push(floorNum);
_.remove(downWaiting, function(num) { return num == floorNum; });
};
head = _.filter(elevator.destinationQueue, function(num) { return num <= floorNum; });
tail = _.filter(elevator.destinationQueue, function(num) { return num > floorNum; });
head.sort().reverse();
tail.sort();
};
elevator.destinationQueue = _.uniq(head.concat(tail));
elevator.checkDestinationQueue();
});
elevator.on("stopped_at_floor", function() {
if (elevator.loadFactor() < minLoad){
elevator.goToFloor(elevator.currentFloor(), true);
};
});
});
_.each(floors, function(floor) {
floor.on("down_button_pressed", function() {
//elevators[rotator++ % elevators.length].goToFloor(floor.level);
downWaiting = _.uniq(downWaiting.push(floor.level));
});
floor.on("up_button_pressed", function() {
//elevators[rotator++ % elevators.length].goToFloor(floor.level);
upWaiting = _.uniq(upWaiting.push(floor.level));
})
});
},
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