//internal static void SchedulerGames(Guid tournamentId, List<PlayersEntity> playersPairs)
        //{
        //    TournamentInfo tournamentInfo = GetTournamentStartDate(tournamentId);
        //    DateTime dts = new DateTime(tournamentInfo.StartDate.Year, tournamentInfo.StartDate.Month, tournamentInfo.StartDate.Day, tournamentInfo.TimeWindowStart, 0, 0);
        //    int i = 0;
        //    int numberOfGamesPerDay = 3;
        //    foreach (PlayersEntity playerPair in playersPairs)
        //    {
        //        if ((i % numberOfGamesPerDay) == 0)
        //        {
        //            dts = GetNextDaye(dts);
        //            dts = new DateTime(dts.Year, dts.Month, dts.Day, tournamentInfo.TimeWindowStart, 0, 0);
        //        }
        //        DateTime dte = dts.AddMinutes(15);
        //        SchedulesBL.ScheduleInsert(tournamentId, playerPair.PlayerAId, playerPair.PlayerBId, dts, dte);
        //        dts = dte;
        //        i++;
        //    }
        //}
        internal static void ScheduleGames(ref List<Matchup> matchups, DateTime dts, Guid orgId)
        {
            int numberOfGamesPerDay = 3;
            int numberOfGamesCounter = 0;
            int minutesPerGame = 15;
            CultureManager cultureManager = new CultureManager(orgId);
            IEnumerator<DateTime> dayEnumerator = cultureManager.GetNextBusinessDay(dts).GetEnumerator();
            DateTime dt;
            foreach (Matchup mu in matchups)
            {
                if (numberOfGamesCounter % numberOfGamesPerDay == 0)
                {
                    dayEnumerator.MoveNext();
                    dts = dayEnumerator.Current;
                    dts = new DateTime(dts.Year, dts.Month, dts.Day, 12, 0, 0);
                }

                mu.Start = dts;
                numberOfGamesCounter++;
                dts = dts.AddMinutes(minutesPerGame);
                mu.End = dts;
            }
        }
 protected void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
 {
     CultureManager cm = new CultureManager(this.Master.OrgBasicInfo.Id);
     cm.ClearCache();
 }
 protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
 {
     CultureManager cm = new CultureManager(this.Master.OrgBasicInfo.Id);
     cm.ClearCache();
 }
        internal static void ScheduleGames(TournamentInfo tournamentInfo)
        {
            //check arguments that are needed
            if (tournamentInfo == null)
                throw new ArgumentNullException("tournamentInfo");

            if (tournamentInfo.TournamentId == Guid.Empty)
                throw new ArgumentNullException("TorunamentId");

            if (tournamentInfo.OrganisationId == Guid.Empty)
                throw new ArgumentNullException("OrganisationId");

            if (tournamentInfo.StartDate == new DateTime())
                throw new ArgumentException("Incorrect tournament start date");

            //set up helpers.
            //need current culture.
            CultureManager cultManager = new CultureManager(tournamentInfo.OrganisationId);

            PlayerManager playerManager = new PlayerManager();
            playerManager.AlgoType = tournamentInfo.PairAlgo;
            playerManager.MatchType = tournamentInfo.MatchType;

            DateTime startDate = tournamentInfo.StartDate;

            IEnumerable<PlayersEntity> playerPairs = playerManager.BuildPairs( tournamentInfo.TournamentId);

            int i = 0;
            int numberOfGamesPerDay = 3;
            DateTime dts = startDate;
            List<Matchup> scheds = new List<Matchup>();
            //schedule 3 games per day within 15 min intervals

            IEnumerator<DateTime> dayEnumerator = cultManager.GetNextBusinessDay(startDate).GetEnumerator();

            foreach (PlayersEntity matches in playerPairs)
            {
                if ((i % numberOfGamesPerDay) == 0)
                {
                    dayEnumerator.MoveNext();
                    dts = dayEnumerator.Current;
                    dts = new DateTime(dts.Year, dts.Month, dts.Day, tournamentInfo.TimeWindowStart, 0, 0);
                }

                DateTime dte = dts.AddMinutes(15);
                //insert here
                scheds.Add(new Matchup()
                                {
                                    TournamentId = tournamentInfo.TournamentId,
                                    PlayerAId = matches.PlayerAId,
                                    PlayerA = matches.PlayerAName,
                                    PlayerBId = matches.PlayerBId,
                                    PlayerB = matches.PlayerBName,
                                    Start = dts,
                                    End = dts.AddMinutes(15) // i need end date, otherwise i cant insert.
                                });
                dts = dte;
                i++;
            }

            using (TournaDataContext db = new TournaDataContext())
            {

                //db.TournamentMatchups.DeleteAllOnSubmit(db.TournamentMatchups.Where(x => x.TournamentId == tournamentInfo.TournamentId));
                //db.TournamentMatchups.InsertAllOnSubmit(scheds);
                //db.SubmitChanges();
            }
        }