Created
March 15, 2016 18:47
-
-
Save vvscode/bcc1a2b8c35fffec31f3 to your computer and use it in GitHub Desktop.
Задача про лифт
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var DIRECTION_DOWN = -1 | |
| var DIRECTION_NONE = 0 | |
| var DIRECTION_UP = 1 | |
| function HardwareElevator(){}; | |
| HardwareElevator.prototype = { | |
| moveUp:function(){console.log('moving up');}, | |
| moveDown:function(){console.log('moving down');}, | |
| stopAndOpenDoors:function(){console.log('stopping and opening doors');}, | |
| getCurrentFloor:function(){console.log('getting current floor');}, | |
| getCurrentDirection:function(){console.log('getting current drection');} | |
| } | |
| function Elevator() { | |
| this.hw = new HardwareElevator(); | |
| this.hw.addEventListener("doorsClosed", _bind(this.onDoorsClosed, this)); | |
| this.hw.addEventListener("beforeFloor", _bind(this.onBeforeFloor, this)); | |
| this.queue = []; // list of tasks | |
| } | |
| Elevator.prototype = { | |
| onDoorsClosed: function(floor) { | |
| this._nextMovement(); | |
| }, | |
| onBeforeFloor: function(floor, direction) { | |
| this._prevDirection = direction; | |
| if(var task = this.queue.find(item => item.floor === floor && item.direction === direction)) { | |
| this.queue = this.queue.filter(i => i === task); | |
| this.stopAndOpenDoors(); | |
| }else if(!this.queue.find(item => isNext(item.floor, floor, direction)) { | |
| this.queue = this.queue.filter(i => i.floor !== floor); | |
| this.stopAndOpenDoors(); | |
| } | |
| }, | |
| floorButtonPressed: function(floor, direction) { | |
| this.queue.push({floor, direction}); | |
| (this.queue.length === 1) && this._nextMovement(); | |
| }, | |
| cabinButtonPressed: function(floor) { | |
| // if currentFloor not equ to floor - add request to queue | |
| direction = floor - this.getCurrentFloor(); // ?? | |
| direction /= Math.abs(direction); | |
| direction && this.queue.push({floor, direction}); | |
| // { floor, direction } === { floor: floor, direction: direction }; | |
| (this.queue.length === 1) && this. _nextMovement(); | |
| }, | |
| _nextMovement() { | |
| var currentFloor = this.getCurrentFloor(); | |
| var direction = this._prevDirection || this.getCurrentDirection(); | |
| if(!this.queue.length) { | |
| return; | |
| } | |
| var targetFloor; | |
| if(direction === DIRECTION_UP) { | |
| targetFloor = Math.max(...this.queue.map(i => i.floor)); | |
| } else if(direction === DIRECTION_DOWN) { | |
| targetFloor = Math.min(...this.queue.map(i => i.floor)); | |
| } else { | |
| targetFloor = this.queue[0].floor; | |
| } | |
| var taskDirection = targetFloor - currentFloor; | |
| taskDirection /= Math.abs(taskDirection); | |
| var movement = taskDirection === DIRECTION_DOWN ? 'modeDown' : 'moveUp'; | |
| taskDirection && this.hw[movement](); | |
| } | |
| } | |
| function isNext(floor, currentFloor, direction) { | |
| if(direction === DIRECTION_UP) { | |
| return floor > currentFloor; | |
| } else if (direction === DIRECTION_DOWN) { | |
| return floor < currentFloor; | |
| } | |
| return floor !== currentFloor; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment