public ParseHeaderResult(
     string team,
     string opponent,
     int turn,
     DateTime date,
     string location,
     OilPatternInformation oilPattern)
 {
     Team       = team ?? throw new ArgumentNullException(nameof(team));
     Opponent   = opponent ?? throw new ArgumentNullException(nameof(opponent));
     Turn       = turn;
     Date       = date;
     Location   = location ?? throw new ArgumentNullException(nameof(location));
     OilPattern = oilPattern;
 }
 public RosterHeaderViewModel(
     string?rosterId,
     string?team,
     string?teamLevel,
     string?location,
     string?opponent,
     DateTime date,
     OilPatternInformation oilPattern,
     string?matchResultId,
     bool matchTimeChanged)
 {
     RosterId         = rosterId;
     Team             = team;
     TeamLevel        = teamLevel;
     Location         = location;
     Opponent         = opponent;
     Date             = date;
     OilPattern       = oilPattern;
     MatchResultId    = matchResultId;
     MatchTimeChanged = matchTimeChanged;
 }
Example #3
0
        private async Task <IHttpActionResult> Handle(GetRostersFromBitsMessage message)
        {
            WebsiteConfig websiteConfig = DocumentSession.Load <WebsiteConfig>(WebsiteConfig.GlobalId);

            Log.Info($"Importing BITS season {websiteConfig.SeasonId} for {TenantConfiguration.FullTeamName} (ClubId={websiteConfig.ClubId})");
            RosterSearchTerms.Result[] rosterSearchTerms =
                DocumentSession.Query <RosterSearchTerms.Result, RosterSearchTerms>()
                .Where(x => x.Season == websiteConfig.SeasonId)
                .Where(x => x.BitsMatchId != 0)
                .ProjectFromIndexFieldsInto <RosterSearchTerms.Result>()
                .ToArray();
            Roster[] rosters       = DocumentSession.Load <Roster>(rosterSearchTerms.Select(x => x.Id));
            var      foundMatchIds = new HashSet <int>();

            // Team
            Log.Info($"Fetching teams");
            TeamResult[] teams = await bitsClient.GetTeam(websiteConfig.ClubId, websiteConfig.SeasonId);

            foreach (TeamResult teamResult in teams)
            {
                // Division
                Log.Info($"Fetching divisions");
                DivisionResult[] divisionResults = await bitsClient.GetDivisions(teamResult.TeamId, websiteConfig.SeasonId);

                // Match
                if (divisionResults.Length != 1)
                {
                    throw new Exception($"Unexpected number of divisions: {divisionResults.Length}");
                }
                DivisionResult divisionResult = divisionResults[0];
                Log.Info($"Fetching match rounds");
                MatchRound[] matchRounds = await bitsClient.GetMatchRounds(teamResult.TeamId, divisionResult.DivisionId, websiteConfig.SeasonId);

                var dict = matchRounds.ToDictionary(x => x.MatchId);
                foreach (int key in dict.Keys)
                {
                    foundMatchIds.Add(key);
                }

                // update existing rosters
                foreach (Roster roster in rosters.Where(x => dict.ContainsKey(x.BitsMatchId)))
                {
                    Log.Info($"Updating roster {roster.Id}");
                    MatchRound matchRound = dict[roster.BitsMatchId];
                    roster.OilPattern = OilPatternInformation.Create(
                        matchRound.MatchOilPatternName,
                        matchRound.MatchOilPatternId);
                    roster.Date             = matchRound.MatchDate.ToDateTime(matchRound.MatchTime);
                    roster.Turn             = matchRound.MatchRoundId;
                    roster.MatchTimeChanged = matchRound.MatchStatus == 2;
                    if (matchRound.HomeTeamClubId == websiteConfig.ClubId)
                    {
                        roster.Team      = matchRound.MatchHomeTeamAlias;
                        roster.TeamLevel = roster.Team.Substring(roster.Team.LastIndexOf(' ') + 1);
                        roster.Opponent  = matchRound.MatchAwayTeamAlias;
                    }
                    else if (matchRound.AwayTeamClubId == websiteConfig.ClubId)
                    {
                        roster.Team      = matchRound.MatchAwayTeamAlias;
                        roster.TeamLevel = roster.Team.Substring(roster.Team.LastIndexOf(' ') + 1);
                        roster.Opponent  = matchRound.MatchHomeTeamAlias;
                    }
                    else
                    {
                        throw new Exception($"Unknown clubs: {matchRound.HomeTeamClubId} {matchRound.AwayTeamClubId}");
                    }

                    roster.Location = matchRound.MatchHallName;
                }

                // add missing rosters
                var existingMatchIds = new HashSet <int>(rosters.Select(x => x.BitsMatchId));
                foreach (int matchId in dict.Keys.Where(x => existingMatchIds.Contains(x) == false))
                {
                    Log.Info($"Adding match {matchId}");
                    MatchRound matchRound = dict[matchId];
                    string     team;
                    string     opponent;
                    if (matchRound.HomeTeamClubId == websiteConfig.ClubId)
                    {
                        team     = matchRound.MatchHomeTeamAlias;
                        opponent = matchRound.MatchAwayTeamAlias;
                    }
                    else if (matchRound.AwayTeamClubId == websiteConfig.ClubId)
                    {
                        team     = matchRound.MatchAwayTeamAlias;
                        opponent = matchRound.MatchHomeTeamAlias;
                    }
                    else
                    {
                        throw new Exception($"Unknown clubs: {matchRound.HomeTeamClubId} {matchRound.AwayTeamClubId}");
                    }

                    var roster = new Roster(
                        matchRound.MatchSeason,
                        matchRound.MatchRoundId,
                        matchRound.MatchId,
                        team,
                        team.Substring(team.LastIndexOf(' ') + 1),
                        matchRound.MatchHallName,
                        opponent,
                        matchRound.MatchDate.ToDateTime(matchRound.MatchTime),
                        matchRound.MatchNbrOfPlayers == 4,
                        OilPatternInformation.Create(matchRound.MatchOilPatternName, matchRound.MatchOilPatternId))
                    {
                        MatchTimeChanged = matchRound.MatchStatus == 2
                    };
                    DocumentSession.Store(roster);
                }
            }

            // remove extraneous rosters
            Roster[] toRemove = rosters.Where(x => foundMatchIds.Contains(x.BitsMatchId) == false).ToArray();
            if (toRemove.Any())
            {
                string body = $"Rosters to remove: {string.Join(",", toRemove.Select(x => $"Id={x.Id} BitsMatchId={x.BitsMatchId}"))}";
                Log.Info(body);
                foreach (Roster roster in toRemove)
                {
                    DocumentSession.Delete(roster);
                }
                await Emails.SendAdminMail($"Removed rosters for {TenantConfiguration.FullTeamName}", body);
            }


            return(Ok());
        }
Example #4
0
    public Roster(
        int season,
        int turn,
        int bitsMatchId,
        string team,
        string?teamLevel,
        string?location,
        string?opponent,
        DateTime date,
        bool isFourPlayer,
        OilPatternInformation oilPattern,
        List <AuditLogEntry>?auditLogEntries = null)
    {
        Season          = season;
        Turn            = turn;
        BitsMatchId     = bitsMatchId;
        Team            = team;
        TeamLevel       = teamLevel ?? Team.Substring(team.Length - 1);
        Location        = location;
        Opponent        = opponent;
        Date            = date;
        IsFourPlayer    = isFourPlayer;
        OilPattern      = oilPattern ?? new OilPatternInformation(string.Empty, string.Empty);
        AuditLogEntries = auditLogEntries ?? new List <AuditLogEntry>();

        // fixup the state
        if (AuditLogEntries.Any(x => x.Before.GetType() != typeof(RosterState)))
        {
            foreach (AuditLogEntry item in AuditLogEntries)
            {
                string[] playersBefore = Array.Empty <string>();
                try
                {
                    playersBefore = ((dynamic)item.Before).Players;
                }
                catch (Exception)
                {
                }

                string[] acceptedPlayersBefore = Array.Empty <string>();
                try
                {
                    acceptedPlayersBefore = ((dynamic)item.Before).AcceptedPlayers;
                }
                catch (Exception)
                {
                }

                RosterState before = new(playersBefore, acceptedPlayersBefore);
                item.SetBefore(before);

                string[] playersAfter = Array.Empty <string>();
                try
                {
                    playersAfter = ((dynamic)item.After).Players;
                }
                catch (Exception)
                {
                }

                string[] acceptedPlayersAfter = Array.Empty <string>();
                try
                {
                    acceptedPlayersAfter = ((dynamic)item.After).AcceptedPlayers;
                }
                catch (Exception)
                {
                }

                RosterState after = new(playersAfter, acceptedPlayersAfter);
                item.SetAfter(after);
            }
        }
    }