Skip to content

Instantly share code, notes, and snippets.

@Medeah
Created September 17, 2015 19:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Medeah/e42c1efef39596109f72 to your computer and use it in GitHub Desktop.
Save Medeah/e42c1efef39596109f72 to your computer and use it in GitHub Desktop.
simple elevator logic. with typeScript type definitions
var elevatorSaga = {
init: function(elevators: Elevator[], floors: Floor[]) {
console.log(floors[1]);
function amin(numArray) {
return Math.min.apply(null, numArray);
}
function amax(numArray) {
return Math.max.apply(null, numArray);
}
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var maxFloor = floors.length - 1;
elevators.forEach(function(elevator, n) {
elevator.goingUpIndicator(true)
elevator.goingDownIndicator(false)
elevator.one("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
elevator.on("idle", function() {
});
elevator.on("passing_floor", function(floorNum: number, direction: string) {
if (elevator.getPressedFloors().indexOf(floorNum) > -1) {
elevator.goToFloor(floorNum, true);
} else if (elevator.loadFactor() < 0.7 && floors[floorNum].buttonStates[direction] === "activated") {
elevator.goToFloor(floorNum, true);
}
});
elevator.on("stopped_at_floor", function(floorNum: number) {
if (elevator.currentFloor() == 0) {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
elevator.goToFloor(maxFloor);
} else if (elevator.currentFloor() == maxFloor) {
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
elevator.goToFloor(0);
}
});
})
floors.forEach(function(floor, n) {
floor.on("up_button_pressed", function() {
})
floor.on("down_button_pressed", function() {
})
})
},
update: function(dt: number, elevators: Elevator[], floors: Floor[]) {
// We normally don't need to do anything here
}
}
interface Elevator {
/** Queue the elevator to go to specified floor number. If you specify true as second argument, the elevator will go to that floor directly, and then go to any other queued floors.*/
goToFloor: (floor: number, directly?: boolean) => void;
/** Clear the destination queue and stop the elevator if it is moving. Note that you normally don't need to stop elevators - it is intended for advanced solutions with in-transit rescheduling logic. Also, note that the elevator will probably not stop at a floor, so passengers will not get out.*/
stop: () => void;
/** Gets the floor number that the elevator currently is on.*/
currentFloor: () => number;
/** Gets or sets the going up indicator, which will affect passenger behaviour when stopping at floors.*/
goingUpIndicator: (set?: boolean) => void;
/** Gets or sets the going down indicator, which will affect passenger behaviour when stopping at floors.*/
goingDownIndicator: (set?: boolean) => void;
/** Gets the maximum number of passengers that can occupy the elevator at the same time.*/
maxPassengerCount: () => number;
/** Gets the load factor of the elevator. 0 means empty, 1 means full. Varies with passenger weights, which vary - not an exact measure.*/
loadFactor: () => number;
/** Gets the direction the elevator is currently going to move toward. Can be "up", "down" or "stopped".*/
destinationDirection: () => string;
/** The current destination queue, meaning the floor numbers the elevator is scheduled to go to. Can be modified and emptied if desired. Note that you need to call checkDestinationQueue() for the change to take effect immediately.*/
destinationQueue: number[];
/** Checks the destination queue for any new destinations to go to. Note that you only need to call this if you modify the destination queue explicitly.*/
checkDestinationQueue: () => void;
/** Gets the currently pressed floor numbers as an array.*/
getPressedFloors: () => number[];
/** Listen for events ("idle", "stopped_at_floor", "passing_floor", "floor_button_pressed")*/
on: (events: string, callback: Function) => void;
one: (events: string, callback: Function) => void;
}
interface Floor {
/** Gets the floor number of the floor object.*/
floorNum: () => number;
/** Listen for events ("up_button_pressed", "down_button_pressed")*/
on: (events: string, Function) => void;
/** State of the up and down button at a floor. "activated" or "" */
buttonStates: { down: string, up: string };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment