示例#1
0
 private void UpdatePlayers(bool calculateActions = true)
 {
     foreach (var player in PlayerGroupInfo.GetPlayerList())
     {
         var playerGameInfo = GetPlayerGameInfo(player.PlayerId, calculateActions);
         ClientApiDictionary[player.PlayerId].SetPlayerGameInfo(playerGameInfo);
     }
 }
示例#2
0
        private PlayerInfo GetRandomPlayer()
        {
            var playerList = PlayerGroupInfo.GetPlayerList();

            if (!playerList.Any())
            {
                return(null);
            }
            var randomIndex = _random.Next(playerList.Count - 1);

            return(playerList[randomIndex]);
        }
示例#3
0
        public GameRound(
            Rules rules,
            PlayerGroupInfo playerGroup,
            Action confirmAction,
            PlayerInfo initialPlayer,
            CardPile deck)
        {
            Rules           = rules;
            PlayerGroupInfo = playerGroup;
            ConfirmAction   = confirmAction;
            InitialPlayer   = initialPlayer;
            var playerList = PlayerGroupInfo.GetPlayerList(InitialPlayer.PlayerId);

            PlayerHandDictionary = playerList
                                   .ToDictionary(p => p.PlayerId, p => new CardPile(deck.Draw(9)));
            BetStage           = new BetStage(Rules, PlayerGroupInfo, PlayerHandDictionary, InitialPlayer);
            ExpectedActionType = ActionType.BetAction;
        }
示例#4
0
        public bool StartGame()
        {
            if (GameStage != null)
            {
                return(false);
            }
            if (PlayerGroupInfo.GetPlayerList().Count != 4)
            {
                return(false);
            }
            if (!CheckAndSetBusy())
            {
                return(false);
            }

            RunBusyAction(() =>
            {
                try {
                    GameStage = new GameStage(GameInfo.Rules, PlayerGroupInfo, ConfirmAction);
                }
                catch (Exception e)
                {
                    Log.Error($"Exception in StartGame: {e.Message}", e);
                    throw;
                }
                try {
                    UpdatePlayers();
                }
                catch (Exception e)
                {
                    Log.Error($"Exception in UpdatePlayers: {e.Message}", e);
                    throw;
                }
            });
            return(true);
        }
示例#5
0
        public PlayResult GetPlayResult()
        {
            var isOver = StickRoundList.Count == 9 && CurrentStickRound.StickResult != null;

            if (!isOver)
            {
                return(null);
            }
            var playerResultDictionary = PlayerGroupInfo
                                         .GetPlayerList()
                                         .ToDictionary(p => p, p =>
            {
                var wonCards = StickRoundList
                               .Select(r => r.StickResult)
                               .Where(r => r.Winner == p)
                               .SelectMany(r => r.StickPile)
                               .ToList();
                return(wonCards);
            });
            var playersWithSticks = playerResultDictionary
                                    .Where(p => p.Value.Any())
                                    .Select(p => p.Key)
                                    .ToList();
            var generalPlayer = playersWithSticks.Count == 1
                ? playersWithSticks.Single()
                : null;

            if (generalPlayer != null)
            {
                //Case General
                var generalAmount = new ScoreAmount(isMatch: true, isGeneral: true);
                var zeroAmount    = new ScoreAmount();
                return(new PlayResult
                {
                    PlayerGroupInfo = PlayerGroupInfo,
                    Team1Score = PlayerGroupInfo.GetTeamOfPlayer(generalPlayer.PlayerId) == PlayerGroupInfo.Team1
                        ? generalAmount
                        : zeroAmount,
                    Team2Score = PlayerGroupInfo.GetTeamOfPlayer(generalPlayer.PlayerId) == PlayerGroupInfo.Team2
                        ? generalAmount
                        : zeroAmount
                });
            }
            var teamResultDictionary = playerResultDictionary
                                       .GroupBy(p => PlayerGroupInfo.GetTeamOfPlayer(p.Key.PlayerId))
                                       .ToDictionary(g => g.Key, g => g.ToList().SelectMany(p => p.Value).ToList());
            var teamsWithSticks = teamResultDictionary
                                  .Where(p => p.Value.Any())
                                  .Select(p => p.Key)
                                  .ToList();
            var matchTeam = teamsWithSticks.Count == 1
                ? teamsWithSticks.Single()
                : null;

            if (matchTeam != null)
            {
                //Case Match
                var matchAmount = new ScoreAmount(isMatch: true);
                var zeroAmount  = new ScoreAmount();
                return(new PlayResult
                {
                    PlayerGroupInfo = PlayerGroupInfo,
                    Team1Score = PlayerGroupInfo.GetTeamOfPlayer(generalPlayer.PlayerId) == PlayerGroupInfo.Team1
                        ? matchAmount
                        : zeroAmount,
                    Team2Score = PlayerGroupInfo.GetTeamOfPlayer(generalPlayer.PlayerId) == PlayerGroupInfo.Team2
                        ? matchAmount
                        : zeroAmount
                });
            }
            //Case no General/Match
            var team1Amount = teamResultDictionary[PlayerGroupInfo.Team1]
                              .Select(c => c.GetValue(PlayType))
                              .Sum();
            var team2Amount = teamResultDictionary[PlayerGroupInfo.Team2]
                              .Select(c => c.GetValue(PlayType))
                              .Sum();
            var lastStickTeam = PlayerGroupInfo.GetTeamOfPlayer(StickRoundList.Last().StickResult.Winner.PlayerId);

            if (lastStickTeam == PlayerGroupInfo.Team1)
            {
                team1Amount += 5;
            }
            if (lastStickTeam == PlayerGroupInfo.Team2)
            {
                team2Amount += 5;
            }
            return(new PlayResult
            {
                PlayerGroupInfo = PlayerGroupInfo,
                Team1Score = new ScoreAmount(team1Amount),
                Team2Score = new ScoreAmount(team2Amount)
            });
        }