Esempio n. 1
0
        public Game CreateGame(Game game, String questionSet, int noOfRounds)
        {
            using (var db = new ConquestionDBContext())
            {
                if (!ActiveGamesNames().Contains(game.Name))
                {
                    game.GameStatus  = Game.GameStatusEnum.starting;
                    game.QuestionSet = db.QuestionSets.Include("Questions").Include("Questions.Answers").Where(q => q.Title == questionSet).FirstOrDefault();
                    game.NoOfRounds  = noOfRounds;
                    db.Games.Add(game);
                    db.SaveChanges();

                    var askedQuestionEntity = db.AskedQuestions.Where(q => q.GameId == game.Id).ToList();
                    var allQuestionsEntity  = db.Questions.Include("Answers").Where(q => q.QuestionSetId == game.QuestionSet.Id).ToList();
                    if (askedQuestionEntity.Count == 0)
                    {
                        foreach (Question q in allQuestionsEntity)
                        {
                            db.AskedQuestions.Add(new AskedQuestion {
                                GameId = game.Id, QuestionId = q.Id, HasBeenAsked = false
                            });
                            db.SaveChanges();
                        }
                    }



                    return(game);
                }
                else
                {
                    throw new Exception("Game name is already taken, please select an unique name.");
                }
            }
        }
Esempio n. 2
0
        public bool StartGame(Game game, Player player)
        {
            using (var db = new ConquestionDBContext())
            {
                var gameEntity   = db.Games.Include("Players").Where(g => g.Id == game.Id).FirstOrDefault();
                var playerEntity = db.Players.Where(p => p.Name.Equals(player.Name)).FirstOrDefault();

                if (playerEntity.Name.Equals(gameEntity.Players[0].Name) && gameEntity.GameStatus.Equals(Game.GameStatusEnum.starting))
                {
                    gameEntity.GameStatus = Game.GameStatusEnum.ongoing;
                    gameEntity.Rounds     = new List <Round>();
                    Round firstRound = new Round {
                        RoundNo = 1, QuestionStartTime = DateTime.Now
                    };
                    var randQuestion   = roundCtr.RetrieveRandomQuestion(gameEntity);
                    var questionEntity = db.Questions.Include("Answers").Where(r => r.Id == randQuestion.Id).FirstOrDefault();
                    firstRound.Question = questionEntity;
                    gameEntity.Rounds.Add(firstRound);
                    db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Esempio n. 3
0
        public void AddPlayer(Game game, Player player)
        {
            using (var db = new ConquestionDBContext())
            {
                var gameEntity   = db.Games.Include("Players").Where(g => g.Name.Equals(game.Name)).FirstOrDefault();
                var playerEntity = db.Players.Where(p => p.Name.Equals(player.Name)).FirstOrDefault();
                if (gameEntity != null)

                {
                    if (gameEntity.Players == null)
                    {
                        gameEntity.Players = new List <Player>();
                    }
                    if (!gameEntity.Players.Contains(playerEntity))
                    {
                        gameEntity.Players.Add(playerEntity);
                        db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
        }
Esempio n. 4
0
 public void DeleteQuestionSet(QuestionSet questionSet)
 {
     using (var db = new ConquestionDBContext())
     {
         db.QuestionSets.Remove(questionSet);
         db.SaveChanges();
     }
 }
Esempio n. 5
0
 //Definitely Keep
 public QuestionSet CreateQuestionSet(QuestionSet questionSet)
 {
     using (var db = new ConquestionDBContext())
     {
         db.QuestionSets.Add(questionSet);
         db.SaveChanges();
         return(questionSet);
     }
 }
 public Player CreatePlayer(Player player)
 {
     using (var db = new ConquestionDBContext())
     {
         db.Players.Add(player);
         db.SaveChanges();
         return(player);
     }
 }
        public void CreateRound(Game game)
        {
            using (ConquestionDBContext db = new ConquestionDBContext())
            {
                using (var transaction = db.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
                {
                    // Should be ok
                    try
                    {
                        var gameEntity = db.Games.Include("Players").Include("QuestionSet.Questions.Answers").Include("Rounds.Question.Answers")
                                         .Where(x => x.Id.Equals(game.Id))
                                         .FirstOrDefault();

                        if (gameEntity.Rounds.Count < gameEntity.NoOfRounds)
                        {
                            //Check if rounds list has been initialised
                            if (gameEntity.Rounds == null)
                            {
                                gameEntity.Rounds = new List <Round>();
                            }

                            // if there are rounds get the count and set the round number to the count + 1
                            int?noOfRounds = gameEntity.Rounds.Count();
                            var newRound   = new Round();

                            if (noOfRounds == null || noOfRounds == 0)
                            {
                                newRound.RoundNo = 1;
                            }
                            else
                            {
                                newRound.RoundNo = (int)noOfRounds + 1;
                            }

                            newRound.QuestionStartTime = DateTime.Now;
                            var question       = RetrieveRandomQuestion(gameEntity);
                            var questionEntity = db.Questions.Include("Answers").Where(q => q.Id == question.Id).FirstOrDefault();
                            newRound.Question = questionEntity;
                            gameEntity.Rounds.Add(newRound);

                            db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                        else
                        {
                        }
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        //Explicity dont rollback here because we expect deadlock situations which will be solved by SQL server auto rollbacking no need to here.
                        // transaction.Rollback();
                    }
                }
            }
        }
        public void CreateStartingRound(Game game)
        {
            // Should be ok
            var gameEntity = db.Games.Include("Players").Include("QuestionSet.Questions.Answers").Include("Map").Include("Rounds.RoundActions.Question.Answers")
                             .Where(x => x.Name.Equals(game.Name))
                             .FirstOrDefault();

            //Check if rounds list has been initialised
            if (gameEntity.Rounds == null)
            {
                gameEntity.Rounds = new List <Round>();
            }
            //Check if starting round already exists, if not create one
            var startingRound = gameEntity.Rounds.Where(r => r.RoundType.Equals(Round.RoundTypeEnum.starting)).FirstOrDefault();

            if (startingRound == null)
            {
                startingRound = new Round {
                    RoundType = Round.RoundTypeEnum.starting, RoundActions = new List <RoundAction>()
                };
                gameEntity.Rounds.Add(startingRound);
            }

            //Create firstRoundAction for starting round
            RoundAction firstRoundAction = new RoundAction
            {
                QuestionStartTime = DateTime.Now,
                MapStartTime      = DateTime.Now,
                Question          = gameEntity.QuestionSet.Questions[0]
            };

            startingRound.RoundActions.Add(firstRoundAction);

            db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }
        public Question RetrieveRandomQuestion(Game game)
        {
            using (var db = new ConquestionDBContext())
            {
                var           unaskedQuestion = db.AskedQuestions.AsNoTracking().Include("Question").Include("Question.Answers").Where(q => q.GameId == game.Id && q.HasBeenAsked == false).ToList();
                AskedQuestion question        = new AskedQuestion();
                unaskedQuestion.Shuffle();
                question = unaskedQuestion[0];

                var askedQuestionEntity = db.AskedQuestions.Where(q => q.GameId == game.Id && q.QuestionId == question.QuestionId).FirstOrDefault();
                askedQuestionEntity.HasBeenAsked    = true;
                db.Entry(askedQuestionEntity).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return(question.Question);
            }
        }
 public Game CreateGame(Game game)
 {
     if (!ActiveGamesNames().Contains(game.Name))
     {
         game.GameStatus = Game.GameStatusEnum.starting;
         db.Games.Add(game);
         db.SaveChanges();
         return(game);
     }
     else
     {
         throw new Exception("Game name is already taken, please select an unique name.");
     }
 }
        public void createGame_NameIsUniqueToActiveList()
        {
            //Arrange
            Game game = new Game {
                Name = "TestGame2"
            };

            //Act
            ctr.CreateGame(game);

            //Assert
            var gameEntity = db.Games.Where(g => g.Name.Equals(game.Name)).FirstOrDefault();

            Assert.AreEqual(game.Name, gameEntity.Name);
            db.Games.Remove(gameEntity);
            db.SaveChanges();
        }
Esempio n. 12
0
        //not sure we need these
        public void AddAnswersToAQuestion(Question question, List <Answer> answerList)
        {
            using (var db = new ConquestionDBContext())
            {
                if (question.Answers == null)
                {
                    question.Answers = new List <Answer>();
                }

                foreach (Answer a in answerList)
                {
                    question.Answers.Add(a);
                }

                db.Entry(question).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
Esempio n. 13
0
        //Not sure we need these
        public void AddQuestionsToQuestionSet(QuestionSet questionSet, List <Question> questionlist)
        {
            using (var db = new ConquestionDBContext())
            {
                if (questionSet.Questions == null)
                {
                    questionSet.Questions = new List <Question>();
                }

                foreach (Question q in questionlist)
                {
                    questionSet.Questions.Add(q);
                }

                db.Entry(questionSet).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
Esempio n. 14
0
 public bool CheckIfGameIsFinished(Game game)
 {
     using (var db = new ConquestionDBContext())
     {
         bool finished       = false;
         Game gameEntity     = db.Games.Include("Rounds").Include("Players").AsNoTracking().Where(g => g.Id == game.Id).FirstOrDefault();
         int  elapsedSeconds = (int)(DateTime.Now - gameEntity.Rounds.Last().QuestionStartTime).TotalSeconds;
         if (gameEntity.Rounds.Count() == gameEntity.NoOfRounds)
         {
             if (roundCtr.CheckIfAllPlayersAnswered(gameEntity, gameEntity.Rounds.Last()) || elapsedSeconds == 30)
             {
                 gameEntity.GameStatus      = Game.GameStatusEnum.finished;
                 db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
                 db.SaveChanges();
                 finished = true;
             }
         }
         return(finished);
     }
 }
Esempio n. 15
0
        public bool LeaveGame(Game game, Player player)
        {
            using (var db = new ConquestionDBContext())
            {
                var gameEntity   = db.Games.Include("Players").Where(g => g.Id == game.Id).FirstOrDefault();
                var playerEntity = db.Players.Where(p => p.Name.Equals(player.Name)).FirstOrDefault();

                if (gameEntity != null && playerEntity != null)
                {
                    gameEntity.Players.Remove(playerEntity);
                    db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Esempio n. 16
0
 public void AddQuestionSet(Game game, QuestionSet questionSet)
 {
     using (var db = new ConquestionDBContext())
     {
         var gameEntity        = db.Games.Where(g => g.Name.Equals(game.Name)).FirstOrDefault();
         var questionSetEntity = db.QuestionSets.Where(q => q.Title.Equals(questionSet.Title)).FirstOrDefault();
         if (gameEntity != null && questionSetEntity != null)
         {
             if (gameEntity.QuestionSet == null || gameEntity.QuestionSet.Title.Equals(""))
             {
                 gameEntity.QuestionSet     = questionSetEntity;
                 db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
                 db.SaveChanges();
             }
         }
         else
         {
             throw new Exception();
         }
     }
 }
        public void SubmitAnswer(Round round, PlayerAnswer playerAnswer)
        {
            using (var db = new ConquestionDBContext())
            {
                //This is important to compare to the question start time to see if the player answered in time.
                playerAnswer.PlayerAnswerTime = DateTime.Now;
                var playerEntity = db.Players.Where(p => p.Name.Equals(playerAnswer.Player.Name)).FirstOrDefault();
                playerAnswer.Player = playerEntity;
                var answerEntity = db.Answers.Where(a => a.Id == playerAnswer.AnswerGiven.Id).FirstOrDefault();
                playerAnswer.AnswerGiven = answerEntity;
                Round rEntity = db.Rounds.Include("PlayerAnswers.Player").Include("RoundWinner").Where(r => r.Id == round.Id).FirstOrDefault();

                // Check is the list has been initialised, if not intialise it
                if (rEntity.PlayerAnswers == null)
                {
                    rEntity.PlayerAnswers = new List <PlayerAnswer>();
                }

                //Checking if the player answers in time and that they haven't already submitted an answer
                int  elapsedSeconds      = (int)(playerAnswer.PlayerAnswerTime - rEntity.QuestionStartTime).TotalSeconds;
                bool playerHasntAnswered = true;
                if (rEntity.PlayerAnswers.Where(pa => pa.Player.Id == playerAnswer.Player.Id).FirstOrDefault() != null)
                {
                    playerHasntAnswered = false;
                }

                //Saves the player's answer to the database
                if (elapsedSeconds <= 35 && playerHasntAnswered)
                {
                    rEntity.PlayerAnswers.Add(playerAnswer);
                    if (ValidateAnswer(playerAnswer.AnswerGiven) && rEntity.RoundWinner == null)
                    {
                        rEntity.RoundWinner = playerAnswer.Player;
                    }
                    db.Entry(rEntity).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
Esempio n. 18
0
 public bool JoinGame(Game game, Player player)
 {
     using (var db = new ConquestionDBContext())
     {
         bool success      = false;
         var  playerEntity = db.Players.Where(p => p.Name.Equals(player.Name)).FirstOrDefault();
         using (var transaction = db.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
         {
             try
             {
                 var gameEntity = db.Games.Include("Players").Where(g => g.Id == game.Id).FirstOrDefault();
                 if (gameEntity != null && playerEntity != null)
                 {
                     if (gameEntity.Players.Count < 4)
                     {
                         gameEntity.Players.Add(playerEntity);
                         db.Entry(gameEntity).State = System.Data.Entity.EntityState.Modified;
                         db.SaveChanges();
                         //System.Threading.Thread.Sleep(5000);
                         transaction.Commit();
                         success = true;
                     }
                     else if (gameEntity.Players.Contains(playerEntity))
                     {
                         success = true;
                     }
                 }
             }
             catch (Exception)
             {
                 transaction.Rollback();
             }
         }
         return(success);
     }
 }
 public Player CreatePlayer(Player player)
 {
     db.Players.Add(player);
     db.SaveChanges();
     return(player);
 }
 public Map CreateMap(Map map)
 {
     db.Maps.Add(map);
     db.SaveChanges();
     return(map);
 }
 //Definitely Keep
 public QuestionSet CreateQuestionSet(QuestionSet questionSet)
 {
     db.QuestionSets.Add(questionSet);
     db.SaveChanges();
     return(questionSet);
 }