-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
70 lines (54 loc) · 1.7 KB
/
test.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
"use strict";
var Elevator = require('./elevator');
var Controller = require('./controllers');
var Passenger = require('./passenger');
var process = require('process');
var SatisfactionAnalyzer = require('./satisfactionAnalyzer');
var debug = require('debug')('elevation:test');
let elevator = new Elevator();
let controller = new Controller(elevator);
let done = false;
if (process.argv.length < 3) {
console.log('Please provide an input file (eg: in1) as an argument');
console.log('Eg: $ DEBUG=* node test in1');
process.exit(1);
}
let data = require('./data/' + process.argv[2]);
function* getNextPassenger() {
for (let i = 0; i < data.length; i++) {
data[i].id = i;
yield data[i];
}
}
function generatePassenger() {
let passengerData = generator.next();
let newPassenger;
if (passengerData.done) {
done = true;
setInterval(checkForDone, 1000);
} else {
debug('Creating new passenger. Going from %d to %d', passengerData.value.origin, passengerData.value.destination);
newPassenger = new Passenger(passengerData.value.origin, elevator, passengerData.value.origin, passengerData.value.direction, passengerData.value.destination);
newPassenger.on('disembark', checkForDone);
passengers.push(newPassenger);
setTimeout(generatePassenger, passengerData.value.delay * 1000);
}
}
function checkForDone() {
if (!done) {
return;
}
for (let i = 0; i < passengers.length; i++) {
if (Passenger.DONE !== passengers[i].getState()) {
return;
}
}
for (let i = 0; i < passengers.length; i++) {
SatisfactionAnalyzer.analyze(passengers[i]);
}
debug('All passengers have been delivered safely');
process.exit();
}
let generator = getNextPassenger();
let passengers = [];
generatePassenger();