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) }); }
public static GameStageInfo GetRelativeGameStageInfo(this GameStageInfo gameStageInfo, PlayerGroupInfo playerGroupInfo, string playerId) { var isCurrentTeamTeam1 = playerGroupInfo.GetTeamOfPlayer(playerId).TeamId == playerGroupInfo.Team1.TeamId; var relativePlayerGroupInfo = playerGroupInfo.GetRelativePlayerGroupInfo(playerId); var relativeGameStageInfo = (GameStageInfo)(gameStageInfo?.Clone()); if (relativeGameStageInfo.CurrentPlayer != null) { relativeGameStageInfo.CurrentPlayer = playerGroupInfo.GetRelativePlayerInfo(playerId, gameStageInfo.CurrentPlayer.PlayerId); } if (relativeGameStageInfo.CurrentBetActionList != null) { foreach (var betAction in relativeGameStageInfo.CurrentBetActionList) { betAction.PlayerInfo = playerGroupInfo.GetRelativePlayerInfo(playerId, betAction.PlayerInfo.PlayerId); } } if (relativeGameStageInfo?.CurrentBetResult?.BettingTeam != null) { relativeGameStageInfo.CurrentBetResult.BettingTeam = playerGroupInfo.GetRelativeTeamInfo(playerId, gameStageInfo.CurrentBetResult.BettingTeam.TeamId); } if (relativeGameStageInfo?.StickRoundInfoList != null) { foreach (var stickRoundInfo in relativeGameStageInfo.StickRoundInfoList) { if (stickRoundInfo?.PlayActionList != null) { foreach (var playAction in stickRoundInfo.PlayActionList) { playAction.PlayerInfo = playerGroupInfo.GetRelativePlayerInfo(playerId, playAction.PlayerInfo.PlayerId); } } if (stickRoundInfo?.StickResult?.Winner != null) { stickRoundInfo.StickResult.Winner = playerGroupInfo.GetRelativePlayerInfo(playerId, stickRoundInfo.StickResult.Winner.PlayerId); } } } if (relativeGameStageInfo?.CurrentPlayResult != null) { relativeGameStageInfo.CurrentPlayResult.PlayerGroupInfo = relativePlayerGroupInfo; if (!isCurrentTeamTeam1) { relativeGameStageInfo.CurrentPlayResult.Team1Score = gameStageInfo.CurrentPlayResult.Team2Score; relativeGameStageInfo.CurrentPlayResult.Team2Score = gameStageInfo.CurrentPlayResult.Team1Score; } } if (relativeGameStageInfo?.RoundResultList != null) { foreach (var roundResult in relativeGameStageInfo?.RoundResultList) { if (roundResult != null) { roundResult.PlayerGroupInfo = relativePlayerGroupInfo; roundResult.WinningTeam = playerGroupInfo.GetRelativeTeamInfo(playerId, roundResult.WinningTeam.TeamId); if (!isCurrentTeamTeam1) { var team1Score = roundResult.Team2FinalScore; var team2Score = roundResult.Team1FinalScore; roundResult.Team1FinalScore = team1Score; roundResult.Team2FinalScore = team2Score; } } } } if (relativeGameStageInfo?.GameResult != null) { relativeGameStageInfo.GameResult.PlayerGroupInfo = relativePlayerGroupInfo; relativeGameStageInfo.GameResult.WinningTeam = playerGroupInfo.GetRelativeTeamInfo(playerId, relativeGameStageInfo.GameResult.WinningTeam.TeamId); if (!isCurrentTeamTeam1) { var team1Score = relativeGameStageInfo.GameResult.Team2FinalScore; var team2Score = relativeGameStageInfo.GameResult.Team1FinalScore; relativeGameStageInfo.GameResult.Team1FinalScore = team1Score; relativeGameStageInfo.GameResult.Team2FinalScore = team2Score; } } return(relativeGameStageInfo); }