コード例 #1
0
        /// <summary>
        /// Add answer to subscriber game result
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="subscriberId"></param>
        /// <param name="gameId"></param>
        /// <param name="clueId"></param>
        /// <param name="answer"></param>
        /// <param name="score"></param>
        public void UpdateSubscriberGameResult(Guid tournamentId, Guid subscriberId, Guid gameId, Guid clueId, string answer, int score)
        {
            var tournament = TournamentRepositoryFake.Tournaments.FirstOrDefault(t => t.Id == tournamentId);

            if (tournament == null)
            {
                throw new Exception("Tournament not found!");
            }

            var game = TournamentRepositoryFake.TournamentGames.FirstOrDefault(tg => tg.TournamentId == tournamentId && tg.Id == gameId);

            if (game == null)
            {
                throw new Exception("Game not found!");
            }
            if (game.Clues.FirstOrDefault(c => c.Id == clueId) == null)
            {
                throw new Exception("Clue not found!");
            }

            var tournamentSubscriber = TournamentRepositoryFake.TournamentSubscribers.FirstOrDefault(ts => ts.TournamentId == tournamentId && ts.Id == subscriberId);

            if (tournamentSubscriber == null)
            {
                throw new Exception("Tournament subscriber not found!");
            }

            var subscriberGameResults = TournamentRepositoryFake.SubscriberGameResults.FirstOrDefault(sgr => sgr.TournamentId == tournamentId && sgr.GameId == gameId && sgr.SubscriberId == subscriberId);

            if (subscriberGameResults == null)
            {
                subscriberGameResults = new SubscriberGameResult
                {
                    Id             = Guid.NewGuid(),
                    TournamentId   = tournamentId,
                    GameId         = gameId,
                    SubscriberId   = subscriberId,
                    AnswerAttempts = new List <AnswerAttempt>()
                };
                // Add to repository
                TournamentRepositoryFake.SubscriberGameResults.Add(subscriberGameResults);
            }
            var answerAttempt = subscriberGameResults.AnswerAttempts.FirstOrDefault(aa => aa.ClueId == clueId);

            if (answerAttempt == null)
            {
                answerAttempt = new AnswerAttempt
                {
                    Id     = Guid.NewGuid(),
                    ClueId = clueId
                };
                subscriberGameResults.AnswerAttempts.Add(answerAttempt);
            }
            answerAttempt.Answer = answer;
            answerAttempt.Score  = score;
        }
コード例 #2
0
        public IActionResult GetTournamentSubscriberGameResults(Guid tournamentid, Guid subscriberid, Guid id)
        {
            // Get the tournament
            Tournament tournament = this.TournamentRepository.Get(tournamentid);

            if (tournament == null)
            {
                return(NotFound());
            }
            Game tournamentGame = this.TournamentRepository.GetGame(tournamentid, id);

            if (tournamentGame == null)
            {
                return(NotFound());
            }

            Subscriber subscriber = this.TournamentRepository.GetSubscriber(tournamentid, subscriberid);

            if (subscriber == null)
            {
                return(NotFound());
            }

            // Get the subscriber result of this game
            SubscriberGameResult subscriberGameResults = this.TournamentRepository.GetSubscriberGameResult(tournamentid, subscriberid, id);

            if (subscriberGameResults == null)
            {
                return(NotFound());
            }

            SubscriberGameResultModel model = Mapper.Map <SubscriberGameResult, SubscriberGameResultModel>(subscriberGameResults);

            // Get the subscribers rank in the overall game rankings
            var allRankings = this.GameLogic.BuildGameRankings(this.TournamentRepository.GetAllSubscriberGameResults(tournamentid, id), null);
            var ranking     = allRankings.FirstOrDefault(r => r.SubscriberId == subscriberid);

            if (ranking != null)
            {
                model.Rank = ranking.Rank;
            }
            else
            {
                model.Rank = int.MaxValue;
            }

            // Set subscriber rank by finding him in the game rankings
            return(Ok(model));
        }