/// <summary> /// Tries to build hand history by using buffered packages or buffer the specified package for further using /// </summary> /// <param name="package">Package to buffer</param> /// <param name="handHistory">Hand history</param> /// <returns>True if hand can be built; otherwise - false</returns> public bool TryBuild(PokerBaaziPackage package, out HandHistory handHistory, out PokerBaaziHandBuilderError builderError) { handHistory = null; builderError = PokerBaaziHandBuilderError.None; if (package == null) { return(false); } if (package.PackageType == PokerBaaziPackageType.InitResponse) { ParsePackage <PokerBaaziInitResponse>(package, x => ProcessInitResponse(x)); return(false); } else if (package.PackageType == PokerBaaziPackageType.TournamentDetailsResponse) { ParsePackage <PokerBaaziTournamentDetailsResponse>(package, x => ProcessTournamentDetailsResponse(x)); return(false); } if (!roomPackages.TryGetValue(package.RoomId, out List <PokerBaaziPackage> packages)) { packages = new List <PokerBaaziPackage>(); roomPackages.Add(package.RoomId, packages); } packages.Add(package); if (package.PackageType == PokerBaaziPackageType.WinnerResponse) { handHistory = BuildHand(packages, out builderError); } return(handHistory != null && handHistory.Players.Count > 0); }
public PokerBaaziHandBuilderException(PokerBaaziHandBuilderError error, long gameNumber, string message) : this(gameNumber, message) { Error = error; }
/// <summary> /// Builds hand history from the specified packages /// </summary> /// <param name="packages">Packages</param> /// <returns><see cref="HandHistory"/> or null if it can't be built</returns> private HandHistory BuildHand(List <PokerBaaziPackage> packages, out PokerBaaziHandBuilderError error) { HandHistory handHistory = null; error = PokerBaaziHandBuilderError.None; try { if (!Validate(packages)) { error = PokerBaaziHandBuilderError.NotValid; packages.Clear(); return(handHistory); } foreach (var package in packages) { switch (package.PackageType) { case PokerBaaziPackageType.StartGameResponse: handHistory = new HandHistory(); ParsePackage <PokerBaaziStartGameResponse>(package, (response, timestamp) => ProcessSpectatorResponse(response, timestamp, handHistory)); break; case PokerBaaziPackageType.UserButtonActionResponse: ParsePackage <PokerBaaziUserActionResponse>(package, response => ProcessUserActionResponse(response, handHistory)); break; case PokerBaaziPackageType.RoundResponse: ParsePackage <PokerBaaziRoundResponse>(package, response => ProcessRoundResponse(response, handHistory)); break; case PokerBaaziPackageType.WinnerResponse: ParsePackage <PokerBaaziWinnerResponse>(package, response => ProcessWinnerResponse(response, handHistory)); break; } } AdjustHandHistory(handHistory); return(handHistory); } catch (Exception e) { if (e is PokerBaaziHandBuilderException builderException) { error = builderException.Error; } else { error = PokerBaaziHandBuilderError.System; } LogProvider.Log.Error(this, $"Failed to build hand #{handHistory?.HandId ?? 0} room #{handHistory?.GameDescription.Identifier ?? 0}. [{loggerName}]", e); return(null); } finally { packages.Clear(); } }