Exemplo n.º 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.
            }
        }
Exemplo n.º 2
0
        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            User loggedUser = Help.GetUserSession(Session);

            if (loggedUser != null)
            {
                ErrorLabel.Visible = false;
                for (int rowNum = 1; rowNum < Table1.Rows.Count; rowNum++) //Starts at row 1 since row 0 is header row.
                {
                    DropDownList TeamScore1Control = Table1.Rows[rowNum].Cells[3].FindControl("ddl" + rowNum) as DropDownList;
                    DropDownList TeamScore2Control = Table1.Rows[rowNum].Cells[4].FindControl("ddl#" + rowNum) as DropDownList;
                    int          team1Score        = int.Parse(TeamScore1Control.SelectedValue);
                    int          team2Score        = int.Parse(TeamScore2Control.SelectedValue);
                    int          id;
                    bool         success = int.TryParse(Table1.Rows[rowNum].Cells[6].Text, NumberStyles.Any, CultureInfo.InvariantCulture, out id);
                    Debate       debate  = debates[rowNum - 1];
                    if (team1Score >= 0 && team2Score >= 0)
                    {
                        debate.Team1Score = team1Score;
                        debate.Team2Score = team2Score;

                        bool result = DatabaseHandler.UpdateDebate(Session, debate);
                        for (int i = 0; i < debates.Count; i++)
                        {
                            if (debates[i].Team1.ID == debate.Team1.ID)
                            {
                                debates[i].Team1 = debate.Team1;
                            }
                            else if (debates[i].Team2.ID == debate.Team1.ID)
                            {
                                debates[i].Team2 = debate.Team1;
                            }
                            if (debates[i].Team1.ID == debate.Team2.ID)
                            {
                                debates[i].Team1 = debate.Team2;
                            }
                            else if (debates[i].Team2.ID == debate.Team2.ID)
                            {
                                debates[i].Team2 = debate.Team2;
                            }
                        }
                    }
                    else if (team1Score >= 0 || team2Score >= 0) //If this runs then both teams were not assigned a valid score and only one of them was.
                    {
                        ErrorLabel.Text    = "Both teams must be scored. Check Debate " + id;
                        ErrorLabel.Visible = true;
                        break; //Stops the loop so the user can fix the information before submitting the rest.
                    }
                    else //There was no score at all.
                    {
                        if (debate.Team1Score >= 0 && debate.Team2Score >= 0)
                        {
                            debate.Team1Score = team1Score;
                            debate.Team2Score = team2Score;

                            bool result = DatabaseHandler.UpdateDebate(Session, debate);
                            for (int i = 0; i < debates.Count; i++)
                            {
                                if (debates[i].Team1.ID == debate.Team1.ID)
                                {
                                    debates[i].Team1 = debate.Team1;
                                }
                                else if (debates[i].Team2.ID == debate.Team1.ID)
                                {
                                    debates[i].Team2 = debate.Team1;
                                }
                                if (debates[i].Team1.ID == debate.Team2.ID)
                                {
                                    debates[i].Team1 = debate.Team2;
                                }
                                else if (debates[i].Team2.ID == debate.Team2.ID)
                                {
                                    debates[i].Team2 = debate.Team2;
                                }
                            }
                        }
                    }
                }

                //Now we determine if there is any ties and if so we must generate a new match for each tie.
                if (DatabaseHandler.DebateSeasonScored(debateSeasonID))
                {
                    DebateSeason season              = DatabaseHandler.GetDebateSeason(debateSeasonID);
                    List <Team>  teams               = season.Teams;
                    List <Team>  rankedTeams         = Help.RankTeams(teams);
                    Debate       tieBreaker          = null;
                    DateTime     tieBreakerStartDate = seasonStartDate.AddDays((seasonLength) * 7);
                    bool         seasonUpdated       = false;
                    for (int i = 0; i < rankedTeams.Count; i++)
                    {
                        for (int j = i + 1; j < rankedTeams.Count; j++)
                        {
                            if (rankedTeams[i].Rank == 1 && rankedTeams[j].Rank == 1)
                            {
                                seasonUpdated = true;
                                tieBreaker    = new Debate(0, rankedTeams[i], rankedTeams[j], -1, -1, seasonStartDate, true);
                                int id;
                                DatabaseHandler.AddDebate(Session, tieBreaker, out id);
                                tieBreaker.ID = id;
                                season.Debates.Add(tieBreaker);
                                List <DebateDate> availableDates = new List <DebateDate>();
                                while (availableDates.Count == 0)
                                {
                                    tieBreakerStartDate = tieBreakerStartDate.AddDays(7);
                                    availableDates      = Help.GetAllAvailableDates(season.Debates, tieBreaker, tieBreakerStartDate, 1);
                                }
                                tieBreaker.MorningDebate = availableDates[0].Morning;
                                tieBreaker.Date          = availableDates[0].Date;
                                DatabaseHandler.UpdateDebate(Session, tieBreaker);
                            }
                        }
                    }

                    if (seasonUpdated)
                    {
                        TimeSpan length = tieBreakerStartDate - seasonStartDate;
                        season.Length = (length.Days / 7);
                        DatabaseHandler.UpdateDebateSeason(Session, season);
                    }
                }

                TableCell resetCell;
                for (int i = 1; i < Table1.Rows.Count; i++) //We must now reset all other reschedulings so there is not any out of date possibilities.
                {
                    resetCell = Table1.Rows[i].Cells[5];
                    DropDownList list   = resetCell.FindControl(i.ToString()) as DropDownList;
                    Label        label  = resetCell.FindControl("L" + i) as Label;
                    LinkButton   button = resetCell.FindControl("R" + i) as LinkButton;
                    if (list.Visible)
                    {
                        label.Visible = true;
                        button.Text   = "Reschedule";
                        list.Visible  = false;
                    }
                }

                if (loggedUser.PermissionLevel == 2)
                {
                    Response.Redirect(Request.RawUrl);
                }
                else
                {
                    //Do nothing because you should be the Super.
                    Help.ForcePostBack(this); //We force a post back to remove the reschedule button on scored debates.
                }
            }
        }