|
|
@@ -1,76 +1,97 @@
|
|
1
|
1
|
{
|
|
2
|
2
|
init: function(elevators, floors) {
|
|
3
|
3
|
console.clear();
|
|
4
|
|
- console.log("----" + "\n");
|
|
5
|
4
|
console.log("Starting...");
|
|
6
|
|
- console.log("----" + "\n");
|
|
7
|
5
|
|
|
8
|
|
- let floorCount = floors.length;
|
|
9
|
|
- let bottomFloor = 0;
|
|
10
|
|
- let topFloor = floorCount - 1;
|
|
|
6
|
+ // Get elevator/building info
|
|
|
7
|
+ let floorCount = floors.length;
|
|
|
8
|
+ let bottomFloor = 0;
|
|
|
9
|
+ let topFloor = floorCount - 1;
|
|
11
|
10
|
let elevatorCount = elevators.length;
|
|
12
|
11
|
|
|
13
|
|
- let upRequestFloorSet = new Set();
|
|
14
|
|
- let downRequestFloorSet = new Set();
|
|
|
12
|
+ // Direction/status constants
|
|
|
13
|
+ const UP = 0;
|
|
|
14
|
+ const DOWN = 1;
|
|
|
15
|
+ const IDLE = 2;
|
|
|
16
|
+
|
|
|
17
|
+ // Create a data structure for floor requests
|
|
|
18
|
+ let requests = [new Set(), new Set()];
|
|
15
|
19
|
|
|
16
|
20
|
// Register floor button presses
|
|
17
|
21
|
floors.forEach(function (floor) {
|
|
18
|
|
- floor.on("up_button_pressed", function() {
|
|
19
|
|
- console.log("Up request: " + floor.level);
|
|
20
|
|
- upRequestFloorSet.add(floor.floorNum);
|
|
21
|
|
- });
|
|
22
|
|
-
|
|
23
|
|
- floor.on("down_button_pressed", function() {
|
|
24
|
|
- console.log("Down request: " + floor.level);
|
|
25
|
|
- downRequestFloorSet.add(floor.floorNum);
|
|
26
|
|
- });
|
|
|
22
|
+ floor.on("up_button_pressed", function() { requests[UP].add(floor.floorNum); });
|
|
|
23
|
+ floor.on("down_button_pressed", function() { requests[DOWN].add(floor.floorNum); });
|
|
27
|
24
|
});
|
|
28
|
25
|
|
|
|
26
|
+
|
|
|
27
|
+ // ----
|
|
|
28
|
+ // Functions
|
|
|
29
|
+ // ----
|
|
|
30
|
+
|
|
|
31
|
+ function getClosestFloor(currentFloor, floorList){
|
|
|
32
|
+ let distance = floorCount;
|
|
|
33
|
+ let closestFloor = currentFloor;
|
|
|
34
|
+ floorList.forEach(function(floorNum) {
|
|
|
35
|
+ let newDistance = Math.abs(currentFloor - floorNum);
|
|
|
36
|
+ if (newDistance < distance) {
|
|
|
37
|
+ closestFloor = floorNum;
|
|
|
38
|
+ distance = newDistance;
|
|
|
39
|
+ }
|
|
|
40
|
+ })
|
|
|
41
|
+ return closestFloor;
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ // ----
|
|
29
|
45
|
// Individual elevators
|
|
30
|
|
- let elevator = elevators[0];
|
|
31
|
|
- let elevatorIndex = elevators.indexOf(elevator);
|
|
|
46
|
+ // ----
|
|
32
|
47
|
|
|
33
|
|
- let destinationSet = new Set();
|
|
|
48
|
+ elevators.forEach(function (elevator) {
|
|
|
49
|
+ elevator.on("floor_button_pressed", function(floorNum) { destinations.add(floorNum); });
|
|
34
|
50
|
|
|
35
|
|
- let defaultRange = [
|
|
36
|
|
- (elevatorIndex + 0) * Math.floor(floorCount / (elevatorCount + 1)),
|
|
37
|
|
- (elevatorIndex + 1) * Math.floor(floorCount / (elevatorCount + 1)),
|
|
38
|
|
- (elevatorIndex + 2) * Math.floor(floorCount / (elevatorCount + 1))
|
|
39
|
|
- ]
|
|
|
51
|
+ // Basic elevator info
|
|
|
52
|
+ const elevatorIndex = elevators.indexOf(elevator);
|
|
|
53
|
+ const restFloors = [
|
|
|
54
|
+ (elevatorIndex + 0) * Math.floor(floorCount / (elevatorCount + 1)),
|
|
|
55
|
+ (elevatorIndex + 1) * Math.floor(floorCount / (elevatorCount + 1)),
|
|
|
56
|
+ (elevatorIndex + 2) * Math.floor(floorCount / (elevatorCount + 1))
|
|
|
57
|
+ ]
|
|
40
|
58
|
|
|
41
|
|
- // Elevator starts on floor 0; must be going up
|
|
42
|
|
- elevator.goingUpIndicator(true);
|
|
43
|
|
- elevator.goingDownIndicator(false);
|
|
|
59
|
+ // Elevator variables
|
|
|
60
|
+ let status = UP;
|
|
|
61
|
+ let destinations = new Set();
|
|
44
|
62
|
|
|
45
|
|
- // Go to designated starting floor
|
|
46
|
|
- console.log("Sending elevator[" + elevatorIndex + "] to floor[" + defaultRange[0] + "]")
|
|
47
|
|
- elevator.goToFloor((elevatorIndex + 0) * Math.floor(floorCount / (elevatorCount + 1)));
|
|
|
63
|
+ // Go to designated starting floor
|
|
|
64
|
+ console.log("Sending elevator[" + elevatorIndex + "] to floor[" + restFloors[0] + "]")
|
|
|
65
|
+ elevator.goToFloor(restFloors[0]);
|
|
48
|
66
|
|
|
49
|
|
- elevator.on("idle", function() {
|
|
50
|
|
- console.log("Idle...");
|
|
51
|
|
- elevator.goingUpIndicator(false);
|
|
52
|
|
- elevator.goingDownIndicator(false);
|
|
53
|
|
- });
|
|
|
67
|
+ elevator.on("idle", function() {
|
|
|
68
|
+ status = IDLE;
|
|
|
69
|
+ if (requests[UP].size == 0 && requests[DOWN].size == 0 && destinations.size == 0) {
|
|
|
70
|
+ elevator.goToFloor(restFloors[1]);
|
|
|
71
|
+ } else {
|
|
|
72
|
+ let allStops = new Set([...destinations, ...requests[UP], ...requests[DOWN]]);
|
|
|
73
|
+ elevator.goToFloor(getClosestFloor(elevator.currentFloor(), allStops));
|
|
|
74
|
+ }
|
|
|
75
|
+ });
|
|
54
|
76
|
|
|
55
|
|
- elevator.on("floor_button_pressed", function(floorNum) {
|
|
56
|
|
- console.log("Floor pressed: " + floorNum);
|
|
57
|
|
- destinationSet.add(floorNum);
|
|
58
|
|
- elevator.goToFloor(floorNum);
|
|
59
|
|
- });
|
|
|
77
|
+ elevator.on("passing_floor", function(floorNum, direction) {
|
|
|
78
|
+ if(requests[UP].has(floorNum) || requests[DOWN].has(floorNum) || destinations.has(floorNum)) {
|
|
|
79
|
+ elevator.goToFloor(floorNum, true);
|
|
|
80
|
+ }
|
|
|
81
|
+ });
|
|
60
|
82
|
|
|
61
|
|
- elevator.on("passing_floor", function(floorNum, direction) {
|
|
62
|
|
- if(upRequestFloorSet.has(floorNum) || downRequestFloorSet.has(floorNum) || destinationSet.has(floorNum)) {
|
|
63
|
|
- elevator.goToFloor(floorNum);
|
|
64
|
|
- }
|
|
65
|
|
- });
|
|
|
83
|
+ elevator.on("stopped_at_floor", function(floorNum) {
|
|
|
84
|
+ requests[UP].delete(floorNum);
|
|
|
85
|
+ requests[DOWN].delete(floorNum);
|
|
|
86
|
+ destinations.delete(floorNum);
|
|
66
|
87
|
|
|
67
|
|
- elevator.on("stopped_at_floor", function(floorNum) {
|
|
68
|
|
- console.log("Stopped at: " + floorNum);
|
|
69
|
|
- upRequestFloorSet.delete(floorNum);
|
|
70
|
|
- downRequestFloorSet.delete(floorNum);
|
|
71
|
|
- destinationSet.delete(floorNum);
|
|
|
88
|
+ if (destinations.size > 0 || requests[UP] > 0 || requests[DOWN] > 0) {
|
|
|
89
|
+ let allStops = new Set([...destinations, ...requests[UP], ...requests[DOWN]]);
|
|
|
90
|
+ elevator.goToFloor(getClosestFloor(elevator.currentFloor(), allStops));
|
|
|
91
|
+ }
|
|
|
92
|
+ });
|
|
72
|
93
|
});
|
|
73
|
94
|
},
|
|
74
|
95
|
|
|
75
|
96
|
update: function(dt, elevators, floors) {}
|
|
76
|
|
-}
|
|
|
97
|
+}
|