/// <summary> /// Removes a player from the game /// </summary> /// <param name="gameID">The ID of the game to leave</param> /// <param name="user">The user leaving the game</param> /// <param name="playerType">Type of player leaving</param> /// <param name="forcedToLeave">The player was forced to leave</param> public void Execute(Int32 gameID, Entities.User user, Entities.Enums.GamePlayerType playerType, Boolean forcedToLeave = false) { Entities.Filters.Game.Select filter = new Entities.Filters.Game.Select(); filter.GameID = gameID; filter.DataToSelect = Entities.Enums.Game.Select.Rounds | Entities.Enums.Game.Select.GamePlayerCards; Entities.Game game = _selectGame.Execute(filter); Boolean wasWaiting = game.IsWaiting(); Boolean wasCurrentCommander = game.IsCurrentCommander(user.UserId) && playerType == Entities.Enums.GamePlayerType.Player; Entities.GamePlayer player = null; if (playerType == Entities.Enums.GamePlayerType.Player) { player = game.Players.Find(x => x.User.UserId == user.UserId); game.Players.Remove(player); game.PlayerCount--; } else { player = game.Spectators.Find(x => x.User.UserId == user.UserId); game.Spectators.Remove(player); game.SpectatorCount--; } if (wasCurrentCommander) { if (game.HasRounds()) { Entities.GameRound current = game.CurrentRound(); if (!current.HasWinner()) { game.Rounds.Remove(current); game.RoundCount--; Entities.Filters.GameRound.Delete deleteRoundFilter = new Entities.Filters.GameRound.Delete(); deleteRoundFilter.GameRoundID = current.GameRoundID; _deleteRound.Execute(deleteRoundFilter); } } Boolean started = false; if (game.PlayerCount > 0) { started = _startRound.Execute(game, game.NextCommander(null)); } if (game.HasRounds() && started) { _sendMessage.CommanderLeft(game, user.DisplayName); } else { _sendMessage.UpdateGame(game, true); } } else if (game.IsWaiting() && wasWaiting) { _sendMessage.UpdateWaiting(game, true); } else { if (game.HasRounds()) { Entities.GameRound current = game.CurrentRound(); current.CurrentPlayerCount--; if (!game.Players.Any(x => x.Hand.Count > 0 && !current.IsCommander(x.User.UserId))) { if (!current.HasWinner() && current.Answers.Count == 0) { _dealCards.Execute(game, false); } } } _sendMessage.UpdateGame(game, true, (forcedToLeave ? (int?)user.UserId : null)); } _leaveGame.Execute(gameID, user, playerType); }
/// <summary> /// Constructor /// </summary> /// <param name="game"></param> /// <param name="userId"></param> /// <param name="hand"></param> /// <param name="playerType"></param> /// <param name="voteToKickList"></param> /// <param name="completedRounds"></param> public GameBoard(Entities.Game game, Int32 userId, Entities.Enums.GamePlayerType playerType, List <Models.Game.Board.VoteToKick> voteToKickList = null, List <Entities.GameRound> completedRounds = null) { Game = game; UserId = userId; Hand = new List <GamePlayerCard>(); if (playerType == Enums.GamePlayerType.Player) { Entities.GamePlayer player = Game.Players.FirstOrDefault(x => x.User.UserId == userId); if (player != null && player.Hand != null) { Hand = player.Hand; } } Hand = playerType == Enums.GamePlayerType.Player ? Game.Players.First(x => x.User.UserId == userId).Hand : new List <GamePlayerCard>(); ActivePlayer = Hand.Count > 0; PlayerType = playerType; Entities.GameRound round = Game.CurrentRound(); if (Game.HasRounds() && round != null) { Answered = round.HasAnswer(UserId); ShowAnswers = round.PlayedCount >= round.CurrentPlayerCount && round.Answers.Count > 0; RoundHasWinner = round.Winner() != null; GroupedAnswers = round.GroupedAnswers(); IsCommander = Game.IsCurrentCommander(UserId) && PlayerType == Entities.Enums.GamePlayerType.Player; } else { Answered = false; ShowAnswers = false; RoundHasWinner = false; IsCommander = false; } ShowHand = ActivePlayer && !Answered && !IsCommander && PlayerType == Entities.Enums.GamePlayerType.Player; ShowWaiting = (round == null || RoundHasWinner) && Game.IsWaiting(); WaitingOnAllAnswersOrWinner = !RoundHasWinner && !ShowAnswers; ShowBoard = !ShowWaiting && !Game.HasWinner(); if (ShowBoard && round != null) { Question = round.Question; } else { Question = null; } AnswersViewModel = new Answers(RoundHasWinner, IsCommander, WaitingOnAllAnswersOrWinner, ShowAnswers, ShowHand, ShowBoard, GroupedAnswers); GameOverViewModel = new GameOver(Game.HasWinner(), Game.GameID, Game.Players); HandViewModel = new Board.Hand(ShowHand, Hand, ShowBoard); LobbyViewModel = new Lobby(PlayerType, Game.Players, Game.MaxNumberOfSpectators > 0, Game.Spectators); RoundQuestionViewModel = new RoundQuestion(round, ShowBoard); WaitingViewModel = new Waiting(ShowWaiting); VotesToKickViewModel = new VotesToKick(voteToKickList ?? new List <Models.Game.Board.VoteToKick>()); AllRoundsViewModel = new AllRounds(completedRounds ?? new List <GameRound>()); HeaderViewModel = new Shared.Header(); if (Game.HasWinner()) { HeaderViewModel.SubHeaderText = "Game Over, man!"; } else if (ShowWaiting) { HeaderViewModel.SubHeaderText = ArmedCards.Entities.Models.Helpers.WaitingHeader.Build(Game, UserId, PlayerType); } else { HeaderViewModel.SubHeaderText = String.Format("{0} is the Card Commander", Game.DetermineCommander().DisplayName); } }