private static MatchOutcome GetScoreType(MatchEntity match)
        {
            var now = DateTime.Now;
            var yesterday = now.Subtract(TimeSpan.FromDays(1));
            if ((match.Date.Date == now.Date && now.Hour >= match.Date.Hour - 10) || (match.Date.Date == yesterday.Date && now.Hour < match.Date.Hour - 10))
            {
                return MatchOutcome.BeingPlayed;
            }
            if (match.Date.Date >= DateTime.Now.Date)
            {
                return MatchOutcome.NotYetPlayed;
            }

            if (match.WalkOver)
            {
                return MatchOutcome.WalkOver;
            }
            if (!match.HomeScore.HasValue || !match.AwayScore.HasValue)
            {
                return MatchOutcome.NotYetPlayed;
            }
            if (match.HomeScore.Value == match.AwayScore.Value)
            {
                return MatchOutcome.Draw;
            }

            if (match.IsHomeMatch.HasValue && match.IsHomeMatch.Value)
            {
                return match.HomeScore.Value < match.AwayScore.Value ? MatchOutcome.Lost : MatchOutcome.Won;
            }
            else
            {
                return match.HomeScore.Value < match.AwayScore.Value ? MatchOutcome.Won : MatchOutcome.Lost;
            }
        }
 public void SyncMatchDetails(MatchEntity matchEntity)
 {
     if (ShouldAttemptMatchSync(matchEntity.Id))
     {
         GetMatchesResponse matches = _frenoy.GetMatches(new GetMatchesRequest
         {
             DivisionId = matchEntity.FrenoyDivisionId.ToString(),
             WithDetailsSpecified = true,
             WithDetails = true,
             MatchId = matchEntity.FrenoyMatchId
         });
         Debug.Assert(matches.MatchCount == "1");
         SyncMatchDetails(matchEntity, matches.TeamMatchesEntries[0]);
     }
 }
 public TeamMatchExcelModel(MatchEntity match, ClubEntity[] clubs)
 {
     Match = match;
     Home = GetTeamDesc(match.HomeClubId, match.HomeTeamCode, clubs);
     Out = GetTeamDesc(match.AwayClubId, match.AwayTeamCode, clubs);
     PlayerDecisions = new Dictionary<string, string>();
     CaptainDecisions = new List<string>();
 }
        private static void AddMatchGames(IndividualMatchResultEntryType frenoyIndividual, int id, MatchEntity matchEntity)
        {
            MatchGameEntity matchResult;
            int homeUniqueIndex, awayUniqueIndex;
            if (!int.TryParse(frenoyIndividual.HomePlayerUniqueIndex, out homeUniqueIndex) || !int.TryParse(frenoyIndividual.AwayPlayerUniqueIndex, out awayUniqueIndex))
            {
                // Sporta doubles match:
                matchResult = new MatchGameEntity
                {
                    Id = id,
                    MatchId = matchEntity.Id,
                    MatchNumber = int.Parse(frenoyIndividual.Position),
                    WalkOver = WalkOver.None
                };
            }
            else
            {
                // Sporta/Vttl singles match
                matchResult = new MatchGameEntity
                {
                    Id = id,
                    MatchId = matchEntity.Id,
                    MatchNumber = int.Parse(frenoyIndividual.Position),
                    HomePlayerUniqueIndex = homeUniqueIndex,
                    AwayPlayerUniqueIndex = awayUniqueIndex,
                    WalkOver = WalkOver.None
                };
            }

            if (frenoyIndividual.IsHomeForfeited || frenoyIndividual.IsAwayForfeited)
            {
                matchResult.WalkOver = frenoyIndividual.IsHomeForfeited ? WalkOver.Home : WalkOver.Out;
            }
            else
            {
                matchResult.HomePlayerSets = int.Parse(frenoyIndividual.HomeSetCount);
                matchResult.AwayPlayerSets = int.Parse(frenoyIndividual.AwaySetCount);
            }
            matchEntity.Games.Add(matchResult);
        }
        private void AddMatchPlayers(TeamMatchPlayerEntryType[] players, MatchEntity match, bool thuisSpeler)
        {
            foreach (var frenoyVerslagSpeler in players)
            {
                MatchPlayerEntity matchPlayerEntity = new MatchPlayerEntity
                {
                    MatchId = match.Id,
                    Ranking = frenoyVerslagSpeler.Ranking,
                    Home = thuisSpeler,
                    Name = GetSpelerNaam(frenoyVerslagSpeler),
                    Position = int.Parse(frenoyVerslagSpeler.Position),
                    UniqueIndex = int.Parse(frenoyVerslagSpeler.UniqueIndex),
                    Status = PlayerMatchStatus.Major
                };
                if (frenoyVerslagSpeler.VictoryCount != null)
                {
                    matchPlayerEntity.Won = int.Parse(frenoyVerslagSpeler.VictoryCount);
                }
                else
                {
                    Debug.Assert(frenoyVerslagSpeler.IsForfeited, "Either a VictoryCount or IsForfeited");
                }

                PlayerEntity dbPlayer = null;
                if (match.IsHomeMatch.HasValue && ((match.IsHomeMatch.Value && thuisSpeler) || (!match.IsHomeMatch.Value && !thuisSpeler)))
                {
                    if (_isVttl)
                    {
                        dbPlayer = _db.Players.SingleOrDefault(x => x.ComputerNummerVttl.HasValue && x.ComputerNummerVttl.Value.ToString() == frenoyVerslagSpeler.UniqueIndex);
                    }
                    else
                    {
                        dbPlayer = _db.Players.SingleOrDefault(x => x.LidNummerSporta.HasValue && x.LidNummerSporta.Value.ToString() == frenoyVerslagSpeler.UniqueIndex);
                    }
                }
                if (dbPlayer != null)
                {
                    matchPlayerEntity.PlayerId = dbPlayer.Id;
                    if (!string.IsNullOrWhiteSpace(dbPlayer.NaamKort))
                    {
                        matchPlayerEntity.Name = dbPlayer.NaamKort;
                    }
                }

                match.Players.Add(matchPlayerEntity);
            }
        }
        private void RemoveExistingMatchPlayersAndGames(MatchEntity matchEntity)
        {
            var oldMatchPlayers = _db.MatchPlayers.Where(x => x.MatchId == matchEntity.Id).ToArray();
            _db.MatchPlayers.RemoveRange(oldMatchPlayers);

            var oldMatchGames = _db.MatchGames.Where(x => x.MatchId == matchEntity.Id).ToArray();
            _db.MatchGames.RemoveRange(oldMatchGames);
        }
 private static void AssertMatchPlayers(MatchEntity matchEntity)
 {
     var testPlayer = matchEntity.Players.Count(x => x.PlayerId != 0) == 0 || matchEntity.Players.Count > 8;
     if (testPlayer && (matchEntity.AwayTeamId.HasValue || matchEntity.HomeTeamId.HasValue))
     {
         Debug.Assert(false, "player problem");
     }
 }
 private static void AddMatchGames(TeamMatchEntryType frenoyMatch, MatchEntity matchEntity)
 {
     if (frenoyMatch.MatchDetails.IndividualMatchResults != null)
     {
         int id = 0;
         foreach (var frenoyIndividual in frenoyMatch.MatchDetails.IndividualMatchResults)
         {
             id--;
             AddMatchGames(frenoyIndividual, id, matchEntity);
         }
     }
 }
        private void SyncMatchDetails(MatchEntity matchEntity, TeamMatchEntryType frenoyMatch)
        {
            if (!matchEntity.IsSyncedWithFrenoy)
            {
                if (frenoyMatch.Score != null)
                {
                    string score = frenoyMatch.Score.ToLowerInvariant();
                    bool isForfeit = score.Contains("ff") || score.Contains("af") || score.Contains("gu");
                    if (!isForfeit)
                    {
                        // Uitslag
                        matchEntity.HomeScore = int.Parse(frenoyMatch.Score.Substring(0, frenoyMatch.Score.IndexOf("-")));
                        matchEntity.AwayScore = int.Parse(frenoyMatch.Score.Substring(frenoyMatch.Score.IndexOf("-") + 1));
                        matchEntity.WalkOver = false;
                    }
                    else
                    {
                        matchEntity.WalkOver = true;
                    }
                }

                if (frenoyMatch.MatchDetails != null && frenoyMatch.MatchDetails.DetailsCreated)
                {
                    AddMatchPlayers(frenoyMatch.MatchDetails.HomePlayers.Players, matchEntity, true);
                    AddMatchPlayers(frenoyMatch.MatchDetails.AwayPlayers.Players, matchEntity, false);
                    //AssertMatchPlayers(matchEntity);

                    AddMatchGames(frenoyMatch, matchEntity);

                    RemoveExistingMatchPlayersAndGames(matchEntity);
                }

                if (frenoyMatch.Score != null && frenoyMatch.MatchDetails != null && frenoyMatch.MatchDetails.DetailsCreated)
                {
                    matchEntity.IsSyncedWithFrenoy = true;
                }

                CommitChanges();
            }
        }
        private void MapMatch(MatchEntity entity, int? teamId, int frenoyDivisionId, TeamMatchEntryType frenoyMatch, int frenoySeason = Constants.FrenoySeason)
        {
            entity.FrenoyMatchId = frenoyMatch.MatchId;
            entity.Date = frenoyMatch.Date + new TimeSpan(frenoyMatch.Time.Hour, frenoyMatch.Time.Minute, 0);
            entity.HomeClubId = GetClubId(frenoyMatch.HomeClub);
            entity.HomeTeamCode = ExtractTeamCodeFromFrenoyName(frenoyMatch.HomeTeam);
            entity.AwayClubId = GetClubId(frenoyMatch.AwayClub);
            entity.AwayTeamCode = ExtractTeamCodeFromFrenoyName(frenoyMatch.AwayTeam);
            entity.Week = int.Parse(frenoyMatch.WeekName);
            entity.FrenoySeason = frenoySeason;
            entity.FrenoyDivisionId = frenoyDivisionId;
            entity.Competition = _settings.Competition;

            //TODO: we zaten hier for the derby problem
            // do not pass teamId here but find out what the Team is based on HomeClubId and HomeTeamCode
            if (teamId.HasValue)
            {
                if (entity.HomeClubId == Constants.OwnClubId)
                {
                    entity.HomeTeamId = teamId;
                }
                else if (entity.AwayClubId == Constants.OwnClubId)
                {
                    entity.AwayTeamId = teamId;
                }
            }
        }
        private void SyncMatches(int? teamId, int frenoyDivisionId, GetMatchesResponse matches, bool alsoSyncMatchDetails = true, int frenoySeason = Constants.FrenoySeason)
        {
            foreach (TeamMatchEntryType frenoyMatch in matches.TeamMatchesEntries.Where(x => !x.HomeTeam.Trim().StartsWith("Vrij") && !x.AwayTeam.Trim().StartsWith("Vrij")))
            {
                Debug.Assert(frenoyMatch.DateSpecified, "Probably need to filter this one out?");
                Debug.Assert(frenoyMatch.TimeSpecified);

                // Kalender entries
                MatchEntity matchEntity = _db.Matches.SingleOrDefault(x => x.FrenoyMatchId == frenoyMatch.MatchId && x.FrenoySeason == frenoySeason);
                if (matchEntity == null)
                {
                    matchEntity = new MatchEntity();
                    MapMatch(matchEntity, teamId, frenoyDivisionId, frenoyMatch, frenoySeason);
                    _db.Matches.Add(matchEntity);
                    CommitChanges();
                }
                else
                {
                    MapMatch(matchEntity, teamId, frenoyDivisionId, frenoyMatch, frenoySeason);
                    CommitChanges();
                }

                if (alsoSyncMatchDetails)
                {
                    SyncMatchDetails(matchEntity, frenoyMatch);
                }
            }
        }
 private Match Map(MatchEntity matchEntity)
 {
     return Mapper.Map<MatchEntity, Match>(matchEntity);
 }