-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.js
147 lines (133 loc) · 4.15 KB
/
room.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var sys = require('sys');
var events = require('events');
var _ = require('underscore')._;
var mongodb = require("mongodb"),
Db = mongodb.Db,
Server = mongodb.Server;
// Initialize the DB
var DB_NAME = 'blotjot-node';
var SHAPES_COLL_NAME = 'shapes';
var ROOMS_COLL_NAME = 'rooms';
var dbclient = new Db(DB_NAME, new Server("127.0.0.1", 27017, {}));
dbclient.open(function() {});
// A room
Room = function(room_id, type) {
this.room_id = room_id;
this.type = type;
this.clients = {};
events.EventEmitter.call(this);
this.on('clientsChanged', function(num) {
console.log('changed some clients: ' + num);
});
}
sys.inherits(Room, events.EventEmitter);
Room.prototype.addNewShapes = function(client, shapes) {
this.emit("newShapes", client, shapes);
this.storeShapes(shapes);
}
Room.prototype.storeShapes = function(shapes) {
doc = {
'room_id':this.room_id,
'shapes': shapes
};
dbclient.collection(SHAPES_COLL_NAME, function(err, collection) {
collection.insert(doc, function(err, docs) {
if (err) {
console.log("DB insert error: " + err + " for doc " + sys.inspect(doc));
return;
}
});
});
}
Room.prototype.getAllShapes = function(callback) {
var self = this;
dbclient.collection(SHAPES_COLL_NAME, function(err, collection) {
if (err) {
console.log('Error getting collection');
return;
}
collection.find({'room_id':self.room_id},{'sort':'_id'},function(err,cursor) {
if (err) {
console.log('Error with find');
return;
}
cursor.toArray(function(err, docs) {
if (err) {
console.log('Error with toArray');
return;
}
callback(docs);
});
});
});
};
Room.prototype.numClients = function() {
return _.keys(this.clients).length;
};
Room.prototype.addClient = function(client) {
var id = client.sessionId;
if (!(id in this.clients)) {
this.clients[id] = {};
this.emit("clientsChanged", this.numClients());
//console.log(sys.inspect(client));
}
var self = this;
client.on('disconnect', function() {
delete self.clients[id];
self.emit("clientsChanged", self.numClients());
});
}
exports.Room = Room;
/*
A manager for currently used room objects. A room may exist in the DB but not
in the cached list here.
*/
var rooms = {};
var getRoom = function(room_id, callback) {
if (!(room_id in rooms)) {
dbclient.collection(ROOMS_COLL_NAME, function(err,collection) {
collection.findOne({'room_id':room_id},function(err,doc) {
if (err || !doc) {
callback(false);
} else {
if (!(room_id in rooms)) {
rooms[room_id] = new Room(doc['room_id'],doc['type']);
callback(rooms[room_id]);
console.log('Made a new room : ' + room_id);
}
}
});
});
} else {
callback(rooms[room_id]);
}
};
/*
Creates a room and returns true if sucessful or false if
there was an error or the room already exists.
*/
var createRoom = function(room_id,type, callback) {
dbclient.collection(ROOMS_COLL_NAME, function(err,collection) {
collection.count({'room_id':room_id},function(err,count) {
if (err || count > 0) {
callback(null);
return;
} else {
var doc = {
'room_id':room_id,
'type':type
};
collection.insert(doc, function(err, docs) {
if (err) {
console.log("DB insert error: " + err + " for doc " + sys.inspect(doc));
callback(false);
return;
}
callback(true);
});
}
});
});
}
exports.getRoom = getRoom;
exports.createRoom = createRoom;