diff --git a/lib/bulk.js b/lib/bulk.js index 967c59f..9efa9a1 100644 --- a/lib/bulk.js +++ b/lib/bulk.js @@ -79,12 +79,32 @@ module.exports = function(collectionName) { id = id._id; } opsCnt++; - bulk.push({op: 'inc', id, key, amount}); + bulk.push({op: 'update', id, update: {$inc: {[key]: amount}}}); + }, + addToSet(id, key, value) { + if(!id) { + return; + } + if (_.isObject(id)) { + id = id._id; + } + opsCnt++; + bulk.push({op: 'update', id, update: {$addToSet: {[key]: value}}}); + }, + pull(id, key, value) { + if(!id) { + return; + } + if (_.isObject(id)) { + id = id._id; + } + opsCnt++; + bulk.push({op: 'update', id, update: {$pull: {[key]: value}}}); }, execute() { if(!opsCnt) return q.when({}); for(var id in updates) { - bulk.push({op: 'update', id, $set: updates[id]}); + bulk.push({op: 'update', id, update: {$set: updates[id]}}); } return common.storage.db[collectionName].bulk(bulk); } diff --git a/lib/index.js b/lib/index.js index 61a73c9..288bcfc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -688,8 +688,27 @@ exports.getRoomIntents = function(roomId) { }; exports.getRoomObjects = function(roomId) { + var result = {}; return db['rooms.objects'].find({room: roomId}) - .then((result) => exports.mapById(result)); + .then((objects) => { + var users = {}; + result.objects = exports.mapById(objects, obj => { + if(obj.user) { + users[obj.user] = true; + } + }); + users = Object.keys(users); + if(users.length) { + return db['users'].find({_id: {$in: users}}) + } + else { + return []; + } + }) + .then(users => { + result.users = exports.mapById(users); + return result; + }); }; exports.getRoomFlags = function(roomId) { @@ -742,9 +761,10 @@ exports.clearMarketIntents = function(roomId) { }; -exports.mapById = function(array) { +exports.mapById = function(array, fn) { return _.reduce(array, (result, i) => { result[i._id.toString()] = i; + fn && fn(i); return result; }, {}); }; @@ -925,6 +945,19 @@ exports.getWorldSize = () => { return worldSize; }; +exports.addRoomToUser = (roomId, user, bulk) => { + if(!user.rooms || user.rooms.indexOf(roomId) == -1) { + bulk.addToSet(user, 'rooms', roomId); + } +}; + +exports.removeRoomFromUser = (roomId, user, bulk) => { + if(user.rooms && user.rooms.indexOf(roomId) != -1) { + bulk.pull(user, 'rooms', roomId); + } +}; + + function EvalCodeError(message) { this.toString = () => message; }