forked from seungkim11/537GreenCorp
/
utils.js
132 lines (103 loc) · 3.87 KB
/
utils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Created by samskim on 5/16/16.
*/
var enums = require('./enums');
exports.updateGlobalMap = function (map, tiles, rover){
tiles.forEach(function (tile) {
// validate that data has all attributes and is in correct format
if (validateTile(tile)) {
var key = tile.x + "/" + tile.y;
// ****** this is the science check logic ******
// if the tile exists in the map
if (map[key]) {
// can overwrite only when the this rover is a sensor
if (rover.sensor !== enums.NONE){
// if sender's tile contains science, overwrite it
if (tile.science !== enums.NONE){
map[key].science = tile.science;
// TODO: found : for debugging
map[key].f = rover.id;
// else if sender's tile doesn't contain science, it can be two cases:
// 1. the science is a type such that can't be detected by sender's sensor - dont overwrite it
// 2. the science has been collected and gone. - overwrite it
}else{
if (map[key].science === rover.sensor) {
map[key].science = tile.science;
// TODO: found : for debugging
map[key].f = rover.id;
}
}
}
// if tile doesn't exist in the global map, add it
} else {
map[key] = tile;
// TODO: found : for debugging
map[key].f = rover.id;
}
// ****** science check logic end
}
})
}
// convert map object to global array
exports.mapToGlobal = function(map, scienceParam) {
var key;
var results = [];
for (key in map) {
if (map.hasOwnProperty(key)) {
// if no parameter, just return the global map
if (!scienceParam) {
results.push(map[key]);
// if scienceParam requested
} else if (scienceParam) {
// if all
if (scienceParam === 'all') {
if (map[key].science !== enums.NONE) {
results.push(map[key]);
}
// excavate or drill
} else if (scienceParam === 'excavate') {
if ((map[key].terrain === enums.terrain.SAND || map[key].terrain === enums.terrain.SOIL)
&& map[key].science !== enums.NONE)
results.push(map[key]);
} else if (scienceParam === 'drill') {
if ((map[key].terrain === enums.terrain.GRAVEL || map[key].terrain === enums.terrain.ROCK)
&& map[key].science !== enums.NONE)
results.push(map[key]);
}
}
}
}
return results;
}
exports.getKey = function(x, y){
if (validateXY(x,y))
return x + '/' + y;
else
return false
}
exports.validateScience = function(science){
if (!science || science !== enums.NONE || science !== enums.science.CRYSTAL || science !== enums.science.ORGANIC
|| science !== enums.science.RADIOACTIVE || science !== enums.science.MINERAL) return false;
return true;
}
var validateXY = function(x, y){
if (!x || !y) return false;
if (!isInt(x) || !isInt(y)) return false;
return true;
}
// object validator
var validateTile = function (tile) {
if (!isInt(tile.x) || !isInt(tile.y)) return false;
// validate science
// validate terrain
return true;
}
// check if coordinate is an integer
var isInt = function (value) {
var x;
if (isNaN(value)) {
return false;
}
x = parseFloat(value);
return (x | 0) === x;
}