private static async Task SeedEvents(VoteDbContext db, IMapper mapper, UserManager <User> userManager, string basePath)
        {
            string fullPath = Path.Combine(basePath, VoteConstants.JsonEvents);

            var jsonEvents = File.ReadAllText(fullPath);

            var eventDtos = JsonConvert.DeserializeObject <EventDto[]>(jsonEvents);

            var user1Id = (await userManager.FindByNameAsync("pesho")).Id;
            var user2Id = (await userManager.FindByNameAsync("gosho")).Id;

            for (int i = 0; i < eventDtos.Length / 2; i++)
            {
                eventDtos[i].CreatorId = user1Id;
            }

            for (int i = eventDtos.Length / 2; i < eventDtos.Length; i++)
            {
                eventDtos[i].CreatorId = user2Id;
            }

            var eventsData = mapper.Map <List <Event> >(eventDtos);

            db.Events.AddRange(eventsData);

            db.SaveChanges();
        }
        private static void SeedQuestions(VoteDbContext db, IMapper mapper, string basePath)
        {
            string fullPath = Path.Combine(basePath, VoteConstants.JsonQuestions);

            var jsonQuestions = File.ReadAllText(fullPath);

            var questionDtos = JsonConvert.DeserializeObject <QuestionDto[]>(jsonQuestions);

            var questionsData = mapper.Map <List <Question> >(questionDtos);

            db.Questions.AddRange(questionsData);

            db.SaveChanges();
        }
        private static void SeedReplies(VoteDbContext db, IMapper mapper, string basePath)
        {
            string fullPath = Path.Combine(basePath, VoteConstants.JsonReplies);

            var jsonReplies = File.ReadAllText(fullPath);

            var repliesDtos = JsonConvert.DeserializeObject <ReplyDto[]>(jsonReplies);

            var repliesData = mapper.Map <List <Reply> >(repliesDtos);

            db.Replies.AddRange(repliesData);

            db.SaveChanges();
        }
Пример #4
0
        public void InitializeTests()
        {
            this.mapper  = MockAutoMapper.GetAutoMapper();
            this.db      = MockDbContext.GetTestDb();
            this.service = new ParticipantRepliesService(this.db, this.mapper);

            var testQuestion = new Question()
            {
                Id = 1, Content = "First test", EventId = 1
            };

            db.Questions.Add(testQuestion);

            db.SaveChanges();
        }
Пример #5
0
        public static VoteDbContext GetTestDb()
        {
            var options = new DbContextOptionsBuilder <VoteDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new VoteDbContext(options);

            var activities = new List <Event>()
            {
                new Event()
                {
                    Id        = 1,
                    Code      = "001",
                    Title     = "First",
                    CreatorId = "ABC",
                    StartDate = DateTime.Now,
                    EndDate   = DateTime.Now.AddDays(3)
                },
                new Event()
                {
                    Id        = 2,
                    Code      = "002",
                    Title     = "Second",
                    CreatorId = "ABC",
                    StartDate = DateTime.Now,
                    EndDate   = DateTime.Now.AddDays(3)
                },
                new Event()
                {
                    Id        = 3,
                    Code      = "003",
                    Title     = "Third",
                    CreatorId = "ABC",
                    StartDate = DateTime.Now,
                    EndDate   = DateTime.Now.AddDays(3)
                }
            };

            db.AddRange(activities);

            db.SaveChanges();

            return(db);
        }
Пример #6
0
 public bool Save()
 {
     return(_context.SaveChanges() >= 0);
 }
Пример #7
0
        public void InitializeTests()
        {
            this.mapper  = MockAutoMapper.GetAutoMapper();
            this.db      = MockDbContext.GetTestDb();
            this.service = new ManagerPollsService(this.db, this.mapper);

            var testPollQuestion = new PollQuestion()
            {
                Id = 1, Content = "Are you ready?"
            };

            var testPollAnswers = new List <PollAnswer>()
            {
                new PollAnswer()
                {
                    Id = 1, Content = "Yes"
                },
                new PollAnswer()
                {
                    Id = 2, Content = "No"
                }
            };

            var testPoll = new Poll()
            {
                Id           = 99,
                EventId      = 1,
                PollQuestion = testPollQuestion,
                PollAnswers  = testPollAnswers,
                IsDeleted    = false,
                IsActive     = false
            };

            var testPollQuestion2 = new PollQuestion()
            {
                Id = 2, Content = "Are you ready again?"
            };

            var testPollAnswers2 = new List <PollAnswer>()
            {
                new PollAnswer()
                {
                    Id = 3, Content = "Yes"
                },
                new PollAnswer()
                {
                    Id = 4, Content = "No"
                }
            };

            var testPoll2 = new Poll()
            {
                Id           = 100,
                EventId      = 1,
                PollQuestion = testPollQuestion2,
                PollAnswers  = testPollAnswers2,
                IsDeleted    = false,
                IsActive     = false
            };

            db.Polls.AddRange(testPoll, testPoll2);
            db.SaveChanges();
        }
Пример #8
0
        // POST: api/Vote
        public StatusCodeResult Post([FromBody] VotePost v)
        {
            int ipId = GetIpId();


            // Make sure the call isn't malformed

            if (!ModelState.IsValid || v.WinId == v.LoseId)
            {
                //return BadRequest();
                return(StatusCode(400));
            }


            // Check if the player names are valid

            if (!_context.Players.Where(x => x.Id == v.WinId).Any() ||
                !_context.Players.Where(x => x.Id == v.LoseId).Any())
            {
                //return BadRequest();
                return(StatusCode(400));
            }


            // Check if the user has made this vote combination before

            bool hasUserMadeThisVote = (from votes in _context.Votes
                                        where votes.IpId == ipId &&
                                        votes.WinId == v.WinId &&
                                        votes.LoseId == v.LoseId
                                        select votes).Any();

            bool hasUserMadeOppositeVote = (from votes in _context.Votes
                                            where votes.IpId == ipId &&
                                            votes.WinId == v.LoseId &&
                                            votes.LoseId == v.WinId
                                            select votes).Any();

            if (hasUserMadeThisVote || hasUserMadeOppositeVote)
            {
                //return Forbid();
                return(StatusCode(403));
            }


            // Prepare data

            Player playerWin  = _context.Players.Where(x => x.Id == v.WinId).SingleOrDefault();
            Player playerLose = _context.Players.Where(x => x.Id == v.LoseId).SingleOrDefault();

            _context.Update(playerWin);
            _context.Update(playerLose);


            // Update vote counts

            playerWin.Wins++;
            playerLose.Losses++;


            // Calculate change in player rating

            int oldWinRating  = playerWin.Shitlo;
            int oldLoseRating = playerLose.Shitlo;
            int ratingDiff    = Math.Abs(oldWinRating - oldLoseRating);

            const int MIN_CHANGE  = 1;                  // Minimum rating change for a comparison
            const int MAX_CHANGE  = 50;                 // Maximum rating change for a comparison
            const int EVEN_CHANGE = 15;                 // Rating change for an even comparison

            const double EULERS_NUMBER      = 2.71828;  //                                       |   1000 beats 1000 -> +/- 15
            const double EXPECTED_SCALING   = -0.00264; // k value of    15 e^(-k 500) = 4       |   1500 beats 1000 -> +/- 4
            const double UNEXPECTED_SCALING = 0.00240;  // k value of    15 e^( k 500) = 50      |   1000 beats 1500 -> +/- 50

            double CalcRatingChange(double scaling)
            {
                return(EVEN_CHANGE * Math.Pow(EULERS_NUMBER, scaling * ratingDiff));
            }                                                                                                                                                                                                   // Ae^(kx), HUGE thanks to Itheory and Eight Bit for the formula & helping me implement it.

            int change = Convert.ToInt32((oldWinRating >= oldLoseRating) ? Math.Max(CalcRatingChange(EXPECTED_SCALING), MIN_CHANGE)                                                                             // Expected result
                                                                                                                                                 : Math.Min(CalcRatingChange(UNEXPECTED_SCALING), MAX_CHANGE)); // Unexpected result

            playerWin.Shitlo  = oldWinRating + change;
            playerLose.Shitlo = oldLoseRating - change;


            // Save vote to database

            Vote vote = new Vote
            {
                Date   = DateTime.Now,
                IpId   = ipId,
                WinId  = v.WinId,
                LoseId = v.LoseId
            };

            _context.Votes.Add(vote);


            // Send this vote to subscribed clients

            _hub.Clients.All.SendAsync("recentvote", new string[] { playerWin.Name, playerLose.Name });


            // Commit changes & return success

            _context.SaveChanges();             // TODO: return different status code if this fails

            //return Ok();
            return(StatusCode(201));
        }