/// <summary> /// Takes the database representaiton of the queue and expands it into a queue that is fully /// filled except for song ratings. /// </summary> /// <param name="raw">The databse representation of the queue.</param> /// <param name="queue">Out object representation of the queue.</param> /// <param name="DJID">The DJID of the DJ.</param> /// <param name="db">An object that allows for database connectivity.</param> /// <returns>The outcome of the operation.</returns> public static Response DBToFullList(string raw, out List<queueSinger> queue, int DJID, DatabaseConnectivity db) { queue = new List<queueSinger>(); Response r = new Response(); int count = 0; string[] clientRequests = raw.Split('`'); for (int i = 0; i < clientRequests.Length; i++) { string[] parts = clientRequests[i].Split('~'); if (parts.Length == 0) { r.error = true; r.message = "Error in DBtoList 1"; return r; } queueSinger qs = new queueSinger(); qs.songs = new List<Song>(); User u = new User(); u.userID = int.Parse(parts[0]); if (u.userID < 0) r = db.DJGetTempUserName(u.userID, DJID); else r = db.MobileIDtoUsername(u.userID); if (r.error) return r; if (r.message.Trim().Length == 0) { r.error = true; r.message = "DB Username lookup exception in DBToFullList!"; return r; } u.userName = r.message.Trim(); qs.user = u; for (int j = 1; j < parts.Length; j++) { Song s = new Song(); r = GetSongInformation(int.Parse(parts[j]), DJID, -1, out s, db, true); if (r.error) return r; qs.songs.Add(s); } queue.Add(qs); count++; } return r; }
/// <summary> /// Add an achievement to the list of DJ achievements stored in the server. /// If the achievement is invalid, an error is returned. /// </summary> /// <param name="achievement">The achievement to add.</param> /// <param name="DJKey">The DJKey for the venue.</param> /// <returns>The outcome of the operation.</returns> public Response DJAddAchievement(Achievement achievement, long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; foreach(AchievementSelect a in achievement.selectList) { if (a.startDate.Year < 1754) { a.startDate = new DateTime(1900, 1, 1); } } string sqlText; List<SqlCommand> cmds; r = AchievementParser.CreateAchievementSQL(achievement, DJID, out sqlText, out cmds); if (r.error) return r; r = db.DJAddAchievement(DJID, achievement); if (r.error) return r; achievement.ID = r.result; r = Common.EvaluateAchievement(DJID, achievement, db); if (r.error) return r; r.message = achievement.ID.ToString(); r.result = achievement.ID; return r; } }
/// <summary> /// Change a user's email. /// </summary> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <param name="newEmail">The new email address.</param> /// <param name="role">The role, DJ or mobile.</param> /// <returns>The outcome of the operation.</returns> public Response ChangeEmail(int ID, string role, string newEmail) { Response r = new Response(); if (!role.Equals("DJ") && !role.Equals("Mobile")) { r.error = true; r.message = "Bad role"; return r; } // Validate the email address. try { var address = new System.Net.Mail.MailAddress(newEmail); } catch { r.error = true; r.message = "Email address is not valid"; return r; } using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection r = db.OpenConnection(); if (r.error) return r; if (role == "DJ") r = db.DJSetEmail(ID, newEmail); else r = db.MobileSetEmail(ID, newEmail); if (r.error) return r; return r; } }
/// <summary> /// Change a user's email. /// </summary> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <param name="newEmail">The new email address.</param> /// <param name="role">The role, DJ or mobile.</param> /// <returns>The outcome of the operation.</returns> public Response ChangeEmail(int ID, string role, string newEmail) { ExpResponse r = new ExpResponse(); if (!role.Equals("DJ") && !role.Equals("Mobile")) { r.setErMsgStk(true, "Bad Role Given", Environment.StackTrace); return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web); } // Validate the email address. try { var address = new System.Net.Mail.MailAddress(newEmail); } catch { r.setErMsg(true, Messages.ERR_BAD_EMAIL); return r; } using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection r = db.OpenConnection(); if (r.error) return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web); if (role == "DJ") r = db.DJSetEmail(ID, newEmail); else r = db.MobileSetEmail(ID, newEmail); if (r.error) return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web); return r; } }
/// <summary> /// View all the achievements belonging to the DJ. /// </summary> /// <param name="DJKey">The DJKey for the venue.</param> /// <param name="achievements">Out list of achievements.</param> /// <returns>The outcome of the operation.</returns> public Response DJViewAchievements(long DJKey, out List<Achievement> achievements) { int DJID = -1; achievements = new List<Achievement>(); using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; r = db.DJViewAchievements(DJID, out achievements); if (r.error) return r; return r; } }
/// <summary> /// Attempts to sign in the DJ using the given credentials. /// If an error occurs, the LogInResponse will have the error field as true, and the error will be in message. /// </summary> /// <param name="username">Username to sign in with.</param> /// <param name="password">Password to sign in with.</param> /// <returns>LogInReponse returns the outcome. The UserKey sent back is used for all communicaiton in further methods.</returns> /// public LogInResponse DJSignIn(string username, string password) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return new LogInResponse(r); // Get the salt from the database and salt/hash the password. string salt; r = db.DJGetSalt(username, out salt); if (r.error) return new LogInResponse(r); string saltHashPassword = Common.CreatePasswordHash(password, salt); // See if the username/password combination is valid. // If it is valid, the DJID will be stored in r.message. // If it is not valid, r.message will be empty. r = db.DJValidateUsernamePassword(username, saltHashPassword); if (r.error) return new LogInResponse(r); // If the username/password couldn't be found, inform user. if (r.message.Trim() == string.Empty) { r.error = true; r.message = "Username/Password is incorrect."; return new LogInResponse(r); } // Get the DJID stored in r.message. if (!int.TryParse(r.message.Trim(), out DJID)) { r.error = true; r.message = "Exception in DJSignIn: Unable to parse DJID from DB!"; return new LogInResponse(r); } // Make sure the DJ is not logged in. RIGHT NOW: JUST DON'T CHECK ANYTHING USEFUL TO ALLOW FOR LOGINS TO OCCUR WHEN LOGGED IN! r = DJValidateStatus(DJID, "!4", db); if (r.error) return new LogInResponse(r); // Information seems valid, attempt to sign in. r = db.DJSetStatus(DJID, 1); if (r.error) return new LogInResponse(r); // Attempt to change the DJID into a userKey long userKey; r = DJGenerateKey(DJID, out userKey, db); if (r.error) return new LogInResponse(r); // If there was no error, create a loginResponse with the successful information. LogInResponse lr = new LogInResponse(); lr.result = r.result; lr.userKey = userKey; User u = new User(); u.userName = username; u.userID = DJID; return lr; } }
/// <summary> /// Ban the user from the venue. The user is expelled from the queue and venue. /// The user is unallowed to join the venue while banned. /// </summary> /// <param name="userToBan">The user to ban.</param> /// <param name="DJKey">The DJKey for the venue.</param> /// <returns>The outcome of the operation.</returns> public Response DJBanUser(User userToBan, long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Attempt to convert DJKey to DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; r = db.MobileValidateID(userToBan.userID); if (r.error) return r; if (r.message.Length == 0) { r.message = "Could not find that userID"; r.error = true; return r; } r = db.DJBanUser(DJID, userToBan.userID); if (r.error) return r; if (r.result != 1) { r.message = "DB failed to ban user?"; r.error = true; return r; } r = db.DJRemoveUserFromVenueIfAtVenue(DJID, userToBan.userID); if (r.error) return r; DJRemoveUser(userToBan.userID, DJKey); Common.PushMessageToMobile(userToBan.userID, "exit", db); return r; } }
/// <summary> /// Get the DJ's queue from the server. /// </summary> /// <param name="queue">The out parameter that represents the queue.</param> /// <param name="DJKey">The DJKey assigned to the DJ.</param> /// <returns>The outcome of the operation.</returns> public Response DJGetQueue(out List<queueSinger> queue, long DJKey) { queue = new List<queueSinger>(); int DJID = -1, count = 0; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ is not logged out. r = DJValidateStatus(DJID, "2", db); if (r.error) return r; r = db.GetSongRequests(DJID); if (r.error) return r; string raw = r.message; if (raw.Trim() == "") { r.error = false; r.message = "Empty Queue"; return r; } r = Common.DBToFullList(raw, out queue, DJID, db); if (r.error) return r; r.result = count; return r; } }
/// <summary> /// Move a song in a user's song requets new a new position in his/her song requests. /// Does not move singers, moves a singer's songs. /// </summary> /// <param name="sr">The song request to move</param> /// <param name="newIndex">The new index to insert the song into.</param> /// <param name="DJKey">The unique key that describes the DJ.</param> /// <returns>The outcome of the operation.</returns> public Response DJMoveSongRequest(SongRequest sr, int newIndex, long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ isn't logged out. r = DJValidateStatus(DJID, "2", db); if (r.error) return r; // Get the current song Requests r = db.GetSongRequests(DJID); if (r.error) return r; string requests = r.message; string newRequests = string.Empty; // If there were no requests, simply add the single request. if (requests.Trim().Length == 0) { r.error = true; r.message = "The queue is empty"; return r; } // Since there is a list of requests, call to parse the raw string data into an list of queuesingers. List<queueSinger> queue; r = Common.DBToMinimalList(requests, out queue); if (r.error) return r; // Search to see if the user is already in this list of singers. for (int i = 0; i < queue.Count; i++) { // We found the userID already in here. if (queue[i].user.userID == sr.user.userID) { // Loop through the songs to see if the user is already singing this song. for (int j = 0; j < queue[i].songs.Count; j++) { // If we find the song. if (queue[i].songs[j].ID == sr.songID) { // Make sure the indexes are in range. if (newIndex < 0) newIndex = 0; if (newIndex > queue[i].songs.Count - 1) newIndex = queue[i].songs.Count - 1; // Get the song out, and insert it into the new index. Song temp = queue[i].songs[j]; queue[i].songs.RemoveAt(j); queue[i].songs.Insert(newIndex, temp); Common.MinimalListToDB(queue, out newRequests); r = db.SetSongRequests(DJID, newRequests); if (r.error) return r; if (j == 0 || newIndex == 0) Common.PushMessageToUsersOfDJ(DJID, "queue", db); else Common.PushMessageToMobile(sr.user.userID, "queue", db); return r; } } // If we can't find the current song. r.error = true; r.message = "Could not find the song to move."; return r; } } r.error = true; r.message = "Could not find client in the queue."; return r; } }
/// <summary> /// Discard the current QR code for the DJ and generate a new QR code. /// </summary> /// <param name="DJKey">The DJKey of the DJ.</param> /// <returns>The outcome of the operation.</returns> public Response DJGenerateNewQRNumber(long DJKey) { using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; int DJID; r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; String s = DJID.ToString() + DateTime.Now.ToString(); System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1CryptoServiceProvider(); byte[] res = sha.ComputeHash(System.Text.Encoding.ASCII.GetBytes(s)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < res.Length; i++) sb.Append(res[i].ToString("x2")); String hex = sb.ToString().Substring(0, 8); r = db.DJSetQR(hex, DJID); if (r.error) return r; return r; } }
/// <summary> /// Gets the most or the least popular songs at a venue. /// </summary> /// <param name="DJKey">The unique DJ key.</param> /// <param name="limitToVenue">If true, results are only from this DJ's venue, otherwise, results are from all venues.</param> /// <param name="start">Results start at the given index.</param> /// <param name="count">Sets the number of results.</param> /// <param name="songs">Out list of songs.</param> /// <param name="counts">Out list of counts that contains how many times that song shows up. Indexes match song indexes. ie counts[2] is the number of times songs[2] was sung.</param> /// <returns></returns> public Response DJGetMostPopularSongs(long DJKey, bool limitToVenue, int start, int count, out List<Song> songs, out List<int> counts) { int DJID = -1; songs = new List<Song>(); counts = new List<int>(); List<Song> songIDs; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Attempt to convert DJKey to DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; int chosenVenue = -1; if(limitToVenue) chosenVenue = DJID; r = db.GetMostPopularSongs(chosenVenue, start, count, out songIDs, out counts); if (r.error) return r; foreach(Song s in songIDs) { Song fullSong; Common.GetSongInformation(s.ID, DJID, -1, out fullSong, db, true); songs.Add(fullSong); } return r; } }
/// <summary> /// Changes a user's song request. /// </summary> /// <param name="newSR">The new song request to user.</param> /// <param name="oldSR">The old song request to replace.</param> /// <param name="DJKey">The DJ's assigned key.</param> /// <returns>The outcome of the operation.</returns> public Response DJChangeSongRequest(SongRequest newSR, SongRequest oldSR, long DJKey) { int DJID = -1; int songID = -1; bool songChangeMade = false; bool requireSendToAll = false; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; if (newSR.user.userID != oldSR.user.userID) { r.error = true; r.message = "User must be the same between song requets"; return r; } // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ isn't logged out. r = DJValidateStatus(DJID, "2", db); if (r.error) return r; // Check to see if song exists. r = db.SongExists(DJID, newSR.songID); if (r.error) return r; // Make sure a songID was sent back. if (!int.TryParse(r.message.Trim(), out songID)) { r.error = true; r.message = "Could not find new song."; return r; } // Make sure the mobile user is valid. r = db.MobileValidateID(oldSR.user.userID); if (r.error) return r; // See if an ID was returned. if (r.message.Trim() == String.Empty) { string s = r.message.Trim(); r.error = true; r.message = "Client ID could not be validated."; return r; } // Get the current song Requests r = db.GetSongRequests(DJID); if (r.error) return r; string requests = r.message; string newRequests = string.Empty; // If there were no requests, simply add the single request. if (requests.Trim().Length == 0) { r.error = true; r.message = "There are no song requests"; return r; } // Since there is a list of requests, call to parse the raw string data into an list of queuesingers. List<queueSinger> queue; r = Common.DBToMinimalList(requests, out queue); if (r.error) return r; // Search to see if the user is already in this list of singers. for (int i = 0; i < queue.Count; i++) { // We found the userID already in here. if (queue[i].user.userID == oldSR.user.userID) { // Loop through the songs to see if the user is already singing this song. for (int j = 0; j < queue[i].songs.Count; j++) { if (queue[i].songs[j].ID == newSR.songID) { r.error = true; r.message = "User is already singing that song"; return r; } if (queue[i].songs[j].ID == oldSR.songID) { queue[i].songs[j].ID = newSR.songID; songChangeMade = true; if (j == 0) requireSendToAll = true; } } if (songChangeMade) { Common.MinimalListToDB(queue, out newRequests); r = db.SetSongRequests(DJID, newRequests); if (r.error) return r; if (requireSendToAll) Common.PushMessageToUsersOfDJ(DJID, "queue", db); else Common.PushMessageToMobile(oldSR.user.userID, "queue", db); return r; } // We didn't find the old song. r.error = true; r.message = "Could not find the old song."; return r; } } // We didn't find the user. r.error = true; r.message = "Could not find the user."; return r; } }
/// <summary> /// Deletes the achievement with the given ID. /// </summary> /// <param name="achievementID"></param> /// <param name="DJKey">The DJKey for the venue.</param> /// <returns>The outcome of the operation.</returns> public Response DJDeleteAchievement(int achievementID, long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; r = db.DJDeleteAchievement(DJID, achievementID); if (r.error) return r; if (r.result < 1 && achievementID != -1) { r.error = true; r.message = "The achievement didn't exist? nothing deleted"; return r; } r = Common.RunAchievements(DJID, db); if (r.error) return r; return r; } }
/// <summary> /// Unban the user from the venue. The user can now rejoin the venue. /// </summary> /// <param name="userToUnban">User to unban.</param> /// <param name="DJKey">The DJKey for the venue.</param> /// <returns>The outcome of the operation.</returns> public Response DJUnbanUser(User userToUnban, long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Attempt to convert DJKey to DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; r = db.MobileValidateID(userToUnban.userID); if (r.error) return r; if (r.message.Length == 0) { r.message = "Could not find that userID"; r.error = true; return r; } bool isBanned; r = db.MobileIsBanned(DJID, userToUnban.userID, out isBanned); if (r.error) return r; if (!isBanned) { r.error = true; r.message = "User is not banned!"; return r; } r = db.DJUnbanUser(DJID, userToUnban.userID); if (r.error) return r; if (r.result != 1) { r.message = "DB failed to unban user?"; r.error = true; return r; } return r; } }
/// <summary> /// Create a test Queue for the Rick account. Does not work with any other account. /// </summary> /// <param name="DJKey">The DJ Key assigned to the Rick account.</param> /// <returns>The outcome of the operation.</returns> public Response DJTestQueueFill(long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; if (DJID != 5 && DJID != 4) { r.error = true; r.message = "You are not the rick or jakub account."; return r; } // Make sure the DJ isn't logged out. r = DJValidateStatus(DJID, "2", db); if (r.error) return r; // Get the current song Requests r = db.GetSongRequests(DJID); if (r.error) return r; string newRequests = string.Empty; if (DJID == 5) newRequests = "1~32066~44022`3~23565~23504`2003~32155~32158`2~41440~41438"; else if (DJID == 4) newRequests = "1~55474~56758`2~59321~42050`3~43357~47751"; r = db.SetSongRequests(DJID, newRequests); if (r.error) return r; Common.PushMessageToUsersOfDJ(DJID, "queue", db); return r; } }
/// <summary> /// Close a DJ's session. The DJ must have a session running for this to work. /// </summary> /// <param name="DJKey">The DJKey assigned to the DJ.</param> /// <returns>The outcome of the operation.b</returns> public Response DJStopSession(long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Attempt to convert DJKey to DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ has a session running. r = DJValidateStatus(DJID, "2", db); if (r.error) return r; // Set the status of the DJ to logged in. r = db.DJSetStatus(DJID, 1); if (r.error) return r; Common.PushMessageToUsersOfDJ(DJID, "exit", db); // Delete the song request field. r = db.DJDeleteSongRequests(DJID); if (r.error) return r; r = db.DJRemoveAllTempUsers(DJID); if (r.error) return r; r = db.DJRemoveUsersFromVenue(DJID); if (r.error) return r; return r; } }
/// <summary> /// Registers a DJ for the Mobioke service. /// If an error occurs, the response will describe the error. /// </summary> /// <param name="username">The username to use. Must not be in use by the service already</param> /// <param name="password">The password to use.</param> /// <param name="venue">Object that describes the DJ's venue.</param> /// <param name="email">The email address of the DJ</param> /// <returns>A Response object indicating the result of the operation.</returns> public Response DJSignUp(string username, string password, Venue venue, string email) { using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Escape to allow the DJTestClient to list all DJ information // WILL BE REMOVED FOR RELEASE! if (username.Equals("list", StringComparison.OrdinalIgnoreCase)) { Response listResponse = db.DJListMembers(); if (listResponse.error) return listResponse; if (r.error) return r; return listResponse; } // Validate that username and password are not blank. if (username.Length == 0 || password.Length == 0) { r.error = true; r.message = "Username or password is blank."; return r; } // Validate that username and password are not too long. if (username.Length > 20 || password.Length > 20) { r.error = true; r.message = "Username or password is longer than 20 characters."; return r; } // Try to see if the username already exists. If it does, inform the client. r = db.DJValidateUsername(username); if (r.error) return r; if (r.message.Trim() != string.Empty) { r.error = true; r.message = "That username already exists."; return r; } // Validate the email address. try { var address = new System.Net.Mail.MailAddress(email); } catch { r.error = true; r.message = "Email address is not valid"; return r; } if (venue == null) { r.error = true; r.message = "Venue information must be passed in."; return r; } if (venue.venueName == null || venue.venueName.Length == 0) { r.error = true; r.message = "Venue name must be set"; return r; } if (venue.venueName.Length > 20) { r.error = true; r.message = "Venue name is longer than 20 characters."; return r; } if (venue.venueAddress.Length > 100) { r.error = true; r.message = "Venue address is longer than 100 characters"; return r; } if (venue.venueAddress == null || venue.venueAddress.Length == 0) { r.error = true; r.message = "Venue address must be set"; return r; } // Information seems valid, create a salt and hash the password. string salt = Common.CreateSalt(16); string hashSaltPassword = Common.CreatePasswordHash(password, salt); // Sign up the user. r = db.DJSignUp(username, hashSaltPassword, email, venue.venueName, venue.venueAddress, salt); if (r.error) return r; return r; } }
/// <summary> /// Attempt to sign out the DJ. /// </summary> /// <param name="DJKey">The DJKey of the DJ.</param> /// <returns>The outcome of the operation.</returns> public Response DJSignOut(long DJKey) { int DJID; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ is not logged out. r = DJValidateStatus(DJID, "!0", db); if (r.error) return r; // A sign out seems to be valid. r = db.DJSetStatus(DJID, 0); if (r.error) return r; Common.PushMessageToUsersOfDJ(DJID, "exit", db); // Remove the key from the DB. r = db.DJSetKey(DJID, null); if (r.error) return r; // Close out the song requests for this DJ. r = db.DJDeleteSongRequests(DJID); if (r.error) return r; r = db.DJRemoveAllTempUsers(DJID); if (r.error) return r; r = db.DJRemoveUsersFromVenue(DJID); if (r.error) return r; return r; } }
/// <summary> /// Starts a DJ session up. Mobile users can now make song requests, The DJ can now control the queue. /// </summary> /// <param name="DJKey">The DJ's assigned key.</param> /// <returns>The outcome of the operation.</returns> public Response DJCreateSession(long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Attempt to convert DJKey to DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ is not logged out. r = DJValidateStatus(DJID, "!0", db); if (r.error) return r; // Set the status of the DJ to accepting songs. r = db.DJSetStatus(DJID, 2); if (r.error) return r; // Create a new field for song requests. r = db.DJOpenSongRequests(DJID); return r; } }
/// <summary> /// Debug method to insert batch song history into the database. Allows for somewhat realistic song histories to /// be created easily. Will take in a list of bands, and find numberPerBand random songs per band and add them to /// the song history of the user with the given mobileID. /// </summary> /// <param name="DJKey">The Venue's unique key.</param> /// <param name="bands">A list of bands.</param> /// <param name="numberPerBand">The number of songs to add per band.</param> /// <param name="mobileID">The user to add these songs to.</param> /// <returns>The outcome of the operation.</returns> public Response InsertFauxSongHistory(long DJKey, List<string> bands, int numberPerBand, int mobileID) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; string extraMessage = string.Empty; foreach (string band in bands) { List<int> songIDs = new List<int>(); r = db.GetRandomSongsForArtist(band, DJID, numberPerBand, out songIDs); if (r.error) extraMessage += "Error looking for : " + band + "\n" + r.message; for (int i = 0; i < songIDs.Count; i++) { r = db.MobileAddSongHistory(mobileID, DJID, songIDs[i], DateTime.Now); if (r.error) return r; } r.message = extraMessage; } return r; } }
/// <summary> /// Method to force evaulate achievements, unlikely to be used. /// </summary> /// <param name="DJKey">The DJKey for the venue.</param> /// <returns>The outcome of the operation.</returns> public Response DJEvaluateAchievements(long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; r = Common.RunAchievements(DJID, db); if (r.error) return r; return r; } }
/// <summary> /// Method to view the sql generated for an achievement. Unlikely to be used other than for debugign. /// </summary> /// <param name="DJKey">The DJKey for the venue.</param> /// <param name="achievementID">The ID of the achievement.</param> /// <returns>The outcome of the operation.</returns> public Response ViewAchievementSql(long DJKey, int achievementID) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; List<Achievement> achievements; r = db.DJViewAchievements(DJID, out achievements); if (r.error) return r; foreach (Achievement a in achievements) { if (a.ID == achievementID) { string sql; List<SqlCommand> sqlCommands; r = AchievementParser.CreateAchievementSQL(a, DJID, out sql, out sqlCommands); if (r.error) return r; r.message = sql; r.error = false; return r; } } r.error = true; r.message = "Achievement not found"; return r; } }
/// <summary> /// List the banned users of this venue. /// </summary> /// <param name="DJKey">The DJKey for the venue.</param> /// <param name="users">Out list of banned users.</param> /// <returns>The outcome of the operation.</returns> public Response DJGetBannedUsers(long DJKey, out List<User> users) { int DJID = -1; users = new List<User>(); using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Attempt to convert DJKey to DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; r = db.DJGetBannedUsers(DJID, out users); if (r.error) return r; for (int i = 0; i < users.Count; i++) { r = db.MobileIDtoUsername(users[i].userID); if (r.error) return r; users[i].userName = r.message.Trim(); } return r; } }
/// <summary> /// Convert a DJID to a DJKey. /// </summary> /// <param name="DJID">The DJID</param> /// <param name="DJKey">OUT parameter for the DJKey</param> /// <returns></returns> private Response DJGenerateKey(int DJID, out long DJKey, DatabaseConnectivity db) { DJKey = -1; Response r = new Response(); System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1CryptoServiceProvider(); Random rand = new Random(); byte[] randomBytes = new byte[64]; byte[] result; long tempKey; for (; ; ) { rand.NextBytes(randomBytes); result = sha.ComputeHash(randomBytes); tempKey = BitConverter.ToInt64(result, 0); r = db.DJGetIDFromKey(tempKey); if (r.error) return r; if (r.message.Trim().Length != 0) continue; r = db.DJSetKey(DJID, tempKey); if (r.error) return r; DJKey = tempKey; return r; } }
/// <summary> /// Get the QR Code belonging the the DJ. Returned in Response.message. /// </summary> /// <param name="DJKey">The DJKey of the DJ.</param> /// <returns></returns> public Response DJGetQRNumber(long DJKey) { using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; int DJID; r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; r = db.DJGetQR(DJID); if (r.error) return r; if (r.message.Trim().Length == 0) { r = DJGenerateNewQRNumber(DJKey); if (r.error) return r; return DJGetQRNumber(DJKey); } return r; } }
/// <summary> /// Convert a DJKey to a DJID. /// </summary> /// <param name="DJKey">The DJKey.</param> /// <param name="DJID">OUT parameter for the DJID.</param> /// <returns>The outcome of the operation.</returns> private Response DJKeyToID(long DJKey, out int DJID, DatabaseConnectivity db) { DJID = -1; Response r = db.DJGetIDFromKey(DJKey); if (r.error) return r; if (r.message.Trim().Length == 0) { r.error = true; r.message = "DJKey is not valid"; return r; } if (!int.TryParse(r.message.Trim(), out DJID)) { r.error = true; r.message = "Exception in DJKeyToID: DJID Parse Fail"; return r; } return r; }
/// <summary> /// Get all the songs that belong to the given DJ. /// </summary> /// <param name="songs">OUT parameter that holds the list of songs.</param> /// <param name="DJKey">Unique Key that describes the DJ.</param> /// <returns>The outcome of the operation.</returns> public Response DJListSongs(out List<Song> songs, long DJKey) { Stopwatch sw = new Stopwatch(); sw.Start(); songs = new List<Song>(); int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ isn't logged out r = DJValidateStatus(DJID, "!0", db); if (r.error) return r; r = db.DJListSongs(DJID, out songs); sw.Stop(); r.result = (int)sw.ElapsedMilliseconds; return r; } }
/// <summary> /// Check the status of the DJ. Returns whether the desired status was found. /// </summary> /// <param name="DJID">The DJID for the DJ.</param> /// <param name="desiredStatus">The desired status, 2 or !1 etc.</param> /// <param name="db">The database object to use.</param> /// <returns>The outcome of the operation.</returns> private Response DJValidateStatus(int DJID, string desiredStatus, DatabaseConnectivity db) { Response r; int DJStatus, desired; bool notStatus = false; // Get the status of the DJ. r = db.DJGetStatus(DJID); if (r.error) return r; // Attempt to parse that status of the DJ. if (!int.TryParse(r.message.Trim(), out DJStatus)) { r.error = true; r.message = "Exception in DJCheckStatus: Unable to parse status from DB!"; return r; } if (desiredStatus[0] == '!') { notStatus = true; desiredStatus = desiredStatus.Substring(1); } if(!int.TryParse(desiredStatus, out desired)) { r.error = true; r.message = "Exception in DJCheckStatus: Cannot parse desired Status"; return r; } if (!notStatus) { if (DJStatus != desired) { r.error = true; if (desired == 0) r.message = "You are not signed out."; else if (desired == 1) r.message = "You are not signed in."; else r.message = "You are in the wrong state, possibly not created a session?"; return r; } } else if (DJStatus == desired) { r.error = true; if (desired == 0) r.message = "You are signed out and cannot do that."; else if (desired == 1) r.message = "You are signed in and cannot do that."; else r.message = "You are in the wrong state, do you have a session running?"; return r; } r.result = DJStatus; return r; }
/// <summary> /// Add a song request to the queue. Automatically figures out of the user is already in the queue or not. /// If the song request userID is > 0, matches based on registered user id. /// If the song request userID is 0, matches based in registered user name. /// If the song request uesrID is less than 0, matches based on temporary user name. /// Automaticlaly creates the temporary user if needed. /// </summary> /// <param name="sr">The song request to add.</param> /// <param name="queueIndex">The position to add the user in, if they don't already have song requests in the queue.</param> /// <param name="DJKey">The DJ's assigned key.</param> /// <returns>The outcome of the operation. If the operation is sucessful, the client ID number is returned in result and message.</returns> public Response DJAddQueue(SongRequest sr, int queueIndex, long DJKey) { int DJID = -1; int songID = -1; int clientID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ isn't logged out. r = DJValidateStatus(DJID, "2", db); if (r.error) return r; // Check to see if song exists. r = db.SongExists(DJID, sr.songID); if (r.error) return r; // Make sure the songExists method returned a song. if (!int.TryParse(r.message.Trim(), out songID)) { r.error = true; r.message = "Could not find song"; return r; } if (sr.user.userID < -1) sr.user.userID = -1; // when userID == -1, we are dealing with creating an anonmymous user. if (sr.user.userID == -1) { // See if this username exists. r = db.DJValidateTempUserName(sr.user.userName, DJID); if (r.error) return r; // In this case, the username does not exist. if (r.message.Trim().Length == 0) { // Add the tempUser. r = db.DJAddTempUser(sr.user.userName, DJID); if (r.error) return r; // Get the tempUser's ID from the DB. r = db.DJValidateTempUserName(sr.user.userName, DJID); if (r.error) return r; // Parse the ID. if (!int.TryParse(r.message.Trim(), out clientID)) { r.error = true; r.message = "Unable to get the clientID of the new user."; return r; } } // In this case, the username already exists. else { // Get the tempUser's ID from the DB. r = db.DJValidateTempUserName(sr.user.userName, DJID); if (r.error) return r; // Parse the ID. if (!int.TryParse(r.message.Trim(), out clientID)) { r.error = true; r.message = "Unable to get the clientID of the temp user."; return r; } } } // When userID == 0, we look the user up by username instead of userID. else if (sr.user.userID == 0) { r = db.MobileValidateUsername(sr.user.userName); if (r.error) return r; if (!int.TryParse(r.message.Trim(), out clientID)) { r.error = true; r.message = "Client name could not be validated."; return r; } } // If a userID is passed in. else { r = db.MobileValidateID(sr.user.userID); if (r.error) return r; // See if an ID was returned. if (r.message.Trim() == String.Empty) { string s = r.message.Trim(); r.error = true; r.message = "Client ID could not be validated."; return r; } clientID = sr.user.userID; } // Get the current song Requests r = db.GetSongRequests(DJID); if (r.error) return r; string requests = r.message; string newRequests = string.Empty; // If there were no requests, simply add the single request. if (requests.Trim().Length == 0) { newRequests = clientID.ToString() + "~" + sr.songID.ToString(); //r = Common.PushMessageToMobile(sr.user.userID, "queue", db); Common.PushMessageToUsersOfDJ(DJID, "queue", db); r = db.SetSongRequests(DJID, newRequests); return r; } // Since there is a list of requests, call to parse the raw string data into an list of queuesingers. List<queueSinger> queue; r = Common.DBToMinimalList(requests, out queue); if (r.error) return r; // Search to see if the user is already in this list of singers. for (int i = 0; i < queue.Count; i++) { // We found the userID already in here. if (queue[i].user.userID == clientID) { // Loop through the songs to see if the user is already singing this song. for (int j = 0; j < queue[i].songs.Count; j++) { if (queue[i].songs[j].ID == sr.songID) { r.error = true; r.message = "User is already singing that song"; return r; } } // They dont' already have the song in the list, add them to the list Song s = new Song(); s.ID = sr.songID; queue[i].songs.Add(s); Common.MinimalListToDB(queue, out newRequests); r = db.SetSongRequests(DJID, newRequests); if (r.error) return r; Common.PushMessageToMobile(clientID, "queue", db); //Common.PushMessageToUsersOfDJ(DJID, "queue", db); r.message = clientID.ToString(); r.result = clientID; return r; } } // Now they are not in the queue, add them at queueIndex. queueSinger qs = new queueSinger(); qs.songs = new List<Song>(); qs.user = sr.user; qs.user.userID = clientID; Song song = new Song(); song.ID = sr.songID; qs.songs.Add(song); if (queueIndex < 0) queueIndex = 0; if (queueIndex > queue.Count) queueIndex = queue.Count; queue.Insert(queueIndex, qs); Common.MinimalListToDB(queue, out newRequests); r = db.SetSongRequests(DJID, newRequests); if (r.error) return r; Common.PushMessageToUsersOfDJ(DJID, "queue", db); r.message = clientID.ToString(); r.result = clientID; return r; } }
/// <summary> /// Add songs to a DJ's library. If a song with a matching artist and title exists, /// the song is updated to the newly supplied duration and path on disk, otherwise /// a new song is added to the DJ's library. /// </summary> /// <param name="songs">Songs to add.</param> /// <param name="DJKey">Unique DJKey that describes the DJ.</param> /// <returns>The outcome of the operaiton.</returns> public Response DJAddSongs(List<Song> songs, long DJKey) { int DJID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Attempt to convert DJKey to DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ is not logged out. r = DJValidateStatus(DJID, "!0", db); if (r.error) return r; // Adding songs seems to be valid, add the list of songs to the DJ. r = db.DJAddSongsUpdatingDuplicates(songs, DJID); return r; } }