forked from LaunchCodeEducation/Mars-Rover-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover.js
48 lines (41 loc) · 1.42 KB
/
rover.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
class Rover {
constructor(position) {
this.position = position;
this.mode = 'NORMAL';
this.generatorWatts = 110;
}
receiveMessage(message) {
let response = {
message: message.name,
results: []
};
for (let i = 0; i < message.commands.length; i++) {
if (message.commands[i].commandType === 'STATUS_CHECK') {
response.results.push({
completed: true,
roverStatus: {
mode: this.mode,
generatorWatts: this.generatorWatts,
position: this.position
}
});
} else if (message.commands[i].commandType === 'MOVE') {
if (this.mode === 'NORMAL') {
this.position = message.commands[i].value;
response.results.push({ completed: true });
} else {
response.results.push({ completed: false });
}
} else if (message.commands[i].commandType === 'MODE_CHANGE') {
if (message.commands[i].value === 'LOW_POWER' || message.commands[i].value === 'NORMAL') {
this.mode = message.commands[i].value;
response.results.push({ completed: true });
} else {
response.results.push({ completed: false });
}
}
}
return response;
}
}
module.exports = Rover;