forked from LaunchCodeEducation/Mars-Rover-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrover.js
More file actions
52 lines (49 loc) · 1.51 KB
/
Copy pathrover.js
File metadata and controls
52 lines (49 loc) · 1.51 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
const Command = require('./command.js');
const Message = require('./message.js');
class Rover {
constructor(position) {
this.position = position;
this.mode = 'NORMAL';
this.generatorWatts = 110;
}
receiveMessage(message) {
let response = [];
if(message.commands){
for(let command of message.commands){
if(command.commandType === "STATUS_CHECK"){
response.push({
completed: true,
roverStatus: {
mode: this.mode,
generatorWatts: this.generatorWatts,
position: this.position,
}
});
}
else if(command.commandType === "MODE_CHANGE") {
this.mode = command.value;
response.push({completed: true});
}
else if(command.commandType === "MOVE"){
if(this.mode === "NORMAL"){
this.position = command.value;
response.push({completed: true});
}
else if(this.mode === "LOW_POWER"){
response.push({completed: false});
}
}
}
}
return {
message: message.name,
results: response
};
}
}
let commands = [new Command('MODE_CHANGE', 'LOW_POWER'), new Command('STATUS_CHECK')];
let message = new Message('Test message with two commands', commands);
let rover = new Rover(98382);
let response = rover.receiveMessage(message);
console.log(response);
module.exports = Rover;