/// <summary> /// Starts a round if certain requirements are met /// </summary> /// <param name="game">The game to start a new round for</param> /// <param name="commander">The new round's commander</param> /// <returns>If a round was successfully started</returns> public Boolean Execute(Entities.Game game, Entities.User commander) { Boolean successful = false; //Check to make sure game still has required number of players if (game.HasRequiredNumberOfPlayers()) { //Check to make sure the game has not been ended. if (game.GameOver.HasValue == false && !game.HasWinner()) { Entities.GameRound round = _insertGameRound.Execute(game.GameID, commander); successful = round.GameRoundID > 0; if (successful) { game.Rounds.Add(round); game.RoundCount++; //Deal Cards _dealCards.Execute(game); } } } return(successful); }
private void SendWinnerSelected(Entities.Game game, Entities.GameRound round, IEnumerable <Entities.ActiveConnection> connections, List <Entities.GamePlayer> users) { Entities.GamePlayer sendToPlayer = null; foreach (Entities.ActiveConnection connection in connections) { sendToPlayer = users.FirstOrDefault(player => player.User.UserId == connection.User_UserId); if (sendToPlayer != null) { Entities.Models.Game.Board.GameBoard model = GetGameBoardModal(connection, game); Entities.Models.Game.Board.Answers answersModel = new Entities.Models.Game.Board.Answers(true, model.IsCommander, false, false, false, true, round.GroupedAnswers()); //The round history tab repurposed this to be the winner of the round when the page is loaded //so setting this here so that when pushed into the observable array it will look correct round.CardCommander = round.Winner(); _hub.Clients.Client(connection.ActiveConnectionID) .WinnerSelected(answersModel, model, game.IsWaiting(), game.HasWinner(), round); } } }
/// <summary> /// Play a list of cards from a user's hand /// </summary> /// <param name="cardIDs">The card IDs the user has selected </param> /// <param name="gameID">The game ID in which the user wants to play the card</param> /// <param name="userId">The user Id</param> /// <param name="autoPlayed">Were these cards auto played</param> /// <returns>PlayCard action result containing any errors and the round the card was played.</returns> public Entities.ActionResponses.PlayCard Execute(List <Int32> cardIDs, Int32 gameID, Int32 userId, Boolean autoPlayed) { Entities.ActionResponses.PlayCard playResponse = new Entities.ActionResponses.PlayCard(); List <Entities.GamePlayerCard> cards = _selectGamePlayerCard.Execute(gameID, userId); //Check that the user did not play cards they do not have List <Int32> missedIds = CheckHand(cards, cardIDs); if (missedIds.Count == 0) { Entities.GameRound round = _selectGameRound.Execute(gameID, true); //Validate the user isn't trying to answer multiple times if (round.HasAnswer(userId) == false) { //Validate the correct number of cards were played if (round.ValidateCardPlayedCount(cardIDs.Count)) { //Create GameRoundCards for played cards List <Entities.GameRoundCard> playedCards = CreateRoundCards(cardIDs, userId, round.GameRoundID, gameID, autoPlayed); //Insert playedCards _insertGameRoundCard.Execute(playedCards); if (autoPlayed) { playResponse.AutoPlayedSuccess = true; } //Select round with game cards round = _selectGameRound.Execute(gameID, true); //Remove cards from player's hand _deleteGamePlayerCard.Execute(cardIDs, gameID, userId); playResponse.CurrentRound = round; playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.Success; } else { playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.InvalidNumberOfCardsPlayed; } } else { playResponse.CurrentRound = round; playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.Success; } } else { playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.InvalidCardPlayed; } return(playResponse); }
/// <summary> /// Insert new game round /// </summary> /// <param name="gameID">The game ID for the new round</param> /// <param name="commander">The round's card commander</param> /// <returns>The inserted round</returns> public Entities.GameRound Execute(Int32 gameID, Entities.User commander) { Entities.GameRound round = new Entities.GameRound(); round.GameID = gameID; round.CardCommander = commander; _insertGameRound.Execute(round); return round; }
/// <summary> /// Insert new game round /// </summary> /// <param name="gameID">The game ID for the new round</param> /// <param name="commander">The round's card commander</param> /// <returns>The inserted round</returns> public Entities.GameRound Execute(Int32 gameID, Entities.User commander) { Entities.GameRound round = new Entities.GameRound(); round.GameID = gameID; round.CardCommander = commander; _insertGameRound.Execute(round); return(round); }
/// <summary> /// Complete the current round /// </summary> /// <param name="gameID">The ID of the game that contains the round</param> /// <param name="cardIDs">The IDs of the winning cards</param> /// <param name="userId">The user Id trying to complete the round</param> /// <returns></returns> public Entities.ActionResponses.RoundComplete Execute(Int32 gameID, List <Int32> cardIDs, Int32 userId) { Entities.ActionResponses.RoundComplete response = new Entities.ActionResponses.RoundComplete(); Entities.GameRound currentRound = _selectGameRound.Execute(gameID, true); //Validate that the user trying to complete the round is in fact the commander if (currentRound.IsCommander(userId)) { //Validate that select cards were actually played during the round List <Int32> invalidWinners = currentRound.ValidateWinnerSelection(cardIDs); if (invalidWinners.Count == 0) { Entities.User newCommander = currentRound.Winner(); //Update cards as winners Entities.Filters.GameRoundCard.UpdateWinner cardfilter = new Entities.Filters.GameRoundCard.UpdateWinner(); cardfilter.CardIDs = cardIDs; cardfilter.GameID = gameID; Boolean autoPlayed = _updateGameRoundCard.Execute(cardfilter); if (!autoPlayed) { //Update player points Entities.Filters.GamePlayer.UpdatePoints playerFilter = new Entities.Filters.GamePlayer.UpdatePoints(); playerFilter.GameID = gameID; playerFilter.UserId = newCommander.UserId; _updateGamePlayer.Execute(playerFilter); } //Start round Entities.Filters.Game.Select gameFilter = new Entities.Filters.Game.Select(); gameFilter.GameID = gameID; gameFilter.DataToSelect = Entities.Enums.Game.Select.GamePlayerCards; Entities.Game game = _selectGame.Execute(gameFilter); response.NewRoundCreated = _startGameRoud.Execute(game, game.NextCommander(newCommander)); response.CompletedRound = currentRound; response.Game = game; if (!response.NewRoundCreated) { response.Game.Rounds.Add(currentRound); } } } return(response); }
/// <summary> /// Selects the current round for a game /// </summary> /// <param name="filter">Filter used to select game rounds</param> /// <returns>The current round</returns> public Entities.GameRound Execute(Entities.Filters.GameRound.SelectCurrent filter) { Entities.GameRound round = _selectGameRound.Execute(filter); if (filter.SelectCards && round != null) { round.CardsPlayed = _selectGameRoundCards.Execute(round.GameRoundID); } return(round); }
/// <summary> /// Insert new game round /// </summary> /// <param name="round">Round to insert</param> public void Execute(Entities.GameRound round) { using (DbCommand cmd = _db.GetStoredProcCommand("GameRound_Insert")) { _db.AddInParameter(cmd, "@Started", DbType.DateTime, DateTime.UtcNow); _db.AddInParameter(cmd, "@Game_GameID", DbType.Int32, round.GameID); _db.AddInParameter(cmd, "@CardCommander_UserId", DbType.Int32, round.CardCommander.UserId); _db.AddOutParameter(cmd, "NewID", DbType.Int32, sizeof(Int32)); _db.ExecuteScalar(cmd); round.GameRoundID = Int32.Parse(_db.GetParameterValue(cmd, "NewID").ToString()); } }
/// <summary> /// Update the game view when the commander has selected the winner of the round /// </summary> /// <param name="game">The game to update</param> /// <param name="round">The game's current round</param> /// <param name="sendToSpectators">Should this update go to spectators</param> public void SendWinnerSelected(Entities.Game game, Entities.GameRound round, Boolean sendToSpectators) { Entities.Filters.ActiveConnection.SelectAll filter = new Entities.Filters.ActiveConnection.SelectAll(); filter.GroupName = String.Format("Game_{0}", game.GameID); List <Entities.ActiveConnection> connections = _selectActiveConnection.Execute(filter); SendWinnerSelected(game, round, connections.Where(x => x.ConnectionType == Entities.Enums.ConnectionType.GamePlayer), game.Players); if (sendToSpectators) { SendWinnerSelected(game, round, connections.Where(x => x.ConnectionType == Entities.Enums.ConnectionType.GameSpectator), game.Spectators); } }
/// <summary> /// Play cards for all players that have yet to play /// </summary> /// <param name="gameID">The game ID</param> public void Execute(int gameID) { Entities.Filters.Game.Select gameFilter = new Entities.Filters.Game.Select(); gameFilter.DataToSelect = Entities.Enums.Game.Select.GamePlayerCards | Entities.Enums.Game.Select.Rounds; gameFilter.GameID = gameID; Entities.Game game = _selectGame.Execute(gameFilter); Entities.GameRound round = game.CurrentRound(); List <Entities.GamePlayer> players = game.Players.Where(x => !round.HasAnswer(x.User.UserId) && game.IsCurrentPlayer(x.User.UserId) && !game.IsCurrentCommander(x.User.UserId)) .Select(x => x).ToList(); Int32 selectCount = 1 + (Int32)round.Question.Instructions; Random rdm = new Random(); List <Int32> cardIDs = null; Entities.ActionResponses.PlayCard response = null; foreach (Entities.GamePlayer player in players) { cardIDs = player.Hand.OrderBy(x => rdm.Next()).Take(selectCount).Select(x => x.CardID).ToList(); response = _playCard.Execute(cardIDs, gameID, player.User.UserId, true); if (response.AutoPlayedSuccess) { if (player.IdlePlayCount + 1 == 3) { //Player is forced to leave game now UnityConfig.Container.Resolve <AppServices.Game.Base.ILeave>().Execute(gameID, player.User, Entities.Enums.GamePlayerType.Player, true); } else { Entities.Filters.GamePlayer.UpdateIdlePlayCount idlePlayCount = new Entities.Filters.GamePlayer.UpdateIdlePlayCount { GameID = gameID, UserId = player.User.UserId }; _updateGamePlayer.Execute(idlePlayCount); } } } }
private Entities.GameRoundCard CreateQuestion(IEnumerable <Entities.Card> cards, Entities.Game game) { Entities.Card card = cards.FirstOrDefault(); Entities.GameRoundCard question = null; if (card != null) { Entities.GameRound round = game.CurrentRound(); question = new Entities.GameRoundCard(card, round.CardCommander.UserId, round.GameRoundID, game.GameID); } return(question); }
/// <summary> /// Constructor /// </summary> /// <param name="round"></param> /// <param name="showQuestion"></param> public RoundQuestion(Entities.GameRound round, Boolean showQuestion) { if (round == null) { Question = new ArmedCards.Entities.Card { Type = ArmedCards.Entities.Enums.Card.CardType.Question }; } else { Question = round.Question; } this.ShowQuestion = showQuestion; this.Instructions = (Int32)Question.Instructions; }
/// <summary> /// Get the next commander /// </summary> /// <param name="newCommander">The winner of the last round</param> /// <returns></returns> public Entities.User NextCommander(Entities.User newCommander) { if (newCommander == null) { Entities.GameRound current = CurrentRound(); if (current != null) { newCommander = current.Winner(); } } if (newCommander == null || (newCommander != null && Players.Any(x => x.User.UserId == newCommander.UserId) == false)) { newCommander = Players.OrderByDescending(x => x.Points).First().User; } return(newCommander); }
/// <summary> /// Insert new game round /// </summary> /// <param name="round">Round to insert</param> public void Execute(Entities.GameRound round) { _insertGameRound.Execute(round); }
/// <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); } }