-
Notifications
You must be signed in to change notification settings - Fork 0
/
managerAttempt.js
89 lines (70 loc) · 2.76 KB
/
managerAttempt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{
init: function(elevators, floors) {
var DirectionEnum = { NONE : 0, UP : 1, DOWN : 2 };
var Elevator = function (elevator, manager) {
var self = this;
self.elevator = elevator;
self.manager = manager;
self.currentRequest = -1;
self.currentDirection = DirectionEnum.NONE;
self.stopFloors = [];
self.elevator.on("stopped_at_floor", function(floorNum) {
if(floorNum == self.currentRequest)
{
self.currentRequest = -1;
self.currentDirection = DirectionEnum.NONE;
}
});
self.elevator.on("floor_button_pressed", function(floorNum) {
});
self.handleRequest = function(floorNum) {
if(self.canHandleRequest(floorNum))
{
self.stopFloors.push(floorNum);
if(self.currentRequest == -1)
{
self.currentRequest = floorNum;
elevator.goToFloor(floorNum);
}
return true;
}
return false;
};
self.canHandleRequest = function(floorNum) {
if(self.currentDirection == DirectionEnum.NONE)
{
return true;
}
return false;
};
self.requestIsUp = function(floorNum) {
return floorNum < currentFloor;
};
};
var Manager = function(elevators) {
var self = this;
self.elevators = _.map(elevators, function (e) { return new Elevator(e) });
self.requestedFloors = [];
self.requestElevator = function(floorNum) {
self.requestedFloors.push(floorNum);
self.getElevator().handleRequest(floorNum);
};
self.getElevator = function(floorNum)
{
return self.elevators[0];
}
};
var manager = new Manager(elevators);
_.forEach(floors, function(floor) {
floor.on("up_button_pressed", function() {
manager.requestElevator(this.floorNum());
});
floor.on("down_button_pressed", function() {
manager.requestElevator(this.floorNum());
});
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}