Skip to content

Instantly share code, notes, and snippets.

@mcfog
Last active August 29, 2015 14:14
Show Gist options
  • Save mcfog/97a8e64e2fa4f99bc33d to your computer and use it in GitHub Desktop.
Save mcfog/97a8e64e2fa4f99bc33d to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
elevators.forEach(initElevator);
floors.forEach(initFloor);
var topFloor = floors.length - 1;
function initElevator(elevator) {
elevator.on("idle", function() {
});
elevator.on("stopped_at_floor", function(floorNum) {
if (elevator.getPressedFloors().length === 0) {
if(floorNum > 0) {
elevator.destinationQueue = getFloorDownQueue();
if (elevator.destinationQueue.length === 0) {
elevator.destinationQueue = [0];
}
elevator.checkDestinationQueue();
indicate('down');
} else {
elevator.destinationQueue = getFloorUpQueue();
if (elevator.destinationQueue.length === 0) {
elevator.destinationQueue = [topFloor];
}
elevator.checkDestinationQueue();
indicate('up');
}
}
});
elevator.on("floor_button_pressed", function(floorNum) {
if (elevator.destinationQueue.indexOf(floorNum) === -1) {
elevator.destinationQueue.push(floorNum);
elevator.destinationQueue.sort(function(a, b) {
return (b - a) * (elevator.goingDownIndicator() ? 1 : -1);
});
elevator.checkDestinationQueue();
}
});
function indicate(direction) {
elevator.goingDownIndicator(direction === 'down');
elevator.goingUpIndicator(direction === 'up');
}
}
function initFloor(floor) {
floor.on("up_button_pressed", function() {
// Maybe tell an elevator to go to this floor?
});
floor.on("down_button_pressed", function() {
// Maybe tell an elevator to go to this floor?
});
}
function getFloorDownQueue() {
return floors.reverse().filter(function(floor) {
return !!floor.buttonStates.down;
//https://github.com/magwo/elevatorsaga/issues/57
}).map(function(floor) {
return floor.floorNum();
});
}
function getFloorUpQueue() {
return floors.filter(function(floor) {
return !!floor.buttonStates.up;
//https://github.com/magwo/elevatorsaga/issues/57
}).map(function(floor) {
return floor.floorNum();
});
}
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
{
init: function(elevators, floors) {
elevators.forEach(initElevator);
floors.forEach(initFloor);
var topFloor = floors.length - 1;
var idleElevators = [];
function initElevator(elevator, idx) {
indicate('up');
elevator.on("idle", function() {
idleElevators.push(elevator);
});
elevator.on("stopped_at_floor", function(floorNum) {
if (elevator.destinationQueue.length === 0) {
elevator.destinationQueue = elevator.getPressedFloors();
if(floorNum > 0) {
indicate('down');
elevator.destinationQueue = elevator.destinationQueue.concat(getFloorDownQueue());
if (elevator.destinationQueue.length === 0) {
elevator.destinationQueue = [0];
}
} else {
indicate('up');
elevator.destinationQueue = elevator.destinationQueue.concat(getFloorUpQueue());
if (elevator.destinationQueue.length === 0) {
elevator.destinationQueue = [topFloor];
}
}
elevator.destinationQueue.sort(sortFloor);
elevator.checkDestinationQueue();
log(idx, elevator.goingDownIndicator()?'d':'u', elevator.loadFactor(), elevator.destinationQueue);
}
});
elevator.on("floor_button_pressed", function(floorNum) {
if (elevator.loadFactor() > 0.4) {
elevator.destinationQueue = elevator.getPressedFloors();
} else if (elevator.destinationQueue.indexOf(floorNum) === -1) {
elevator.destinationQueue.push(floorNum);
}
elevator.destinationQueue.sort(sortFloor);
elevator.checkDestinationQueue();
});
function indicate(direction) {
elevator.goingDownIndicator(direction === 'down');
elevator.goingUpIndicator(direction === 'up');
}
function sortFloor(a, b) {
return (b - a) * (elevator.goingDownIndicator() ? 1 : -1);
}
}
function initFloor(floor) {
floor.on("up_button_pressed", function() {
var elevator;
if(elevator = idleElevators.pop()) {
elevator.goToFloor(floor.floorNum());
}
});
floor.on("down_button_pressed", function() {
var elevator;
if(elevator = idleElevators.pop()) {
elevator.goToFloor(floor.floorNum());
}
});
}
function getFloorDownQueue() {
return floors.reverse().filter(function(floor) {
return !!floor.buttonStates.down;
//https://github.com/magwo/elevatorsaga/issues/57
}).map(function(floor) {
return floor.floorNum();
});
}
function getFloorUpQueue() {
return floors.filter(function(floor) {
return !!floor.buttonStates.up;
//https://github.com/magwo/elevatorsaga/issues/57
}).map(function(floor) {
return floor.floorNum();
});
}
function log(idx) {
// if(idx === 0) {
console.log.apply(console, arguments);
// }
}
},
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