Skip to content

Instantly share code, notes, and snippets.

@jonasporto
Created September 16, 2016 22:00
Show Gist options
  • Save jonasporto/7bec39fd30ab02f15ae83a8d925eb690 to your computer and use it in GitHub Desktop.
Save jonasporto/7bec39fd30ab02f15ae83a8d925eb690 to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
var upQueue = [], downQueue = [], pressedButtons = [];
elevators.forEach(function(elevator) {
console.log('max', elevator.maxPassengerCount());
elevator.clearDestination = function() {
this.destinationQueue = [];
this.checkDestinationQueue();
}
elevator.on("idle", function() {
console.log('idle',this.currentFloor(), upQueue, downQueue);
if (this.goingUpIndicator() && upQueue.length > 0) {
var toFloor = upQueue.shift();
this.goToFloor(toFloor, true);
} else if(upQueue.length > 0) {
var toFloor = upQueue.pop();
this.goToFloor(toFloor, true);
}
if (this.goingDownIndicator() && downQueue.length > 0) {
var toFloor = downQueue.shift();
this.goToFloor(toFloor, true);
} else if(downQueue.length > 0) {
var toFloor = downQueue.pop();
this.goToFloor(toFloor, true);
}
this.goToFloor(0);
});
elevator.on("floor_button_pressed", function(floorNum) {
if (pressedButtons.indexOf(floorNum) == -1) {
pressedButtons.push(floorNum);
}
if (this.getPressedFloors().length == 1) {
this.goToFloor(floorNum, true);
} else {
this.goToFloor(floorNum);
}
});
elevator.on("passing_floor", function(floorNum, direction) {
if (this.getPressedFloors().indexOf(floorNum) != -1) {
this.goToFloor(floorNum, true);
console.log('passing_floor', floorNum, this.getPressedFloors());
}
if (
this.goingUpIndicator()
&& direction == 'up'
&& this.loadFactor != 1
&& upQueue.indexOf(floorNum) != -1
) {
upQueue.splice(upQueue.indexOf(floorNum), 1);
this.goToFloor(floorNum, true);
}
if (
this.goingDownIndicator()
&& direction == 'down'
&& this.loadFactor != 1
&& downQueue.indexOf(floorNum) != -1
) {
downQueue.splice(downQueue.indexOf(floorNum), 1);
this.goToFloor(floorNum, true);
}
});
elevator.on("stopped_at_floor", function(floorNum) {
pressedButtons.splice(pressedButtons.indexOf(floorNum));
if (this.loadFactor == 0) {
this.clearDestination();
}
})
});
floors.forEach(function(floor){
floor.on("up_button_pressed", function() {
if (upQueue.indexOf(this.floorNum()) == -1 && pressedButtons.indexOf(this.floorNum()) == -1) {
upQueue.push(this.floorNum());
}
});
floor.on("down_button_pressed", function() {
if (downQueue.indexOf(this.floorNum()) == -1 && pressedButtons.indexOf(this.floorNum()) == -1) {
downQueue.push(this.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