예제 #1
0
 public static GameModelShortInfo CreateGameModelShortInfo(Game game)
 {
     return new GameModelShortInfo
     {
         Id = game.GameId,
         Name = game.Name,
         Blue = NO_BLUE_PLAYER_YET,
         Red = game.RedPlayer.UserName,
         GameState = game.State.ToString(),
         DateCreated = game.CreatedOn
     };
 }
예제 #2
0
        private IHttpActionResult ProcessGameEnd(Game game, GuessModel guess)
        {
            var redPlayer = game.RedPlayer;
            var bluePlayer = game.BluePlayer;

            if (game.RedPlayerId == guess.UserId)
            {
                redPlayer.WinsCount++;
                bluePlayer.LoosesCount++;

                game.State = GameState.WonByRed;
            }
            else
            {
                redPlayer.LoosesCount++;
                bluePlayer.WinsCount++;

                game.State = GameState.WonByBlue;
            }

            this.Data.SaveChanges();

            return Ok("Congratulations " + guess.Username + " ! You won the game!");
        }
예제 #3
0
        /// <summary>
        /// Help method after guess is made
        /// </summary>
        /// <param name="game">The current game</param>
        /// <param name="guess">User guess</param>
        /// <param name="userId">The current user id</param>
        /// <param name="onTurn">Which is on turn(red or blue player)</param>
        /// <returns></returns>
        private GuessModel GetNewGuessModel(Game game, string guess, string userId, PlayerOnTurn onTurn)
        {
            var user = this.Data.Users.All().FirstOrDefault(u => u.Id == userId);

            var numberToGuess = string.Empty;
            if (onTurn == PlayerOnTurn.Red)
            {
                numberToGuess = game.BluePlayer.NumberToGuess;
            }
            else
            {
                numberToGuess = game.Number;
            }

            var guessResult = this.resultValidator.GetResult(numberToGuess, guess, onTurn);
            var newGuess = new Guess
            {
                BullsCount = guessResult.BullsCount,
                CowsCount = guessResult.CowsCount,
                DateMade = DateTime.Now,
                UserId = userId,
                User = user,
                Game = game,
                GameId = game.GameId,
                Number = guess
            };

            if (onTurn == PlayerOnTurn.Red)
            {
                game.State = GameState.BlueInTurn;
            }
            else if (onTurn == PlayerOnTurn.Blue)
            {
                game.State = GameState.RedInTurn;
            }

            this.Data.Guesses.Add(newGuess);
            this.Data.SaveChanges();

            return GuessModel.FromGuess(newGuess);
        }
예제 #4
0
        /// <summary>
        /// Method for getting the detailed information about a game
        /// </summary>
        /// <param name="game">Current game which is played</param>
        /// <param name="userId">The current user id</param>
        /// <returns>Returns detailed information about a game</returns>
        private GameModelDetailedInfo GetGameModelDetailedInfo(Game game, string userId)
        {
            var yourNumber = game.Number;
            var yourColor = "red";
            var yourGuesses = game.RedPlayer.Guesses.Select(GuessModel.FromGuess);
            var opponentGuesses = game.BluePlayer.Guesses.Select(GuessModel.FromGuess);

            if (game.BluePlayerId == userId)
            {
                yourGuesses = game.BluePlayer.Guesses.Select(GuessModel.FromGuess);
                opponentGuesses = game.RedPlayer.Guesses.Select(GuessModel.FromGuess);
                yourNumber = game.BluePlayer.NumberToGuess;
                yourColor = "blue";
            }

            var gameDetails = new GameModelDetailedInfo
            {
                Id = game.GameId,
                Name = game.Name,
                DateCreated = game.CreatedOn,
                Red = game.RedPlayer.UserName,
                Blue = game.BluePlayer.UserName,
                YourNumber = yourNumber,
                YourGuesses = yourGuesses,
                OpponentGuesses = opponentGuesses,
                YourColor = yourColor,
                GameState = game.State.ToString()
            };

            return gameDetails;
        }
예제 #5
0
        public IHttpActionResult Post(GameDataModel game)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(this.ModelState);
            }

            //if (this.Data.Games.All().FirstOrDefault(g => g.Number == game.Number) != null)
            //{
            //    return BadRequest("There is already a game with that number. You can not create games with equal game numbers.");
            //}

            if (!this.resultValidator.IsPlayerGuessNumberInputValid(game.Number))
            {
                return BadRequest(NUMBER_ERROR_MESSAGE);
            }

            var newGame = new Game()
            {
                State = GameState.WaitingForOpponent,
                Name = game.Name,
                Number = game.Number,
                CreatedOn = DateTime.Now
            };

            newGame.RedPlayerId = this.userIdProvider.GetUserId();
            newGame.RedPlayer = this.Data.Users.All().Where(u => u.Id == newGame.RedPlayerId).FirstOrDefault();

            this.Data.Games.Add(newGame);
            this.Data.SaveChanges();

            var gameInfoToReturn = GameModelShortInfo.CreateGameModelShortInfo(newGame);

            return Created(this.Url.ToString(), gameInfoToReturn);
        }
예제 #6
0
        public IHttpActionResult Create(GameModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }

            var currentUserId = this.User.Identity.GetUserId();
            var currentUser = this.data
              .Users
              .All()
              .Where(x => x.Id == currentUserId)
              .FirstOrDefault();

            var game = new Game()
            {
                Name = model.Name,
                RedPlayer = currentUser,
                RedPlayerGuess = model.Number,
                DateCreated = DateTime.Now,
            };

            this.data.Games.Add(game);
            currentUser.Games.Add(game);
            this.data.SaveChanges();

            model.Id = game.Id;
            model.GameState = game.State.ToString();
            model.Red = currentUser.UserName;
            model.DateCreated = game.DateCreated;

            return Ok(model);
        }