Skip to content

Commit

Permalink
BREAKING CHANGE: remove getRuntimeData and getAllTerrainData from…
Browse files Browse the repository at this point in the history
… exports and refactor it to the `runtime/` folder
  • Loading branch information
artch committed Sep 18, 2017
1 parent 13e0a7c commit 2237933
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 225 deletions.
220 changes: 1 addition & 219 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ var bulk = require('./bulk'),
runtimeCache = {},
roomStatsUpdates = {},
zlib = require('zlib'),
accessibleRoomsCache = {
timestamp: 0
},
worldSize;

_.extend(config.engine, {
Expand Down Expand Up @@ -74,17 +71,6 @@ function checkNotificationOnline(userId) {
return q.when(true); // TODO
}

function getAccessibleRooms() {
if(Date.now() > accessibleRoomsCache.timestamp + 60*1000) {
accessibleRoomsCache.timestamp = Date.now();
return env.get(env.keys.ACCESSIBLE_ROOMS).then(data => {
accessibleRoomsCache.data = JSON.parse(data);
return accessibleRoomsCache.data;
});
}
return q.when(accessibleRoomsCache.data);

}

exports.connect = function(processType) {

Expand Down Expand Up @@ -164,201 +150,6 @@ exports.getUserData = function(userId) {
});
};

var cachedMarketOrders = {
gameTime: 0,
orders: {}
};

function getCachedMarketOrders(gameTime) {
if(gameTime == cachedMarketOrders.gameTime) {
return q.when(cachedMarketOrders.orders);
}
return db['market.orders'].find({active: true})
.then(orders => {
var result = {all: {}};
orders.forEach(i => {
i.id = ""+i._id;
delete i._id;
result[i.resourceType] = result[i.resourceType] || {};
result[i.resourceType][i.id] = i;
result.all[i.id] = i;
});
cachedMarketOrders.orders = result;
cachedMarketOrders.gameTime = gameTime;
return result;
});
}

exports.getRuntimeData = function(userId, onlyInRoom) {
var userObjects, runtimeData;

var objectsQuery, memoryKey;

if(onlyInRoom) {
objectsQuery = {$and: [{user: userId}, {room: onlyInRoom}]};
memoryKey = userId+','+onlyInRoom;
}
else {
objectsQuery = {user: userId};
memoryKey = userId;
}

var userIdsHash = {[userId]: true};

return db['rooms.objects'].find(objectsQuery)
.then((_userObjects) => {

if(!_userObjects.length) {
if(!onlyInRoom) {
db.users.update({_id: userId}, {$set: {active: 0}});
}
return q.reject(false);
}

userObjects = exports.mapById(_userObjects);

var roomIdsHash = {}, roomIds = [];
_userObjects.forEach((i) => {

if(i.type == 'flag' || i.type == 'constructionSite') {
return;
}
roomIdsHash[i.room] = true;
if(i.type == 'observer') {
roomIdsHash[i.observeRoom] = true;
}
if(i.type == 'controller' && i.sign) {
userIdsHash[i.sign.user] = true;
}
});
for(var i in roomIdsHash) {
roomIds.push(i);
}

return q.all([
db.users.findOne({_id: userId}),
db['users.code'].findOne({$and: [{user: userId}, {activeWorld: true}]}),
env.get(env.keys.MEMORY+memoryKey),
db['users.console'].find({user: userId}),
common.getGametime(),
db.rooms.find({_id: {$in: roomIds}}),
db['rooms.objects'].find({$and: [{room: {$in: roomIds}}, {user: {$ne: userId}}]}),
getAccessibleRooms(),
db.transactions.findEx({sender: userId}, {sort: {time:-1}, limit: 100}),
db.transactions.findEx({recipient: userId}, {sort: {time:-1}, limit: 100}),
db['rooms.flags'].find({user: userId})
]);
}).then((result) => {

var gameTime = result[4];

db['users.console'].removeWhere({_id: {$in: _.map(result[3], (i) => i._id)}});

var cpu, cpuBucket;
if(result[0].cpu) {
cpuBucket = result[0].cpuAvailable || 0;
if(cpuBucket < 0) {
cpuBucket = 0;
}
cpu = cpuBucket + result[0].cpu;
if(cpu > config.engine.cpuMaxPerTick) {
cpu = config.engine.cpuMaxPerTick;
}
}
else {
cpu = Infinity;
cpuBucket = Infinity;
}

var modules = result[1] && result[1].modules || {};
for(var key in modules) {
var newKey = key.replace(/\$DOT\$/g, '.');
newKey = newKey.replace(/\$SLASH\$/g, '/');
newKey = newKey.replace(/\$BACKSLASH\$/g, '\\');
if(newKey != key) {
modules[newKey] = modules[key];
delete modules[key];
}
}

var userIds = [];
result[6].forEach((i) => {
if(i.user) {
userIdsHash[i.user] = true;
}
if(i.type == 'controller' && i.reservation) {
userIdsHash[i.reservation.user] = true;
}
if(i.type == 'controller' && i.sign) {
userIdsHash[i.sign.user] = true;
}
});
result[8].forEach(i => i.recipient && (userIdsHash[i.recipient] = true));
result[9].forEach(i => i.sender && (userIdsHash[i.sender] = true));
Object.getOwnPropertyNames(userIdsHash).forEach(function(i) {
userIds.push(i);
});

runtimeData = {
userObjects,
user: result[0],
userCode: modules,
userCodeTimestamp: result[1] && result[1].timestamp || 0,
userMemory: {data: result[2] || "", userId},
consoleCommands: result[3],
time: gameTime,
rooms: exports.mapById(result[5]),
roomObjects: _.extend(exports.mapById(result[6]), userObjects),
flags: result[10],
accessibleRooms: result[7],
transactions: {
outgoing: result[8],
incoming: result[9]
},
cpu,
cpuBucket
};

return q.all([
db.users.find({_id: {$in: userIds}}),
getCachedMarketOrders(gameTime),
db['market.orders'].find({user: userId}),
result[0].activeSegments && result[0].activeSegments.length > 0 ?
env.hmget(env.keys.MEMORY_SEGMENTS+userId, result[0].activeSegments) :
q.when(),
result[0].activeForeignSegment && result[0].activeForeignSegment.user_id && result[0].activeForeignSegment.id ?
q.all([
env.hget(
env.keys.MEMORY_SEGMENTS+result[0].activeForeignSegment.user_id,
result[0].activeForeignSegment.id),
env.get(env.keys.PUBLIC_MEMORY_SEGMENTS+result[0].activeForeignSegment.user_id)
]) :
q.when()
]);

}).then((result) => {
runtimeData.users = exports.mapById(result[0]);
runtimeData.market = {
orders: result[1],
myOrders: result[2]
};
if(result[3]) {
runtimeData.memorySegments = {};
for(var i=0; i<runtimeData.user.activeSegments.length; i++) {
runtimeData.memorySegments[runtimeData.user.activeSegments[i]] = result[3][i] || "";
}
}
if(result[4] && result[4][1] && result[4][1].split(',').indexOf(""+runtimeData.user.activeForeignSegment.id) != -1) {
runtimeData.foreignMemorySegment = {
username: runtimeData.user.activeForeignSegment.username,
id: runtimeData.user.activeForeignSegment.id,
data: result[4][0]
};
}
return runtimeData;
})

};

exports.getAllUsers = function() {
return q.all([
Expand Down Expand Up @@ -396,7 +187,7 @@ exports.makeRuntime = function(userId, onlyInRoom) {
runtimeChild.kill('SIGKILL');
}

runtimeChild = child_process.fork(__dirname + '/runtime.js');
runtimeChild = child_process.fork(__dirname + '/runtime/runtime.js');

console.log(`New child runtime process ${runtimeChild.pid}`);

Expand Down Expand Up @@ -932,15 +723,6 @@ exports.commitDbBulk = () => {
return q.when();
};

exports.getAllTerrainData = () => {
return env.get(env.keys.TERRAIN_DATA)
.then(compressed => {
const buf = Buffer.from(compressed, 'base64');
return q.ninvoke(zlib, 'inflate', buf);
})
.then(data => JSON.parse(data));
};

exports.getWorldSize = () => {
return worldSize;
};
Expand Down
Loading

0 comments on commit 2237933

Please sign in to comment.