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));
            }
        }
Exemple #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);
            }
        }
        public MoveResultModel MakeMove(int userId, int gameId, int position)
        {
            using (var ctx = new FightFleetDataContext())
            {
                var game = ctx.Games.First(c => c.GameId == gameId);
                if (game.Player1Id != userId && (game.Player2Id == null || game.Player2Id != userId))
                {
                    return new MoveResultModel {
                               MoveResult = MoveResult.InvalidGame.ToString()
                    }
                }
                ;

                var board = game.Boards.First(c => c.UserId != userId);

                var moves = ctx.Moves.OrderByDescending(c => c.CreatedDate).Where(c => c.GameId == gameId).ToList();

                //first we check all moves to make sure it's this person's turn
                if (moves.Count != 0 && moves.ElementAt(0).UserId == userId)
                {
                    return new MoveResultModel {
                               MoveResult = MoveResult.NotYourTurn.ToString()
                    }
                }
                ;

                //now we only look at their moves
                moves = moves.Where(c => c.UserId == userId).ToList();

                if (moves.Any(c => c.Position == position))
                {
                    return new MoveResultModel {
                               MoveResult = MoveResult.PositionAlreadyPlayed.ToString()
                    }
                }
                ;

                ctx.Moves.InsertOnSubmit(new Move
                {
                    CreatedDate = DateTime.Now,
                    UserId      = userId,
                    GameId      = gameId,
                    Position    = position,
                });


                var boardData = PopulateBoardData(board, moves, userId);

                if (boardData.IndexOf('1') < 0)
                {
                    game.GameStatusId = (int)GameStatus.Finished;
                }


                ctx.SubmitChanges();

                return(new MoveResultModel
                {
                    GameStatus = ((GameStatus)game.GameStatusId).ToString(),
                    MoveResult = board.BoardDataArray[position] == "0" ? MoveResult.Miss.ToString() : MoveResult.Hit.ToString(),
                    Xcoord = position % 10,
                    Ycoord = position / 10
                });
            }
        }
Exemple #4
0
        public MoveResultModel MakeMove(int userId, int gameId, int position)
        {
            using (var ctx = new FightFleetDataContext())
            {
                var game = ctx.Games.First(c => c.GameId == gameId);
                if(game.Player1Id != userId && (game.Player2Id == null || game.Player2Id != userId))
                    return new MoveResultModel{ MoveResult = MoveResult.InvalidGame.ToString() };

                var board = game.Boards.First(c => c.UserId != userId);

                var moves = ctx.Moves.OrderByDescending(c => c.CreatedDate).Where(c => c.GameId == gameId).ToList();

                //first we check all moves to make sure it's this person's turn
                if (moves.Count != 0 && moves.ElementAt(0).UserId == userId)
                    return new MoveResultModel{ MoveResult = MoveResult.NotYourTurn.ToString() };

                //now we only look at their moves
                moves = moves.Where(c => c.UserId == userId).ToList();

                if (moves.Any(c => c.Position == position))
                    return new MoveResultModel{ MoveResult = MoveResult.PositionAlreadyPlayed.ToString() };

                ctx.Moves.InsertOnSubmit(new Move
                {
                    CreatedDate = DateTime.Now,
                    UserId = userId,
                    GameId = gameId,
                    Position = position,
                });

                var boardData = PopulateBoardData(board, moves, userId);

                if (boardData.IndexOf('1') < 0)
                    game.GameStatusId = (int)GameStatus.Finished;

                ctx.SubmitChanges();

                return new MoveResultModel
                {
                    GameStatus = ((GameStatus)game.GameStatusId).ToString(),
                    MoveResult = board.BoardDataArray[position] == "0" ? MoveResult.Miss.ToString() : MoveResult.Hit.ToString(),
                    Xcoord = position % 10,
                    Ycoord = position / 10
                };

            }
        }