浏览代码

Improve code legibility

master
父节点
当前提交
3745effa3b
共有 1 个文件被更改,包括 115 次插入33 次删除
  1. 115
    33
      elevator.js

+ 115
- 33
elevator.js 查看文件

1
 {
1
 {
2
 	init: function(elevators, floors) {
2
 	init: function(elevators, floors) {
3
 		console.clear();
3
 		console.clear();
4
-		console.log("Starting...");
5
 
4
 
6
 		// Get elevator/building info
5
 		// Get elevator/building info
6
+		let elevatorCount = elevators.length;
7
 		let floorCount    = floors.length;
7
 		let floorCount    = floors.length;
8
-		let bottomFloor   = 0;
9
 		let topFloor      = floorCount - 1;
8
 		let topFloor      = floorCount - 1;
10
-		let elevatorCount = elevators.length;
9
+		let bottomFloor   = 0;
10
+
11
+		console.log("Starting (" + floorCount + " floors; " + elevatorCount + " elevator" + ((floorCount == 1) ? "s" : "") +")...");
11
 
12
 
12
-		// Direction/status constants
13
+		// Direction constants
13
 		const UP   = 0;
14
 		const UP   = 0;
14
 		const DOWN = 1;
15
 		const DOWN = 1;
15
 		const IDLE = 2;
16
 		const IDLE = 2;
17
 		// Create a data structure for floor requests
18
 		// Create a data structure for floor requests
18
 		let requests = [new Set(), new Set()];
19
 		let requests = [new Set(), new Set()];
19
 
20
 
20
-		// Register floor button presses
21
-		floors.forEach(function (floor) {
22
-			floor.on("up_button_pressed", function() { requests[UP].add(floor.floorNum); });
23
-			floor.on("down_button_pressed", function() { requests[DOWN].add(floor.floorNum); });
24
-		});
25
-
26
-
27
 		// ----
21
 		// ----
28
 		// Functions
22
 		// Functions
29
 		// ----
23
 		// ----
30
 
24
 
31
-		function getClosestFloor(currentFloor, floorList){
32
-			let distance = floorCount;
25
+		function getClosestFloor(currentFloor, floorList, direction = IDLE, ignoredFloors = new Set()){
33
 			let closestFloor = currentFloor;
26
 			let closestFloor = currentFloor;
27
+			let distance     = floorCount;
28
+			let newDistance  = distance;
29
+
34
 			floorList.forEach(function(floorNum) {
30
 			floorList.forEach(function(floorNum) {
35
-				let newDistance = Math.abs(currentFloor - floorNum);
36
-				if (newDistance < distance) {
31
+				switch (direction) {
32
+					case UP:
33
+						newDistance = floorNum - currentFloor;
34
+					case DOWN:
35
+						newDistance = currentFloor - floorNum;
36
+					case IDLE:
37
+						newDistance = Math.abs(floorNum - currentFloor);
38
+				}
39
+				if (newDistance < distance && newDistance > 0 && !(ignoredFloors.has(floorNum))) {
37
 					closestFloor = floorNum;
40
 					closestFloor = floorNum;
38
 					distance = newDistance;
41
 					distance = newDistance;
39
 				}
42
 				}
40
 			})
43
 			})
41
-			return closestFloor;
44
+
45
+			if (closestFloor != currentFloor) {
46
+				return closestFloor;
47
+			} else {
48
+				throw new Error("No floor in selected direction");
49
+			}
50
+		}
51
+
52
+		function sendToNext(elevator, destinations, requests, restFloors) {
53
+			if (elevator.loadFactor() < 1) {
54
+				if (requests[UP].size > 0 || requests[DOWN].size > 0 || destinations.size > 0) {
55
+					let allStops = new Set([...destinations, ...requests[UP], ...requests[DOWN]]);
56
+					try {
57
+						elevator.goToFloor(getClosestFloor(elevator.currentFloor(), allStops));
58
+					} catch (e) {
59
+						elevator.goToFloor(restFloors[1]);
60
+					}
61
+				}
62
+			} else {
63
+				elevator.goToFloor(getClosestFloor(elevator.currentFloor(), destinations));
64
+			}
65
+		}
66
+
67
+		function bringClosestElevator(floorNum) {
68
+			let closestElevator = 0;
69
+			let distance        = floorCount;
70
+			let newDistance     = distance;
71
+
72
+			elevators.forEach(function (elevator) {
73
+				newDistance = Math.abs(elevator.currentFloor() - floorNum);
74
+				if (newDistance < distance) {
75
+					distance = newDistance;
76
+					closestElevator = elevators.indexOf(elevator);
77
+				}
78
+			});
79
+
80
+			elevators[closestElevator].goToFloor(floorNum);
81
+		}
82
+
83
+		function setDirection(elevator, direction){
84
+			switch (direction) {
85
+				case UP:
86
+					elevator.goingUpIndicator(true);
87
+					elevator.goingDownIndicator(false);
88
+					break
89
+				case DOWN:
90
+					elevator.goingUpIndicator(false);
91
+					elevator.goingDownIndicator(true);
92
+					break
93
+				default:
94
+					elevator.goingUpIndicator(true);
95
+					elevator.goingDownIndicator(true);
96
+			}
42
 		}
97
 		}
43
 
98
 
44
 		// ----
99
 		// ----
45
-		// Individual elevators
100
+		// Floors
101
+		// ----
102
+
103
+		floors.forEach(function (floor) {
104
+			floor.on("up_button_pressed", function() {
105
+				console.log("> Up request on floor[" + floor.level + "]");
106
+				requests[UP].add(floor.floorNum);
107
+				bringClosestElevator(floor.floorNum);
108
+			});
109
+			floor.on("down_button_pressed", function() {
110
+				console.log("> Down request on floor[" + floor.level + "]");
111
+				requests[DOWN].add(floor.floorNum);
112
+				bringClosestElevator(floor.floorNum);
113
+			});
114
+		});
115
+
116
+		// ----
117
+		// Elevators
46
 		// ----
118
 		// ----
47
 
119
 
48
 		elevators.forEach(function (elevator) {
120
 		elevators.forEach(function (elevator) {
49
-		elevator.on("floor_button_pressed", function(floorNum) { destinations.add(floorNum); });
50
 
121
 
51
-		// Basic elevator info
52
-		const elevatorIndex = elevators.indexOf(elevator);
122
+			// Basic elevator info
123
+			const elevatorIndex = elevators.indexOf(elevator);
53
 			const restFloors = [
124
 			const restFloors = [
54
 				(elevatorIndex + 0) * Math.floor(floorCount / (elevatorCount + 1)),
125
 				(elevatorIndex + 0) * Math.floor(floorCount / (elevatorCount + 1)),
55
 				(elevatorIndex + 1) * Math.floor(floorCount / (elevatorCount + 1)),
126
 				(elevatorIndex + 1) * Math.floor(floorCount / (elevatorCount + 1)),
56
 				(elevatorIndex + 2) * Math.floor(floorCount / (elevatorCount + 1))
127
 				(elevatorIndex + 2) * Math.floor(floorCount / (elevatorCount + 1))
57
 			]
128
 			]
58
 
129
 
130
+			elevator.on("floor_button_pressed", function(floorNum) {
131
+				console.log("> Pressed floor[" + floorNum + "] on elevator[" + elevatorIndex + "]");
132
+				destinations.add(floorNum);
133
+				sendToNext(elevator, destinations, requests, restFloors);
134
+			});
135
+
59
 			// Elevator variables
136
 			// Elevator variables
60
-			let status = UP;
61
 			let destinations = new Set();
137
 			let destinations = new Set();
62
 
138
 
63
-			// Go to designated starting floor
64
-			console.log("Sending elevator[" + elevatorIndex + "] to floor[" + restFloors[0] + "]")
65
-			elevator.goToFloor(restFloors[0]);
139
+			// Start
140
+			setDirection(elevator, UP); // Starts on the ground floor
141
+			console.log("> Sending elevator[" + elevatorIndex + "] to floor[" + restFloors[0] + "]");
142
+			elevator.goToFloor(restFloors[0]); // Closest rest floor
66
 
143
 
144
+			// Idle
67
 			elevator.on("idle", function() {
145
 			elevator.on("idle", function() {
68
-				status = IDLE;
146
+				console.log("> Elevator[" + elevatorIndex + "] is idle")
147
+				setDirection(elevator, IDLE);
69
 				if (requests[UP].size == 0 && requests[DOWN].size == 0 && destinations.size == 0) {
148
 				if (requests[UP].size == 0 && requests[DOWN].size == 0 && destinations.size == 0) {
70
 					elevator.goToFloor(restFloors[1]);
149
 					elevator.goToFloor(restFloors[1]);
71
 				} else {
150
 				} else {
72
-					let allStops = new Set([...destinations, ...requests[UP], ...requests[DOWN]]);
73
-					elevator.goToFloor(getClosestFloor(elevator.currentFloor(), allStops));
151
+					sendToNext(elevator, destinations, requests, restFloors);
74
 				}
152
 				}
75
 			});
153
 			});
76
 
154
 
155
+			// Passing
77
 			elevator.on("passing_floor", function(floorNum, direction) {
156
 			elevator.on("passing_floor", function(floorNum, direction) {
78
-				if(requests[UP].has(floorNum) || requests[DOWN].has(floorNum) || destinations.has(floorNum)) {
157
+				if (elevator.loadFactor() < 1) {
158
+					if (requests[UP].has(floorNum) || requests[DOWN].has(floorNum) || destinations.has(floorNum)) {
159
+						elevator.goToFloor(floorNum, true);
160
+					}
161
+				} else if (destinations.has(floorNum)) {
79
 					elevator.goToFloor(floorNum, true);
162
 					elevator.goToFloor(floorNum, true);
80
 				}
163
 				}
81
 			});
164
 			});
82
 
165
 
166
+			// Stopped
83
 			elevator.on("stopped_at_floor", function(floorNum) {
167
 			elevator.on("stopped_at_floor", function(floorNum) {
168
+				console.log("> elevator[" + elevatorIndex + "] stopped at floor[" + floorNum + "]");
169
+				destinations.delete(floorNum);
84
 				requests[UP].delete(floorNum);
170
 				requests[UP].delete(floorNum);
85
 				requests[DOWN].delete(floorNum);
171
 				requests[DOWN].delete(floorNum);
86
-				destinations.delete(floorNum);
87
 
172
 
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
-				}
173
+				sendToNext(elevator, destinations, requests, restFloors);
92
 			});
174
 			});
93
 		});
175
 		});
94
 	},
176
 	},

正在加载...
取消
保存