public void Sync() { // TODO: map all other results of the teams in the division aswell... var frenoyTeams = _frenoy.GetClubTeams(new GetClubTeamsRequest { Club = _options.FrenoyClub, Season = _options.FrenoySeason }); foreach (var frenoyTeam in frenoyTeams.TeamEntries) { // Create new division=reeks for each team in the club // Check if it already exists: Two teams could play in the same division Reeks reeks = _db.Reeksen.SingleOrDefault(x => x.FrenoyDivisionId.ToString() == frenoyTeam.DivisionId); if (reeks == null) { reeks = CreateReeks(frenoyTeam); _db.Reeksen.Add(reeks); CommitChanges(); // Create the teams in the new division=reeks var frenoyDivision = _frenoy.GetDivisionRanking(new GetDivisionRankingRequest { DivisionId = frenoyTeam.DivisionId }); foreach (var frenoyTeamsInDivision in frenoyDivision.RankingEntries) { var clubPloeg = CreateClubPloeg(reeks, frenoyTeamsInDivision); _db.ClubPloegen.Add(clubPloeg); } CommitChanges(); } // Add Erembodegem players to the home team var ploeg = _db.ClubPloegen.Single(x => x.ClubId == _thuisClubId && x.ReeksId == reeks.Id && x.Code == frenoyTeam.Team); if (MapTeamPlayers) { var players = _options.Players[ploeg.Code]; foreach (var playerName in players) { var clubPloegSpeler = new ClubPloegSpeler { Kapitein = playerName == players.First() ? TeamPlayerType.Captain : TeamPlayerType.Standard, SpelerId = GetSpelerId(playerName), ClubPloegId = ploeg.Id }; _db.ClubPloegSpelers.Add(clubPloegSpeler); } CommitChanges(); } // Create the matches=kalender table in the new division=reeks var matches = _frenoy.GetMatches(new GetMatchesRequest { Club = _options.FrenoyClub, Season = _options.FrenoySeason, DivisionId = reeks.FrenoyDivisionId.ToString(), Team = ploeg.Code, WithDetailsSpecified = true, WithDetails = true, }); foreach (TeamMatchEntryType frenoyMatch in matches.TeamMatchesEntries.Where(x => x.HomeTeam.Trim() != "Vrij" && x.AwayTeam.Trim() != "Vrij")) { Debug.Assert(frenoyMatch.DateSpecified); Debug.Assert(frenoyMatch.TimeSpecified); // Kalender entries var kalender = _db.Kalender.SingleOrDefault(x => x.FrenoyMatchId == frenoyMatch.MatchId); if (kalender == null) { kalender = CreateKalenderMatch(reeks, frenoyMatch, ploeg.Code); _db.Kalender.Add(kalender); } // Wedstrijdverslagen if (frenoyMatch.MatchDetails != null && frenoyMatch.MatchDetails.DetailsCreated) { bool isForfeit = frenoyMatch.Score == null || frenoyMatch.Score.ToLowerInvariant().Contains("ff") || frenoyMatch.Score.ToLowerInvariant().Contains("af"); var verslag = _db.Verslagen.SingleOrDefault(x => x.KalenderId == kalender.Id); if (verslag == null) { verslag = new Verslag { Kalender = kalender, KalenderId = kalender.Id, IsSyncedWithFrenoy = true, SpelerId = Constants.SuperPlayerId, }; if (!isForfeit) { verslag.UitslagThuis = int.Parse(frenoyMatch.Score.Substring(0, frenoyMatch.Score.IndexOf("-"))); verslag.UitslagUit = int.Parse(frenoyMatch.Score.Substring(frenoyMatch.Score.IndexOf("-") + 1)); verslag.WO = 0; } else { verslag.WO = 1; } _db.Verslagen.Add(verslag); } bool deleteExisting = true; if (!isForfeit) { if (deleteExisting) { var oldVerslagSpelers = _db.VerslagenSpelers.Where(x => x.MatchId == verslag.KalenderId).ToArray(); _db.VerslagenSpelers.RemoveRange(oldVerslagSpelers); AddVerslagPlayers(frenoyMatch.MatchDetails.HomePlayers.Players, verslag, true); AddVerslagPlayers(frenoyMatch.MatchDetails.AwayPlayers.Players, verslag, false); } var hasIndividualMatches = _db.VerslagenIndividueel.Any(x => x.MatchId == verslag.KalenderId); if (!hasIndividualMatches) { int id = 0; foreach (var frenoyIndividual in frenoyMatch.MatchDetails.IndividualMatchResults) { var matchResult = new VerslagIndividueel { Id = id--, MatchId = verslag.KalenderId, MatchNumber = int.Parse(frenoyIndividual.Position), HomePlayerUniqueIndex = int.Parse(frenoyIndividual.HomePlayerUniqueIndex), OutPlayerUniqueIndex = int.Parse(frenoyIndividual.AwayPlayerUniqueIndex), WalkOver = WalkOver.None }; if (frenoyIndividual.IsHomeForfeited || frenoyIndividual.IsAwayForfeited) { matchResult.WalkOver = frenoyIndividual.IsHomeForfeited ? WalkOver.Home : WalkOver.Out; } else { matchResult.HomePlayerSets = int.Parse(frenoyIndividual.HomeSetCount); matchResult.OutPlayerSets = int.Parse(frenoyIndividual.AwaySetCount); } _db.VerslagenIndividueel.Add(matchResult); } } } } CommitChanges(); } } }