forked from LaunchCodeEducation/Mars-Rover-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover.js
67 lines (64 loc) · 2.32 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
63
64
65
66
67
class Rover {
// Write code here!
constructor(position)
{
this.mode = 'NORMAL';
this.position=position;
// if (!mode) {
// throw Error("Rover mode required.");
// }
this.generatorWatts = 110;
}
receiveMessage(message)
{
// return `message: ${message.name} + results: [${message.commands}]`;
// let results = message.commands;
// results.push({mode:this.mode, generatorWatts:this.generatorWatts, position:this.position})
let result = {'message':message.name};
// if (message.commands.commandType == 'STATUS_CHECK')
// {
// result["results"] = message.commands;
// }
// if (message.commands[1].commandType == 'MODE_CHANGE')
// {
// result["results"] = message.commands;
// }
if (message.commands != undefined )/*&& message.commands.length > 1)*/
{
result["results"] = [];
// result["results"] = [];
// result["results"].push({});
// result["results"].push({});
for(let i = 0; i < message.commands.length; i ++)
{
if((message.commands)[i].commandType == 'MOVE')
{
if(this.mode == 'LOW_POWER')
{
result["results"].push({"completed":false});
//console.log("pushing false...");
}
else
{
this.position = (message.commands)[i].value;
result["results"].push({"completed":true, "position":this.position});
//console.log("pushing true...");
}
}
else if ((message.commands)[i].commandType == 'STATUS_CHECK')
{
result["results"].push({"completed": true, "roverStatus": {"mode": this.mode, "generatorWatts": this.generatorWatts, "position": this.position}});
//result["results"].push({"completed":true});
}
else
{
//MODE_CHANGE
this.mode = 'LOW_POWER';
result["results"].push({"completed":true});
}
}
}
return result;
}
}
module.exports = Rover;