public GuessModel(Guess resultGuess, ApplicationUser user)
 {
     this.Id = resultGuess.Id;
     this.UserId = user.Id;
     this.Username = user.UserName;
     this.GameId = resultGuess.GameId;
     this.Number = resultGuess.Number;
     this.DateMade = resultGuess.DateMade;
     this.BullsCount = resultGuess.BullsCount;
     this.CowsCount = resultGuess.CowsCount;
 }
        private void ChechForWinner(MakeGuessModel guess, Game game, ApplicationUser user)
        {
            if (user.Id == game.RedId && guess.Number == game.BlueNumber)
            {
                game.GameState = GameState.Finished;
                this.SendWinnerNotification(user, game);
            }

            if (user.Id == game.BlueId && guess.Number == game.RedNumber)
            {
                game.GameState = GameState.Finished;
                this.SendWinnerNotification(user, game);
            }
        }
        private static IEnumerable<ApplicationUser> GenerateValidPlayer(int count)
        {
            var users = new ApplicationUser[count];
            for (var index = 0; index < count; index++)
            {
                var user = new ApplicationUser
                {
                    Id = "asfasfafas" + index,
                    UserName = "******" + index,
                    Email = "*****@*****.**" + index,
                    UserLossesCount = index * 2 + 1,
                    UserWinsCount = index * 3,
                };

                users[index] = user;
            }

            return users;
        }
        public OnGoingGameDetailsModel(ApplicationUser user, Game game)
        {
            this.Id = game.Id;
            this.Name = game.Name;
            this.DateCreated = game.DateCreated;
            this.Blue = game.Blue.UserName;
            this.Red = game.Red.UserName;

            if (user == game.Red)
            {
                this.YourNumber = game.RedNumber;
                this.YourGuesses = game.RedGuesses.AsQueryable().Select(GuessModel.FromGuess).ToArray();
            }

            if (user == game.Blue)
            {
                this.YourNumber = game.BlueNumber;
                this.YourGuesses = game.BlueGuesses.AsQueryable().Select(GuessModel.FromGuess).ToArray();
            }
        }
        public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var info = await Authentication.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return InternalServerError();
            }

            var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);
            if (!result.Succeeded)
            {
                return GetErrorResult(result); 
            }
            return Ok();
        }
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            return Ok();
        }
        private void SendWinnerNotification(ApplicationUser user, Game game)
        {
            var otherUser = game.Red == user ? game.Blue : game.Red;

            var newnotification = new Notification
            {
                User = otherUser,
                Message = string.Format(YouLostTo, user.UserName, game.Name),
                State = NotificationState.Unread,
                DateCreated = DateTime.Now,
                Game = game,
                GameId = game.Id,
                Type = NotificationType.GameLost,
            };


            user.Notifications.Add(new Notification
                                       {
                                           User = user,
                                           Message =
                                               string.Format(YouHaveWon, otherUser.UserName, game.Name),
                                           State = NotificationState.Unread,
                                           DateCreated = DateTime.Now,
                                           Game = game,
                                           GameId = game.Id,
                                           Type = NotificationType.GameWon,
                                       });

            otherUser.Notifications.Add(newnotification);
            this.BullsAndCowsData.SaveChanges();
        }
 private bool CheckTurn(Game game, ApplicationUser user)
 {
     return (game.Red == user && game.GameState == GameState.BlueInTurn) || (game.Blue == user && game.GameState == GameState.RedInTurn);
 }
 private bool CheckIfAValidPlayer(Game game, ApplicationUser user)
 {
     return game.RedId != user.Id && game.BlueId != user.Id;
 }
        private void SendNotification(ApplicationUser user, Game game)
        {
            var otherUser = game.Red == user ? game.Blue : game.Red;

            var newnotification = new Notification
            {
                User = otherUser,
                Message =
                    string.Format(ItsYourTurn, game.Name),
                State = NotificationState.Unread,
                DateCreated = DateTime.Now,
                Game = game,
                GameId = game.Id,
                Type = NotificationType.YourTurn,
            };

            otherUser.Notifications.Add(newnotification);
            this.BullsAndCowsData.SaveChanges();
        }
        private Guess BuildGuess(MakeGuessModel guess, Game game, ApplicationUser user)
        {
            var bullCount = 0;
            var cowCount = 0;

            if (user == game.Red)
            {
                GameManager.CountBullsAndCows(guess.Number, game.BlueNumber, out bullCount, out cowCount);
            }

            if (user == game.Blue)
            {
                GameManager.CountBullsAndCows(guess.Number, game.RedNumber, out bullCount, out cowCount);
            }

            var newGuess = new Guess
            {
                DateMade = DateTime.Now,
                Number = guess.Number,
                BullsCount = bullCount,
                CowsCount = cowCount,
                User = user,
                Game = game,
            };


            if (user == game.Red)
            {
                this.BullsAndCowsData.Games.All().First().RedGuesses.Add(newGuess);
                game.GameState = GameState.BlueInTurn;

            }

            if (user == game.Blue)
            {
                this.BullsAndCowsData.Games.All().First().BlueGuesses.Add(newGuess);
                game.GameState = GameState.RedInTurn;
            }

            this.SendNotification(user, game);
            this.BullsAndCowsData.SaveChanges();
            return newGuess;
        }
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

            var result = await this.UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return this.GetErrorResult(result);
            }

            return this.Ok();
        }
 public ScoreModel(ApplicationUser user)
 {
     this.Rank = 100 * user.UserWinsCount + 15 * user.UserLossesCount;
     this.Username = user.UserName;
 }