-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
260 lines (236 loc) · 8.97 KB
/
server.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var express = require('express'),
app = express(),
http = require('http').createServer(app),
io = require('socket.io').listen(http);
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = 'mongodb://localhost:27017/expenseTrack';
var mongoose = require('mongoose');
var db = mongoose.connection;
db.on('error', console.error);
db.once('open', function () {
//these are database schemas, they tell it what to expect
var userSchema = new mongoose.Schema({
username: String,
password: String,
name: String,
room: String,
userID: Number
});
var roomSchema = new mongoose.Schema({
roomName: String,
password: String,
users: Array,
userIDs: Array,
items: Array
});
var itemSchema = new mongoose.Schema({
itemID: Number,
amount: Number,
description: String,
user: String,
room: String,
timeStamp: Date
});
//these are models based on the schemas
var User = mongoose.model('User', userSchema);
var Room = mongoose.model('Room', roomSchema);
var Item = mongoose.model('Item', itemSchema);
console.log("initialized");
http.listen(8888);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/files/app.html');
});
app.use(express.static('files'));
io.on('connection', function (socket) {
//create a new user in the database
socket.on('newUser', function (data) {
var userCount;
User.count({}, function (err, count) {
userCount = count;
});
User.count({ username: data.username }, function (err, count) {
if (count == 0) {
console.log("creating new user");
console.log(data);
var tempUser = new User({
username: data.username,
password: data.password,
name: data.name,
room: null,
userID: userCount
});
tempUser.save(function (err, tempUser) {
if (err) return console.error(err);
});
console.log("emit true");
socket.emit("createUserResponse", {
'success': true,
'userID': userCount
});
}
else {
console.log("emit false");
socket.emit("createUserResponse", {
'success': false,
});
}
});
});
socket.on('newRoom', function (data) {
Room.count({ roomName: data.roomName }, function (err, count) {
if (count == 0) {
console.log("creating new room");
console.log(data);
var tempRoom = new Room({
roomName: data.roomName,
password: data.password,
items: data.items,
users: data.users,
userIDs: data.userIDs
});
tempRoom.save(function (err, tempRoom) {
if (err) return console.error(err);
});
User.findOne({ 'username': data.username }, function(err, tempUser){
if (tempUser == null) {
console.log("this shoiuld never happen");
}
else {
tempUser.room = data.roomName;
tempUser.save();
}
});
socket.emit("createRoomResponse", true);
}
else {
socket.emit("createRoomResponse", false);
}
});
});
socket.on('roomLogin', function (data) {
Room.findOne({ 'roomName': data.roomName }, function (err, tempRoom) {
if (tempRoom == null) {
socket.emit("roomLoginResponse", {
'result': false,
'error': "Invalid Room Name"
});
}
else if (tempRoom.password == data.password) {
console.log("login to room successful");
console.log(data);
console.log(tempRoom);
tempRoom.users.push(data.name);
tempRoom.userIDs.push(data.userID);
tempRoom.save();
User.findOne({ 'username': data.username }, function (err, doc) {
doc.room = tempRoom.roomName;
doc.save();
});
socket.emit("roomLoginResponse", {
'result': true,
'roomName': tempRoom.roomName,
'password': tempRoom.password,
'items': tempRoom.items,
'users': tempRoom.users
});
}
else {
console.log("room login failed");
socket.emit("roomLoginResponse", {
'result': false,
'error': "Invalid Password"
});
}
});
});
socket.on('getRoomData', function (data) {
Room.findOne({ "roomName": data.roomName }, function (err, tempRoom) {
if (tempRoom == null) {
console.log("this should never happen");
}
else {
console.log("returning room data");
socket.emit("returnRoomData", {
'roomName': tempRoom.roomName,
'items': tempRoom.items,
'users': tempRoom.users,
'userIDs':tempRoom.userIDs
});
}
});
});
//try to login
socket.on('loginAttempt', function (data) {
console.log(data);
User.findOne({ 'username': data.username }, function (err, tempUser) {
if (tempUser == null) {
console.log('null name');
socket.emit('loginResponse', {
'result': false,
'error': "Invalid Username"
});
}
else if (tempUser.password == data.password) {
console.log('password correct');
console.log(tempUser);
socket.emit('loginResponse', {
'result': true,
'username' : tempUser.username,
'firstname': tempUser.name,
'room': tempUser.room,
'userID': tempUser.userID,
'name': tempUser.name
});
}
else {
console.log('password wrong');
console.log(tempUser.password);
socket.emit('loginResponse', {
'result': false,
'error': "Incorrect Password"
});
}
});
});
socket.on('addItem', function (data) {
console.log("attemtp to add itme");
console.log(data);
Room.findOne({ 'roomName': data.roomName }, function (err, tempRoom) {
if (tempRoom == null) {
console.log("error in adding item");
}
else {
var tempItem = {
'type': data.type,
'creatorID': data.creatorID,
'cost': data.cost,
'date': data.date,
'description': data.description,
'recipientID': data.recipientID
}
if (tempRoom.items == null) {
tempRoom.items = [tempItem];
}
else {
tempRoom.items.push(tempItem);
}
tempRoom.save();
console.log("item saved successfully");
}
});
});
socket.on('delete', function (data) {
Room.findOne({ 'roomName': data.roomName }, function (err, tempRoom) {
if (tempRoom == null) {
console.log("error in deleteion");
}
else {
tempRoom.items.splice(data.index, 1);
tempRoom.save();
}
});
});
});
});
mongoose.connect(url);