Ejemplo n.º 1
0
        public static GameStateModel GetGameState(int gameId, int userId)
        {
            var context = new BullsAndCowsEntities();

            using (context)
            {
                var user = GetUser(userId, context);
                var game = GetGame(gameId, context);

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

                if (game.RedUser != user && game.BlueUser != user)
                {
                    throw new ServerErrorException("User not in game", "ERR_INV_GAME");
                }

                var gameStateModel = new GameStateModel()
                {
                    Id                = (int)game.Id,
                    Title             = game.Title,
                    RedPlayer         = game.RedUser.Nickname,
                    BluePlayer        = game.BlueUser.Nickname,
                    PlayerInTurn      = (game.RedUser.Id == game.UserInTurn) ? "red" : "blue",
                    RedPlayerGuesses  = ParseGuessesToModels(game.Guesses.Where(g => g.User == game.RedUser)),
                    BluePlayerGuesses = ParseGuessesToModels(game.Guesses.Where(g => g.User == game.BlueUser))
                };
                return(gameStateModel);
            }
        }
        public static GameStateModel GetGameState(int gameId, int userId)
        {
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var user = GetUser(userId, context);
                var game = GetGame(gameId, context);

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

                if (game.RedUser != user && game.BlueUser != user)
                {
                    throw new ServerErrorException("User not in game", "ERR_NOT_IN_GAME");
                }

                var gameStateModel = new GameStateModel()
                {
                    Id = (int)game.Id,
                    Title = game.Title,
                    RedPlayer = game.RedUser.Nickname,
                    BluePlayer = game.BlueUser.Nickname,
                    PlayerInTurn = (game.RedUser.Id == game.UserInTurn) ? "red" : "blue",
                    RedPlayerGuesses = ParseGuessesToModels(game.Guesses.Where(g => g.User == game.RedUser)),
                    BluePlayerGuesses = ParseGuessesToModels(game.Guesses.Where(g => g.User == game.BlueUser))
                };
                return gameStateModel;
            }
        }