/// <summary> /// Generates up to maxSuggestsions song suggestions for a user based off of their song history. /// Suggested songs are from the same artists as songs they have previously sung with a bias /// to artists they have sung more often. Suggestions are always song they have not sung yet. /// </summary> /// <param name="maxSuggestions"></param> /// <param name="mobileID"></param> /// <param name="DJID"></param> /// <param name="songs"></param> /// <param name="db"></param> /// <returns></returns> private ExpResponse SuggestSongsNotSungByMostSungArtists(int maxSuggestions, int mobileID, int DJID, out List<Song> songs, DatabaseConnectivity db) { List<SongAndCount> sAc; songs = new List<Song>(); ExpResponse r; // Get up to 10 unique artists this user has sung, ordered by the most sung first. r = db.MobileGetUniqueArtistsSung(mobileID, 0, 10, out sAc); if (r.error) return r; // Loop through the unique artists. // store total number of song sings in singCount // Store the iteration singCount in tmp for each sAc. int singCount = 0; for (int i = 0; i < sAc.Count; i++) { singCount += sAc[i].count; sAc[i].tmp = singCount; } // Generate a random number between 0 and the number of total sings. // Loop through the artists, if the random number is less than the // store singCount iteration number, choose to suggest a song based off that artist. Random rand = new Random(DateTime.Now.Millisecond); for (int i = 0; i < maxSuggestions; i++) { int rn = rand.Next(0, singCount); for (int j = 0; j < sAc.Count; j++) { if (rn < sAc[j].tmp) { sAc[j].tmp2++; break; } } } // For each artist, if we selected to suggest based off them, do so the number of times requested. foreach (SongAndCount s in sAc) { if (s.tmp2 > 0) { List<int> songIDs; r = db.MobileGetRandomSongsFromExactArtistNeverSung(s.song.artist, DJID, s.tmp2, mobileID, out songIDs); if (r.error) return r; foreach (int id in songIDs) { Song song = new Song(); song.ID = id; songs.Add(song); } } } return r; }