-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverElevator-philippe.js
213 lines (197 loc) · 5.13 KB
/
serverElevator-philippe.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
206
207
208
209
210
211
212
213
// Montardon & diodfr
var express= require('express');
var url= require('url');
var querystring = require('querystring');
var app= express();
var floor = 0;
var direction = "UP";
var doorsOpened = false;
var calls = [];
var callsUp = [];
var callsDown = [];
var directions = [];
var gos = [];
var min = 0;
var max = 5;
// Frequencies
var ticks = 0;
var callsFrequencies = [0,0,0,0,0,0];
var updateMinMax = function(userCalls,userGos) {
var maxCalls = Math.max.apply(Math, userCalls);
var maxGos = Math.max.apply(Math,userGos);
max = Math.max(maxCalls,maxGos);
var minCalls = Math.min.apply(Math, userCalls);
var minGos = Math.min.apply(Math,userGos);
min = Math.min(minCalls,minGos);
};
app.get('/call', function(req,res) {
var params = querystring.parse(url.parse(req.url).query);
calls.push(parseInt(params['atFloor'],10));
if (params['to'] === "UP") {
callsUp.push(parseInt(params['atFloor'],10));
} else {
callsDown.push(parseInt(params['atFloor'],10));
}
callsFrequencies[parseInt(params['atFloor'])]++;
res.writeHead(200);
res.end();
updateMinMax(calls,gos);
console.log("call "+params['atFloor']+" "+params['to']);
console.log("Calls "+calls);
console.log("Gos "+gos);
});
app.get('/go', function(req,res) {
res.writeHead(200);
res.end();
var params = querystring.parse(url.parse(req.url).query);
gos.push(parseInt(params['floorToGo'],10));
updateMinMax(calls,gos);
console.log("go "+params['floorToGo']);
});
app.get('/userHasEntered', function(req,res) {
res.writeHead(200);
res.end();
console.log("userHasEntered");
});
app.get('/userHasExited', function(req,res) {
res.writeHead(200);
res.end();
console.log("userHasExited");
});
app.get('/reset', function(req,res) {
ticks = 0;
var params = querystring.parse(url.parse(req.url).query);
console.log("reset "+params['cause']+"==========================================");
floor = 0;
min = 0;
max = 5;
calls = [];
gos = [];
callsUp = [];
callsDown = [];
doorsOpened = false;
callsFrequencies = [0,0,0,0,0,0];
res.writeHead(200);
res.end();
});
var openDoors = function(floor) {
for(var i = gos.length - 1; i >= 0; i--) {
if(gos[i] === floor) {
gos.splice(i, 1);
}
}
for(var i = calls.length - 1; i >= 0; i--) {
if(calls[i] === floor) {
calls.splice(i, 1);
}
}
for(var i = callsUp.length - 1; i >= 0; i--) {
if(callsUp[i] === floor) {
callsUp.splice(i, 1);
}
}
for(var i = callsDown.length - 1; i >= 0; i--) {
if(callsDown[i] === floor) {
callsDown.splice(i, 1);
}
}
doorsOpened = true;
};
var closeDoors = function(res) {
res.end("CLOSE");
console.log("CLOSE");
doorsOpened = false;
};
var doNothing = function(res) {
res.end("NOTHING");
console.log("NOTHING");
}
var move = function(afloor,res) {
console.log("Floor: "+floor+" min "+min+" max "+max);
if (afloor >= max && direction == "UP") {
direction = "DOWN";
}
if (afloor <= min && direction == "DOWN") {
direction = "UP";
}
if (direction == "DOWN") {
floor--;
res.end("DOWN");
console.log("DOWN");
} else if (direction == "UP") {
floor++;
res.end("UP");
console.log("UP");
}
};
app.get('/status', function(req,res) {
res.writeHead(200);
res.end();
var sum = callsFrequencies[0]+callsFrequencies[1]+callsFrequencies[2]+callsFrequencies[3]+callsFrequencies[4]+callsFrequencies[5];
console.log("status");
console.log("======");
console.log("5 "+callsFrequencies[5]);
console.log("4 "+callsFrequencies[4]);
console.log("3 "+callsFrequencies[3]);
console.log("2 "+callsFrequencies[2]);
console.log("1 "+callsFrequencies[1]);
console.log("0 "+callsFrequencies[0]);
console.log("Total : "+sum+" calls");
});
app.get('/nextCommand', function(req,res) {
console.log("nextCommand");
ticks++;
res.writeHead(200);
// Pas d'appels et pas d'utilisateurs, on ne fait rien.
if (calls === [] && gos === []) {
doNothing(res);
}
// Les portes sont ouvertes, on les ferme sinon on ne peut pas se déplacer
if (doorsOpened === true) {
closeDoors(res);
} else {
// Les portes sont fermées, on peut éventuellement se déplacer
// Doit-on ouvrir les portes ?
if ((direction === "UP" && callsUp.indexOf(floor) != -1) ||
(direction === "DOWN" && callsDown.indexOf(floor) != -1)||
((floor === min || floor=== max) && calls.indexOf(floor)!=-1)){
console.log("Floor found in calls");
console.log("Calls length="+calls.length);
/* if (calls.length >0 && calls[calls.length-1] === floor) {*/
res.end("OPEN");
console.log("OPEN");
openDoors(floor);
updateMinMax(calls,gos);
return;
/* } */
} else {
console.log("Floor not found in calls");
}
if (gos.indexOf(floor)!=-1) {
console.log("Floor found in gos");
console.log("Gos length="+gos.length);
res.end("OPEN");
console.log("OPEN");
openDoors(floor);
updateMinMax(calls,gos);
return;
}
console.log("Floor: "+floor+" min "+min+" max "+max);
if (floor >= max && direction == "UP") {
direction = "DOWN";
}
if (floor <= min && direction == "DOWN") {
direction = "UP";
}
if (direction == "DOWN") {
floor--;
res.end("DOWN");
console.log("DOWN");
} else if (direction == "UP") {
floor++;
res.end("UP");
console.log("UP");
}
}
});
app.listen(80);