Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
init: function(elevators, floors) {
var upWaiters = {},
downWaiters = {},
idleElevators = [],
floorCount = floors.length,
arrayUnique = function(a) {
return a.reduce(function(p, c) {
if (p.indexOf(c) < 0) p.push(c);
return p;
}, []);
},
removeIdleElevator = function(nonIdleElevator) {
idleElevators = idleElevators.filter(function(elevator) {
return elevator != nonIdleElevator;
});
},
checkIdleElevators = function() {
idleElevators.forEach(function(elevator) {
var currentFloor = elevator.currentFloor();
for (var i = 0; i < floorCount; i++) {
var checkDownFloor = (currentFloor - i) % floorCount,
checkUpFloor = (currentFloor + i) % floorCount;
if (upWaiters[checkUpFloor]) {
elevator.goToFloor(checkUpFloor);
upWaiters[checkUpFloor] = false;
removeIdleElevator(elevator);
break;
} else if (upWaiters[checkDownFloor]) {
elevator.goToFloor(checkDownFloor);
upWaiters[checkDownFloor] = false;
removeIdleElevator(elevator);
break;
} else if (downWaiters[checkUpFloor]) {
elevator.goToFloor(checkUpFloor);
downWaiters[checkUpFloor] = false;
removeIdleElevator(elevator);
break;
} else if (downWaiters[checkDownFloor]) {
elevator.goToFloor(checkDownFloor);
downWaiters[checkDownFloor] = false;
removeIdleElevator(elevator);
break;
}
}
});
};
elevators.forEach(function(elevator) {
console.log('Elevator capacity: ' + elevator.maxPassengerCount());
elevator.on("idle", function() {
console.log('bosta. floorCount ' + floorCount, upWaiters, downWaiters);
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
idleElevators.push(elevator);
checkIdleElevators()
});
elevator.on("floor_button_pressed", function(floorNum) {
removeIdleElevator(elevator);
if (elevator.destinationQueue.length === 0) {
elevator.goToFloor(floorNum);
if (floorNum > elevator.currentFloor()) {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
} else {
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
}
} else {
var up = elevator.destinationQueue[0] > elevator.currentFloor(),
upNumbers = elevator.destinationQueue.filter(function(i) {
return i > elevator.currentFloor();
}),
downNumbers = elevator.destinationQueue.filter(function(i) {
return i < elevator.currentFloor();
});
if (floorNum > elevator.currentFloor()) {
upNumbers.push(floorNum);
upNumbers = arrayUnique(upNumbers);
upNumbers.sort();
} else {
downNumbers.push(floorNum);
downNumbers = arrayUnique(downNumbers);
downNumbers.sort().reverse();
}
console.log(upNumbers, downNumbers, up);
if (up) {
elevator.destinationQueue = upNumbers.concat(downNumbers);
} else {
elevator.destinationQueue = downNumbers.concat(upNumbers);
}
console.log(elevator.destinationQueue);
elevator.checkDestinationQueue();
}
});
elevator.on("passing_floor", function(floorNum, direction) {
if (elevator.loadFactor() < 0.5) {
if (direction == 'up') {
if (upWaiters[floorNum]) {
elevator.goToFloor(floorNum, true);
upWaiters[floorNum] = false;
}
}
if (direction == 'down') {
if (downWaiters[floorNum]) {
elevator.goToFloor(floorNum, true);
downWaiters[floorNum] = false;
}
}
}
});
// elevator.on("stopped_at_floor", function(floorNum) {
// var up = elevator.destinationQueue[0] > floorNum;
// elevator.goingDownIndicator(!up);
// elevator.goingUpIndicator(up);
// //elevator.destinationQueue = [];
// elevator.checkDestinationQueue();
// });
});
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
upWaiters[floor.floorNum()] = true;
checkIdleElevators();
// console.log('Floor ' + floor.floorNum() + ': up');
});
floor.on("down_button_pressed", function() {
downWaiters[floor.floorNum()] = true;
checkIdleElevators();
// console.log('Floor ' + floor.floorNum() + ': down');
});
});
},
update: function(dt, elevators, floors) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.