Skip to content

Instantly share code, notes, and snippets.

@skahack
Created January 27, 2015 18:53
Show Gist options
  • Save skahack/8137c0515e7389992942 to your computer and use it in GitHub Desktop.
Save skahack/8137c0515e7389992942 to your computer and use it in GitHub Desktop.
elevator-saga-solution
{
elevators: null,
floors: null,
init: function(elevators, floors) {
this.elevators = elevators;
this.floors = floors;
var self = this;
elevators.forEach(function(v){
v.on("idle", self.onIdle.bind(self, v));
v.on("floor_button_pressed", self.onFloorButtonPress.bind(self, v));
v.on("passing_floor", self.onPassingFloor.bind(self, v));
v.on("stopped_at_floor", self.onStoppedAtFloor.bind(self, v));
v.goingDownIndicator(false);
v.goingUpIndicator(true);
});
floors.forEach(function(v){
v.on("up_button_pressed", self.onUpButtonPress.bind(self, v));
v.on("down_button_pressed", self.onDownButtonPress.bind(self, v));
});
},
update: function(dt, elevators, floors) {},
//
// Elevator Events
//
onIdle: function(elevator) {
},
onFloorButtonPress: function(elevator, floorNum) {
elevator.goToFloor(floorNum);
},
onPassingFloor: function(elevator, floorNum, direction) {
num = elevator.destinationQueue.indexOf(floorNum);
if (num >= 0 && elevator.loadFactor() < 0.8) {
elevator.goToFloor(floorNum, true);
}
},
onStoppedAtFloor: function(elevator, floorNum) {
var q = elevator.destinationQueue.filter(function(v, k){
return v !== floorNum;
});
elevator.destinationQueue = q;
this.setIndicator(elevator);
},
//
// Floor Events
//
onUpButtonPress: function(floor) {
console.log('Up', floor.floorNum());
var i, weight = [];
for (i = 0; i < this.elevators.length; i++) {
var elevator = this.elevators[i];
weight[i] = this.getScore(floor.floorNum(), elevator, 'up');
}
this.addElevatorQueue(floor, weight);
},
onDownButtonPress: function(floor) {
console.log('Down', floor.floorNum());
var i, weight = [];
for (i = 0; i < this.elevators.length; i++) {
var elevator = this.elevators[i];
weight[i] = this.getScore(floor.floorNum(), elevator, 'down');
}
this.addElevatorQueue(floor, weight);
},
//
// Elevator Utils
//
getScore: function(floorNum, elevator, direction) {
var current = elevator.currentFloor();
if (elevator.destinationQueue.length === 0) {
return this.floors.length + 1000 - Math.abs(current - floorNum);
}
var next = elevator.destinationQueue[0];
if (current >= next && current >= floorNum && elevator.goingDownIndicator() && direction === 'down') {
return current - next;
} else if (current <= next && current <= floorNum && elevator.goingUpIndicator() && direction === 'up') {
return next - current;
}
return 0;
},
addElevatorQueue: function(floor, weight) {
console.log(weight);
var re = weight.reduce(function(memo, v, i, array){
if (memo.value < v) {
return { index: i, value: v }
}
return memo;
}, { index: 0, value: 0 });
var elevator = this.elevators[re.index];
elevator.goToFloor(floor.floorNum());
this.setIndicator(elevator);
},
setIndicator: function(elevator) {
if (elevator.destinationQueue.length === 0) {
elevator.goingDownIndicator(true);
elevator.goingUpIndicator(true);
return
}
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(false);
var next = elevator.destinationQueue[0];
if (elevator.currentFloor() > next) {
elevator.goingDownIndicator(true);
} else {
elevator.goingUpIndicator(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment