Browse Source

Worked on removing namespace socket from stations.

KrisVos130 8 years ago
parent
commit
f2fac0f1c8
4 changed files with 67 additions and 51 deletions
  1. 23 25
      backend/logic/coreHandler.js
  2. 3 0
      backend/logic/global.js
  3. 5 14
      backend/logic/socketHandler.js
  4. 36 12
      backend/logic/stations.js

+ 23 - 25
backend/logic/coreHandler.js

@@ -142,39 +142,37 @@ module.exports = {
 		}));
 	},
 
-	'/stations/join/:id': (id, user, cb) => {
+	'/station/:id/join': (stationId, socketId, cb) => {
 
-		const station = stations.getStation(id);
+		const station = stations.getStation(stationId);
 
 		if (station) {
 
-			user.stationId = id;
+			var response = station.handleUserJoin(socketId);
 
-			/*io.sockets.emit('station-joined', {
-				user: {
-					id: user.id,
-					username: user.username
-				}
-			});*/
-
-			return cb({
-				status: 'joined',
-				data: {
-					displayName: station.getDisplayName(),
-					users: station.getUsers(),
-					currentSong: station.getCurrentSong(),
-					timePaused: station.getTimePaused(),
-					paused: station.isPaused(),
-					currentTime: Date.now()
-				}
-			});
+			return cb(response);
+		}
+		else {
+			return cb({ status: 'error', message: 'Room with that ID does not exists' });
+		}
+	},
+
+	'/station/:id/skip': (stationId, socketId, cb) => {
+
+		const station = stations.getStation(stationId);
+
+		if (station) {
+
+			var response = station.handleUserJoin(socketId);
+
+			return cb(response);
 		}
 		else {
 			return cb({ status: 'error', message: 'Room with that ID does not exists' });
 		}
 	},
 
-	'/stations/search/:query': (query, cb) => {
+	/*'/stations/search/:query': (query, cb) => {
 
 		const params = [
 			'part=snippet',
@@ -197,9 +195,9 @@ module.exports = {
 				}
 			}
 		});
-	},
+	},*/
 
-	'/songs/:id/toggleLike': (songId, userId, cb) => {
+	'/song/:id/toggleLike': (songId, userId, cb) => {
 
 		var user = global.db.user.findOne(userId);
 		var song = global.db.song.findOne(songId);
@@ -227,7 +225,7 @@ module.exports = {
 
 	},
 
-	'/user/ratings': (userId, cb) => {
+	'/user/:id/ratings': (userId, cb) => {
 
 		var user = global.db.user.findOne(userId);
 		if (user !== undefined) {

+ 3 - 0
backend/logic/global.js

@@ -68,5 +68,8 @@ module.exports = {
 		}
 		return result.join("");
 	},
+	getSocketFromId: function(socketId) {
+		return this.io.sockets.sockets[socketId];
+	},
 	Timer
 };

+ 5 - 14
backend/logic/socketHandler.js

@@ -2,15 +2,6 @@
 
 module.exports = (core, io) => {
 
-	// tell all the users in this room that someone is joining it
-	core.on('station-joined', user => {
-		io.sockets.clients().forEach(socket => {
-			if (socket.request.user.id != user.user.id && socket.request.user.roomId === id) {
-				socket.emit('station-joined', user);
-			}
-		});
-	});
-
 	io.on('connection', socket => {
 		console.log("User has connected");
 		socket.on('disconnect', () => {
@@ -29,19 +20,19 @@ module.exports = (core, io) => {
 			});
 		});
 
-		socket.on('/stations/join/:id', (id, cb) => {
-			core['/stations/join/:id'](id, socket.request.user, result => {
+		socket.on('/station/:id/join', (id, cb) => {
+			core['/station/:id/join'](id, socket.id, result => {
 				cb(result);
 			});
 		});
 
-		socket.on('/stations/search/:query', (query, cb) => {
+		/*socket.on('/stations/search/:query', (query, cb) => {
 			core['/stations/search/:query'](query, result => {
 				cb(result);
 			});
-		});
+		});*/
 
 		// this lets the client socket know that they can start making request
-		socket.emit('ready', socket.request.user.logged_in);
+		//socket.emit('ready', socket.request.user.logged_in);
 	});
 };

+ 36 - 12
backend/logic/stations.js

@@ -29,7 +29,7 @@ module.exports = {
 			this.paused = data.paused;
 			this.locked = data.locked;
 			this.skipVotes = 0;
-			this.users = [];
+			this.sockets = [];
 			this.displayName = data.displayName;
 			this.description = data.description;
 			this.timePaused = 0;
@@ -138,23 +138,47 @@ module.exports = {
 			return this.description;
 		}
 
-		addUser(user) {
-			this.users.add(user);
-			this.nsp.emit("updateUsers", this.users);
+		getSockets() {
+			return this.sockets;
 		}
 
-		removeUser(user) {
-			this.users.splice(this.users.indexOf(user), 1);
-			this.nsp.emit("updateUsers", this.users);
+		getTimePaused() {
+			console.log(this.timePaused, "        ", this.timer.getTimePaused());
+			return this.timePaused + this.timer.getTimePaused();
 		}
 
-		getUsers() {
-			return this.users;
+		emitToStation(...data) {
+			this.sockets.forEach(function(socketId) {
+				let socket = global.getSocketFromId(socketId);
+				if (socket !== undefined) {
+					socket.emit.apply(data);
+				} else {
+					// Remove socket and emit it
+				}
+			});
 		}
 
-		getTimePaused() {
-			console.log(this.timePaused, "        ", this.timer.getTimePaused());
-			return this.timePaused + this.timer.getTimePaused();
+		handleUserJoin(socketId) {
+			const socket = global.getSocketFromId(socketId);
+			if (socket !== undefined) {
+				//TODO Check if banned from room & check if allowed to join room
+				if (this.sockets.indexOf(socketId) === -1) {
+					this.emitToStation("userJoin", "Person");
+					this.sockets.push(socketId);
+					return {
+						displayName: this.getDisplayName(),
+						users: this.getSockets().length,
+						currentSong: this.getCurrentSong(),
+						timePaused: this.getTimePaused(),
+						paused: this.isPaused(),
+						currentTime: Date.now()
+					};
+				} else {
+					return {err: "Already in that station."};
+				}
+			} else {
+				return {err: "Invalid socket id."};
+			}
 		}
 	},
 	addStation: station => {