Exemple #1
0
        private static void LoadRostersNewWeek()
        {
            try
            {
                NPGGFFLDataContext context = new NPGGFFLDataContext(ConfigurationManager.ConnectionStrings["NPGGFFLConnectionString"].ConnectionString);
                var TriggerDate = DateTime.Now.AddDays(-7);
                var lastSeasonWeek = context.SeasonWeeks.Where(sw => sw.StartDtm <= TriggerDate && sw.EndDtm >= TriggerDate).First();
                var currentSeasonWeek = context.SeasonWeeks.Where(sw => sw.StartDtm <= DateTime.Now && sw.EndDtm >= DateTime.Now).First();
                foreach (var o in context.Owners)
                {
                    var teamLastWeek = context.OwnerTeams.Where(ot => ot.SeasonWeekId == lastSeasonWeek.SeasonWeekId && ot.OwnerId == o.OwnerId);
                    var teamThisWeek = context.OwnerTeams.Where(ot => ot.SeasonWeekId == currentSeasonWeek.SeasonWeekId && ot.OwnerId == o.OwnerId);

                    if (teamThisWeek.Count() == 0)
                    {
                        foreach (var tlw in teamLastWeek)
                        {
                            OwnerTeams ownerTeam = new OwnerTeams { OwnerId = o.OwnerId, SeasonWeekId = currentSeasonWeek.SeasonWeekId, PlayerId = tlw.PlayerId, IsLocked = false, TeamId = tlw.TeamId };
                            context.OwnerTeams.InsertOnSubmit(ownerTeam);
                        }
                        context.SubmitChanges();
                    }
                }

            }
            catch (Exception)
            {
                throw;
            }
        }
        public bool SaveLineup(string fromAddress, string body)
        {
            try
            {
                WeeksController controller = new WeeksController();
                var seasonWeek = controller.GetSeasonWeek(DateTime.Now.ToShortDateString());
                var ownerExists = context.Owners.Where(o => o.EmailAddress.Contains(fromAddress)).FirstOrDefault();
                if (ownerExists == null)
                {
                    return false;
                }
                else
                {
                    var lineupText = body.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

                    List<NPGGFFL.Data.Players> lineup = new List<NPGGFFL.Data.Players>();
                    foreach (var line in lineupText)
                    {
                        if (!line.Trim().Equals(string.Empty))
                        {
                            Dictionary<int, int> playerMatches = new Dictionary<int, int>();
                            var words = line.Split(' ');
                            foreach (var word in words)
                            {
                                var playerFound = context.Players.Where(p => (p.Name.ToUpper().Contains(word.ToUpper()) || p.Alias.Contains(word.ToUpper())));
                                foreach (var player in playerFound)
                                {
                                    if (playerMatches.ContainsKey(player.PlayerId))
                                    {
                                        playerMatches[player.PlayerId]++;
                                    }
                                    else
                                    {
                                        playerMatches.Add(player.PlayerId, 1);
                                    }
                                }
                            }

                            if (playerMatches.Count() > 0)
                            {
                                var highestMatchCount = playerMatches.OrderByDescending(m => m.Value).First().Value;
                                if (playerMatches.Where(m => m.Value == highestMatchCount).Count() == 1)
                                {
                                    lineup.Add(context.Players.Where(p => p.PlayerId == playerMatches.Where(m => m.Value == highestMatchCount).First().Key).First());
                                }
                                else
                                {
                                    var distanceMatches = playerMatches.Where(m => m.Value == highestMatchCount).Select(m => new
                                    {
                                        PlayerId = m.Key
                                        ,
                                        Distance = LevenshteinDistance.Compute((context.Players.Where(p => p.PlayerId == m.Key).First().Alias == null ? context.Players.Where(p => p.PlayerId == m.Key).First().Name : context.Players.Where(p => p.PlayerId == m.Key).First().Alias).ToUpper(), line.ToUpper())
                                    }).OrderBy(m => m.Distance);

                                    var bestMatch = distanceMatches.First();
                                    lineup.Add(context.Players.Where(p => p.PlayerId == bestMatch.PlayerId).First());
                                }
                            }
                        }
                    }

                    //load lineup
                    if (lineup.Count > 0)
                    {
                        var ownerTeam = context.OwnerTeams.Where(ot => ot.OwnerId == ownerExists.OwnerId && ot.SeasonWeekId == seasonWeek.SeasonWeekId);
                        foreach (var ot in ownerTeam)
                        {
                            context.OwnerTeams.DeleteOnSubmit(ot);
                        }
                        context.SubmitChanges();

                        foreach (var player in lineup)
                        {
                            var ot = new NPGGFFL.Data.OwnerTeams
                            {
                                OwnerId = ownerExists.OwnerId,
                                PlayerId = player.PlayerId,
                                SeasonWeekId = seasonWeek.SeasonWeekId,
                                IsLocked = false
                            };
                            context.OwnerTeams.InsertOnSubmit(ot);
                        }
                        context.SubmitChanges();
                    }

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
		private void detach_OwnerTeams(OwnerTeams entity)
		{
			this.SendPropertyChanging();
			entity.Owners = null;
		}
		private void attach_OwnerTeams(OwnerTeams entity)
		{
			this.SendPropertyChanging();
			entity.Owners = this;
		}
 partial void DeleteOwnerTeams(OwnerTeams instance);
 partial void UpdateOwnerTeams(OwnerTeams instance);
 partial void InsertOwnerTeams(OwnerTeams instance);
Exemple #8
0
        public static bool SaveLineup(NPGGFFLDataContext context, string fromAddress, string body, int seasonWeekId)
        {
            try
            {
                var weekDate = DateTime.Now;
                //var seasonWeek = context.SeasonWeeks.Where(sw2 => weekDate >= sw2.StartDtm && weekDate <= sw2.EndDtm).First();
                var seasonWeek = context.SeasonWeeks.Where(sw2 => sw2.SeasonWeekId == seasonWeekId).First();
                var ownerExists = context.Owners.Where(o => o.EmailAddress.ToUpper().Contains(fromAddress)).FirstOrDefault();
                if (ownerExists == null)
                {
                    return false;
                }
                else
                {
                    var lineupText = body.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

                    List<NPGGFFL.Data.Players> lineup = new List<NPGGFFL.Data.Players>();
                    foreach (var line in lineupText)
                    {
                        if (!line.Trim().Equals(string.Empty))
                        {
                            Dictionary<int, int> playerMatches = new Dictionary<int, int>();
                            var words = line.Split(' ');
                            foreach (var word in words)
                            {
                                var playerFound = context.Players.Where(p => (p.Name.ToUpper().Contains(word.ToUpper()) || p.Alias.Contains(word.ToUpper())));
                                foreach (var player in playerFound)
                                {
                                    if (playerMatches.ContainsKey(player.PlayerId))
                                    {
                                        playerMatches[player.PlayerId]++;
                                    }
                                    else
                                    {
                                        playerMatches.Add(player.PlayerId, 1);
                                    }
                                }
                            }

                            if (playerMatches.Count() > 0)
                            {
                                var highestMatchCount = playerMatches.OrderByDescending(m => m.Value).First().Value;
                                if (playerMatches.Where(m => m.Value == highestMatchCount).Count() == 1)
                                {
                                    lineup.Add(context.Players.Where(p => p.PlayerId == playerMatches.Where(m => m.Value == highestMatchCount).First().Key).First());
                                }
                                else
                                {
                                    var distanceMatches = playerMatches.Where(m => m.Value == highestMatchCount).Select(m => new
                                    {
                                        PlayerId = m.Key,
                                        Distance = LevenshteinDistance.Compute((context.Players.Where(p => p.PlayerId == m.Key).First().Alias == null ? context.Players.Where(p => p.PlayerId == m.Key).First().Name : context.Players.Where(p => p.PlayerId == m.Key).First().Alias).ToUpper(), line.ToUpper())
                                    }).OrderBy(m => m.Distance);

                                    var bestMatch = distanceMatches.First();
                                    lineup.Add(context.Players.Where(p => p.PlayerId == bestMatch.PlayerId).First());
                                }
                            }
                        }
                    }

                    //load lineup
                    if (lineup.Count > 0)
                    {
                        var playerCount = context.OwnerTeams.Where(ot => ot.OwnerId == ownerExists.OwnerId && ot.SeasonWeekId == seasonWeek.SeasonWeekId).Count();
                        var unlockedPlayerCount = context.OwnerTeams.Where(ot => ot.OwnerId == ownerExists.OwnerId && ot.SeasonWeekId == seasonWeek.SeasonWeekId && ot.IsLocked == false).Count();

                        if (playerCount == 0 || unlockedPlayerCount > 0)
                        {
                            var ownerTieBreaker = context.OwnerTieBreakers.Where(otb => otb.OwnerId == ownerExists.OwnerId && otb.SeasonWeekId == seasonWeek.SeasonWeekId).FirstOrDefault();
                            if (ownerTieBreaker != null)
                            {
                                context.OwnerTieBreakers.DeleteOnSubmit(ownerTieBreaker);
                                context.SubmitChanges();
                            }
                            else
                            {
                                var vikingsTeam = context.Teams.Where(t => t.Abbreviation.Equals("min")).First();
                                var tieBreakerGame = context.Games.Where(g => g.SeasonWeekId == seasonWeek.SeasonWeekId && (g.HomeTeamId == vikingsTeam.TeamId || g.AwayTeamId == vikingsTeam.TeamId)).First();
                                NPGGFFL.Data.OwnerTieBreaker otb = new NPGGFFL.Data.OwnerTieBreaker {
                                    OwnerId = ownerExists.OwnerId,
                                    GameId = tieBreakerGame.GameId,
                                    SeasonWeekId = seasonWeek.SeasonWeekId,
                                    HomeScore = 1,
                                    AwayScore = 1,
                                    IsLocked = false
                                };

                                context.OwnerTieBreakers.InsertOnSubmit(otb);
                                context.SubmitChanges();
                            }

                            var ownerTeam = context.OwnerTeams.Where(ot => ot.OwnerId == ownerExists.OwnerId && ot.SeasonWeekId == seasonWeek.SeasonWeekId);
                            foreach (var ot in ownerTeam)
                            {
                                context.OwnerTeams.DeleteOnSubmit(ot);
                            }
                            context.SubmitChanges();

                            foreach (var player in lineup)
                            {
                                var ot = new NPGGFFL.Data.OwnerTeams
                                {
                                    OwnerId = ownerExists.OwnerId,
                                    PlayerId = player.PlayerId,
                                    SeasonWeekId = seasonWeek.SeasonWeekId,
                                    IsLocked = false
                                };
                                context.OwnerTeams.InsertOnSubmit(ot);
                            }
                            context.SubmitChanges();
                        }
                    }

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        public bool SaveOwnerTeams(string uniqueId, int seasonWeekId, string playerList)
        {
            try
            {
                var players = playerList.Split(',').Select(p => Convert.ToInt32(p));
                var playerDBCount = context.Players.Where(p => players.Contains(p.PlayerId)).Count();
                var ownerExists = context.Owners.Where(o => o.UniqueId == uniqueId).FirstOrDefault();
                var seasonWeek = context.SeasonWeeks.Where(sw => sw.SeasonWeekId == seasonWeekId).FirstOrDefault();
                var tieBreakerGame = context.Games.Where(g => g.SeasonWeekId == seasonWeek.SeasonWeekId && (g.HomeTeam.Abbreviation.Equals("min") || g.AwayTeam.Abbreviation.Equals("min"))).FirstOrDefault();

                if (tieBreakerGame == null)
                {
                    tieBreakerGame = context.Games.Where(g => g.SeasonWeekId == seasonWeek.SeasonWeekId).OrderBy(g => g.GameDtm).FirstOrDefault();
                }

                if (ownerExists == null || seasonWeek == null || tieBreakerGame == null || players.Count() != playerDBCount)
                {
                    return false;
                }
                else
                {
                    var tieBreakerExists = context.OwnerTieBreakers.Where(otb => otb.OwnerId == ownerExists.OwnerId && otb.GameId == tieBreakerGame.GameId).FirstOrDefault();
                    if (tieBreakerExists != null)
                    {
                        tieBreakerExists.HomeScore = 1;
                        tieBreakerExists.AwayScore = 1;
                    }
                    else
                    {
                        NPGGFFL.Data.OwnerTieBreaker tieBreaker = new NPGGFFL.Data.OwnerTieBreaker { OwnerId = ownerExists.OwnerId, GameId = tieBreakerGame.GameId, HomeScore = 1, AwayScore = 1, IsLocked = false, SeasonWeekId = seasonWeek.SeasonWeekId };
                        context.OwnerTieBreakers.InsertOnSubmit(tieBreaker);
                    }

                    var ownerTeams = context.OwnerTeams.Where(ot => ot.OwnerId == ownerExists.OwnerId && ot.SeasonWeekId == seasonWeekId);
                    foreach (var ot in ownerTeams)
                    {
                        context.OwnerTeams.DeleteOnSubmit(ot);
                    }

                    foreach (var player in players)
                    {
                        OwnerTeams ownerPlayer = new OwnerTeams { OwnerId = ownerExists.OwnerId, SeasonWeekId = seasonWeek.SeasonWeekId, PlayerId = player, IsLocked = false };
                        context.OwnerTeams.InsertOnSubmit(ownerPlayer);
                    }
                    context.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// Save an owner's roster & tiebreaker for a given seasonweek
        /// </summary>
        /// <param name="uniqueId">The owner code (uniqueId) for the owner.</param>
        /// <param name="seasonWeekId">The season week id representing the season week.</param>
        /// <param name="gameId">The game id representing the tiebreaker game.</param>
        /// <param name="homeScore">The home score for the tiebreaker game.</param>
        /// <param name="awayScore">The away score for the tiebreaker game.</param>
        /// <param name="playerList">The list of player ids to save as the roster.</param>
        public bool SaveOwnerTeams(string uniqueId, int seasonWeekId, int gameId, int homeScore, int awayScore, int loserTeam1, int loserTeam2, string playerList)
        {
            try
            {
                var players = playerList.Split(',').Select(p => Convert.ToInt32(p));
                var playerDBCount = context.Players.Where(p => players.Contains(p.PlayerId)).Count();
                var ownerExists = context.Owners.Where(o => o.UniqueId == uniqueId).FirstOrDefault();
                var seasonWeek = context.SeasonWeeks.Where(sw => sw.SeasonWeekId == seasonWeekId).FirstOrDefault();
                var tieBreakerGame = context.Games.Where(g => g.GameId == gameId).First();

                if (ownerExists == null || seasonWeek == null || tieBreakerGame == null || players.Count() != playerDBCount)
                {
                    return false;
                }
                else
                {
                    foreach (var lte in context.LoserPoolPicks.Where(lpp => lpp.OwnerId == ownerExists.OwnerId && lpp.SeasonWeekId == seasonWeek.SeasonWeekId))
                    {
                        context.LoserPoolPicks.DeleteOnSubmit(lte);
                    }
                    NPGGFFL.Data.LoserPoolPicks loserPoolPick1 = new NPGGFFL.Data.LoserPoolPicks
                    {
                        OwnerId = ownerExists.OwnerId,
                        TeamId = loserTeam1,
                        SeasonWeekId = seasonWeek.SeasonWeekId,
                        IsLocked = false,
                        IsBye = (loserTeam1 < 0 ? true: false)
                    };
                    context.LoserPoolPicks.InsertOnSubmit(loserPoolPick1);

                    NPGGFFL.Data.LoserPoolPicks loserPoolPick2 = new NPGGFFL.Data.LoserPoolPicks
                    {
                        OwnerId = ownerExists.OwnerId,
                        TeamId = loserTeam2,
                        SeasonWeekId = seasonWeek.SeasonWeekId,
                        IsLocked = false,
                        IsBye = (loserTeam2 < 0 ? true : false)
                    };
                    context.LoserPoolPicks.InsertOnSubmit(loserPoolPick2);

                    var tieBreakerExists = context.OwnerTieBreakers.Where(otb => otb.OwnerId == ownerExists.OwnerId && otb.GameId == gameId).FirstOrDefault();
                    if (tieBreakerExists != null)
                    {
                        tieBreakerExists.HomeScore = homeScore;
                        tieBreakerExists.AwayScore = awayScore;
                    }
                    else
                    {
                        NPGGFFL.Data.OwnerTieBreaker tieBreaker = new NPGGFFL.Data.OwnerTieBreaker { OwnerId = ownerExists.OwnerId, GameId = gameId, HomeScore = homeScore, AwayScore = awayScore, IsLocked = false };
                        context.OwnerTieBreakers.InsertOnSubmit(tieBreaker);
                    }

                    var ownerTeams = context.OwnerTeams.Where(ot => ot.OwnerId == ownerExists.OwnerId && ot.SeasonWeekId == seasonWeekId);
                    foreach (var ot in ownerTeams)
                    {
                        context.OwnerTeams.DeleteOnSubmit(ot);
                    }

                    foreach (var player in players)
                    {
                        OwnerTeams ownerPlayer = new OwnerTeams { OwnerId = ownerExists.OwnerId, SeasonWeekId = seasonWeek.SeasonWeekId, PlayerId = player, IsLocked = false };
                        context.OwnerTeams.InsertOnSubmit(ownerPlayer);
                    }
                    context.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }