public GameState GetGameState(ClaimsPrincipal user) { lock (gameLogicLock) { GameState gameState = new GameState(); gameState.UserTurn = GetCurrentPlayerTurn(); gameState.User = GetPlayer(user); gameState.Claims = CurrentClaims; gameState.Players = GameUsers; if (CurrentClaims.Count > 0) { var recentClaimIndex = GameCard .Ranks .FindIndex((item) => { return(item == CurrentClaims.Last().Claims[0].Rank); }); gameState.LowerBoundRank = GetLowerBoundRank(recentClaimIndex); gameState.UpperBoundRank = GetUpperBoundRank(recentClaimIndex); gameState.MiddleBoundRank = GetMiddleBoundRank(recentClaimIndex); } gameState.CurrentGamePhase = CurrentGamePhase; if (TurnTimer != null) { gameState.TurnPhaseDuration = TurnTimer.Interval; } if (CallTimer != null) { gameState.CallPhaseDuration = CallTimer.Interval; } if (PreparationTimer != null) { gameState.PreparationPhaseDuration = PreparationTimer.Interval; } gameState.ActionHistory = ActionHistory; return(gameState); } }
public void SubmitDefaultClaim() { var currentTurnUser = GetCurrentPlayerTurn(); var defaultClaims = new List <GameCard>(); var defaultActual = new List <GameCard>(); var firstUserCard = currentTurnUser.Hand.First().Value; defaultActual.Add(new GameCard() { Rank = firstUserCard.Rank, Suit = firstUserCard.Suit }); if (CurrentClaims.Count > 0) { var recentClaimIndex = GameCard .Ranks .FindIndex((item) => { return(item == CurrentClaims.Last().Claims[0].Rank); }); var middleRank = GetMiddleBoundRank(recentClaimIndex); defaultClaims.Add(new GameCard() { Rank = middleRank }); } else { var rank = GameCard.Ranks[0]; defaultClaims.Add(new GameCard() { Rank = rank }); } SubmitClaim(currentTurnUser.UserPrincipal, defaultClaims, defaultActual); }
public void SubmitClaim(ClaimsPrincipal user, List <GameCard> claims, List <GameCard> actual) { lock (gameLogicLock) { if (!IsCurrentTurn(user)) { throw new Exception("Somebody may be cheating"); } if (claims.Count != actual.Count) { throw new Exception("Somebody may be cheating"); } if (TurnTimer != null) { TurnTimer.Stop(); TurnTimer.Dispose(); } var referenceCard = claims[0]; foreach (var card in claims) { if (card.Rank != referenceCard.Rank) { throw new Exception("Somebody may be cheating"); } } if (CurrentClaims.Count > 0) { if (CurrentClaims.Last().ClaimUser.UserPrincipal.GetUserLoginID() == user.GetUserLoginID()) { throw new Exception("Somebody may be cheating"); } } if (CurrentClaims.Count > 0) { var recentClaimIndex = GameCard .Ranks .FindIndex((item) => { return(item == CurrentClaims.Last().Claims[0].Rank); }); var lowerBoundRank = GetLowerBoundRank(recentClaimIndex); var upperBoundRank = GetUpperBoundRank(recentClaimIndex); var middleBoundRank = GetMiddleBoundRank(recentClaimIndex); if ( referenceCard.Rank != lowerBoundRank && referenceCard.Rank != middleBoundRank && referenceCard.Rank != upperBoundRank) { throw new Exception("Somebody may be cheating"); } } var gameUser = GetPlayer(user); CurrentClaims.Add(new GameClaim(claims, actual, gameUser)); foreach (var card in actual) { gameUser.Hand.Remove(card); } ActionHistory.Add($"{user.GetUserName()} has submitted {claims.Count} cards, with claim {referenceCard.Rank}"); } }
private GameCheat GameCheat() { var currentUser = GetCurrentPlayerTurn(); var cheatCallSuccessful = false; var cheatCaller = GetCheatCaller(); var lastClaimUser = CurrentClaims.Last().ClaimUser; var preCheatClaims = CurrentClaims; if (cheatCaller == null) { return(null); } var lastClaim = CurrentClaims.Last(); for (int i = 0; i < lastClaim.Claims.Count; i++) { if (lastClaim.Claims[i].Rank != lastClaim.Actual[i].Rank) { cheatCallSuccessful = true; continue; } } var rankToCountMappings = new Dictionary <string, int>(); lastClaim.Actual.ForEach((value) => { if (rankToCountMappings.ContainsKey(value.Rank)) { rankToCountMappings[value.Rank] = rankToCountMappings[value.Rank] + 1; } else { rankToCountMappings[value.Rank] = 1; } }); var actualCards = new List <string>(); foreach (var pair in rankToCountMappings) { actualCards.Add($"×{pair.Value} {pair.Key}"); } if (cheatCallSuccessful) { var claimCardsCount = 0; foreach (var claim in CurrentClaims) { foreach (var actualCard in claim.Actual) { claimCardsCount += 1; lastClaimUser.Hand.Add(actualCard, actualCard); } } ActionHistory.Add($"Call Cheat Success!"); ActionHistory.Add($"Call Cheat Success! {cheatCaller.UserPrincipal.GetUserName()} is the cheat caller. {lastClaimUser.UserPrincipal.GetUserName()} collected {claimCardsCount} cards. Actual cards were {String.Join(",", actualCards)}"); } else { var claimCardsCount = 0; foreach (var claim in CurrentClaims) { foreach (var actualCard in claim.Actual) { claimCardsCount += 1; cheatCaller.Hand.Add(actualCard, actualCard); } } ActionHistory.Add($"Call Cheat Failed!"); ActionHistory.Add($"{cheatCaller.UserPrincipal.GetUserName()} is the cheat caller. {cheatCaller.UserPrincipal.GetUserName()} collected {claimCardsCount} cards. Actual cards were {String.Join(",", actualCards)}"); } CurrentClaims = new List <GameClaim>(); UsersCallingCheat = new List <GameUser>(); return(new GameCheat { CheatCallSuccessful = cheatCallSuccessful, CheatCaller = cheatCaller, LastClaimUser = lastClaimUser, PreCheatClaims = preCheatClaims }); }