/* public members */
        public static void MakeGuess(int userId, GuessModel guessModel)
        {
            ValidateUserNumber(guessModel.Number);
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var user = GetUser(userId, context);
                var game = GetGame(guessModel.GameId, context);

                if (game.GameStatus != context.GameStatuses.First((st) => st.Status == GameStatusInProgress))
                {
                    throw new ServerErrorException("Game is not in progress", "INV_OP_GAME_STAT");
                }

                ValidateUserInGame(game, user);

                var guessNumber = guessModel.Number.ToString();
                string otherUserNumber = GetOtherUserNumber(game, user);

                int bullsCount, cowsCount;
                CountBullsAndCows(guessNumber, otherUserNumber, out bullsCount, out cowsCount);

                var guess = new Guess()
                {
                    Bulls = bullsCount,
                    Cows = cowsCount,
                    Game = game,
                    Number = guessModel.Number,
                    User = user
                };
                context.Guesses.Add(guess);

                var otherUser = (game.RedUser == user) ? game.BlueUser : game.RedUser;

                if (bullsCount == UserNumberLength)
                {
                    FinishGame(user, otherUser, game, context);
                }
                else
                {
                    game.UserInTurn = otherUser.Id;

                    MessageType guessMadeMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGuessMade);
                    string messageText = string.Format(MessageTextGuessMade, user.Nickname, guessNumber, game.Title);
                    SendMessage(messageText, otherUser, game, guessMadeMessageType, context);
                }
                context.SaveChanges();
            }
        }
Exemple #2
0
        public static void MakeGuess(int userId, GuessModel guessModel)
        {
            ValidateUserNumber(guessModel.Number);
            var context = new BullsAndCowsEntities();

            using (context)
            {
                var user = GetUser(userId, context);
                var game = GetGame(guessModel.GameId, context);

                if (game.GameStatus != context.GameStatuses.First((st) => st.Status == GameStatusInProgress))
                {
                    throw new ServerErrorException("Game is not in progress", "ERR_INV_OP");
                }

                ValidateUserInGame(game, user);

                string otherUserNumber = GetOtherUserNumber(game, user);
                var    guessNumber     = guessModel.Number.ToString();

                int bullsCount, cowsCount;
                CountBullsAndCows(guessNumber, otherUserNumber, out bullsCount, out cowsCount);

                var guess = new Guess()
                {
                    Bulls  = bullsCount,
                    Cows   = cowsCount,
                    Game   = game,
                    Number = guessModel.Number,
                    User   = user
                };
                context.Guesses.Add(guess);

                var otherUser = (game.RedUser == user) ? game.BlueUser : game.RedUser;
                if (bullsCount == UserNumberLength)
                {
                    //game finished
                    game.GameStatus = context.GameStatuses.First(st => st.Status == GameStatusFinished);

                    //set score
                    var guessesMadeCount = game.Guesses.Count(g => g.User == user);
                    var score            = new Score()
                    {
                        User  = user,
                        Value = 1000 / guessesMadeCount
                    };

                    context.Scores.Add(score);

                    //send msg
                    MessageType gameFinishedMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGameFinished);
                    string      messageTextWinner       = string.Format(MessageTextGameFinishedWinner, game.Title, otherUser.Nickname, guessesMadeCount);
                    string      messageTextLoser        = string.Format(MessageTextGameFinishedLoser, user.Nickname, game.Title);

                    SendMessage(context, messageTextWinner, game, user, gameFinishedMessageType);
                    SendMessage(context, messageTextLoser, game, otherUser, gameFinishedMessageType);
                }
                else
                {
                    game.UserInTurn = otherUser.Id;

                    MessageType guessMadeMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGuessMade);
                    string      messageText          = string.Format(MessageTextGuessMade, user.Nickname, guessNumber, game.Title);
                    SendMessage(context, messageText, game, otherUser, guessMadeMessageType);
                }
                context.SaveChanges();
            }
        }
        public static void MakeGuess(int userId, GuessModel guessModel)
        {
            ValidateUserNumber(guessModel.Number);
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var user = GetUser(userId, context);
                var game = GetGame(guessModel.GameId, context);

                if (game.GameStatus != context.GameStatuses.First((st) => st.Status == GameStatusInProgress))
                {
                    throw new ServerErrorException("Game is not in progress", "ERR_INV_OP");
                }

                ValidateUserInGame(game, user);

                string otherUserNumber = GetOtherUserNumber(game, user);
                var guessNumber = guessModel.Number.ToString();

                int bullsCount, cowsCount;
                CountBullsAndCows(guessNumber, otherUserNumber, out bullsCount, out cowsCount);

                var guess = new Guess()
                {
                    Bulls = bullsCount,
                    Cows = cowsCount,
                    Game = game,
                    Number = guessModel.Number,
                    User = user
                };
                context.Guesses.Add(guess);

                var otherUser = (game.RedUser == user) ? game.BlueUser : game.RedUser;
                if (bullsCount == UserNumberLength)
                {
                    //game finished
                    game.GameStatus = context.GameStatuses.First(st => st.Status == GameStatusFinished);

                    //set score
                    var guessesMadeCount = game.Guesses.Count(g => g.User == user);
                    var score = new Score()
                    {
                        User = user,
                        Value = 1000 / guessesMadeCount
                    };

                    context.Scores.Add(score);

                    //send msg
                    MessageType gameFinishedMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGameFinished);
                    string messageTextWinner = string.Format(MessageTextGameFinishedWinner, game.Title, otherUser.Nickname, guessesMadeCount);
                    string messageTextLoser = string.Format(MessageTextGameFinishedLoser, user.Nickname, game.Title);

                    SendMessage(context, messageTextWinner, game, user, gameFinishedMessageType);
                    SendMessage(context, messageTextLoser, game, otherUser, gameFinishedMessageType);
                }
                else
                {
                    game.UserInTurn = otherUser.Id;

                    MessageType guessMadeMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGuessMade);
                    string messageText = string.Format(MessageTextGuessMade, user.Nickname, guessNumber, game.Title);
                    SendMessage(context, messageText, game, otherUser, guessMadeMessageType);
                }
                context.SaveChanges();
            }
        }