// Create new playlist // 0 : no error // 1 : name or URL empty // 2 : Name already used // 42: Get error string to know what happend public int createPlaylist(int user_index, string src_name) { int status = 0; try { // checks if source string and int are empty if (src_name == "") { status = 1; } // If they aren't empty else { bool is_name_used_by_user = false; foreach (BTPlaylist playlist_in_db in database.user[user_index].playlists) { if (playlist_in_db.name == src_name) { is_name_used_by_user = true; } } if (is_name_used_by_user == false) { // Creating new user BTPlaylist playlist = new BTPlaylist(); playlist.user_id = database.user[user_index].user_id; playlist.name = src_name; playlist.tracklist = new List <DeezerTrack>(); // Adding playlist to database database.user[user_index].playlists.Add(playlist); // Saving changes to database.json file saveDatabase(); } else { status = 2; } } } catch (Exception ex) { status = 42; error = ex.Message; } return(status); }
// Returns playlist by index public BTPlaylist getPlaylist(int user_index, int playlist_index) { try { return(database.user[user_index].playlists[playlist_index]); } catch { BTPlaylist bad_result = new BTPlaylist(); return(bad_result); } }
// Rename playlist // 0 : no error // 1 : name or user index empty // 42: Get error string to know what happend public int renamePlaylist(int user_index, int playlist_index, string src_name) { int status = 0; try { // checks if source string is empty if (src_name == "") { status = 1; } // If they aren't empty else { // get playlist first BTPlaylist playlist = database.user[user_index].playlists[playlist_index]; // Change values playlist.name = src_name; // Editing playlist in database database.user[user_index].playlists[playlist_index] = playlist; // Saving changes to database.json file saveDatabase(); } } catch (Exception ex) { status = 42; error = ex.Message; } return(status); }
/* =================================================================================================================== */ /* Playlist part */ /* =================================================================================================================== */ // Set selected playlist stored here public void setSelectedPlaylist(BTPlaylist playlist) { selected_playlist = playlist; }
// Create new game // 0 : no error // 1 : userID is not in database // 2 : game_title or created is Empty // 3 : Playlist is just bearly initialized // 4 : invalid scores // 5 : name already given // 42: Get error string to know what happend public int createGame(int userID, string game_title, string created, BTPlaylist playlist, int title_score, int artist_score, int album_score, int duration_score) { int status = 0; bool temp_status = false; try { // Check if userID is correct foreach (BTUser user in database.user) { if (user.user_id == userID) { temp_status = true; } } if (temp_status == false) { status = 1; } else { // Check if game_title and created are empty if (game_title == "" || created == "") { status = 2; } else { // Check if given Playlist Object isn't just initialized if (playlist.Equals(default(BTPlaylist))) { status = 3; } else { // Check if scores are initialized if (title_score >= 0 && artist_score >= 0 && album_score >= 0 && duration_score >= 0) { // Let's create that game! // Default gameID is 0 int gameID = 0; // If games already exists if (database.game.Count > 0) { // Let's generate that game // To generate random int between 0 and 10000 Random random_int = new Random(); temp_status = true; while (temp_status == true) { // Generating gameID gameID = random_int.Next(0, 10000); // Checking if it exists foreach (BTGame game in database.game) { // If gameID not attributed, exit the loop if (game.game_id != gameID) { temp_status = false; } else { temp_status = true; } } } } // Now that gameID is generated, let's create that game! BTGame game_to_create = new BTGame(); game_to_create.game_id = gameID; game_to_create.user_id = userID; game_to_create.game_title = game_title; DateTime created_date = DateTime.Parse(created); game_to_create.created = created_date.ToString(); game_to_create.tracklist = playlist.tracklist; BTGameScores scores_to_assign = new BTGameScores(); scores_to_assign.title = title_score; scores_to_assign.artist = artist_score; scores_to_assign.album = album_score; scores_to_assign.duration = duration_score; scores_to_assign.history = new List <BTGameHistory>(); game_to_create.scores = scores_to_assign; // Check if game title is not already given bool is_game_title_already_given = false; foreach (BTGame game in database.game) { if (game.user_id == userID) { if (game.game_title == game_title) { is_game_title_already_given = true; } } } if (is_game_title_already_given == true) { status = 5; } else { // Add game to database database.game.Add(game_to_create); // Save changes saveDatabase(); } } else { status = 4; } } } } } // If something else happend catch (Exception ex) { status = 42; error = ex.Message; } return(status); }