示例#1
0
 public Move AddMove(Move move)
 {
     using (var db = new HeartsEntities())
     {
         if (db.Games.FirstOrDefault(x => x.GameId == move.GameId) == null)
         {
             throw new Exception("Game not found.");
         }
         if (db.Users.FirstOrDefault(x => x.UserId == move.PlayerId) == null)
         {
             throw new Exception("User not found.");
         }
         if (db.Games.FirstOrDefault(
                 x => x.GameId == move.GameId && (
                     x.Player1 != move.PlayerId || x.Player2 != move.PlayerId ||
                     x.Player3 != move.PlayerId || x.Player4 != move.PlayerId)) == null)
         {
             throw new Exception("User not in game.");
         }
         db.Moves.Add(move);
         db.SaveChanges();
         return(move);
     }
 }
示例#2
0
        public void UpdateGameAfterMove(Game update_game, int userId)
        {
            using (var db = new HeartsEntities())
            {
                var game = db.Games.FirstOrDefault(x => x.GameId == update_game.GameId);
                if (game == null)
                {
                    throw new CustomException("Invalid Game.");
                }

                game.Turn         = update_game.Turn;
                game.LeadingSuit  = update_game.LeadingSuit;
                game.HeartsPlayed = update_game.HeartsPlayed;
                game.SpadesPlayed = update_game.SpadesPlayed;

                game.Player1Hand = update_game.Player1Hand;
                game.Player2Hand = update_game.Player2Hand;
                game.Player3Hand = update_game.Player3Hand;
                game.Player4Hand = update_game.Player4Hand;

                if (game.PassOrPlay == 2)
                {
                    game.Player1Stash = update_game.Player1Stash;
                    game.Player2Stash = update_game.Player2Stash;
                    game.Player3Stash = update_game.Player3Stash;
                    game.Player4Stash = update_game.Player4Stash;

                    game.Player1Score = update_game.Player1Score;
                    game.Player2Score = update_game.Player2Score;
                    game.Player3Score = update_game.Player3Score;
                    game.Player4Score = update_game.Player4Score;

                    game.Player1Card = update_game.Player1Card;
                    game.Player2Card = update_game.Player2Card;
                    game.Player3Card = update_game.Player3Card;
                    game.Player4Card = update_game.Player4Card;
                }

                else if (game.PassOrPlay == 1)
                {
                    if (userId == game.Player1)
                    {
                        game.Player1Trash = update_game.Player1Trash;
                    }
                    if (userId == game.Player2)
                    {
                        game.Player2Trash = update_game.Player2Trash;
                    }
                    if (userId == game.Player3)
                    {
                        game.Player3Trash = update_game.Player3Trash;
                    }
                    if (userId == game.Player4)
                    {
                        game.Player4Trash = update_game.Player4Trash;
                    }

                    if (game.Player1Trash != null && game.Player2Trash != null && game.Player3Trash != null && game.Player4Trash != null)
                    {
                        if (game.Player1Trash.Length > 1 && game.Player2Trash.Length > 1 && game.Player3Trash.Length > 1 && game.Player4Trash.Length > 1)
                        {
                            // Passing the trash
                            if (game.GameRound.Value == 1)
                            {
                                game.Player1Hand += game.Player4Trash;
                                game.Player2Hand += game.Player1Trash;
                                game.Player3Hand += game.Player2Trash;
                                game.Player4Hand += game.Player3Trash;
                            }
                            else if (game.GameRound.Value == 2)
                            {
                                game.Player4Hand += game.Player1Trash;
                                game.Player3Hand += game.Player4Trash;
                                game.Player2Hand += game.Player3Trash;
                                game.Player1Hand += game.Player2Trash;
                            }
                            else if (game.GameRound.Value == 3)
                            {
                                game.Player1Hand += game.Player3Trash;
                                game.Player3Hand += game.Player1Trash;
                                game.Player2Hand += game.Player4Trash;
                                game.Player4Hand += game.Player2Trash;
                            }
                            game.Player1Trash = null;
                            game.Player2Trash = null;
                            game.Player3Trash = null;
                            game.Player4Trash = null;

                            game.PassOrPlay = 2;
                        }
                    }
                }

                db.SaveChanges();
            }
        }