コード例 #1
0
        public IHttpActionResult Create(CreateGameModel createGame)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var userId = this.User.Identity.GetUserId();
            var user = this.BullsAndCowsData.Users.All().FirstOrDefault(u => u.Id == userId);

            var newGame = new Game
                              {
                                  RedId = userId, 
                                  Red = user, 
                                  DateCreated = DateTime.Now, 
                                  Name = createGame.Name, 
                                  GameState = GameState.WaitingForOpponent, 
                                  RedNumber = createGame.Number
                              };

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

            var createdGameModel = new GameModel(newGame);

            return this.CreatedAtRoute("DefaultApi", new { id = createdGameModel.Id }, createdGameModel);
        }
コード例 #2
0
 public GameDetailsDataModel(Game game)
 {
     this.ID = game.ID;
     this.Name = game.Name;
     this.DateCreated = game.DateCreated;
     this.GameState = game.GameState;
     this.Blue = game.BlueUser;
     this.Red = game.RedUser;
 }
コード例 #3
0
 public GameModel(Game newGame)
 {
     this.DateCreated = newGame.DateCreated;
     this.GameState = newGame.GameState.ToString();
     this.Id = newGame.Id;
     this.Name = newGame.Name;
     this.Red = newGame.Red.UserName;
     this.Blue = NoBluePlayerYet;
 }
コード例 #4
0
        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<Game> GenerateValidGames(int count)
        {
            var games = new Game[count];
            for (var index = 0; index < count; index++)
            {
                var game = new Game
                {
                    Id = index,
                };

                games[index] = game;
            }

            return games;
        }
コード例 #6
0
        public Game Create(string userId, string gameName, string playerNumber)
        {
            Game gameToCreate = new Game
            {
                RedUserId = userId,
                Name = gameName,
                RedPlayerNumber = playerNumber,
                GameState = GameState.WaitingForOpponent,
                DateCreated = DateTime.Now
            };

            this.gamesRepo.Add(gameToCreate);
            this.gamesRepo.SaveChanges();

            var newGameId = gameToCreate.GameId;
            var response = this.GetById(newGameId);

            return response;
        }
コード例 #7
0
        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();
            }
        }
コード例 #8
0
        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();
        }
コード例 #9
0
 private bool CheckTurn(Game game, ApplicationUser user)
 {
     return (game.Red == user && game.GameState == GameState.BlueInTurn) || (game.Blue == user && game.GameState == GameState.RedInTurn);
 }
コード例 #10
0
 private bool CheckIfAValidPlayer(Game game, ApplicationUser user)
 {
     return game.RedId != user.Id && game.BlueId != user.Id;
 }
コード例 #11
0
        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();
        }
コード例 #12
0
        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;
        }
コード例 #13
0
        private void CreateNotificationForTurn(Game game)
        {

            ApplicationUser user;

            if (game.GameState == GameState.BlueInTurn)
            {
                user = game.Blue;
            }
            else
            {
                user = game.Red;
            }

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

            user.Notifications.Add(newnotification);
            this.BullsAndCowsData.SaveChanges();
        }