Ejemplo n.º 1
0
        public GameModel CreateGame(int userId)
        {
            Game game;
            var  generator = new RandomlyGenerateBoard();

            generator.Generate();

            using (var ctx = new FightFleetDataContext())
            {
                var pendingGame = ctx.Games.FirstOrDefault(c => c.Player1Id != userId &&
                                                           (c.Player2Id == null || c.Player2Id != userId) &&
                                                           c.GameStatusId == (int)GameStatus.Pending);
                if (pendingGame != null)
                {
                    game = pendingGame;


                    pendingGame.Player2Id    = userId;
                    pendingGame.GameStatusId = (int)GameStatus.InProgress;
                    ctx.SubmitChanges();
                }
                else
                {
                    game = new Game
                    {
                        Player1Id    = userId,
                        GameStatusId = (int)GameStatus.Pending,
                        CreatedDate  = DateTime.Now
                    };
                    ctx.Games.InsertOnSubmit(game);
                }
                game.Boards.Add(new Board
                {
                    UserId    = userId,
                    BoardData = generator.Board.ToString()
                });
                ctx.SubmitChanges();

                return(GetGameModel(game.GameId, userId));
            }
        }
Ejemplo n.º 2
0
        public GameModel CreateGame(int userId)
        {
            Game game;
            var generator = new RandomlyGenerateBoard();
            generator.Generate();

            using (var ctx = new FightFleetDataContext())
            {
                var pendingGame = ctx.Games.FirstOrDefault(c => c.Player1Id != userId &&
                                                            (c.Player2Id == null || c.Player2Id != userId) &&
                                                            c.GameStatusId == (int)GameStatus.Pending);
                if (pendingGame != null)
                {
                    game = pendingGame;

                    pendingGame.Player2Id = userId;
                    pendingGame.GameStatusId = (int)GameStatus.InProgress;
                    ctx.SubmitChanges();
                }
                else
                {
                    game = new Game
                    {
                        Player1Id = userId,
                        GameStatusId = (int)GameStatus.Pending,
                        CreatedDate = DateTime.Now
                    };
                    ctx.Games.InsertOnSubmit(game);
                }
                game.Boards.Add(new Board
                    {
                        UserId = userId,
                        BoardData = generator.Board.ToString()
                    });
                ctx.SubmitChanges();

                return GetGameModel(game.GameId, userId);
            }
        }