private static void FinishGame(User user, User otherUser, Game game, BullsAndCowsEntities context)
        {
            game.GameStatus = context.GameStatuses.First(st => st.Status == GameStatusFinished);

            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);
            SendMessage(messageTextWinner, user, game, gameFinishedMessageType, context);

            string messageTextLoser = string.Format(MessageTextGameFinishedLoser, user.Nickname, game.Title);
            SendMessage(messageTextLoser, otherUser, game, gameFinishedMessageType, context);
        }
Exemplo n.º 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();
            }
        }