public Task HandleAsync(MarkSessionFinishedCommand command, CancellationToken cancellationToken = new CancellationToken()) { var session = _dbContext.Sessions.First(x => x.Id == command.SessionId); session.IsFinished = true; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public Task HandleAsync(MarkCreateAnswersRoundCompleteCommand command, CancellationToken cancellationToken = new CancellationToken()) { var session = _dbContext.Sessions.First(x => x.Id == command.SessionId); session.CreateAnswersRoundIsCompleted = true; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public Task HandleAsync(MarkAnswerAsReadCommand command, CancellationToken cancellationToken = new CancellationToken()) { var question = _dbContext.Answers.First(x => x.Id == command.AnswerId); question.IsRead = true; _dbContext.Entry(question).Property(x => x.IsRead).IsModified = true; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public Task HandleAsync(MarkAnswerAsCurrentCommand command, CancellationToken cancellationToken = new CancellationToken()) { var answer = _dbContext.Answers.First(x => x.Id == command.AnswerId); answer.IsCurrent = command.IsCurrent; _dbContext.Entry(answer).Property(x => x.IsCurrent).IsModified = true; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public Task HandleAsync(SetCardAmountCommand command, CancellationToken cancellationToken = new CancellationToken()) { var session = _dbContext.Sessions.First(x => x.Id == command.SessionId); session.CardAmount = command.CardAmount; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public Task HandleAsync(MarkReadingRoundStartedCommand command, CancellationToken cancellationToken = new CancellationToken()) { var session = _dbContext.Sessions.First(x => x.Id == command.SessionId); session.ReadingRoundHasStarted = true; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public Task HandleAsync(MarkQuestionAsCurrentCommand command, CancellationToken cancellationToken = new CancellationToken()) { var question = _dbContext.Questions.First(x => x.Id == command.QuestionId); question.IsCurrent = command.IsCurrent; _dbContext.Entry(question).Property(x => x.IsRead).IsModified = true; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public async Task <PlayerTbl> HandleAsync(CreatePlayerQuery query, CancellationToken cancellationToken = new CancellationToken()) { var player = _dbContext.Players.Add(new PlayerTbl { Name = query.Name, SessionId = query.SessionId, IsGameMaster = query.IsGameMaster }); await _dbContext.SaveChangesAsync(cancellationToken); return(player.Entity); }
public async Task <SessionTbl> HandleAsync(CreateSessionQuery query, CancellationToken cancellationToken = new CancellationToken()) { var session = _dbContext.Add(new SessionTbl { Number = _sessionIdGenerator.Generate(), CardAmount = 3 }); await _dbContext.SaveChangesAsync(cancellationToken); return(session.Entity); }
public Task HandleAsync(StartSessionCommand command, CancellationToken cancellationToken = new CancellationToken()) { var session = _dbContext.Sessions.First(x => x.Id == command.SessionId); session.StartTime = DateTimeOffset.Now; session.CardAmount = command.CardAmount; return(_dbContext.SaveChangesAsync(cancellationToken)); }
public async Task HandleAsync(CreateQuestionCommand command, CancellationToken cancellationToken = new CancellationToken()) { var player = _dbContext.Players.First(x => x.Id == command.PlayerId); var questions = command.Questions.Select(x => new QuestionTbl { Id = Guid.NewGuid(), CreatedByPlayerId = command.PlayerId, Content = x.EndsWith("?") ? x : x + "?", SessionId = player.SessionId }); _dbContext.Questions.AddRange(questions); player.HasSubmittedQuestions = true; await _dbContext.SaveChangesAsync(cancellationToken); }
public Task HandleAsync(SubmitAnswersCommand command, CancellationToken cancellationToken = new CancellationToken()) { var player = _dbContext.Players.First(x => x.Id == command.PlayerId); var answers = command.Requests.Select(x => new AnswerTbl { Id = Guid.NewGuid(), CreatedByPlayerId = command.PlayerId, QuestionId = x.QuestionId, Content = x.Answer, SessionId = player.SessionId }); player.HasSubmittedAnswers = true; _dbContext.Answers.AddRange(answers); return(_dbContext.SaveChangesAsync(cancellationToken)); }
public async Task HandleAsync(AssignQuestionsCommand command, CancellationToken cancellationToken = new CancellationToken()) { var playerQuestions = await _dbContext.Players.Where(x => x.SessionId == command.SessionId).Join(_dbContext.Questions, player => player.Id, question => question.CreatedByPlayerId, (player, question) => new { PlayerId = player.Id, Question = question }).ToListAsync(cancellationToken); playerQuestions.Shuffle(); var playerIds = playerQuestions.Select(x => x.PlayerId).Distinct().ToList(); var questions = playerQuestions.Select(x => x.Question).ToList(); for (var i = 0; i < playerIds.Count; i++) { var playerId = playerIds[i]; var nextPlayerId = playerIds[i + 1 < playerIds.Count ? i + 1 : 0]; var questionsToAssign = questions.Where(x => x.CreatedByPlayerId == nextPlayerId); foreach (var question in questionsToAssign) { question.AssignedToPlayerId = playerId; } } await _dbContext.SaveChangesAsync(cancellationToken); }
public async Task HandleAsync(AssignAnswersAndQuestionsCommand command, CancellationToken cancellationToken = new CancellationToken()) { var questions = await _dbContext.Questions.Where(x => x.SessionId == command.SessionId).ToListAsync(cancellationToken); var answers = await _dbContext.Answers.Where(x => x.SessionId == command.SessionId).ToListAsync(cancellationToken); var playerIds = await _dbContext.Players.Where(x => x.SessionId == command.SessionId).Select(x => x.Id).ToListAsync(cancellationToken); var cardPerPlayerCount = questions.Count / playerIds.Count; var cardsByPlayer = new Dictionary <Guid, List <(QuestionTbl, AnswerTbl)> >(); foreach (var question in questions) { var answer = answers.First(y => y.QuestionId == question.Id); var playerIndex = questions.IndexOf(question) / cardPerPlayerCount; var playerId = playerIds[playerIndex]; question.PlayerToReadQuestionId = playerId; answer.PlayerToReadAnswerId = playerId; var card = (question, answer); if (cardsByPlayer.TryGetValue(playerId, out var playerCards)) { playerCards.Add(card); } else { cardsByPlayer.Add(playerId, new List <(QuestionTbl, AnswerTbl)> { card }); } } var firstCard = cardsByPlayer.First().Value.First(); for (int i = 0; i < cardPerPlayerCount; i++) { foreach (var playerCards in cardsByPlayer) { var card = playerCards.Value.First(); playerCards.Value.Remove(card); var playerIndex = playerIds.IndexOf(playerCards.Key); var nextPlayerId = playerIds[playerIndex + 1 < playerIds.Count ? playerIndex + 1 : 0]; var nextCard = cardsByPlayer[nextPlayerId].FirstOrDefault(); if (nextCard.Item1 is null) { nextCard = firstCard; } card.Item1.AssignedAnswerId = nextCard.Item2.Id; } } //var cards = cardsByPlayer.SelectMany(x => x.Value).ToList(); //var unusedCards = cardsByPlayer.SelectMany(x => x.Value).ToList(); //var firstCard = cards.First(); //var stayInLoop = true; //while (stayInLoop) //{ // var nextCard = unusedCards.FirstOrDefault(x => x.Item1.PlayerToReadQuestionId != firstCard.Item1.PlayerToReadQuestionId); // if (nextCard.Item1 is null) // { // nextCard = cards.First(); // stayInLoop = false; // } // firstCard.Item1.AssignedAnswerId = nextCard.Item2.Id; // unusedCards.Remove(firstCard); // firstCard = nextCard; //} //foreach (var card in cards) //{ // var nextCard = unusedCards.FirstOrDefault(x => x.Item1.PlayerToReadQuestionId != card.Item1.PlayerToReadQuestionId); // if (nextCard.Item1 is null) // nextCard = cards.First(); // card.Item1.AssignedAnswerId = nextCard.Item2.Id; // unusedCards.Remove(card); //} //var initialCard = cards.First(); //var assignedAnswers = 0; //while (assignedAnswers != cards.Count) //{ // var nextCard = cards.First(x => x.Item1.Id != initialCard.Item1.Id && x.Item1.PlayerToReadQuestionId != initialCard.Item1.PlayerToReadQuestionId && x.Item1.AssignedAnswerId != initialCard.Item2.Id); // initialCard.Item1.AssignedAnswerId = nextCard.Item2.Id; // assignedAnswers++; // initialCard = nextCard; //} //for (var i = 0; i < cardPerPlayerCount; i++) //{ // foreach (var playerId in playerIds) // { // var nextCard = unassignedCards.First(x => x.Item1.Id != initialCard.Item1.Id && x.Item1.PlayerToReadQuestionId != initialCard.Item1.PlayerToReadQuestionId); // initialCard.Item1.AssignedAnswerId = nextCard.Item2.Id; // unassignedCards.Remove(initialCard); // //var index = playerIds.IndexOf(playerId); // //var nextPlayerId = playerIds[index + 1 < playerIds.Count ? index + 1 : 0]; // //var card = cardsByPlayer[playerId][i]; // //var nextCard = cardsByPlayer[nextPlayerId].First(x => x.Item1.AssignedAnswerId != card.Item2.Id); // //card.Item1.AssignedAnswerId = nextCard.Item2.Id; // } //} //foreach (var playerCardGroup in cards) //{ // var index = playerIds.IndexOf(playerCardGroup.Key); // var nextPlayerId = playerIds[index + 1 < playerIds.Count ? index + 1 : 0]; // var nextPlayerCards = cards[nextPlayerId]; // foreach (var card in playerCardGroup.Value) // { // card.Item1.PlayerToReadQuestionId = playerCardGroup.Key; // card.Item2.PlayerToReadAnswerId = playerCardGroup.Key; // var cardIndex = playerCardGroup.Value.IndexOf(card); // var nextPlayerCard = nextPlayerCards[cardIndex]; // nextPlayerCard.Item1.AssignedAnswerId = card.Item2.Id; // } //} await _dbContext.SaveChangesAsync(cancellationToken); }