Skip to content

Instantly share code, notes, and snippets.

@foxyblocks
Created January 25, 2015 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxyblocks/50f3fdf152b32e61b9b9 to your computer and use it in GitHub Desktop.
Save foxyblocks/50f3fdf152b32e61b9b9 to your computer and use it in GitHub Desktop.
Elevator Saga (classic elevator strategy)
{
init: function(elevators, floors) {
var floorsWaiting = {
up: [],
down: []
};
var self = this;
var distance = function(num1, num2) {
return Math.abs(num1 - num2);
};
var uniq = function(array) {
return array.filter( function(value, index, self) {
return self.indexOf(value) === index;
});
};
var closestNum = function(array, num) {
closest = 0;
array.forEach(function(candidate){
var d = distance(candidate, num);
var currentBestDistance = distance(closest, num);
if (d < currentBestDistance){
closest = candidate;
}
});
return closest;
};
var closestFloorNumWaiting = function (elevator) {
var allFloorsNumsWaiting = floorsWaiting['up'].concat(floorsWaiting['down']);
return closestNum(allFloorsNumsWaiting, elevator.currentFloor());
};
var closestIdleElevator = function (floorNum) {
var idles = elevators.filter(function(elevator){
return elevator.destinationQueue.length == 0;
});
return idles[0];
}
var floorIsWaiting = function(floorNum, direction) {
return floorsWaiting[direction].indexOf(floorNum) > -1
}
var addWaiting = function(floorNum, direction) {
if (!floorIsWaiting(floorNum, direction)) {
floorsWaiting[direction].push(floorNum);
}
}
var handleCallRequest = function(floor, direction) {
if (!floorIsWaiting(floor.level, direction)) {
addWaiting(floor.level, direction);
var idle = closestIdleElevator(floor.level);
//
if(idle) {
console.log('idle found', idle);
idle.goToFloor(floor.level);
var goingUp = (floor.level > idle.currentFloor());
idle.goingUpIndicator(goingUp);
idle.goingDownIndicator(!goingUp);
}
}
};
var handleUpRequest = function() {
console.log('upRequest', this);
handleCallRequest(this, 'up')
};
var handleDownRequest = function() {
console.log('downRequest', this);
handleCallRequest(this, 'down')
};
// bind up/down requests to all floors
floors.forEach(function(floor){
floor.on('up_button_pressed', handleUpRequest);
floor.on('down_button_pressed', handleDownRequest);
})
var checkIdle = function() {
var num = closestFloorNumWaiting(this)
console.log('idle', this, num, floorsWaiting);
if(typeof num !== 'undefined') {
var goingUp = (num > this.currentFloor());
this.goingUpIndicator(goingUp);
this.goingDownIndicator(!goingUp);
this.goToFloor(num);
} else {
this.goToFloor(0);
}
}
// bind elevator events
elevators.forEach(function(elevator){
elevator.on("floor_button_pressed", function(floorNum) {
var goingUp = floorNum > this.currentFloor();
if (this.destinationQueue.indexOf(floorNum) == -1) {
this.destinationQueue.push(floorNum);
this.destinationQueue = this.destinationQueue.sort();
if(!goingUp){
this.destinationQueue = this.destinationQueue.reverse()
}
}
this.checkDestinationQueue()
console.log('floor_button_pressed', floorNum, this.destinationQueue)
})
elevator.on("idle", checkIdle);
elevator.on("passing_floor", function(floorNum, direction) {
var goingUp = (direction == 'up');
console.log('passing_floor: ' + floorNum, floorsWaiting, 'going up: ' + this.goingUpIndicator(), 'going down: ' + this.goingDownIndicator());
var index = floorsWaiting[direction].indexOf(floorNum);
if(index > -1) {
this.goToFloor(floorNum, true)
floorsWaiting[direction].splice(index, 1);
}
});
elevator.on("stopped_at_floor", function(floorNum) {
console.log('stopped_at_floor', floorNum, this.destinationQueue);
// //at the bottom
if (floorNum == 0) {
this.goingUpIndicator(true);
this.goingDownIndicator(false);
}
// //at the top
if (floorNum == floors.length - 1) {
this.goingDownIndicator(true);
this.goingUpIndicator(false);
}
});
});
},
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