public GameSimpleModel(Game game)
 {
     this.Id = game.Id;
     this.Name = game.Name;
     this.Red = game.Red.UserName;
     this.DateCreated = game.DateCreated;
     this.GameState = game.GameState.ToString();
     this.Blue = "No blue player yet";
 }
        public void MakeNewGameWhenRedPlayerNumberIsMinusOneShoudReturn400()
        {
            var unitOfwork = Mock.Create<IApplicationData>();

            var game = new Game { RedPlayerNumber = -1 };

            var server = new InMemoryHttpServer(InMemoryServerUrl, unitOfwork);

            var response = server.CreatePostRequest(ApiArticles, game);

            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public void MakeNewGameShoudReturn201AndLocationHeader()
        {
            var unitOfwork = Mock.Create<IApplicationData>();

            var article = new Game
                              {
                                  Name = "validName"
                              };

            Mock.Arrange(() => unitOfwork.Games.Add(Arg.IsAny<Game>())).Returns(() => article);

            var server = new InMemoryHttpServer(InMemoryServerUrl, unitOfwork);

            var response = server.CreatePostRequest(ApiArticles, article);

            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            Assert.IsNotNull(response.Headers.Location);
        }
        private Game[] GenerateValidItems(int itemsCount = 15)
        {
            Game[] itemsSet = new Game[itemsCount];

            for (int i = 0; i < itemsCount; i++)
            {
                var currentItem = new Game()
                {
                    Id = i * 5,
                    Name = string.Format("Test Name: {0}", i),
                    DateCreated = DateTime.Now.AddDays(RandomNumber.Next(10)),
                    Red = new User() { UserName = string.Format("Test Name: {0}", i) },
                    GameState = GameState.WaitingForOpponent,
                    Blue = new User(),
                };

                itemsSet[i] = currentItem;
            }

            return itemsSet;
        }
        public GameModelDetails(Game game, string color)
        {
            this.Id = game.Id;
            this.Name = game.Name;
            this.DateCreated = game.DateCreated;
            this.Red = game.RedPlayer.UserName;
            this.Blue = game.BluePlayer.UserName;
            if (color == "red")
            {
                this.YourNumber = game.RedPlayerNumber;
                this.YourGuesses = game.RedPlayerGuesses.AsQueryable().Select(GuessModel.FromGuess).ToArray();
                this.OpponentGuesses = game.BluePlayerGuesses.AsQueryable().Select(GuessModel.FromGuess).ToArray();
                this.YourColor = "red";
            }
            else
            {
                this.YourNumber = game.BluePlayerNumber;
                this.YourGuesses = game.BluePlayerGuesses.AsQueryable().Select(GuessModel.FromGuess).ToArray();
                this.OpponentGuesses = game.RedPlayerGuesses.AsQueryable().Select(GuessModel.FromGuess).ToArray();
                this.YourColor = "blue";
            }

            this.GameState = game.GameState.ToString();
        }
        public IHttpActionResult CreateGame(CreateGameModel gameModel)
        {
            if(!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (gameModel.Number.ToString().Length != 4)
            {
                return this.BadRequest("Please enter number with 4 digits");
            }

            if (ContainsDuplicates(gameModel.Number))
            {
                return this.BadRequest("Your number should consist of 4 different digits");
            }

            var userId = this.User.Identity.GetUserId();

            var game = new Game
            {
                Name = gameModel.Name,
                RedPlayerId = userId,
                RedPlayerNumber = gameModel.Number,
                GameState = GameState.WaitingForOpponent,
                DateCreated = DateTime.Now,
                isFinished = false
            };

            var addedGame = this.ApplicationData.Games.Add(game);
            this.ApplicationData.SaveChanges();

            var gameModelToSend = new GameModel();

            gameModelToSend.Name = addedGame.Name;
            gameModelToSend.Id = addedGame.Id;
            gameModelToSend.Blue = "No blue player yet";
            gameModelToSend.Red = this.User.Identity.Name;
            gameModelToSend.GameState = GameState.WaitingForOpponent.ToString(); ;
            gameModelToSend.DateCreated = addedGame.DateCreated;

            return Created("api/games", gameModelToSend);
        }
        public IHttpActionResult CreateGame(CreateGameModel item)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var userId = this.userIdProvider.GetUserId();
            var currentUser = this.data.Users.Find(userId);

            // Checks if the current user exists. It's a token bug.
            if (currentUser == null)
            {
                return this.BadRequest(string.Format(INVALID_ITEM_FORMAT, "user"));
            }

            if (!this.IsValidNumber(item.Number))
            {
                return this.BadRequest("Invalid Number");
            }

            var newItem = new Game()
            {
                Name = item.Name,
                GameState = GameState.WaitingForOpponent,
                DateCreated = DateTime.Now,
                Red = currentUser,
                RedPlayerNumber = item.Number
            };

            this.data.Games.Add(newItem);
            this.data.SaveChanges();

            var returnItem = new GameSimpleModel(newItem);

            return this.Ok(returnItem);
        }