-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.js
53 lines (47 loc) · 1.43 KB
/
factory.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
var core = require('./core'),
ObjectId = require('mongoose').Types.ObjectId,
models = require('./models'),
protos = require('./protos'),
traits = require('./traits');
exports.createRoom = function(roomProps, fromModel) {
if (typeof fromModel === 'undefined') {
var fromModel = false;
}
if (!fromModel) {
//make sure exits are object IDs.
core.ensureObjectId(roomProps);
if ('exits' in roomProps) {
for (var exitName in roomProps.exits) {
if (roomProps.exits.hasOwnProperty(exitName)) {
var exit = roomProps.exits[exitName];
if (typeof exit !== 'undefined' && exit !== null) {
if (!(exit instanceof ObjectId)) {
roomProps.exits[exitName] = new ObjectId(exit);
}
}
}
}
}
}
if (!fromModel) {
return core.createObject(models.Room, protos.Room, traits.Container, roomProps);
}
else {
return core.createFromModel(roomProps, protos.Room, traits.Container);
}
};
exports.createMobile = function(mobProps) {
core.ensureObjectId(mobProps);
return core.createObject(protos.Mobile, mobProps);
}
exports.createPlayer = function(playerProps) {
core.ensureObjectId(playerProps);
return core.createObject(models.Player, protos.Mobile, traits.Player, playerProps);
};
exports.loadRoom = function(objectID, callback) {
models.Room.findById(objectID, function(err, roomData) {
var room = exports.createRoom(roomData, true);
if (err) return callback(err);
callback(null, room);
});
}