Advertisement
Guest User

elevator saga solution

a guest
Jan 24th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.        
  3.     init: function(elevators, floors) {
  4.         var sortedElevators = function(elevators){
  5.             var tuples = [];
  6.             var out = [];
  7.             var debuglog = '';
  8.             for (e = 0; e < elevators.length; e++) {
  9.                 if(elevators[e].loadFactor() < 0.6)
  10.                     tuples.push([e, elevators[e].loadFactor()]);
  11.             }
  12.             tuples.sort(function(a, b) {
  13.                 a = a[1];
  14.                 b = b[1];
  15.                 return a < b ? -1 : (a > b ? 1 : 0);
  16.             });
  17.             for (i = 0; i < tuples.length; i++){
  18.                 out.push( tuples[i][0] );
  19.                 debuglog = debuglog + 'E' + tuples[i][0] + '(' +  tuples[i][1] + ') ';
  20.             }
  21.             console.log('Result: '  + debuglog);
  22.             return out;
  23.         }
  24.         var goTo = function(elevators, elevatorNum, floorNum){
  25.             elevator = elevators[elevatorNum];
  26.             console.log('E: goTo E'+ elevatorNum + ' ' + floorNum + ' ' + elevator.direction);
  27.             elevator.goToFloor(floorNum);
  28.            
  29.             elevator.destinationQueue.sort();
  30.             if(elevator.direction == 'down')
  31.                 elevator.destinationQueue.reverse();
  32.             elevator.checkDestinationQueue();
  33.            
  34.         }
  35.         var elevatorStatus = function(elevators) {
  36.             out = '\t\t\t\t\t';
  37.             for (a = 0; a < elevators.length; a++) {
  38.                 out = out + 'E' + a + '@' + elevators[a].currentFloor();
  39.                 if(elevators[a].direction == 'up')
  40.                     out = out + '^';
  41.                 else
  42.                     out = out + 'v';
  43.                 out = out + ' (';
  44.                 for (i = 0; i < elevators[a].destinationQueue.length; i++) {
  45.                     out = out + elevators[a].destinationQueue[i] + ' ';
  46.                 }
  47.                 out = out + ')';
  48.                 out = out + '[';
  49.                 for (i = 0; i < elevators[a].later.length; i++){
  50.                     out = out + ' ' + elevators[a].later[i];
  51.                 }
  52.                 out = out + ']  -  ';
  53.             }
  54.             out = out + ' L: ';
  55.             for (i = 0; i < globalLater.length; i++){
  56.                 out = out + ' ' + globalLater[i];
  57.             }
  58.  
  59.             return out;
  60.         }
  61.         var showQueue = function(elevators) {
  62.             console.log('showQueue: \t\t' + elevatorStatus(elevators));
  63.         };        
  64.        
  65.         console.log('o hai!');
  66.        
  67.         for (a = 0; a < elevators.length; a++){
  68.             elevators[a].nr = a;
  69.             elevators[a].later = [];
  70.             elevators[a].goToFloor(0);
  71.         }
  72.          
  73.         var globalLater = [];
  74.  
  75.        
  76.         for (e = 0; e < elevators.length; e++) {
  77.            
  78.             elevators[e].on("idle", function() {
  79.                 console.log('E'+this.nr+': im IDLE');
  80.                 if(this.direction == 'up'){
  81.                     this.direction = 'down';
  82.                 }
  83.                 else{
  84.                     this.direction = 'up';
  85.                 }
  86.                 console.log('E'+this.nr+': new direction -> ' + this.direction + ' ' + elevatorStatus(elevators) );
  87.                 while(this.later.length){
  88.                     f = this.later.pop();
  89.                     goTo(elevators, this.nr, f);
  90.                 }
  91.                
  92.                 while(this.destinationQueue.length < 3 && globalLater.length){
  93.                     f = globalLater.pop();
  94.                     goTo(elevators, this.nr, f);
  95.                 }
  96.                
  97.                 showQueue(elevators);            
  98.                 if(!this.destinationQueue.length) this.goToFloor(0);
  99.             } );
  100.        
  101.             elevators[e].on("floor_button_pressed", function(floorNum) {
  102.                 console.log('### E'+this.nr+': floor_button_pressed: goto '+ floorNum);
  103.            
  104.                     if( ( this.direction == 'up' && floorNum >= this.currentFloor() ) ||
  105.                        ( this.direction == 'down' && floorNum <= this.currentFloor() ) ){
  106.                         goTo(elevators, this.nr, floorNum);
  107.                     }
  108.                     else{
  109.                         this.later.push(floorNum);
  110.                     }
  111.            
  112.                 showQueue(elevators);
  113.             } );
  114.             elevators[e].on("stopped_at_floor", function(floorNum) {
  115.                 console.log('E'+this.nr+': stop @ floor:'+ floorNum + ' ' + elevatorStatus(elevators));
  116.            
  117.                 showQueue(elevators);
  118.             });
  119.         }
  120.         for (a = 0; a < floors.length; a++) {
  121.             floors[a].on("up_button_pressed down_button_pressed", function() {
  122.                 console.log('###: up/down butt @ floor:'+ this.floorNum() + ' ' + elevatorStatus(elevators));
  123.                 service = 0;
  124.                 ele = sortedElevators(elevators);
  125.                 while(ele.length){
  126.                     e = ele.shift();
  127.                     if( (elevators[e].direction == 'up' && this.floorNum() >= elevators[e].currentFloor() ) ||
  128.                             (elevators[e].direction == 'down' && this.floorNum() <= elevators[e].currentFloor() ) ){
  129.                         goTo(elevators, e, this.floorNum());
  130.                         service = 1;
  131.                         break;
  132.                     }
  133.                 }
  134.                 if(!service){
  135.                     globalLater.push(this.floorNum());
  136.                 }
  137.                 showQueue(elevators);
  138.             } );
  139.         }
  140.            
  141.     },
  142.     update: function(dt, elevators, floors) {
  143.         // We normally don't need to do anything here
  144.     },
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement