public PresentationService(LudoContext ludoContext)
 {
     this.ludoContext = ludoContext;
     Menu             = new Menu(ludoContext);
     GameConsole      = new GameConsole();
     ScoreBoards      = new List <ScoreBoard>();
 }
Esempio n. 2
0
        // 6. Assign turn to Player and Decide if he should Roll Dice or Move
        private void SetGameFlow()
        {
            if (GameDetails.GameProgress.CurrentPlayerDiceRolled)
            {
                List <PiecePosition> piecePositions = GetAllPiecesValidMoves();

                // Check if Current Player has any move
                if (piecePositions.Count > 1)
                {
                    HighlightPiecesOfCurrentPlayer();
                }
                else if (piecePositions.Count == 1)
                {
                    MovePiece(piecePositions[0].Piece);
                }
                else
                {
                    Player nextPlayer = this.GetNextPlayer();
                    if (nextPlayer != null)
                    {
                        using (LudoContext ludoContext = new LudoContext())
                        {
                            GameService gameService = new GameService(ludoContext);
                            gameService.UpdateCurrentPlayer(GameDetails.Game.Id, nextPlayer.Id);
                        }
                    }
                    Load();
                }
            }
        }
Esempio n. 3
0
        private void dice_Rolled(object sender, DiceRollEventArgs e)
        {
            using (LudoContext ludoContext = new LudoContext())
            {
                GameService gameService = new GameService(ludoContext);

                ServiceResponse response = gameService.DiceRolled(GameId);
            }
            Load();
        }
Esempio n. 4
0
        public ServiceResponse Start()
        {
            int currentPlayerId = 0;
            // Assign positions to Pieces - GamePlayerPiecePosition
            List <GamePlayer> gamePlayers = LudoContext.GamePlayers
                                            .Where(c => c.GameId == Game.Id).ToList();

            List <GamePlayerPiecePosition> gamePlayerPiecePositions = new List <GamePlayerPiecePosition>();

            foreach (GamePlayer gamePlayer in gamePlayers)
            {
                AddGamePlayerPiecePosition(gamePlayerPiecePositions, gamePlayer.PlayerId, gamePlayer.Quadrant);

                if (gamePlayer.Quadrant == 0)
                {
                    currentPlayerId = gamePlayer.PlayerId;
                }
            }

            LudoContext.GamePlayerPiecePositions.AddRange(gamePlayerPiecePositions);

            // Change GameStatusId - Games
            LudoContext.Games
            .Single(c => c.Id == Game.Id)
            .GameStatusId = 2;

            // Create New Entry in GameProgress
            GameProgress gameProgress = new GameProgress
            {
                GameId                  = Game.Id,
                CurrentPlayerId         = currentPlayerId,
                CurrentPlayerDiceRolled = false,
                LastActionDateTime      = DateTime.Now
            };

            LudoContext.GameProgresses.Add(gameProgress);

            LudoContext.DiceStacks.Add(new DiceStack
            {
                GameId     = Game.Id,
                DiceValue1 = -1,
                DiceValue2 = -1,
                DiceValue3 = -1
            });

            GameDetails gameDetails = new GameDetails(Game, gamePlayers, gamePlayerPiecePositions, gameProgress);

            LudoContext.SaveChanges();

            return(new ServiceResponse
            {
                Data = gameDetails,
                Messages = new List <Message>()
            });
        }
Esempio n. 5
0
        // 1. Fetch Game Details
        private GameDetails GetGameDetails(int gameId)
        {
            using (LudoContext ludoContext = new LudoContext())
            {
                GameService gameService = new GameService(ludoContext);

                ServiceResponse response = gameService.GetGameDetails(gameId);

                return(response.Data as GameDetails);
            }
        }
Esempio n. 6
0
        public void AddPlayer(int playerId, int quadrant)
        {
            LudoContext.GamePlayers.Add(
                new GamePlayer
            {
                GameId   = Game.Id,
                PlayerId = playerId,
                Quadrant = quadrant
            });

            LudoContext.SaveChanges();
        }
Esempio n. 7
0
        public ServiceResponse GameOver(int gameId)
        {
            Game game = LudoContext.Games.Single(c => c.Id == gameId);

            game.GameStatusId = 3;

            LudoContext.SaveChanges();

            return(new ServiceResponse
            {
                Data = game,
                Messages = new List <Message>()
            });
        }
        public void StoreNewGame()
        {
            //arrange
            LudoContext context = new LudoContext();
            Game game = new Game();
            context = new LudoContext();
            game.HasFinished = true;
            game.WinnerPlayerName = "Osborn";

            //act
            context.Game.Add(game);
            context.SaveChanges();

            //assert
            Assert.Equal("Osborn", context.Game.Where()
        }
Esempio n. 9
0
        public ServiceResponse DiceRolled(int gameId)
        {
            int diceValue = GetDiceRandomValue();

            GameProgress gameProgress = LudoContext.GameProgresses
                                        .Single(c => c.GameId == gameId);

            gameProgress.CurrentPlayerDiceRolled = true;
            gameProgress.LastDiceValue           = diceValue;
            gameProgress.LastActionDateTime      = DateTime.Now;

            DiceStack diceStack = LudoContext.DiceStacks
                                  .Single(c => c.GameId == gameId);

            if (diceStack.DiceValue1 == -1)
            {
                diceStack.DiceValue1 = diceValue;
                diceStack.DiceValue2 = -1;
                diceStack.DiceValue3 = -1;
            }
            else if (diceStack.DiceValue2 == -1)
            {
                diceStack.DiceValue2 = diceValue;
                diceStack.DiceValue3 = -1;
            }
            else if (diceStack.DiceValue3 == -1)
            {
                diceStack.DiceValue3 = diceValue;
            }
            else
            {
                diceStack.DiceValue1 = diceValue;
                diceStack.DiceValue2 = -1;
                diceStack.DiceValue3 = -1;
            }

            // Check if Current Player has any move, else assign the next player as Current Player

            LudoContext.SaveChanges();

            return(new ServiceResponse
            {
                Data = diceValue,
                Messages = new List <Message>()
            });
        }
Esempio n. 10
0
        public ServiceResponse TakeOpponentPiece(int gameId, int opponentPlayerId, int pieceNumber)
        {
            GamePlayerPiecePosition gamePlayerPiecePosition =
                LudoContext.GamePlayerPiecePositions.Single(c => c.GameId == gameId && c.PlayerId
                                                            == opponentPlayerId && c.PieceNumber == pieceNumber);

            GamePlayer gamePlayer = LudoContext.GamePlayers.Single(c => c.GameId == gameId && c.PlayerId == opponentPlayerId);

            gamePlayerPiecePosition.GhorPosition = pieceNumber;
            gamePlayerPiecePosition.GhorType     = Util.GetGhorTypeFromEnum(GhorType.Home);
            gamePlayerPiecePosition.Quadrant     = gamePlayer.Quadrant;

            LudoContext.SaveChanges();

            return(new ServiceResponse
            {
                Data = gamePlayerPiecePosition,
                Messages = new List <Message>()
            });
        }
Esempio n. 11
0
        public ServiceResponse Create()
        {
            Game = new Game
            {
                GameType        = "Basic",
                CreatedByUserId = GetCreateByUserId(),
                GameStatusId    = 1
            };

            Game.CreatedDateTime = DateTime.Now;

            LudoContext.Games.Add(Game);
            LudoContext.SaveChanges();

            AddPlayer(GetCreateByUserId(), 0);

            return(new ServiceResponse
            {
                Data = Game,
                Messages = new List <Message>()
            });
        }
Esempio n. 12
0
        private void MovePiece(Piece piece)
        {
            // Show Transition
            List <GameBoardPosition> positions = this.CheckValidMove(CurrentPlayer, piece);

            if (positions.Count > 0)
            {
                Player nextPlayer = GetNextPlayer();
                if (nextPlayer != null)
                {
                    using (LudoContext ludoContext = new LudoContext())
                    {
                        GameService gameService = new GameService(ludoContext);
                        gameService.MovePiece(GameId, CurrentPlayer.Id,
                                              piece.Position, positions[positions.Count - 1], nextPlayer.Id);
                    }
                }
                piece.GameBoardPosition   = positions[positions.Count - 1];
                piece.TransitionPositions = positions;
                ShowTransitions(piece);
            }
        }
Esempio n. 13
0
        public ServiceResponse UpdateCurrentPlayer(int gameId, int playerId)
        {
            GameProgress gameProgress = LudoContext.GameProgresses.Single(c => c.GameId == gameId);

            gameProgress.CurrentPlayerId         = playerId;
            gameProgress.CurrentPlayerDiceRolled = false;

            // Clear Dice Stack
            DiceStack diceStack = LudoContext.DiceStacks
                                  .Single(c => c.GameId == gameId);

            diceStack.DiceValue1 = -1;
            diceStack.DiceValue2 = -1;
            diceStack.DiceValue3 = -1;

            LudoContext.SaveChanges();

            return(new ServiceResponse
            {
                Data = gameProgress,
                Messages = new List <Message>()
            });
        }
Esempio n. 14
0
 public async Task Update(LudoContext ludoContext)
 {
     LastAction = DateTime.Now;
     ludoContext.Game.Update(this);
     await ludoContext.SaveChangesAsync();
 }
Esempio n. 15
0
        public ServiceResponse MovePiece(int gameId, int currentPlayerId, int pieceNumber,
                                         Ludo.UI.Class.GameBoardPosition position, int nextPlayerId)
        {
            GamePlayerPiecePosition gamePlayerPiecePosition =
                LudoContext.GamePlayerPiecePositions.Single(c => c.GameId == gameId && c.PlayerId
                                                            == currentPlayerId && c.PieceNumber == pieceNumber);

            gamePlayerPiecePosition.GhorPosition = position.Ghor.Position;
            gamePlayerPiecePosition.GhorType     = Util.GetGhorTypeFromEnum(position.Ghor.GhorType);
            gamePlayerPiecePosition.Quadrant     = position.Quadrant.QuadrantPosition;

            GameProgress gameProgress = LudoContext.GameProgresses.Single(c => c.GameId == gameId);

            // Here check the Dice Stack
            DiceStack diceStack = LudoContext.DiceStacks
                                  .Single(c => c.GameId == gameId);

            if (gameProgress.LastDiceValue == 6)
            {
                if (diceStack.DiceValue3 == 6)
                {
                    gameProgress.CurrentPlayerId         = nextPlayerId;
                    diceStack.DiceValue1                 = -1;
                    diceStack.DiceValue2                 = -1;
                    diceStack.DiceValue3                 = -1;
                    gameProgress.CurrentPlayerDiceRolled = false;
                }
                else
                {
                    gameProgress.CurrentPlayerDiceRolled = false;
                }
            }
            else
            {
                gameProgress.CurrentPlayerId         = nextPlayerId;
                diceStack.DiceValue1                 = -1;
                diceStack.DiceValue2                 = -1;
                diceStack.DiceValue3                 = -1;
                gameProgress.CurrentPlayerDiceRolled = false;
            }
            gameProgress.LastActionDateTime = DateTime.Now;

            //Take opponent pice
            List <GamePlayerPiecePosition> otherPlayerPiecePositions =
                LudoContext.GamePlayerPiecePositions.Where(c => c.GameId == gameId && c.PlayerId
                                                           != currentPlayerId).ToList();

            foreach (GamePlayerPiecePosition piecePosition in otherPlayerPiecePositions)
            {
                if (piecePosition.GhorPosition == gamePlayerPiecePosition.GhorPosition &&
                    piecePosition.Quadrant == gamePlayerPiecePosition.Quadrant)
                {
                    GamePlayer gamePlayer = LudoContext.GamePlayers
                                            .Single(c => c.GameId == gameId && c.PlayerId == piecePosition.PlayerId);

                    piecePosition.Quadrant     = gamePlayer.Quadrant;
                    piecePosition.GhorPosition = piecePosition.PieceNumber;
                    piecePosition.GhorType     = Util.GetGhorTypeFromEnum(GhorType.Home);

                    // Continue Current Player turn
                    gameProgress.CurrentPlayerId         = currentPlayerId;
                    gameProgress.CurrentPlayerDiceRolled = false;
                }
            }

            LudoContext.SaveChanges();

            return(new ServiceResponse
            {
                Data = gamePlayerPiecePosition,
                Messages = new List <Message>()
            });
        }
Esempio n. 16
0
 // Inject DBContext into the Constructor
 public GameService(LudoContext ludoContext)
 {
     LudoContext = ludoContext;
 }
Esempio n. 17
0
 public Menu(LudoContext ludoContext) => this.ludoContext = ludoContext;
Esempio n. 18
0
 public static Game Load(LudoContext ludoContext) =>
 ludoContext.Game
 .Include(b => b.Board)
 .ThenInclude(p => p.Pieces)
 .ThenInclude(a => a.Player)
 .FirstOrDefault(g => g.Completed == false);
Esempio n. 19
0
 public void Save(LudoContext ludoContext)
 {
     LastAction = DateTime.Now;
     ludoContext.Game.Add(this);
     ludoContext.SaveChanges();
 }