浏览代码

Moved Timer to utils module. Move station logic to it's own module

Cameron Kline 8 年之前
父节点
当前提交
9420f3efb2
共有 3 个文件被更改,包括 213 次插入178 次删除
  1. 0 175
      logic/coreHandler.js
  2. 178 0
      logic/stations.js
  3. 35 3
      logic/utils.js

+ 0 - 175
logic/coreHandler.js

@@ -16,181 +16,6 @@ const utils = require('./utils');
 
 var dbConnection = null;
 
-
-// station stuff
-var stations = [];
-
-//TODO Find better name for this (createStation will be to add a station to the db)
-function makeStation(id) {
-	if (!getStation(id)) {
-		var station = new Station(id);
-		stations.push(station);
-		return station;
-	} else {
-		return false;
-	}
-}
-
-function getStation(id) {
-	stations.forEach(function(station) {
-		this.station = station;
-	});
-	return this.station;
-}
-
-function Station(id) {
-	//TODO Add startedAt and timePaused
-	var self = this;
-	var playlist = [
-		{
-			mid: "3498fd83",
-			duration: 20000,
-			title: "Test1"
-		},
-		{
-			mid: "3498fd83434",
-			duration: 10000,
-			title: "Test2"
-		}
-	];//TODO Get this from RethinkDB
-	var currentSong = playlist[0];
-	var currentSongIndex = 0;//TODO Get this from RethinkDB
-	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) {
-			if (timer !== undefined) {
-				timer.pause();
-			}
-			if (currentSongIndex+1 < playlist.length) {
-				currentSongIndex++;
-			} else {
-				currentSongIndex = 0;
-			}
-			skipVotes = 0;
-			currentSong = playlist[currentSongIndex];
-			timer = new Timer(function() {
-				console.log("Skip!");
-				self.skipSong();
-			}, currentSong.duration, paused);
-
-			//io.emit("skipSong " + id, currentSong);
-		}
-	};
-	this.toggleVoteSkip = function(userId) {
-		if (skipVotes.indexOf(userId) === -1) {
-			skipVotes.push(userId);
-		} else {
-			skipVotes = skipVotes.splice(skipVotes.indexOf(userId), 1);
-		}
-		//TODO Calculate if enough people voted to skip
-		//TODO Emit
-	};
-	this.retrievePlaylist = function() {
-		//TODO Use Rethink to get the Playlist for this room
-	};
-	this.pause = function() {
-		if (!paused) {
-			paused = true;
-			timer.pause();
-		}
-		//TODO Emit
-	};
-	this.unpause = function() {
-		if (paused) {
-			paused = false;
-			timer.resume();
-		}
-		//TODO Emit
-	};
-	this.isPaused = function() {
-		return paused;
-	};
-	this.getCurrentSong = function() {
-		return currentSong;
-	};
-	this.lock = function() {
-		if (!locked) {
-			locked = true;
-		}
-		//TODO Emit
-	};
-	this.unlock = function() {
-		if (locked) {
-			locked = false;
-		}
-		//TODO Emit
-	};
-	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();
-}
-
-function Timer(callback, delay, paused) {
-	var timerId, start, remaining = delay;
-	var timeWhenPaused = 0;
-	var timePaused = Date.now();
-
-	this.pause = function () {
-		clearTimeout(timerId);
-		remaining -= Date.now() - start;
-		timePaused = Date.now();
-	};
-
-	this.resume = function () {
-		start = Date.now();
-		clearTimeout(timerId);
-		timerId = setTimeout(callback, remaining);
-		timeWhenPaused += Date.now() - timePaused;
-	};
-
-	this.resetTimeWhenPaused = function() {
-		timeWhenPaused = 0;
-	};
-
-	this.timeWhenPaused = function () {
-		return timeWhenPaused;
-	};
-
-	if (paused === false) {
-		this.resume();
-	}
-}
-
-
 module.exports = {
 
 	setup: function (dbConn) {

+ 178 - 0
logic/stations.js

@@ -0,0 +1,178 @@
+
+// custom modules
+const utils = require('./utils');
+
+function Station (id, data, dbConn) {
+
+	var self = this;
+
+	//TODO Add startedAt and timePaused
+	var playlist = data.playlist;
+	var currentSong = playlist[0];
+	var currentSongIndex = data.currentSongIndex;
+	var paused = data.paused;
+	var locked = data.locked;
+	var skipVotes = data.skipVotes;
+	var users = data.users;
+	var displayName = data.displayName;
+	var description = data.description;
+	var timer;
+	var dbConnection = dbConn;
+
+	this.skipSong = function() {
+		if (playlist.length > 0) {
+			if (timer !== undefined) {
+				timer.pause();
+			}
+			if (currentSongIndex+1 < playlist.length) {
+				currentSongIndex++;
+			} else {
+				currentSongIndex = 0;
+			}
+			skipVotes = 0;
+			currentSong = playlist[currentSongIndex];
+			timer = new utils.Timer(function() {
+				console.log("Skip!");
+				self.skipSong();
+			}, currentSong.duration, paused);
+
+			//io.emit("skipSong " + id, currentSong);
+		}
+	};
+	this.toggleVoteSkip = function(userId) {
+		if (skipVotes.indexOf(userId) === -1) {
+			skipVotes.push(userId);
+		} else {
+			skipVotes = skipVotes.splice(skipVotes.indexOf(userId), 1);
+		}
+		//TODO Calculate if enough people voted to skip
+		//TODO Emit
+	};
+	this.retrievePlaylist = function() {
+		//TODO Use Rethink to get the Playlist for this room
+	};
+	this.pause = function() {
+		if (!paused) {
+			paused = true;
+			timer.pause();
+		}
+		//TODO Emit
+	};
+	this.unpause = function() {
+		if (paused) {
+			paused = false;
+			timer.resume();
+		}
+		//TODO Emit
+	};
+	this.isPaused = function() {
+		return paused;
+	};
+	this.getCurrentSong = function() {
+		return currentSong;
+	};
+	this.lock = function() {
+		if (!locked) {
+			locked = true;
+		}
+		//TODO Emit
+	};
+	this.unlock = function() {
+		if (locked) {
+			locked = false;
+		}
+		//TODO Emit
+	};
+	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();
+}
+
+module.exports = {
+
+	stations: [],
+	dbConnection: null,
+
+	setup: function (dbConn) {
+		this.dbConnection = dbConn;
+	},
+
+	initStation: function (id, data) {
+		if (!this.getStation(id)) {
+			var station = new Station(id, data, this.dbConnection);
+			this.stations.push(station);
+			return station;
+		}
+		else {
+			return false;
+		}
+	},
+
+	getStation: function (id) {
+		var s = null;
+		this.stations.forEach(function (station) {
+			if (station.id == id) s = station;
+		});
+		return s;
+	},
+
+	// creates a brand new station
+	createStation: function (data) {
+		//TODO: add createStation functionality
+		this.initStation(null, data);
+	},
+
+	// loads a station from the database
+	loadStation: function (data) {
+		//TODO: Get this from RethinkDB
+		this.initStation({
+			playlist: [
+				{
+					mid: "3498fd83",
+					duration: 20000,
+					title: "Test1"
+				},
+				{
+					mid: "3498fd83434",
+					duration: 10000,
+					title: "Test2"
+				}
+			],
+			currentSongIndex: 0,
+			paused: false,
+			locked: false,
+			skipVotes: [],
+			users: [],
+			displayName: "",
+			description: ""
+		});
+	}
+
+};

+ 35 - 3
logic/utils.js

@@ -1,5 +1,37 @@
+function Timer(callback, delay, paused) {
+	var timerId, start, remaining = delay;
+	var timeWhenPaused = 0;
+	var timePaused = Date.now();
+
+	this.pause = function () {
+		clearTimeout(timerId);
+		remaining -= Date.now() - start;
+		timePaused = Date.now();
+	};
+
+	this.resume = function () {
+		start = Date.now();
+		clearTimeout(timerId);
+		timerId = setTimeout(callback, remaining);
+		timeWhenPaused += Date.now() - timePaused;
+	};
+
+	this.resetTimeWhenPaused = function() {
+		timeWhenPaused = 0;
+	};
+
+	this.timeWhenPaused = function () {
+		return timeWhenPaused;
+	};
+
+	if (paused === false) {
+		this.resume();
+	}
+}
+
 module.exports = {
-    htmlEntities: function(str) {
-        return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
-    }
+	htmlEntities: function(str) {
+		return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
+	},
+	Timer: Timer
 };