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
69 lines (58 loc) · 2 KB
/
Copy pathrover.js
File metadata and controls
69 lines (58 loc) · 2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Rover {
constructor(position) {
this.position = position;
this.mode = 'NORMAL';
this.generatorWatts = 110;
}
receiveMessage(message) {
let response = {
message: message.name,
results: []
}
// Check if message.commands is defined and ensure it is an array
let commands = [];
if (message.commands) {
commands = Array.isArray(message.commands) ? message.commands : [message.commands];
}
if(commands.length > 0) {
for(let i = 0; i < commands.length; i++) {
if(commands[i].commandType === 'STATUS_CHECK') {
let statusCheckReturn = {
completed: true,
roverStatus: {
mode: this.mode,
generatorWatts: this.generatorWatts,
position: this.position,
}
}
response.results.push(statusCheckReturn);
} else if(commands[i].commandType === 'MODE_CHANGE'){
if(commands[i].value === 'LOW_POWER') {
this.mode = 'LOW_POWER';
} else if (commands[i].value === 'NORMAL') {
this.mode = 'NORMAL';
}
let modeChangeReturn = {
completed: true,
}
response.results.push(modeChangeReturn);
} else if(commands[i].commandType === 'MOVE') {
let moveReturn = {
completed: false,
};
if(this.mode === 'LOW_POWER') {
moveReturn.completed = false;
} else if (this.mode === 'NORMAL') {
this.position = commands[i].value;
moveReturn.completed = true;
}
response.results.push(moveReturn);
} else {
response.results.push(commands[i]);
}
}
}
return response;
}
}
module.exports = Rover;