Exemplo n.º 1
0
        /// <summary>
        /// Generates the leaderboard and user's rank and points needed.
        /// </summary>
        /// <returns>The leaderboard and user's rank/points needed as a partial view.</returns>
        public ActionResult Leaderboard()
        {
            var db = new UsersContext();
            var profiles = db.UserProfiles;
            var activeUser = new UserProfile();
            var username = User.Identity.Name;

            //if there is a logged in user
            if (username != null && username !="")
            {
                var user = from p in profiles
                           where p.UserName == username
                           select p;
                activeUser = user.FirstOrDefault();
            }

            //select top 10 users, ordered by points descending.
            List<UserProfile> results = (from p in profiles orderby p.Points descending select p).Take(10).ToList();

            List<string> leaders = new List<string>();
            List<int> points = new List<int>();

            //create the leaderboard list
            foreach (var result in results)
            {
                leaders.Add(result.UserName);
                points.Add(result.Points);
            }
            ViewBag.leaders = leaders;
            ViewBag.points = points;

            //update user's view of rank and points.
            if (activeUser != null)
            {
                ViewBag.myrank = activeUser.Rank;
                ViewBag.mypoints = activeUser.Points;

                string origrank = activeUser.Rank;
                string currentRank = origrank;
                int pointsTilNext = 0;
                int myPoints = activeUser.Points;

                //calculate points needed to rank up.
                while (currentRank == origrank)
                {
                    pointsTilNext++;
                    currentRank = Ranks.GetRank(myPoints + pointsTilNext);
                }

                ViewBag.pointstogo = pointsTilNext;

            }

            //return as partial view so it can be on many pages.
            return PartialView("Leaderboard");
        }
Exemplo n.º 2
0
        public bool Lyrics(LyricSegment segment, String input, double startTime, UserProfile activeUser)
        {
            int userID = 0;
            TimeSpan now = DateTime.UtcNow - new DateTime(1970, 1, 1);
            bool match = false;

            //New entry in LyricUser table
            LyricsUser userEntry = new LyricsUser();
            userEntry.LyricSegmentID = segment.LyricSegmentID;
            userEntry.Lyrics = input;
            userEntry.UserID = userID;
            userEntry.Time = (int)(now.TotalSeconds-startTime);
            db.LyricUsers.Add(userEntry);

            //Strip out non alphanumeric characters including spaces
            Regex rgx = new Regex("[^a-zA-Z0-9]");
            String inputMod = rgx.Replace(input, "");

               //Get all stat entries corresponding to segment
            IList<LyricsStats> lyricStatEntries = segment.LyricStats.ToList();

            bool newInput = true; //has the input been submitted before by another user
            int maxVotes = 0; //number of votes the top input has recieved
            int votes = 0; //number of total votes
            LyricsStats topStat = null; //top input
            LyricsStats userSub = null;
            //Go through all previously submitted inputs
            for (int i = 0; i < lyricStatEntries.Count(); i++)
            {
                String entryMod = rgx.Replace(lyricStatEntries[i].Lyrics, "");

                //If the stripped input mathes the stripped previous submission
                if(inputMod.Equals(entryMod))
                {
                    lyricStatEntries[i].Votes++; //increase votes for this submission
                    userSub = lyricStatEntries[i];
                    newInput = false; //the users submission was already in the table
                }

                //Is the analyzed input the top submission
                if (lyricStatEntries[i].Votes > maxVotes)
                {
                    maxVotes = lyricStatEntries[i].Votes;
                    topStat= lyricStatEntries[i];
                }

                votes += lyricStatEntries[i].Votes;
            }

            //If the user's submission does not already exist, create an entry for it
            if (newInput == true)
            {
                LyricsStats newEntry = new LyricsStats();
                newEntry.Lyrics = input;
                newEntry.LyricSegmentID = segment.LyricSegmentID;
                newEntry.Available = false;
                newEntry.Votes = 1;
                userSub = newEntry;
                db.LyricStats.Add(newEntry);

            }
            //If the user's submission is already in a table check if the segment is complete
            else
            {
                double voteRatio = (double)(maxVotes)/votes;
                if (voteRatio > 0.5 && maxVotes > 9)
                {
                    topStat.LyricSegment.Complete = true;

                    //If the segment is complete check if the song is complete
                    songCompletion(topStat.LyricSegment.Music);
                }

                //Check if user input is in top 5 submissions
                List<LyricsStats> topResults = (from p in db.LyricStats where p.LyricSegmentID == segment.LyricSegmentID orderby p.Votes descending select p).Take(5).ToList();
                for (int i = 0; i < topResults.Count(); i++)
                {
                    if (topResults.Count() != 0)
                    {
                        String entryMod = rgx.Replace(lyricStatEntries[i].Lyrics, "");
                        if (entryMod.Equals(inputMod))
                            match = true;
                    }
                }

            }

            List<UserSegmentVotes> otherVote = db.UserSegmentVotes.Where(us => us.LyricSegmentID == segment.LyricSegmentID).ToList();
            if (otherVote.Count() == 0)
            {
                UserSegmentVotes newEntryUser = new UserSegmentVotes();
                newEntryUser.LyricSegmentID = segment.LyricSegmentID;
                newEntryUser.LyricsStatsID = userSub.LyricsStatsID;
                newEntryUser.UserID = activeUser.UserId;
                db.UserSegmentVotes.Add(newEntryUser);
            }
            else
            {
                otherVote.First().LyricsStatsID = userSub.LyricsStatsID;
            }

            //Mark all lyricstats meeting threshold as available to be voted on
            for (int i = 0; i < lyricStatEntries.Count(); i++)
            {
                double statRatio = (double)(lyricStatEntries[i].Votes) / votes;
                if (lyricStatEntries[i].Votes > 9 &&  statRatio > 0.5)
                    lyricStatEntries[i].Available = true;
                else
                    lyricStatEntries[i].Available = false;

            }

            db.SaveChanges();

            return match;
        }