コード例 #1
0
        protected void Button_CreateSchedule_Click(object sender, EventArgs e)
        {
            int  currentSeasonID = Help.GetDebateSeasonID(Application);
            bool seasonHasScore  = DatabaseHandler.DebateSeasonHasAScore(currentSeasonID);

            if (currentSeasonID == -1 || !seasonHasScore) //If the current debate season is -1 or if the current season does not have a score set we can create a schedule...
            {
                bool errorOccured = false;

                List <Team> teams = GetTeams();

                if (nameError)
                {
                    Label_ScheduleError.Text    = "There are errors with the info given. Some team names are invalid.";
                    Label_ScheduleError.Visible = true;
                    errorOccured = true;
                }

                if (Calendar_Start.SelectedDates.Count <= 0)
                {
                    Label_ScheduleError.Text    = "There are errors with the info given. There is no start date specified.";
                    Label_ScheduleError.Visible = true;
                    errorOccured = true;
                }

                if (!errorOccured)
                {
                    Label_ScheduleError.Visible = false;
                    //Generate schedule:
                    DateTime startDate    = Calendar_Start.SelectedDate;
                    int      seasonLength = int.Parse(DropDownList_Weeks.SelectedValue);
                    DateTime endDate      = startDate.AddDays((seasonLength - 1) * 7);

                    //Adding the teams to the database
                    for (int i = 0; i < teams.Count; i++)
                    {
                        if (updateTeams.Count > 0 && i < updateTeams.Count)
                        {
                            teams[i].ID = updateTeams[i].ID;
                            DatabaseHandler.UpdateTeam(Session, teams[i]); //We update the team rather than creating a new one since these teams already exist in the database.
                        }
                        else
                        {
                            int id;
                            DatabaseHandler.AddTeam(Session, teams[i], out id);
                            teams[i].ID = id;
                        }
                    }

                    //Creating the actual debates
                    List <DateTime> saturdays = Help.SatBetween(startDate, endDate);
                    List <Debate>   debates   = Help.MatchMake(saturdays, teams);

                    if (debates != null) //Creating the debates was successful.
                    {
                        foreach (Debate d in debates)
                        {
                            int assignedID;
                            d.Team1Score = -1;
                            d.Team2Score = -1;
                            DatabaseHandler.AddDebate(Session, d, out assignedID);
                            d.ID = assignedID;
                        }

                        int          seasonID  = currentSeasonID;
                        DebateSeason newSeason = new DebateSeason(currentSeasonID, false, teams, debates, startDate, seasonLength);

                        if (currentSeasonID == -1) //If the current season ID does not exist we create a new debate season.
                        {
                            DatabaseHandler.AddDebateSeason(Session, newSeason, out seasonID);
                        }
                        else //If the current season ID is a thing, we need to update the debate season instead.
                        {
                            DatabaseHandler.UpdateDebateSeason(Session, newSeason);
                        }

                        Help.SetDebateID(Application, seasonID);

                        Response.Redirect(Help.scheduleURL);
                    }
                    else
                    {
                        foreach (Team t in teams) //We must remove all the teams added to the database since there is no possible pairing.
                        {
                            DatabaseHandler.RemoveTeam(Session, t.ID, false);
                        }

                        Label_ScheduleError.Text    = "A debate pairing was not found. There are too many teams for " + seasonLength + " weeks. Increase the season length.";
                        Label_ScheduleError.Visible = true;
                    }
                }
            }
            else
            {
                Panel_RecreateSeason.Visible = false;
                Response.Redirect(Request.RawUrl); //We refresh the page so that the correct view of the page is shown.
            }
        }