Exemplo n.º 1
0
        // public async Task<ServiceResponse<List<GetTriviaDto>>> GetTriviaObjects()
        // {
        //     ServiceResponse<List<GetTriviaDto>> serviceResponse = new ServiceResponse<List<GetTriviaDto>>();

        //     List<GetTriviaDto> trivia = await getListOfNTrivia(4);

        //     // serviceResponse.Data = (trivia.Select(c => _mapper.Map<Trivium>(c))).ToList();
        //     serviceResponse.Data = trivia;

        //     return serviceResponse;
        // }

        // private async Task<List<GetTriviaDto>> getListOfNTrivia(int numberOfTrivium)
        // {
        //     // List<Question> dbQuestions = await _context.Questions.ToListAsync();
        //     // List<Answer> dbAnswers = await _context.Answers.ToListAsync();
        //     // List<Trivium> dbTrivia = await _context.Trivia.ToListAsync();

        //     List<GetTriviaDto> Trivia = new List<GetTriviaDto>();

        //     for (int i = 0; i < numberOfTrivium; i++)
        //     {
        //         int randomQuestionIndex = rnd.Next(dbTrivia.Count) + 1; // needed the +1 because there is no question with id = 0

        //         Trivium t = dbTrivia.First(q => q.Id == randomQuestionIndex);
        //         string q = t.Question;
        //         string a = t.Answer;
        //         Category c = t.Category;
        //         // List<Answer> optionsWithAnswer = addAnswerIfNotPresent(dbAnswers, a);


        //         var opts = dbTrivia.Where(opt => opt.Category == c).Select(opt => opt.Answer).OrderBy(a => rnd.Next()).Take(4).ToList();
        //         var ans = opts.FirstOrDefault(answer => answer.Equals(a));

        //         if (ans == null)
        //         {
        //             int randomIndexToReplace = rnd.Next(4);
        //             opts[randomIndexToReplace] = a;
        //         }

        //         GetTriviaDto GetTriviaDto = new GetTriviaDto
        //         {
        //             Question = q,
        //             // Options = dbTrivia.Select(opt => opt.Answer).Take(4).ToList()
        //             Options = opts
        //         };

        //         Trivia.Add(GetTriviaDto);
        //     }

        //     return Trivia;
        // }

        // public async Task<List<TriviumRound>> GetTriviumRounds(int numberOfTrivia, string roomname)
        // {

        //     List<TriviumRound> TriviumRounds = _gameRoomService.GetGameRoom(roomname).TriviumRounds;

        //     if (TriviumRounds == null)
        //     {
        //         // from database
        //         List<Trivium> dbTrivia = await _context.Trivia.ToListAsync();

        //         TriviumRounds = new List<TriviumRound>();

        //         for (int i = 0; i < numberOfTrivia; i++)
        //         {
        //             TriviumRound tr = new TriviumRound();

        //             int randomQuestionIndex = rnd.Next(dbTrivia.Count) + 1; // needed the +1 because there is no question with id = 0

        //             // here is the AnswerTrivium
        //             Trivium t = dbTrivia.First(q => q.Id == randomQuestionIndex);
        //             string q = t.Question;
        //             string a = t.Answer;
        //             Category c = t.Category;

        //             tr.AnswerTrivium = t;

        //             // List<Answer> optionsWithAnswer = addAnswerIfNotPresent(dbAnswers, a);

        //             // Now get three Wrong Trivia

        //             List<Trivium> options = dbTrivia.Where(opt => opt.Category == c).OrderBy(a => rnd.Next()).Take(3).ToList();
        //             tr.WrongTrivia = options;

        //             // var opts = dbTrivia.Where(opt => opt.Category == c).Select(opt => opt.Answer).OrderBy(a => rnd.Next()).Take(4).ToList();
        //             // var ans = opts.FirstOrDefault(answer => answer.Equals(a));

        //             // if (ans == null)
        //             // {
        //             //     int randomIndexToReplace = rnd.Next(4);
        //             //     opts[randomIndexToReplace] = a;
        //             // }

        //             // GetTriviaDto GetTriviaDto = new GetTriviaDto
        //             // {
        //             //     Question = q,
        //             //     // Options = dbTrivia.Select(opt => opt.Answer).Take(4).ToList()
        //             //     Options = opts
        //             // };

        //             // Trivia.Add(GetTriviaDto);

        //             TriviumRounds.Add(tr);
        //         }

        //         _gameRoomService.SetRoundsForGameRoom(TriviumRounds, roomname);
        //     }



        //     return _gameRoomService.GetGameRoom(roomname).TriviumRounds;
        // }

        public TriviumRoundDto GetTriviumRoundDtoFromRoom(int roundNumber, string roomname)
        {
            TriviumRound triviumRound = _gameRoomService.GetGameRoom(roomname).TriviumRounds[roundNumber];

            // answer trivium
            Trivium        at = triviumRound.AnswerTrivium;
            List <Trivium> wt = triviumRound.WrongTrivia;
            // get the question dto
            TriviumQuestionDto tqd = _mapper.Map <TriviumQuestionDto>(at);
            // TriviumAnswerDto tad = _mapper.Map<TriviumAnswerDto>(wt);

            List <TriviumAnswerDto> tadl = wt.Select(t => _mapper.Map <TriviumAnswerDto>(t)).ToList();


            tadl.Add(_mapper.Map <TriviumAnswerDto>(at));

            // shuffle the tadl
            tadl = tadl.OrderBy(t => rnd.Next()).ToList();

            // add Dtos to TriviumRoundDto
            TriviumRoundDto tdto = new TriviumRoundDto();

            tdto.QuestionTrivium = tqd;
            tdto.WrongTrivia     = tadl;
            return(tdto);
        }
Exemplo n.º 2
0
        public TriviumRoundDto FetchNextRound(string roomCode)
        {
            int          roundNumber = gameRooms.First(rm => rm.RoomName == roomCode).RoundNumber;
            TriviumRound tr          = gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds[roundNumber];

            // build a trivium round dto from the trivium round
            TriviumRoundDto trdto = new TriviumRoundDto();

            trdto.RoundNumber = roundNumber;
            Trivium trvium = tr.AnswerTrivium;
            var     q      = trvium.Question;
            var     a      = trvium.Answer;
            var     i      = trvium.Id;

            trdto.QuestionTrivium.Question = q;



            List <TriviumAnswerDto> tadl = new List <TriviumAnswerDto>();

            tadl.Add(new TriviumAnswerDto {
                Id = i, Answer = a
            });

            foreach (var t in tr.WrongTrivia)
            {
                tadl.Add(new TriviumAnswerDto {
                    Id = t.Id, Answer = t.Answer
                });
            }

            // shuffle the tadl
            Random rnd = new Random();

            tadl = tadl.OrderBy(t => rnd.Next()).ToList();

            trdto.WrongTrivia = tadl;

            return(trdto);
        }