Skip to content

Instantly share code, notes, and snippets.

@gangstead
Last active August 29, 2015 14:14

Revisions

  1. gangstead revised this gist Jan 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion elevatorsaga.js
    Original file line number Diff line number Diff line change
    @@ -13,9 +13,9 @@
    if(goToFloorSafe(elevator,floorNum)){
    var goingUp = elevator.destinationQueue[0] > elevator.currentFloor();
    elevator.destinationQueue.sort(); //lowest floor next
    var safety = floors.length;
    if(goingUp) {
    //we're headed up, lowest floor is next in queue. Make it so lowest floor above is next
    var safety = floors.length;
    while(elevator.destinationQueue[0] < elevator.currentFloor() && safety) {
    frontToBack(elevator.destinationQueue);
    safety--;
  2. gangstead created this gist Jan 27, 2015.
    106 changes: 106 additions & 0 deletions elevatorsaga.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    {
    init: function(elevators, floors) {
    console.log('elev:' + elevators.length + ' flrs:'+floors.length);
    elevators.forEach(function(elevator,index){
    elevator.on("idle", function() {
    console.log(index + " the devil's plaything");
    var idleFloor = [0,0,0,0,0,7,0,0,0,0];
    goToFloorSafe(elevator,idleFloor[index]);
    elevator.goingUpIndicator(true);
    elevator.goingDownIndicator(true);
    });
    elevator.on("floor_button_pressed", function(floorNum) {
    if(goToFloorSafe(elevator,floorNum)){
    var goingUp = elevator.destinationQueue[0] > elevator.currentFloor();
    elevator.destinationQueue.sort(); //lowest floor next
    if(goingUp) {
    //we're headed up, lowest floor is next in queue. Make it so lowest floor above is next
    var safety = floors.length;
    while(elevator.destinationQueue[0] < elevator.currentFloor() && safety) {
    frontToBack(elevator.destinationQueue);
    safety--;
    }
    } else {
    elevator.destinationQueue.reverse();
    //headed down, highest floor is next in queue, make it so highest floor lower than current is next
    while(elevator.destinationQueue[0] > elevator.currentFloor() && safety) {
    frontToBack(elevator.destinationQueue);
    safety--;
    }
    }
    } else { console.log(index + ' already headed to '+ floorNum);}
    if(_.uniq(elevator.destinationQueue).length != elevator.destinationQueue.length) {
    console.log(index + ':'+elevator.currentFloor()+' q:'+elevator.destinationQueue);
    console.log(elevator);
    }
    elevator.checkDestinationQueue();
    });
    elevator.on("stopped_at_floor", function(floorNum){
    var nextFloor = elevator.destinationQueue[0];

    if(elevator.destinationQueue.length == 0 || elevator.loadFactor() == 0) {
    elevator.goingUpIndicator(true);
    elevator.goingDownIndicator(true);
    }
    else if(nextFloor > floorNum){
    elevator.goingUpIndicator(true);
    elevator.goingDownIndicator(false);
    } else {
    elevator.goingUpIndicator(false);
    elevator.goingDownIndicator(true);
    }

    });
    elevator.on("passing_floor",function(floorNum,direction){
    var headedThereAnyways = elevator.destinationQueue.indexOf(floorNum);
    if(!floors[floorNum]) {console.log('WHHHAAAATTTT FLOOR '+floorNum); console.log(floors); return;} //this was happening for some reason
    var goingRequestedDirection = floors[floorNum].buttonStates[direction];
    if( (headedThereAnyways > 0) && goingRequestedDirection) {
    //queue out of order. This elevator was going to stop at this floor anyways but not the next stop
    console.log(index+ ' before splice:'+ elevator.destinationQueue + ' floorNum:'+floorNum);
    elevator.destinationQueue.splice(headedThereAnyways,1);
    elevator.destinationQueue.unshift(floorNum);
    console.log(index+ ' after splice:' + elevator.destinationQueue);
    elevator.checkDestinationQueue();
    } else if(headedThereAnyways == -1 && goingRequestedDirection && elevator.loadFactor() < .5 ) {
    console.log('watch out for hop ons');
    //we have room to pick up someone and we're headed in the direction they want to go and we weren't right about to stop at that floor anyways
    elevator.destinationQueue.unshift(floorNum);
    elevator.checkDestinationQueue();
    }
    });
    });

    function leastFull(goingUp) {
    var sameDirection = elevators.filter(function(elev){ return goingUp? elev.velocityY > 0 : elev.velocityY < 0});
    if(sameDirection.length == 0) sameDirection = elevators.slice();
    return sameDirection.sort(function(a,b){return a.loadFactor() - b.loadFactor()})[0]; //switch to most full for fewer moves and greater wait time
    };

    function frontToBack(arr){
    arr.push(arr.shift());
    };

    floors.forEach(function(floor,index){
    floor.on("up_button_pressed", function(event) {
    goToFloorSafe(leastFull(true),index);
    });
    floor.on("down_button_pressed", function(event) {
    goToFloorSafe(leastFull(false),index);
    });

    });

    function goToFloorSafe(el, floorNum){
    if(el.destinationQueue.indexOf(floorNum) == -1 ) {
    el.goToFloor(floorNum);
    return true;
    } else {
    return false;
    }
    }
    },
    update: function(dt, elevators, floors) {
    // We normally don't need to do anything here
    }
    }