Ejemplo n.º 1
0
        /// <summary>
        /// method generates a new gameSession instance for the user,
        /// if they user has an existing session reset it to a new session.
        /// </summary>
        /// <param name="Username"></param>
        /// <returns>gameSessionModel</returns>
        public GameSessionModel CreateNewGameForUser(string Username)
        {
            // this is used to know wether we need to use the update or the add method as using the
            // add method with an existing session will cause a primary key duplicate.
            bool fromDb = true;

            GameSessionModel gameSession = _gameRepo.GetCurrentGame(Username);

            if (gameSession == null)
            {
                gameSession = new GameSessionModel();
                fromDb      = false;
            }

            gameSession.Player      = _userService.GetUserByUsername(Username);
            gameSession.Currentword = _wordsRepo.GetFiveLetterWord().Word;
            gameSession.Guesses     = 0;
            gameSession.LastGuess   = DateTime.Now;
            gameSession.Score       = 0;

            if (fromDb)
            {
                _gameRepo.UpdateGameSession(gameSession);
                _gameRepo.SaveChanges();
            }
            else
            {
                _gameRepo.AddGameSession(gameSession);
                _gameRepo.SaveChanges();
            }

            if (_gameRepo.SaveChanges())
            {
                return(gameSession);
            }

            return(null);
        }