Skip to content

Instantly share code, notes, and snippets.

@andrefabbro
Last active October 2, 2016 15:25
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 andrefabbro/f1dc6073a695e71c679243680e142d49 to your computer and use it in GitHub Desktop.
Save andrefabbro/f1dc6073a695e71c679243680e142d49 to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
var availables = [];
var callingFloorsUp = [];
var callingFloorsDown = [];
elevators.forEach(function(elevator) {
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(false);
});
function goToFloor(e, n) {
console.log("Elevador " + elevators.indexOf(e) + " vai para o andar " + n);
e.goToFloor(n);
}
function controlIndicatorForDestinationFloor(floorNum, elevator) {
elevator.goingUpIndicator(floorNum > elevator.currentFloor());
elevator.goingDownIndicator(floorNum < elevator.currentFloor());
}
elevators.forEach(function(elevator) {
elevator.on("idle", function() {
if(availables.indexOf(this) <= -1)
availables.push(this);
});
elevator.on("floor_button_pressed", function(floorNum) {
if(availables.indexOf(this) > -1)
availables.splice(availables.indexOf(this), 1);
console.log("Pressionado " + floorNum + " elevator " + elevators.indexOf(this) + " vai?");
if(this.destinationQueue.length == 0) {
// controlIndicatorForDestinationFloor(floorNum, this);
this.goingUpIndicator(floorNum > this.currentFloor());
this.goingDownIndicator(floorNum < this.currentFloor());
}
console.log("Passou aqui");
this.destinationQueue.push(floorNum);
this.destinationQueue.sort(function(a, b) {
return this.goingUpIndicator() ? a-b : b-a;
});
this.checkDestinationQueue();
});
elevator.on("passing_floor", function(floorNum, direction) {
// console.log("Elevator " + elevators.indexOf(this) + " vai passar no floor " + floorNum + " em " + direction);
});
elevator.on("stopped_at_floor", function(floorNum) {
// console.log("Elevator " + elevators.indexOf(this) + " chega no floor " + floorNum);
if(callingFloorsUp.indexOf(floorNum) >= 0)
callingFloorsUp.splice(callingFloorsUp.indexOf(floorNum), 1);
if(callingFloorsDown.indexOf(floorNum) >= 0)
callingFloorsDown.splice(callingFloorsDown.indexOf(floorNum), 1);
})
});
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
console.log("Floor " + this.floorNum() + " chama para subir");
if(callingFloorsUp.indexOf(this.floorNum()) <= -1) {
callingFloorsUp.push(this.floorNum());
callingFloorsUp.sort();
}
var elevator = availables.shift();
if(elevator) {
if(elevator.currentFloor() == this.floorNum()) {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
} else controlIndicatorForDestinationFloor(this.floorNum, elevator);
goToFloor(elevator, this.floorNum());
}
});
floor.on("down_button_pressed", function() {
console.log("Floor " + this.floorNum() + " chama para descer");
if(callingFloorsDown.indexOf(this.floorNum()) <= -1) {
callingFloorsDown.push(this.floorNum());
callingFloorsDown.sort();
}
var elevator = availables.shift();
if(elevator) {
if(elevator.currentFloor() == this.floorNum()) {
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
} else controlIndicatorForDestinationFloor(this.floorNum, elevator);
goToFloor(elevator, 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