public static async Task <BitsMatchResult> GetBitsMatchResult(int matchId) { BitsMatchResult matchResult = await Try( $"MatchResult-{matchId}.json", () => Client.GetBitsMatchResult(matchId)); return(matchResult); }
private async Task <IHttpActionResult> Handle(RegisterMatchMessage message) { WebsiteConfig websiteConfig = DocumentSession.Load <WebsiteConfig>(WebsiteConfig.GlobalId); Player[] players = DocumentSession.Query <Player, PlayerSearch>() .ToArray() .Where(x => x.PlayerItem?.LicNbr != null) .ToArray(); Roster pendingMatch = DocumentSession.Load <Roster>(message.RosterId); try { var parser = new BitsParser(players); BitsMatchResult bitsMatchResult = await bitsClient.GetBitsMatchResult(pendingMatch.BitsMatchId); if (bitsMatchResult.HeadInfo.MatchFinished == false) { Log.Info($"Match {pendingMatch.BitsMatchId} not yet finished"); return(Ok()); } if (pendingMatch.IsFourPlayer) { Parse4Result parse4Result = parser.Parse4(bitsMatchResult, websiteConfig.ClubId); if (parse4Result != null) { List <string> allPlayerIds = GetPlayerIds(parse4Result); pendingMatch.SetPlayers(allPlayerIds); ExecuteCommand(new RegisterMatch4Command(pendingMatch, parse4Result)); } } else { ParseResult parseResult = parser.Parse(bitsMatchResult, websiteConfig.ClubId); if (parseResult != null) { List <string> allPlayerIds = GetPlayerIds(parseResult); pendingMatch.SetPlayers(allPlayerIds); ExecuteCommand(new RegisterMatchCommand(pendingMatch, parseResult)); } } } catch (Exception e) { ErrorSignal .FromCurrentContext() .Raise(new Exception($"Unable to auto register match {pendingMatch.Id} ({pendingMatch.BitsMatchId})", e)); Log.Warn(e); return(Ok(e.ToString())); } return(Ok()); }
public async Task CorrectTurn(TestCase testCase) { // Act BitsParser bitsParser = new(Players); BitsMatchResult content = await BitsGateway.GetBitsMatchResult(testCase.BitsMatchId); ParseResult?parseResult = bitsParser.Parse(content, testCase.ClubId); // Assert Assert.That(parseResult?.Turn, Is.EqualTo(testCase.Turn)); }
public ParseResult?Parse(BitsMatchResult bitsMatchResult, int clubId) { if (bitsMatchResult.HeadInfo.MatchFinished == false) { return(null); } ParseResult parseResult; if (bitsMatchResult.HeadInfo.MatchHomeClubId == clubId) { List <ResultSeriesReadModel.Serie> homeSeries = CreateSeries( bitsMatchResult.HeadResultInfo.HomeHeadDetails !, bitsMatchResult.MatchScores, x => GetPlayerId(x).Id, 0); List <ResultSeriesReadModel.Serie> awaySeries = CreateSeries( bitsMatchResult.HeadResultInfo.HomeHeadDetails !, bitsMatchResult.MatchScores, x => x, 2); parseResult = new ParseResult( bitsMatchResult.HeadResultInfo.MatchHeadHomeTotalRp, bitsMatchResult.HeadResultInfo.MatchHeadAwayTotalRp, bitsMatchResult.HeadInfo.MatchRoundId, homeSeries.ToArray(), awaySeries.ToArray()); } else if (bitsMatchResult.HeadInfo.MatchAwayClubId == clubId) { List <ResultSeriesReadModel.Serie> homeSeries = CreateSeries( bitsMatchResult.HeadResultInfo.HomeHeadDetails !, bitsMatchResult.MatchScores, x => GetPlayerId(x).Id, 2); List <ResultSeriesReadModel.Serie> awaySeries = CreateSeries( bitsMatchResult.HeadResultInfo.HomeHeadDetails !, bitsMatchResult.MatchScores, x => x, 0); parseResult = new ParseResult( bitsMatchResult.HeadResultInfo.MatchHeadAwayTotalRp, bitsMatchResult.HeadResultInfo.MatchHeadHomeTotalRp, bitsMatchResult.HeadInfo.MatchRoundId, homeSeries.ToArray(), awaySeries.ToArray()); } else { throw new Exception($"No club matching {clubId}"); } return(parseResult);
private async Task <MatchResult> Act(TestCase testCase) { // Arrange await Transact(session => { foreach (Player player in Players) { session.Store(player); } return(Task.CompletedTask); }); BitsParser bitsParser = new(Players); BitsMatchResult content = await BitsGateway.GetBitsMatchResult(testCase.BitsMatchId); ParseResult parseResult = bitsParser.Parse(content, testCase.ClubId) !; HashSet <string> rosterPlayerIds = new( parseResult.Series.SelectMany(x => x.Tables.SelectMany(y => new[] { y.Game1.Player, y.Game2.Player }))); Roster roster = new(2017, 1, testCase.BitsMatchId, "Fredrikshof", "A", string.Empty, string.Empty, DateTime.Now, false, OilPatternInformation.Empty) { Players = rosterPlayerIds.ToList() }; await Transact(session => { session.Store(roster); return(Task.CompletedTask); }); RegisterMatchCommandHandler.Command command = new(roster.Id !, parseResult); // prepare some results await Transact(session => { Dictionary <string, Player> nicknameToId = Players.ToDictionary(x => x.Nickname !); Dictionary <string, int[]> playerResults = new() { [nicknameToId["Ernest"].Id] = new[] { 205 },
public override async Task Handle(HandlerContext <Command> context) { Roster roster = CompositionRoot.DocumentSession.Load <Roster>(context.Payload.RosterId); if (roster.IsVerified && context.Payload.Force == false) { return; } WebsiteConfig websiteConfig = CompositionRoot.DocumentSession.Load <WebsiteConfig>(WebsiteConfig.GlobalId); HeadInfo result = await CompositionRoot.BitsClient.GetHeadInfo(roster.BitsMatchId); ParseHeaderResult header = BitsParser.ParseHeader(result, websiteConfig.ClubId); // chance to update roster values Roster.Update update = new( Roster.ChangeType.VerifyMatchMessage, "system") { OilPattern = header.OilPattern, Date = header.Date, Opponent = header.Opponent, Location = header.Location }; if (roster.MatchResultId != null) { // update match result values BitsMatchResult bitsMatchResult = await CompositionRoot.BitsClient.GetBitsMatchResult(roster.BitsMatchId); Player[] players = CompositionRoot.DocumentSession.Query <Player, PlayerSearch>() .ToArray() .Where(x => x.PlayerItem?.LicNbr != null) .ToArray(); BitsParser parser = new(players); if (roster.IsFourPlayer) { MatchResult4?matchResult = CompositionRoot.EventStoreSession.Load <MatchResult4>(roster.MatchResultId); Parse4Result?parseResult = parser.Parse4(bitsMatchResult, websiteConfig.ClubId); update.Players = parseResult !.GetPlayerIds(); bool isVerified = matchResult !.Update( t => context.PublishMessage(t), roster, parseResult.TeamScore, parseResult.OpponentScore, roster.BitsMatchId, parseResult.CreateMatchSeries(), players); update.IsVerified = isVerified; } else { MatchResult?matchResult = CompositionRoot.EventStoreSession.Load <MatchResult>(roster.MatchResultId); ParseResult?parseResult = parser.Parse(bitsMatchResult, websiteConfig.ClubId); update.Players = parseResult !.GetPlayerIds(); Dictionary <string, ResultForPlayerIndex.Result> resultsForPlayer = CompositionRoot.DocumentSession.Query <ResultForPlayerIndex.Result, ResultForPlayerIndex>() .Where(x => x.Season == roster.Season) .ToArray() .ToDictionary(x => x.PlayerId); MatchSerie[] matchSeries = parseResult.CreateMatchSeries(); bool isVerified = matchResult !.Update( t => context.PublishMessage(t), roster, parseResult.TeamScore, parseResult.OpponentScore, matchSeries, parseResult.OpponentSeries, players, resultsForPlayer); update.IsVerified = isVerified; } } _ = roster.UpdateWith(context.CorrelationId, update); }
public override async Task Handle(HandlerContext <Command> context) { WebsiteConfig websiteConfig = CompositionRoot.DocumentSession.Load <WebsiteConfig>(WebsiteConfig.GlobalId); Player[] players = CompositionRoot.DocumentSession.Query <Player, PlayerSearch>() .ToArray() .Where(x => x.PlayerItem?.LicNbr != null) .ToArray(); Roster pendingMatch = CompositionRoot.DocumentSession.Load <Roster>(context.Payload.RosterId); try { BitsParser parser = new(players); BitsMatchResult bitsMatchResult = await CompositionRoot.BitsClient.GetBitsMatchResult(pendingMatch.BitsMatchId); if (bitsMatchResult.HeadInfo.MatchFinished == false) { Logger.InfoFormat( "Match {bitsMatchId} not yet finished", pendingMatch.BitsMatchId); return; } if (pendingMatch.IsFourPlayer) { Parse4Result?parse4Result = parser.Parse4(bitsMatchResult, websiteConfig.ClubId); if (parse4Result != null) { if (parse4Result.Series.Length != 4 && parse4Result.Turn <= 20) { Logger.InfoFormat( "detected unfinished match: {length} series have been registered", parse4Result.Series.Length); return; } List <string> allPlayerIds = parse4Result.GetPlayerIds(); pendingMatch.SetPlayers(allPlayerIds); await context.ExecuteCommand( new RegisterMatch4CommandHandler.Command(pendingMatch.Id !, parse4Result, null, null)); } } else { ParseResult?parseResult = parser.Parse(bitsMatchResult, websiteConfig.ClubId); if (parseResult != null) { if (parseResult.Series.Length != 4 && parseResult.Turn <= 20) { Logger.InfoFormat( "detected unfinished match: {length} series have been registered", parseResult.Series.Length); return; } List <string> allPlayerIds = parseResult.GetPlayerIds(); pendingMatch.SetPlayers(allPlayerIds); await context.ExecuteCommand( new RegisterMatchCommandHandler.Command(pendingMatch.Id !, parseResult)); } } } catch (Exception e) { Logger.Warn("Unable to auto-register match", e); ErrorSignal .FromCurrentContext() ?.Raise(new Exception($"Unable to auto register match {pendingMatch.Id} ({pendingMatch.BitsMatchId})", e)); } }
private async Task <IHttpActionResult> Handle(VerifyMatchMessage message) { Roster roster = DocumentSession.Load <Roster>(message.RosterId); if (roster.IsVerified && message.Force == false) { return(Ok()); } WebsiteConfig websiteConfig = DocumentSession.Load <WebsiteConfig>(WebsiteConfig.GlobalId); HeadInfo result = await bitsClient.GetHeadInfo(roster.BitsMatchId); ParseHeaderResult header = BitsParser.ParseHeader(result, websiteConfig.ClubId); // chance to update roster values var update = new Roster.Update( Roster.ChangeType.VerifyMatchMessage, "system") { OilPattern = header.OilPattern, Date = header.Date, Opponent = header.Opponent, Location = header.Location }; if (roster.MatchResultId != null) { // update match result values BitsMatchResult bitsMatchResult = await bitsClient.GetBitsMatchResult(roster.BitsMatchId); Player[] players = DocumentSession.Query <Player, PlayerSearch>() .ToArray() .Where(x => x.PlayerItem?.LicNbr != null) .ToArray(); var parser = new BitsParser(players); if (roster.IsFourPlayer) { MatchResult4 matchResult = EventStoreSession.Load <MatchResult4>(roster.MatchResultId); Parse4Result parseResult = parser.Parse4(bitsMatchResult, websiteConfig.ClubId); update.Players = GetPlayerIds(parseResult); bool isVerified = matchResult.Update( PublishMessage, roster, parseResult.TeamScore, parseResult.OpponentScore, roster.BitsMatchId, parseResult.CreateMatchSeries(), players); update.IsVerified = isVerified; } else { MatchResult matchResult = EventStoreSession.Load <MatchResult>(roster.MatchResultId); ParseResult parseResult = parser.Parse(bitsMatchResult, websiteConfig.ClubId); update.Players = GetPlayerIds(parseResult); var resultsForPlayer = DocumentSession.Query <ResultForPlayerIndex.Result, ResultForPlayerIndex>() .Where(x => x.Season == roster.Season) .ToArray() .ToDictionary(x => x.PlayerId); MatchSerie[] matchSeries = parseResult.CreateMatchSeries(); bool isVerified = matchResult.Update( PublishMessage, roster, parseResult.TeamScore, parseResult.OpponentScore, matchSeries, parseResult.OpponentSeries, players, resultsForPlayer); update.IsVerified = isVerified; } } roster.UpdateWith(Trace.CorrelationManager.ActivityId, update); return(Ok()); }