// 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); }
public RoundResultsDto GetRoundResults(string roomCode) { int roundNumber = gameRooms.First(rm => rm.RoomName == roomCode).RoundNumber; gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds[roundNumber].Fetched = true; TriviumRound triviumRound = gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds[roundNumber]; RoundResultsDto triviumRoundDto = new RoundResultsDto(); List <PlayerDto> players = gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds[roundNumber].Answerers.ToList(); triviumRoundDto.Players = players; // here we need to associate the answerId with the text verison of the answers triviumRoundDto.Question = gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds[roundNumber].AnswerTrivium.Question; Trivium answerTrivium = gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds[roundNumber].AnswerTrivium; triviumRoundDto.Answer = answerTrivium.Answer; List <Trivium> lt = gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds[roundNumber].WrongTrivia; foreach (PlayerDto p in players) { foreach (Trivium t in lt) { if (p.AnswerId == t.Id) { p.Answer = t.Answer; } triviumRoundDto.OtherAnswers.Add(t.Answer); } if (p.AnswerId == answerTrivium.Id) { p.Answer = answerTrivium.Answer; } } AdvanceToTheNextRound(roomCode); if (gameRooms.First(rm => rm.RoomName == roomCode).RoundNumber == gameRooms.First(rm => rm.RoomName == roomCode).TriviumRounds.Count) { triviumRoundDto.LastRound = true; bool over = GameIsOver(roomCode); Console.WriteLine("Game is over, returning final round"); } return(triviumRoundDto); }
public void InitializeTriviumGameRoom(int numberOfTrivia, string roomname) { List <TriviumRound> TriviumRounds = new List <TriviumRound>(); // from database // This is how we access the db using Entity Framework Core // List<Trivium> dbTrivia = _context.Trivia.ToList(); // Now, let's build and access the db the same way we did the hackers/users List <Trivium> dbTrivia = GetTriviaFromDatabase().ToList(); HashSet <int> seenIndices; // we need to get 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 seenIndices = new HashSet <int>(); seenIndices.Add(randomQuestionIndex); // 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 <Trivium> options = dbTrivia.Where(opt => opt.Category == c && !seenIndices.Contains(opt.Id)).OrderBy(a => rnd.Next()).Take(3).ToList(); tr.WrongTrivia = options; TriviumRounds.Add(tr); } _gameRoomService.SetRoundsForGameRoom(TriviumRounds, roomname); }
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); }