-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleAttempt.js
80 lines (66 loc) · 2.59 KB
/
simpleAttempt.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
{
init: function(elevators, floors) {
var Elevator = function (elevator) {
var self = this;
self.elevator = elevator;
self.requestQueue = [];
self.servicingRequest = false;
self.requestedFloor = [];
self.elevator.on("floor_button_pressed", function(floorNum) {
self.goToFloor(floorNum);
});
self.elevator.on("stopped_at_floor", function(floorNum) {
console.log("Arrived at " + floorNum);
self.servicingRequest = false;
self.requestedFloor = 0;
_.pull(self.requestQueue, floorNum);
if(self.requestQueue.length > 0)
{
self.goToFloor(self.requestQueue[0]);
}
});
self.hasRoom = function()
{
return self.elevator.loadFactor < 0.4;
};
self.goToFloor = function(floorNum) {
console.log("Floor Requested " + floorNum);
if(!self.servicingRequest)
{
self.servicingRequest = true;
self.requestedFloor = floorNum;
self.elevator.goToFloor(floorNum);
}
else
{
self.requestQueue.push(floorNum);
}
};
};
var Manager = function(elevators) {
self = this;
self.elevators = _.map(elevators, function (elevator)
{
return new Elevator(elevator);
});
console.log(self.elevators.length);
self.getNextElevator = function()
{
var sorted = _.sortBy(_.filter(self.elevators, 'hasRoom'), function(e) { return e.requestQueue.length });
return sorted[0];
}
};
var manager = new Manager(elevators);
_.forEach(floors, function(floor) {
floor.on("up_button_pressed", function() {
manager.getNextElevator().goToFloor(this.floorNum());
});
floor.on("down_button_pressed", function() {
manager.getNextElevator().goToFloor(this.floorNum());
});
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}