Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- init: function(elevators, floors) {
- var sortedElevators = function(elevators){
- var tuples = [];
- var out = [];
- var debuglog = '';
- for (e = 0; e < elevators.length; e++) {
- if(elevators[e].loadFactor() < 0.6)
- tuples.push([e, elevators[e].loadFactor()]);
- }
- tuples.sort(function(a, b) {
- a = a[1];
- b = b[1];
- return a < b ? -1 : (a > b ? 1 : 0);
- });
- for (i = 0; i < tuples.length; i++){
- out.push( tuples[i][0] );
- debuglog = debuglog + 'E' + tuples[i][0] + '(' + tuples[i][1] + ') ';
- }
- //console.log('Result: ' + debuglog);
- return out;
- }
- var goTo = function(elevators, elevatorNum, floorNum){
- elevator = elevators[elevatorNum];
- //console.log('E: goTo E'+ elevatorNum + ' ' + floorNum + ' ' + elevator.direction);
- elevator.goToFloor(floorNum);
- elevator.destinationQueue.sort();
- if(elevator.direction == 'down')
- elevator.destinationQueue.reverse();
- elevator.checkDestinationQueue();
- }
- var elevatorStatus = function(elevators) {
- out = '\t\t\t\t\t';
- for (a = 0; a < elevators.length; a++) {
- out = out + 'E' + a + '@' + elevators[a].currentFloor();
- if(elevators[a].direction == 'up')
- out = out + '^';
- else
- out = out + 'v';
- out = out + ' (';
- for (i = 0; i < elevators[a].destinationQueue.length; i++) {
- out = out + elevators[a].destinationQueue[i] + ' ';
- }
- out = out + ')';
- out = out + '[';
- for (i = 0; i < elevators[a].later.length; i++){
- out = out + ' ' + elevators[a].later[i];
- }
- out = out + '] - ';
- }
- out = out + ' L: ';
- for (i = 0; i < globalLater.length; i++){
- out = out + ' ' + globalLater[i];
- }
- return out;
- }
- var showQueue = function(elevators) {
- console.log('showQueue: \t\t' + elevatorStatus(elevators));
- };
- console.log('o hai!');
- for (a = 0; a < elevators.length; a++){
- elevators[a].nr = a;
- elevators[a].later = [];
- elevators[a].goToFloor(0);
- }
- var globalLater = [];
- for (e = 0; e < elevators.length; e++) {
- elevators[e].on("idle", function() {
- console.log('E'+this.nr+': im IDLE');
- if(this.direction == 'up'){
- this.direction = 'down';
- }
- else{
- this.direction = 'up';
- }
- console.log('E'+this.nr+': new direction->' + this.direction + ' ' + elevatorStatus(elevators) );
- while(this.later.length){
- f = this.later.pop();
- goTo(elevators, this.nr, f);
- }
- while(this.destinationQueue.length < 3 && globalLater.length){
- f = globalLater.pop();
- goTo(elevators, this.nr, f);
- }
- showQueue(elevators);
- if(!this.destinationQueue.length) this.goToFloor(0);
- } );
- elevators[e].on("floor_button_pressed", function(floorNum) {
- console.log('### E'+this.nr+': floor_button_pressed: goto '+ floorNum);
- if( ( this.direction == 'up' && floorNum >= this.currentFloor() ) ||
- ( this.direction == 'down' && floorNum <= this.currentFloor() ) ){
- goTo(elevators, this.nr, floorNum);
- }
- else{
- this.later.push(floorNum);
- }
- showQueue(elevators);
- } );
- elevators[e].on("stopped_at_floor", function(floorNum) {
- console.log('E'+this.nr+': stop @ floor:'+ floorNum + ' ' + elevatorStatus(elevators));
- //showQueue(elevators);
- });
- }
- for (a = 0; a < floors.length; a++) {
- floors[a].on("up_button_pressed down_button_pressed", function() {
- console.log('###: up/down butt @ floor:'+ this.floorNum() + ' ' + elevatorStatus(elevators));
- service = 0;
- ele = sortedElevators(elevators);
- while(ele.length){
- e = ele.shift();
- if( (elevators[e].direction == 'up' && this.floorNum() >= elevators[e].currentFloor() ) ||
- (elevators[e].direction == 'down' && this.floorNum() <= elevators[e].currentFloor() ) ){
- goTo(elevators, e, this.floorNum());
- service = 1;
- break;
- }
- }
- if(!service){
- globalLater.push(this.floorNum());
- }
- showQueue(elevators);
- } );
- }
- },
- update: function(dt, elevators, floors) {
- // We normally don't need to do anything here
- },
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement