Skip to content

Instantly share code, notes, and snippets.

@ianbishop
Created January 24, 2015 20:56
Show Gist options
  • Save ianbishop/0995da38f667330ff0ff to your computer and use it in GitHub Desktop.
Save ianbishop/0995da38f667330ff0ff to your computer and use it in GitHub Desktop.
Elevator Saga Solution
{
init: function(elevators, floors) {
var floorsWithPassengersDown = [];
var floorsWithPassengersUp = [];
for (var e = 0; e < elevators.length; e++){
elevators[e].on("idle", function() {
var floorsWithPassengers = floorsWithPassengersDown.concat(floorsWithPassengersUp);
var currentFloor = this.currentFloor();
var floorNum = 0;
if (floorsWithPassengers.length > 0) {
var closestFloor = floorsWithPassengers.reduce(function (prev, curr) {
return (Math.abs(curr - currentFloor) < Math.abs(prev - currentFloor) ? curr : prev);
});
floorNum = closestFloor;
removeFloorFromPassengersOnFloor(floorNum);
}
this.goToFloor(floorNum);
/*
var floorNumber = 0;
// go to a floor with passengers
if(floorsWithPassengers.length > 0){
var sorted = floorsWithPassengers.sort();
floorNumber = sorted[sorted.length-1];
removeFloorFromPassengersOnFloor(floorNumber);
}
this.goToFloor(floorNumber);*/
});
elevators[e].on("passing_floor", function(floorNum, direction) {
// below 0.7 there might be place for another passenger
if(this.loadFactor() < 0.7){
if(direction === "up"){
// if the elevator is going UP
// we only pick up from floors where people want to go UP
if(passengersOnFloorUp(floorNum)){
// if there are passengers, make next stop that floor
this.destinationQueue.unshift(floorNum);
this.checkDestinationQueue();
// dont let other elevators go there
removeFloorFromPassengersOnFloor(floorNum);
}
}
if(direction === "down"){
// if the elevator is going DOWN
// we only pick up from floors where people want to go DOWN
if(passengersOnFloorDown(floorNum)){
// if there are passengers, make next stop that floor
if (this.destinationQueue.filter(function (x,i,a) { return x === 0; }).length < 2) {
this.destinationQueue.unshift(floorNum);
this.checkDestinationQueue();
// dont let other elevators go there
removeFloorFromPassengersOnFloor(floorNum);
}
}
}
}
});
elevators[e].on("floor_button_pressed", function(floorNum) {
console.log("Elevator " + e + ": floor pressed on " + floorNum);
// @todo: SORT the destinationQueue starting at the current floor, in direction that we are moving first
// That is: direction: down, currentfloor: 3
// Order: 2 1 0 1 2 3 4 5 (etc.)
this.destinationQueue.push(floorNum);
console.log("Elevator " + e + ": destinationQueue " + this.destinationQueue);
this.destinationQueue.sort();
console.log("Elevator " + e + ": current floor " + this.currentFloor());
var self = this;
var x = this.destinationQueue.findIndex(function(e, i, a) {
return e >= self.currentFloor();
});
if (x === -1) {
this.destinationQueue.reverse();
console.log("Elevator " + e + ": Nothing above, going down with " + this.destinationQueue);
}
else if (this.goingUpIndicator()) {
this.destinationQueue = this.destinationQueue.slice(x).concat(this.destinationQueue.slice(0, x).reverse());
console.log("Elevator " + e + ": Going up with " + this.destinationQueue);
}
else {
this.destinationQueue = this.destinationQueue.slice(0, x).reverse().concat(this.destinationQueue.slice(x));
console.log("Elevator " + e + ": Going down with " + this.destinationQueue);
}
this.checkDestinationQueue();
});
}
for(var f = 0; f < floors.length; f++){
floors[f].on('up_button_pressed', function(){
// register passengers that want to go UP
floorsWithPassengersUp.push(this.level);
});
floors[f].on('down_button_pressed', function(){
// register passengers that want to go DOWN
floorsWithPassengersDown.push(this.level);
});
}
function passengersOnFloorUp(floorNumber){
// Are there passengers on the floor that want to go UP
for(var i = 0; i < floorsWithPassengersUp.length; i++){
if(floorsWithPassengersUp[i] == floorNumber){
return true;
}
}
}
function passengersOnFloorDown(floorNumber){
// Are there passengers on the floor that want to go DOWN
for(var i = 0; i < floorsWithPassengersDown.length; i++){
if(floorsWithPassengersDown[i] == floorNumber){
return true;
}
}
}
function removeFloorFromPassengersOnFloor(floorNumber){
// Remove the floor from registration
for(var i = 0; i < floorsWithPassengersDown.length; i++){
if(floorsWithPassengersDown[i] == floorNumber){
floorsWithPassengersDown.splice(i,1);
}
}
for(var j = 0; j < floorsWithPassengersUp.length; j++){
if(floorsWithPassengersUp[j] == floorNumber){
floorsWithPassengersUp.splice(j,1);
}
}
}
},
update: function(dt, elevators, floors) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment