forked from LaunchCodeEducation/Mars-Rover-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover.js
62 lines (53 loc) · 1.74 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Rover {
// Constructor: Initializes a new Rover instance
constructor(position) {
if (!position) {
throw new Error('Rover position required.');
}
this.position = position;
this.mode = 'NORMAL';
this.generatorWatts = 110;
}
// Method to handle incoming messages with commands
receiveMessage(message) {
let results = [];
// Iterate over each command in the message
for (let command of message.commands) {
// Handle MOVE command
if (command.commandType === 'MOVE') {
if (this.mode === 'LOW_POWER') { // Check if the Rover is in LOW_POWER mode
results.push({ completed: false });
} else {
this.position = command.value; // Update Rover's position
results.push({ completed: true });
}
}
// Handle STATUS_CHECK command
else if (command.commandType === 'STATUS_CHECK') { // Check if the command is STATUS_CHECK
results.push({
completed: true,
roverStatus: {
mode: this.mode,
generatorWatts: this.generatorWatts,
position: this.position,
},
});
}
// Handle MODE_CHANGE command
else if (command.commandType === 'MODE_CHANGE') {
this.mode = command.value; // Change Rover's mode
results.push({ completed: true }); // MODE_CHANGE successfully
}
// Handle any other unrecognized command
else {
results.push({ completed: false }); // MODE_CHANGE failed
}
}
// Return the message name and results of command execution
return {
message: message.name,
results: results,
};
}
}
module.exports = Rover;