@screeps/common 2.15.3 → 2.15.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt CHANGED
@@ -1,10 +1,10 @@
1
- Copyright (c) 2016, Artem Chivchalov <contact@screeps.com>
2
-
3
- Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
4
- provided that the above copyright notice and this permission notice appear in all copies.
5
-
6
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
7
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
8
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
9
- AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
1
+ Copyright (c) 2016, Artem Chivchalov <contact@screeps.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
4
+ provided that the above copyright notice and this permission notice appear in all copies.
5
+
6
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
7
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
8
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
9
+ AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
10
10
  OF THIS SOFTWARE.
package/index.js CHANGED
@@ -1,193 +1,193 @@
1
- const q = require('q');
2
- const net = require('net');
3
- const path = require('path');
4
- const _ = require('lodash');
5
-
6
- exports.configManager = require('./lib/config-manager');
7
- exports.storage = require('./lib/storage');
8
- exports.rpc = require('./lib/rpc');
9
-
10
- exports.findPort = function findPort(port) {
11
- var defer = q.defer();
12
- var server = net.createServer(socket => socket.end());
13
- server.listen(port, function (err) {
14
- server.once('close', function () {
15
- defer.resolve(port);
16
- });
17
- server.close();
18
- });
19
- server.on('error', function (err) {
20
- defer.resolve(findPort(port+1));
21
- });
22
- return defer.promise;
23
- };
24
-
25
- exports.encodeTerrain = function(terrain) {
26
- var result = '';
27
- for(var y=0; y<50; y++) {
28
- for(var x=0; x<50; x++) {
29
- var objects = _.filter(terrain, {x,y}),
30
- code = 0;
31
- if(_.any(objects, {type: 'wall'})) {
32
- code = code | 1;
33
- }
34
- if(_.any(objects, {type: 'swamp'})) {
35
- code = code | 2;
36
- }
37
- result = result + code;
38
- }
39
- }
40
- return result;
41
- };
42
-
43
- exports.decodeTerrain = function(str, room) {
44
- var result = [];
45
-
46
- for(var y=0; y<50; y++) {
47
- for(var x=0; x<50; x++) {
48
- var code = str.charAt(y*50+x);
49
- if(code & 1) {
50
- result.push({room, x, y, type: 'wall'});
51
- }
52
- if(code & 2) {
53
- result.push({room, x, y, type: 'swamp'});
54
- }
55
- }
56
- }
57
-
58
- return result;
59
- };
60
-
61
- exports.checkTerrain = function(terrain, x, y, mask) {
62
- return (parseInt(terrain.charAt(y*50 + x)) & mask) > 0;
63
- };
64
-
65
- exports.getGametime = function() {
66
- return exports.storage.env.get(exports.storage.env.keys.GAMETIME).then(data => parseInt(data));
67
- };
68
-
69
- exports.getDiff = function(oldData, newData) {
70
-
71
- function getIndex(data) {
72
- var index = {};
73
- _.forEach(data, (obj) => index[obj._id] = obj);
74
- return index;
75
- }
76
-
77
-
78
- var result = {},
79
- oldIndex = getIndex(oldData),
80
- newIndex = getIndex(newData);
81
-
82
- _.forEach(oldData, (obj) => {
83
- if(newIndex[obj._id]) {
84
- var newObj = newIndex[obj._id];
85
- var objDiff = result[obj._id] = {};
86
- for(var key in obj) {
87
- if(_.isUndefined(newObj[key])) {
88
- objDiff[key] = null;
89
- }
90
- else if((typeof obj[key]) != (typeof newObj[key]) || obj[key] && !newObj[key]) {
91
- objDiff[key] = newObj[key];
92
- }
93
- else if(_.isObject(obj[key])) {
94
-
95
- objDiff[key] = {};
96
-
97
- for (var subkey in obj[key]) {
98
- if (!_.isEqual(obj[key][subkey], newObj[key][subkey])) {
99
- objDiff[key][subkey] = newObj[key][subkey];
100
- }
101
- }
102
- for (var subkey in newObj[key]) {
103
- if (_.isUndefined(obj[key][subkey])) {
104
- objDiff[key][subkey] = newObj[key][subkey];
105
- }
106
- }
107
- if (!_.size(objDiff[key])) {
108
- delete result[obj._id][key];
109
- }
110
- }
111
- else if(!_.isEqual(obj[key], newObj[key])) {
112
- objDiff[key] = newObj[key];
113
- }
114
- }
115
- for(var key in newObj) {
116
- if(_.isUndefined(obj[key])) {
117
- objDiff[key] = newObj[key];
118
- }
119
- }
120
- if(!_.size(objDiff)) {
121
- delete result[obj._id];
122
- }
123
- }
124
- else {
125
- result[obj._id] = null;
126
- }
127
- });
128
-
129
- _.forEach(newData, (obj) => {
130
- if(!oldIndex[obj._id]) {
131
- result[obj._id] = obj;
132
- }
133
- });
134
-
135
- return result;
136
- };
137
-
138
- exports.qSequence = function(collection, fn) {
139
- return _.reduce(collection, (promise, element, key) => promise.then(() => fn(element,key)), q.when());
140
- };
141
-
142
- exports.roomNameToXY = function(name) {
143
-
144
- name = name.toUpperCase();
145
-
146
- var match = name.match(/^(\w)(\d+)(\w)(\d+)$/);
147
- if(!match) {
148
- return [undefined, undefined];
149
- }
150
- var [,hor,x,ver,y] = match;
151
-
152
- if(hor == 'W') {
153
- x = -x-1;
154
- }
155
- else {
156
- x = +x;
157
- }
158
- if(ver == 'N') {
159
- y = -y-1;
160
- }
161
- else {
162
- y = +y;
163
- }
164
- return [x,y];
165
- };
166
-
167
- exports.getRoomNameFromXY = function(x,y) {
168
- if(x < 0) {
169
- x = 'W'+(-x-1);
170
- }
171
- else {
172
- x = 'E'+(x);
173
- }
174
- if(y < 0) {
175
- y = 'N'+(-y-1);
176
- }
177
- else {
178
- y = 'S'+(y);
179
- }
180
- return ""+x+y;
181
- }
182
-
183
- exports.calcWorldSize = function(rooms) {
184
- var minX = Infinity, minY = Infinity, maxX = 0, maxY = 0;
185
- rooms.forEach(room => {
186
- var [x,y] = exports.roomNameToXY(room._id);
187
- if(x < minX) minX = x;
188
- if(y < minY) minY = y;
189
- if(x > maxX) maxX = x;
190
- if(y > maxY) maxY = y;
191
- });
192
- return Math.max(maxX - minX + 1, maxY - minY + 1);
1
+ const q = require('q');
2
+ const net = require('net');
3
+ const path = require('path');
4
+ const _ = require('lodash');
5
+
6
+ exports.configManager = require('./lib/config-manager');
7
+ exports.storage = require('./lib/storage');
8
+ exports.rpc = require('./lib/rpc');
9
+
10
+ exports.findPort = function findPort(port) {
11
+ var defer = q.defer();
12
+ var server = net.createServer(socket => socket.end());
13
+ server.listen(port, function (err) {
14
+ server.once('close', function () {
15
+ defer.resolve(port);
16
+ });
17
+ server.close();
18
+ });
19
+ server.on('error', function (err) {
20
+ defer.resolve(findPort(port+1));
21
+ });
22
+ return defer.promise;
23
+ };
24
+
25
+ exports.encodeTerrain = function(terrain) {
26
+ var result = '';
27
+ for(var y=0; y<50; y++) {
28
+ for(var x=0; x<50; x++) {
29
+ var objects = _.filter(terrain, {x,y}),
30
+ code = 0;
31
+ if(_.any(objects, {type: 'wall'})) {
32
+ code = code | 1;
33
+ }
34
+ if(_.any(objects, {type: 'swamp'})) {
35
+ code = code | 2;
36
+ }
37
+ result = result + code;
38
+ }
39
+ }
40
+ return result;
41
+ };
42
+
43
+ exports.decodeTerrain = function(str, room) {
44
+ var result = [];
45
+
46
+ for(var y=0; y<50; y++) {
47
+ for(var x=0; x<50; x++) {
48
+ var code = str.charAt(y*50+x);
49
+ if(code & 1) {
50
+ result.push({room, x, y, type: 'wall'});
51
+ }
52
+ if(code & 2) {
53
+ result.push({room, x, y, type: 'swamp'});
54
+ }
55
+ }
56
+ }
57
+
58
+ return result;
59
+ };
60
+
61
+ exports.checkTerrain = function(terrain, x, y, mask) {
62
+ return (parseInt(terrain.charAt(y*50 + x)) & mask) > 0;
63
+ };
64
+
65
+ exports.getGametime = function() {
66
+ return exports.storage.env.get(exports.storage.env.keys.GAMETIME).then(data => parseInt(data));
67
+ };
68
+
69
+ exports.getDiff = function(oldData, newData) {
70
+
71
+ function getIndex(data) {
72
+ var index = {};
73
+ _.forEach(data, (obj) => index[obj._id] = obj);
74
+ return index;
75
+ }
76
+
77
+
78
+ var result = {},
79
+ oldIndex = getIndex(oldData),
80
+ newIndex = getIndex(newData);
81
+
82
+ _.forEach(oldData, (obj) => {
83
+ if(newIndex[obj._id]) {
84
+ var newObj = newIndex[obj._id];
85
+ var objDiff = result[obj._id] = {};
86
+ for(var key in obj) {
87
+ if(_.isUndefined(newObj[key])) {
88
+ objDiff[key] = null;
89
+ }
90
+ else if((typeof obj[key]) != (typeof newObj[key]) || obj[key] && !newObj[key]) {
91
+ objDiff[key] = newObj[key];
92
+ }
93
+ else if(_.isObject(obj[key])) {
94
+
95
+ objDiff[key] = {};
96
+
97
+ for (var subkey in obj[key]) {
98
+ if (!_.isEqual(obj[key][subkey], newObj[key][subkey])) {
99
+ objDiff[key][subkey] = newObj[key][subkey];
100
+ }
101
+ }
102
+ for (var subkey in newObj[key]) {
103
+ if (_.isUndefined(obj[key][subkey])) {
104
+ objDiff[key][subkey] = newObj[key][subkey];
105
+ }
106
+ }
107
+ if (!_.size(objDiff[key])) {
108
+ delete result[obj._id][key];
109
+ }
110
+ }
111
+ else if(!_.isEqual(obj[key], newObj[key])) {
112
+ objDiff[key] = newObj[key];
113
+ }
114
+ }
115
+ for(var key in newObj) {
116
+ if(_.isUndefined(obj[key])) {
117
+ objDiff[key] = newObj[key];
118
+ }
119
+ }
120
+ if(!_.size(objDiff)) {
121
+ delete result[obj._id];
122
+ }
123
+ }
124
+ else {
125
+ result[obj._id] = null;
126
+ }
127
+ });
128
+
129
+ _.forEach(newData, (obj) => {
130
+ if(!oldIndex[obj._id]) {
131
+ result[obj._id] = obj;
132
+ }
133
+ });
134
+
135
+ return result;
136
+ };
137
+
138
+ exports.qSequence = function(collection, fn) {
139
+ return _.reduce(collection, (promise, element, key) => promise.then(() => fn(element,key)), q.when());
140
+ };
141
+
142
+ exports.roomNameToXY = function(name) {
143
+
144
+ name = name.toUpperCase();
145
+
146
+ var match = name.match(/^(\w)(\d+)(\w)(\d+)$/);
147
+ if(!match) {
148
+ return [undefined, undefined];
149
+ }
150
+ var [,hor,x,ver,y] = match;
151
+
152
+ if(hor == 'W') {
153
+ x = -x-1;
154
+ }
155
+ else {
156
+ x = +x;
157
+ }
158
+ if(ver == 'N') {
159
+ y = -y-1;
160
+ }
161
+ else {
162
+ y = +y;
163
+ }
164
+ return [x,y];
165
+ };
166
+
167
+ exports.getRoomNameFromXY = function(x,y) {
168
+ if(x < 0) {
169
+ x = 'W'+(-x-1);
170
+ }
171
+ else {
172
+ x = 'E'+(x);
173
+ }
174
+ if(y < 0) {
175
+ y = 'N'+(-y-1);
176
+ }
177
+ else {
178
+ y = 'S'+(y);
179
+ }
180
+ return ""+x+y;
181
+ }
182
+
183
+ exports.calcWorldSize = function(rooms) {
184
+ var minX = Infinity, minY = Infinity, maxX = 0, maxY = 0;
185
+ rooms.forEach(room => {
186
+ var [x,y] = exports.roomNameToXY(room._id);
187
+ if(x < minX) minX = x;
188
+ if(y < minY) minY = y;
189
+ if(x > maxX) maxX = x;
190
+ if(y > maxY) maxY = y;
191
+ });
192
+ return Math.max(maxX - minX + 1, maxY - minY + 1);
193
193
  };
@@ -1,57 +1,57 @@
1
- const _ = require('lodash');
2
- const fs = require('fs');
3
- const path = require('path');
4
- const config = {common: {
5
- constants: require('./constants'),
6
- strongholds: require('./strongholds'),
7
- system: require('./system'),
8
- bots: {}
9
- }};
10
-
11
- exports.load = function() {
12
- if(!process.env.MODFILE) {
13
- throw new Error('MODFILE environment variable is not set!');
14
- }
15
- var modsJsonFilename = path.resolve(process.cwd(), process.env.MODFILE);
16
- try {
17
- fs.statSync(modsJsonFilename);
18
- }
19
- catch(e) {
20
- console.log(`File "${modsJsonFilename}" not found`);
21
- return;
22
- }
23
-
24
- try {
25
- var modsJson = require(modsJsonFilename);
26
- console.log(`Loading mods from "${modsJsonFilename}"`);
27
- if(!modsJson.mods) {
28
- return;
29
- }
30
-
31
- modsJson.mods.forEach(file => {
32
- file = path.resolve(path.dirname(modsJsonFilename), file);
33
- try {
34
- var mod = require(file);
35
- if(!_.isFunction(mod)) {
36
- console.error(`Cannot load "${file}": module.exports is not a function!`);
37
- }
38
- else {
39
- mod(config);
40
- console.log(' - '+file);
41
- }
42
- }
43
- catch(e) {
44
- console.error(`Error loading "${file}": ${e.stack || e}`);
45
- }
46
- });
47
-
48
- if(modsJson.bots) {
49
- config.common.bots = _.mapValues(modsJson.bots, i => path.resolve(path.dirname(modsJsonFilename), i));
50
- }
51
- }
52
- catch(e) {
53
- console.error(`Cannot open "${modsJsonFilename}": ${e}`);
54
- }
55
- };
56
-
57
- exports.config = config;
1
+ const _ = require('lodash');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const config = {common: {
5
+ constants: require('./constants'),
6
+ strongholds: require('./strongholds'),
7
+ system: require('./system'),
8
+ bots: {}
9
+ }};
10
+
11
+ exports.load = function() {
12
+ if(!process.env.MODFILE) {
13
+ throw new Error('MODFILE environment variable is not set!');
14
+ }
15
+ var modsJsonFilename = path.resolve(process.cwd(), process.env.MODFILE);
16
+ try {
17
+ fs.statSync(modsJsonFilename);
18
+ }
19
+ catch(e) {
20
+ console.log(`File "${modsJsonFilename}" not found`);
21
+ return;
22
+ }
23
+
24
+ try {
25
+ var modsJson = require(modsJsonFilename);
26
+ console.log(`Loading mods from "${modsJsonFilename}"`);
27
+ if(!modsJson.mods) {
28
+ return;
29
+ }
30
+
31
+ modsJson.mods.forEach(file => {
32
+ file = path.resolve(path.dirname(modsJsonFilename), file);
33
+ try {
34
+ var mod = require(file);
35
+ if(!_.isFunction(mod)) {
36
+ console.error(`Cannot load "${file}": module.exports is not a function!`);
37
+ }
38
+ else {
39
+ mod(config);
40
+ console.log(' - '+file);
41
+ }
42
+ }
43
+ catch(e) {
44
+ console.error(`Error loading "${file}": ${e.stack || e}`);
45
+ }
46
+ });
47
+
48
+ if(modsJson.bots) {
49
+ config.common.bots = _.mapValues(modsJson.bots, i => path.resolve(path.dirname(modsJsonFilename), i));
50
+ }
51
+ }
52
+ catch(e) {
53
+ console.error(`Cannot open "${modsJsonFilename}": ${e}`);
54
+ }
55
+ };
56
+
57
+ exports.config = config;