示例#1
0
        //submit submission downvote
        public static void DownvoteSubmission(int submissionId, string userWhichDownvoted)
        {
            int result = Voting.CheckIfVoted(userWhichDownvoted, submissionId);

            using (whoaverseEntities db = new whoaverseEntities())
            {
                Message submission = db.Messages.Find(submissionId);

                switch (result)
                {
                    //never voted before
                    case 0:

                        if (submission != null)
                        {
                            submission.Dislikes++;

                            double currentScore = submission.Likes - submission.Dislikes;
                            double submissionAge = Whoaverse.Utils.Submissions.CalcSubmissionAgeDouble(submission.Date);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);

                            submission.Rank = newRank;

                            //register downvote
                            Votingtracker tmpVotingTracker = new Votingtracker();
                            tmpVotingTracker.MessageId = submissionId;
                            tmpVotingTracker.UserName = userWhichDownvoted;
                            tmpVotingTracker.VoteStatus = -1;
                            db.Votingtrackers.Add(tmpVotingTracker);
                            db.SaveChanges();
                        }

                        break;

                    //upvoted before, turn upvote to downvote
                    case 1:

                        if (submission != null)
                        {
                            submission.Likes--;
                            submission.Dislikes++;

                            double currentScore = submission.Likes - submission.Dislikes;
                            double submissionAge = Whoaverse.Utils.Submissions.CalcSubmissionAgeDouble(submission.Date);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);

                            submission.Rank = newRank;

                            //register Turn DownVote To UpVote
                            var votingTracker = db.Votingtrackers
                                .Where(b => b.MessageId == submissionId && b.UserName == userWhichDownvoted)
                                .FirstOrDefault();

                            if (votingTracker != null)
                            {
                                votingTracker.VoteStatus = -1;
                            }
                            db.SaveChanges();
                        }

                        break;

                    //downvoted before, reset
                    case -1:

                        if (submission != null)
                        {
                            submission.Dislikes--;

                            double currentScore = submission.Likes - submission.Dislikes;
                            double submissionAge = Whoaverse.Utils.Submissions.CalcSubmissionAgeDouble(submission.Date);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);

                            submission.Rank = newRank;
                            db.SaveChanges();

                            ResetMessageVote(userWhichDownvoted, submissionId);
                        }

                        break;

                }
            }
        }
示例#2
0
        public async Task<ActionResult> Submit([Bind(Include = "Id,Votes,Name,Date,Type,Linkdescription,Title,Rank,MessageContent")] Message message)
        {
            if (ModelState.IsValid)
            {
                var targetSubverse = db.Subverses.Find(message.Subverse.Trim());
                if (targetSubverse != null)
                {

                    // restrict incoming submissions to announcements subverse (temporary hard-code solution
                    // TODO: add global administrators table with different access levels
                    if (message.Subverse.Equals("announcements", StringComparison.OrdinalIgnoreCase) && User.Identity.Name == "Atko")
                    {
                        message.Subverse = targetSubverse.name;
                        db.Messages.Add(message);
                        await db.SaveChangesAsync();
                    }
                    else if (!message.Subverse.Equals("announcements", StringComparison.OrdinalIgnoreCase))
                    {
                        message.Subverse = targetSubverse.name;
                        db.Messages.Add(message);
                        await db.SaveChangesAsync();
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to post to is restricted.");
                        return View();
                    }

                    //get newly generated message ID and execute ranking and self upvoting                
                    Votingtracker tmpVotingTracker = new Votingtracker();
                    tmpVotingTracker.MessageId = message.Id;
                    tmpVotingTracker.UserName = message.Name;
                    tmpVotingTracker.VoteStatus = 1;
                    db.Votingtrackers.Add(tmpVotingTracker);
                    await db.SaveChangesAsync();

                    return RedirectToAction("Index");
                }
                else
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Sorry, you are doing that too fast. Please try again in a few minutes.");
                return View(message);
            }
        }