From d2271a020b143d7cbac7d4f11e8a596e6c3e27d9 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sun, 2 Jun 2013 13:54:58 -0400 Subject: [PATCH] Add playlist item count and playtime --- database.js | 13 +++++++++---- user.js | 10 ++++++++++ www/assets/js/callbacks.js | 8 ++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/database.js b/database.js index ea254f53..96193d3e 100644 --- a/database.js +++ b/database.js @@ -131,6 +131,8 @@ function init() { "`user` VARCHAR(20) NOT NULL,", "`name` VARCHAR(255) NOT NULL,", "`contents` MEDIUMTEXT NOT NULL,", + "`count` INT NOT NULL,", + "`time` INT NOT NULL,", "PRIMARY KEY (`name`))", "ENGINE = MyISAM;"].join(""); results = db.querySync(query); @@ -753,7 +755,7 @@ function getUserPlaylists(user) { } var query = createQuery( - "SELECT name FROM user_playlists WHERE user=?", + "SELECT name,count,time FROM user_playlists WHERE user=?", [user] ); @@ -804,19 +806,22 @@ function saveUserPlaylist(pl, user, name) { // Strip out unnecessary data var pl2 = []; + var time = 0; for(var i = 0; i < pl.length; i++) { var e = { id: pl[i].id, type: pl[i].type }; + time += pl[i].seconds; pl2.push(e); } + var count = pl2.length; var plstr = JSON.stringify(pl2); var query = createQuery( - "INSERT INTO user_playlists VALUES (?, ?, ?)" + - "ON DUPLICATE KEY UPDATE contents=?", - [user, name, plstr, plstr] + "INSERT INTO user_playlists VALUES (?, ?, ?, ?, ?)" + + "ON DUPLICATE KEY UPDATE contents=?,count=?,time=?", + [user, name, plstr, count, time, plstr, count, time] ); var results = db.querySync(query); diff --git a/user.js b/user.js index 2227dec0..6d4103cc 100644 --- a/user.js +++ b/user.js @@ -12,6 +12,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI var Rank = require("./rank.js"); var Auth = require("./auth.js"); var Channel = require("./channel.js").Channel; +var formatTime = require("./media.js").formatTime; var Server = require("./server.js"); var Database = require("./database.js"); var Logger = require("./logger.js"); @@ -425,6 +426,9 @@ User.prototype.initCallbacks = function() { } var list = Database.getUserPlaylists(this.name); + for(var i = 0; i < list.length; i++) { + list[i].time = formatTime(list[i].time); + } this.socket.emit("listPlaylists", { pllist: list, }); @@ -458,6 +462,9 @@ User.prototype.initCallbacks = function() { error: result ? false : "Unknown" }); var list = Database.getUserPlaylists(this.name); + for(var i = 0; i < list.length; i++) { + list[i].time = formatTime(list[i].time); + } this.socket.emit("listPlaylists", { pllist: list, }); @@ -476,6 +483,9 @@ User.prototype.initCallbacks = function() { Database.deleteUserPlaylist(this.name, data.name); var list = Database.getUserPlaylists(this.name); + for(var i = 0; i < list.length; i++) { + list[i].time = formatTime(list[i].time); + } this.socket.emit("listPlaylists", { pllist: list, }); diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 31bf8416..b65a9b16 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -775,6 +775,14 @@ Callbacks = { $("
").text(pls[i].name).appendTo(li) .css("float", "left") .css("margin-left", "1em"); + var metastr = pls[i].count + " item"; + if(pls[i].count != 1) { + metastr += "s"; + } + metastr +=", playtime " + pls[i].time; + $("
").text(metastr) + .css("float", "right") + .appendTo(li); var bg = $("
").addClass("btn-group") .css("float", "left") .prependTo(li);