-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
206 lines (159 loc) · 6.61 KB
/
script.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
document.getElementById('generate').addEventListener('click', generateBuilding);
let liftState = [];
let pendingRequests = { up: [], down: [] };
let liftBusy = [];
let requestedFloors = { up: new Set(), down: new Set() };
let liftDirections = []; // To track each lift's current direction
function generateBuilding() {
const errorMessage = document.getElementById('error-message');
if (errorMessage) {
errorMessage.remove();
}
const floorsCount = parseInt(document.getElementById('floors').value);
const liftsCount = parseInt(document.getElementById('lifts').value);
// Input validation
if (isNaN(floorsCount) || isNaN(liftsCount) || floorsCount < 1 || liftsCount < 1 || floorsCount == 1) {
displayError('Please enter valid positive numbers greater than 1 for both floors and lifts.');
return;
}
const control = document.getElementById('control-panel');
control.innerHTML = '';
const building = document.getElementById('building');
building.innerHTML = '';
liftState = Array(liftsCount).fill(1);
liftBusy = Array(liftsCount).fill(false);
liftDirections = Array(liftsCount).fill(null); // Initialize lift directions as null
requestedFloors = { up: new Set(), down: new Set() };
// Create floors
for (let i = 1; i <= floorsCount; i++) {
const floor = document.createElement('div');
floor.className = 'floor';
floor.dataset.floor = i;
const floorInfo = document.createElement('div');
floorInfo.className = 'floor-info';
const floorNumber = document.createElement('div');
floorNumber.className = 'floor-number';
floorNumber.innerText = `Floor ${i}`; // Add floor name
const floorButtons = document.createElement('div');
floorButtons.className = 'floor-buttons';
if (i !== floorsCount) {
const upButton = document.createElement('button');
upButton.className = 'up';
upButton.innerText = '↑';
upButton.onclick = () => requestLift(i, 'up');
floorButtons.appendChild(upButton);
}
if (i !== 1) {
const downButton = document.createElement('button');
downButton.className = 'down';
downButton.innerText = '↓';
downButton.onclick = () => requestLift(i, 'down');
floorButtons.appendChild(downButton);
}
floorInfo.appendChild(floorButtons); // Append buttons first
floorInfo.appendChild(floorNumber); // Append floor name next
floor.appendChild(floorInfo);
building.appendChild(floor);
}
// Create lifts
for (let i = 0; i < liftsCount; i++) {
const lift = document.createElement('div');
lift.className = 'lift';
lift.dataset.lift = i;
lift.style.transform = `translateY(0px)`;
lift.style.left = `${(i * 70) + 100}px`;
building.firstChild.appendChild(lift);
}
}
function displayError(message) {
const controlPanel = document.getElementById('control-panel');
const errorMessage = document.createElement('div');
errorMessage.id = 'error-message';
errorMessage.style.color = 'red';
errorMessage.style.marginTop = '10px';
errorMessage.innerText = message;
controlPanel.appendChild(errorMessage);
}
function requestLift(floor, direction) {
const button = document.querySelector(`[data-floor="${floor}"] .${direction}`);
// Change the button color to red when pressed
if (button) {
button.style.backgroundColor = 'red';
}
if (requestedFloors[direction].has(floor)) {
return;
}
requestedFloors[direction].add(floor);
pendingRequests[direction].push(floor);
processNextRequest(direction);
}
function processNextRequest(direction) {
if (pendingRequests[direction].length === 0) return;
const floor = pendingRequests[direction].shift();
const lifts = document.querySelectorAll('.lift');
const targetY = -(floor - 1) * 112;
let closestLift = null;
let minDistance = Infinity;
lifts.forEach(lift => {
const liftIndex = parseInt(lift.dataset.lift);
const currentFloor = liftState[liftIndex];
const isBusy = liftBusy[liftIndex];
const currentDirection = liftDirections[liftIndex];
const distance = Math.abs(currentFloor - floor);
if (!isBusy && (currentDirection === direction || currentDirection === null)) {
if (distance < minDistance) {
closestLift = lift;
minDistance = distance;
}
}
});
if (closestLift) {
const liftIndex = parseInt(closestLift.dataset.lift);
liftBusy[liftIndex] = true;
liftDirections[liftIndex] = direction;
moveLift(closestLift, liftIndex, floor, targetY, direction);
} else {
pendingRequests[direction].push(floor);
setTimeout(() => processNextRequest(direction), 1000);
}
}
function moveLift(lift, liftIndex, targetFloor, targetY, direction) {
const currentFloor = liftState[liftIndex];
const floorsToMove = Math.abs(currentFloor - targetFloor);
const moveTime = floorsToMove * 2000;
setTimeout(() => {
lift.style.transition = `transform ${moveTime}ms ease`;
lift.style.transform = `translateY(${targetY}px)`;
liftState[liftIndex] = targetFloor;
setTimeout(() => {
openDoors(lift, liftIndex, targetFloor, direction);
}, moveTime);
}, 1000);
}
function openDoors(lift, liftIndex, targetFloor, direction) {
if (!lift.classList.contains('door-open')) {
lift.classList.add('door-open');
// Reset the button color back to normal
const button = document.querySelector(`[data-floor="${targetFloor}"] .${direction}`);
if (button) {
button.style.backgroundColor = ''; // Reset to default color
}
setTimeout(() => {
closeDoors(lift, liftIndex, direction);
requestedFloors[direction].delete(targetFloor);
// Check if there are requests in the same direction that the lift can pick up
if (requestedFloors[direction].size > 0) {
processNextRequest(direction);
} else {
liftDirections[liftIndex] = null; // Reset direction if no more requests in that direction
}
}, 2500);
}
}
function closeDoors(lift, liftIndex, direction) {
if (lift.classList.contains('door-open')) {
lift.classList.remove('door-open');
}
liftBusy[liftIndex] = false;
setTimeout(() => processNextRequest(direction), 500);
}