Browse Source

Worked on station constructor, improved/fixed room/rooms functions.

KrisVos130 8 years ago
parent
commit
631ab1a8a4
4 changed files with 66 additions and 50 deletions
  1. 46 45
      logic/coreHandler.js
  2. 16 3
      logic/socketHandler.js
  3. 3 1
      logic/utils.js
  4. 1 1
      public/js/app.js

+ 46 - 45
logic/coreHandler.js

@@ -58,7 +58,10 @@ function Station(id) {
 	var paused = true;//TODO Get this from RethinkDB
 	var locked = false;//TODO Get this from RethinkDB
 	var skipVotes = [];
+	var users = [];
 	var timer;
+	var displayName;//TODO Get this from RethinkDB
+	var description;//TODO Get this from RethinkDB
 
 	this.skipSong = function() {
 		if (playlist.length > 0) {
@@ -127,6 +130,32 @@ function Station(id) {
 	this.isLocked = function() {
 		return locked;
 	};
+	this.updateDisplayName = function(newDisplayName) {
+		//TODO Update RethinkDB
+		displayName = newDisplayName;
+	};
+	this.updateDescription = function(newDescription) {
+		//TODO Update RethinkDB
+		description = newDescription;
+	};
+	this.getId = function() {
+		return id;
+	};
+	this.getDisplayName = function() {
+		return displayName;
+	};
+	this.getDescription = function() {
+		return description;
+	};
+	this.addUser = function(user) {
+		users.add(user);
+	};
+	this.removeUser = function(user) {
+		users.splice(users.indexOf(user), 1);
+	};
+	this.getUsers = function() {
+		return users;
+	};
 	this.skipSong();
 }
 
@@ -208,57 +237,29 @@ module.exports = {
 	},
 
 	rooms: function (cb) {
-		r.table('rooms').run(rc, (err, cursor) => {
-			if (err) {
-				return cb({ status: 'failure', message: 'Error while fetching the rooms' });
-			}
-			else {
-				cursor.toArray((err, result) => {
-					if (err) {
-						return cb({ status: 'failure', message: 'Error while fetching the rooms' });
-					}
-					else {
-						return cb(result);
-					}
-				});
-			}
-		});
+		cb(stations);
 	},
 
-	room: function (id, cb) {
-
-		if (socket.custom.user == null) {
-			return cb({ status: 'error', message: "You can't join a room until you've logged in" });
-		}
+	handleRoomJoin: function (id, cb) {
 
-		r.table('rooms').get(id).run(rc, (err, result) => {
-			if (err) {
-				return cb({ status: 'error', message: 'Room with that id does not exist' });
-			}
-			else {
-				socket.custom.roomId = id;
+		var room = getStation(id);
+		socket.custom.roomId = id;
 
-				var userInfo = {
-					username: socket.custom.user.username
-				};
+		var userInfo = {
+			username: socket.custom.user.username
+		};
 
-				var otherUsersInfo = [];
-
-				// tell all the users in this room that someone is joining it
-				io.sockets.clients().forEach((otherSocket) => {
-					if (otherSocket != socket && otherSocket.custom.roomId == id) {
-						otherUsersInfo.push({ username: otherSocket.custom.user.username });
-						otherSocket.emit('room', { status: 'joining', user: userInfo });
-					}
-				});
+		// tell all the users in this room that someone is joining it
+		io.sockets.clients().forEach(function (otherSocket) {
+			if (otherSocket != socket && otherSocket.custom.roomId === id) {
+				otherSocket.emit('roomUserJoin', { user: userInfo });
+			}
+		});
 
-				return cb({
-					status: 'joined',
-					data: {
-						room: result,
-						users: otherUsersInfo
-					}
-				});
+		return cb({
+			status: 'joined',
+			data: {
+				room: room
 			}
 		});
 	},

+ 16 - 3
logic/socketHandler.js

@@ -22,15 +22,28 @@ module.exports = function (base, io) {
 			});
 		});
 
-		socket.on('rooms', function () {
+		socket.on('getRooms', function () {
 			base.rooms(function (result) {
+				var rooms = result.map(function(result) {
+					return {
+						id: result.getId(),
+						displayName: result.getDisplayName(),
+						description: result.getDescription(),
+						users: result.getUsers()
+					}
+				});
 				socket.emit('rooms', result);
 			});
 		});
 
-		socket.on('room', function (id) {
+		socket.on('room', function (id, cb) {
 			base.room(id, function (result) {
-				socket.emit('room', result);
+				var info = {
+					displayName: result.getDisplayName(),
+					users: result.getUsers(),
+					currentSong: result.getCurrentSong()
+				};
+				cb(info);
 			});
 		});
 

+ 3 - 1
logic/utils.js

@@ -1,3 +1,5 @@
 module.exports = {
-
+    htmlEntities: function(str) {
+        return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
+    }
 };

+ 1 - 1
public/js/app.js

@@ -87,7 +87,7 @@ window.onload = function () {
 			data.home.visible = false;
 			window.setTimeout(function () { data.room.visible = true; }, 500);
 			data.room.id = room.id;
-			data.room.name = room.name;
+			data.room.displayName = room.displayName;
 			data.room.description = room.description;
 		},
 		modalVisibilityChange: function (name) {