Advertisement
Guest User

Untitled

a guest
Jan 24th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     init: function(elevators, floors) {
  3.         function is_in_array(arr,obj) {
  4.           return (arr.indexOf(obj) != -1);
  5.         }
  6.         function elevatorMagic(elevator) {
  7.            
  8.          
  9.           if (elevator.destinationQueue.length > 1 && elevator.destinationQueue[0] > elevator.currentFloor()) {
  10.               // going up.
  11.               elevator.goingUpIndicator(true);
  12.               elevator.goingDownIndicator(false);
  13.               elevator.destinationQueue.sort();
  14.           }
  15.           else if (elevator.destinationQueue.length > 1 && elevator.destinationQueue[0] < elevator.currentFloor()) {
  16.               // going down.
  17.               elevator.goingDownIndicator(true);
  18.               elevator.goingUpIndicator(false);
  19.               elevator.destinationQueue.sort().reverse();
  20.           }
  21.          
  22.  
  23.           // When somebody gets on an elevator and clicks a floor, add that to the queue.
  24.           // only if it's not already in the queue, (todo) and we're going in the right direction.
  25.           elevator.on("floor_button_pressed", function(floorNum) {
  26.             if (!is_in_array(elevator.destinationQueue, floorNum)) {
  27.               console.log('Queued ' + floorNum );
  28.               elevator.goToFloor(floorNum);
  29.             }
  30.             else {
  31.               console.log('Elevator already going to floor ' + floorNum);
  32.             }
  33.            
  34.           });
  35.  
  36.           function callelevator(floorNum, direction) {
  37.               // is an elevator already coming?
  38.               var elevatorcoming = elevators.some(function (elevator, index) {
  39.                   if (is_in_array(elevator.destinationQueue, floorNum)) {
  40.                       console.log('Elevator ' + index + ' already on its way!');
  41.                       return true;
  42.                   }
  43.                   else {
  44.                       return false;
  45.                   }
  46.               });
  47.               if (!elevatorcoming) {
  48.                 console.log('No elevator already coming, finding a candidate elevator.');
  49.                 // Find a candidate elevator and send it this way.
  50.                 var elevatorqueued = elevators.some(function (elevator, index) {
  51.                   console.log('Trying elevator ' + index);
  52.                   // if going up and elevator is below us.
  53.                   if (direction == 'up' && elevator.currentFloor() <= floorNum && elevator.loadFactor < .8) {
  54.                     console.log('Elevator ' + index + ' is below us. Sending it to floor ' + floorNum);
  55.                     //console.log(elevator.destinationQueue);
  56.                     elevator.goingDownIndicator(false);
  57.                     elevator.goingUpIndicator(true);
  58.                     elevator.goToFloor(floorNum);
  59.                     //console.log(elevator.destinationQueue);
  60.                     return true;
  61.                   }
  62.                   else if (direction == 'down' && elevator.currentFloor() >= floorNum && elevator.loadFactor < .8) {
  63.                     console.log('Elevator ' + index + ' is above us. Sending it to floor ' + floorNum);
  64.                     //console.log(elevator.destinationQueue());
  65.                     elevator.goingDownIndicator(true);
  66.                     elevator.goingUpIndicator(false);
  67.                     elevator.goToFloor(floorNum);
  68.                     //console.log(elevator.destinationQueue());
  69.                     return true;
  70.                   }
  71.                   else if (elevator.destinationQueue.length == 0) {
  72.                     console.log('Elevator ' + index + ' is idle. Sending it to floor ' + floorNum + '!');
  73.                    
  74.                     if (elevator.currentFloor() < floorNum) {
  75.                       elevator.goingDownIndicator(false);
  76.                       elevator.goingUpIndicator(true);
  77.                     }
  78.                     else {
  79.                       elevator.goingDownIndicator(true);
  80.                       elevator.goingUpIndicator(false);
  81.                     }
  82.                      
  83.                     elevator.goToFloor(floorNum);
  84.                     return true;
  85.                   }
  86.                   console.log('no');
  87.                   return false;
  88.                });
  89.              }
  90.           }
  91.          
  92.           // Check a floor for a button press, and if pressed, call an elevator.
  93.           function checkforbutton(floor) {
  94.             floor.on("up_button_pressed", function() {
  95.                 //elevator.goToFloor(floor.floorNum());
  96.                 console.log('Somebody wants to go *up* from floor *' + floor.floorNum() + '*');
  97.                 callelevator(floor.floorNum(), 'up');
  98.             });
  99.             floor.on("down_button_pressed", function() {
  100.                 //elevator.goToFloor(floor.floorNum());
  101.                 console.log('Somebody wants to go *down* from floor *' + floor.floorNum() + '*');
  102.                 callelevator(floor.floorNum(), 'down');
  103.             });
  104.           }
  105.          
  106.           // When an elevator is idle, wait until somebody clicks a button on a floor.
  107.           elevator.on("idle", function() {
  108.             console.log('Elevator is idle.');
  109.             elevator.goToFloor(elevator.currentFloor());
  110.             elevator.goingDownIndicator(true);
  111.             elevator.goingUpIndicator(true);
  112.             floors.forEach(checkforbutton);
  113.           });
  114.         }
  115.         // Make magic with each elevator.
  116.         elevators.forEach(elevatorMagic);
  117.     },
  118.     update: function(dt, elevators, floors) {
  119.         // We normally don't need to do anything here
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement