-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
253 lines (223 loc) · 7.58 KB
/
index.js
File metadata and controls
253 lines (223 loc) · 7.58 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
let lifts = [];
let floors = 0;
let requestQueue = [];
const formID = document.getElementById('lift-sim').addEventListener('submit', function (e) {
e.preventDefault();
const floorCount = parseInt(document.getElementById('floorCount').value, 10);
const liftCount = parseInt(document.getElementById('liftCount').value, 10);
floorCount = Math.round(floorCount);
liftCount = Math.round(liftCount);
if (
Number.isNaN(floorCount) ||
!floorCount ||
floorCount < 1 ||
!isValidPositiveInteger(floorCount)
) {
alert('Invalid input Please enter the floor value greater than 1 ');
return;
}
if (
Number.isNaN(liftCount) ||
!liftCount ||
liftCount < 1 ||
!isValidPositiveInteger(liftCount)
) {
alert('Please enter the lift value greater than or equal to 1 ');
return;
}
initializeSimulation();
});
function isValidPositiveInteger(value) {
return /^\d+$/.test(value);
}
function initializeSimulation() {
const floorCount = parseInt(document.getElementById('floorCount').value, 10);
const liftCount = parseInt(document.getElementById('liftCount').value, 10);
floors = floorCount;
lifts = [];
requestQueue = [];
const simulation = document.getElementById('simulation');
simulation.innerHTML = '';
const building = createBuilding(floorCount, liftCount);
simulation.appendChild(building);
const liftsContainer = building.querySelector('.lifts-container');
for (let i = 0; i < liftCount; i++) {
const liftShaft = createLiftShaft(floorCount);
liftsContainer.appendChild(liftShaft);
lifts.push({
element: liftShaft.querySelector('.lift'),
currentFloor: 1,
targetFloor: null,
isMoving: false,
direction: null,
doorsOperating: false,
});
}
}
function createBuilding(floorCount) {
const building = document.createElement('div');
building.className = 'building';
building.style.height = `${floorCount * 100}px`;
const floorsContainer = document.createElement('div');
floorsContainer.className = 'floors-container';
building.appendChild(floorsContainer);
for (let i = 1; i <= floorCount; i++) {
const floor = document.createElement('div');
floor.className = 'floor';
floor.innerHTML = `
<span class="floor-text">Floor ${i}</span>
${
i < floorCount || floorCount === 1
? `<button class="up-button" onclick="callLift(${i}, 'up')" id="up-${i}">Up</button>`
: ''
}
${
i > 1
? `<button class="down-button" onclick="callLift(${i}, 'down')" id="down-${i}">Down</button>`
: ''
}
`;
floorsContainer.appendChild(floor);
}
const liftsContainer = document.createElement('div');
liftsContainer.className = 'lifts-container';
building.appendChild(liftsContainer);
return building;
}
function createLiftShaft() {
const liftShaft = document.createElement('div');
liftShaft.className = 'lift-shaft';
liftShaft.innerHTML = `
<div class="lift">
<div class="doors">
<div class="door left-door"></div>
<div class="door right-door"></div>
</div>
</div>
`;
return liftShaft;
}
function callLift(targetFloor, direction) {
const button = document.getElementById(`${direction}-${targetFloor}`);
if (button && !button.disabled) {
disableButton(button);
// Check if any lift is already at the target floor and not moving
const liftsAtTargetFloor = lifts.filter(
(lift) => lift.currentFloor === targetFloor && !lift.isMoving && !lift.doorsOperating
);
// If there's a lift at the target floor, open and close the doors and skip calling other lifts
if (liftsAtTargetFloor.length > 0) {
openAndCloseDoors(liftsAtTargetFloor[0]);
return; // Skip calling another lift since one is already here
}
// Check how many lifts are at or heading to the target floor
const liftsCalledToFloor = lifts.filter(
(lift) => lift.targetFloor === targetFloor || lift.currentFloor === targetFloor
);
// Only call another lift if fewer than 2 lifts are headed to or already at the floor
if (liftsCalledToFloor.length < 2) {
const nearestAvailableLift = findNearestAvailableLift(targetFloor);
if (nearestAvailableLift) {
prepareLiftMovement(nearestAvailableLift, targetFloor, direction);
} else {
// If no available lift, queue the request
requestQueue.push({ targetFloor, direction });
}
}
}
}
function prepareLiftMovement(lift, targetFloor, direction) {
lift.targetFloor = targetFloor;
lift.direction = direction;
if (lift.doorsOperating) {
return;
}
if (lift.currentFloor !== targetFloor) {
closeDoorsAndMove(lift);
} else {
openAndCloseDoors(lift);
}
}
function closeDoorsAndMove(lift) {
if (lift.doorsOperating) return;
lift.doorsOperating = true;
const doors = lift.element.querySelector('.doors');
doors.classList.remove('open');
setTimeout(() => {
lift.doorsOperating = false;
moveLift(lift);
}, 500);
}
function findNearestAvailableLift(targetFloor) {
return lifts.reduce((nearest, lift) => {
if (lift.isMoving || lift.doorsOperating) return nearest;
if (lift.currentFloor === targetFloor && lift.direction === direction) return lift;
const distance = Math.abs(lift.currentFloor - targetFloor);
return !nearest || distance < Math.abs(nearest.currentFloor - targetFloor) ? lift : nearest;
}, null);
}
function moveLift(lift) {
lift.isMoving = true;
const targetFloor = lift.targetFloor;
const moveTime = Math.abs(targetFloor - lift.currentFloor) * 2000;
lift.element.style.transition = `bottom ${moveTime}ms ease-in-out`;
lift.element.style.bottom = `${(targetFloor - 1) * 100}px`;
setTimeout(() => {
lift.currentFloor = targetFloor;
lift.isMoving = false;
openAndCloseDoors(lift);
const liftsAtFloor = lifts.filter((lift) => lift.currentFloor === targetFloor);
if (liftsAtFloor.length < 2) {
enableButton(document.getElementById(`up-${targetFloor}`));
enableButton(document.getElementById(`down-${targetFloor}`));
}
checkQueue();
}, moveTime);
}
function openAndCloseDoors(lift) {
if (lift.doorsOperating) return;
lift.doorsOperating = true;
openDoors(lift);
setTimeout(() => closeDoors(lift), 2000);
}
function openDoors(lift) {
const doors = lift.element.querySelector('.doors');
doors.classList.add('open');
}
function closeDoors(lift) {
const doors = lift.element.querySelector('.doors');
doors.classList.remove('open');
setTimeout(() => {
lift.doorsOperating = false;
resetLiftState(lift);
checkQueue();
enableButton(document.getElementById(`up-${lift.currentFloor}`));
enableButton(document.getElementById(`down-${lift.currentFloor}`));
}, 2000);
}
function resetLiftState(lift) {
lift.isMoving = false;
lift.targetFloor = null;
lift.direction = null;
}
function checkQueue() {
if (requestQueue.length > 0) {
const availableLift = findNearestAvailableLift(requestQueue[0].targetFloor);
if (availableLift) {
const nextRequest = requestQueue.shift();
prepareLiftMovement(availableLift, nextRequest.targetFloor, nextRequest.direction);
}
}
}
function disableButton(button) {
if (button) {
button.disabled = true;
button.style.cursor = 'not-allowed';
}
}
function enableButton(button) {
if (button) {
button.disabled = false;
button.style.cursor = 'pointer';
}
}