示例#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.
            }
        }
        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.
                }
            }
        }
示例#3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            int currentDebateID = Help.GetDebateSeasonID(Application);

            if (currentDebateID != -1)
            {
                minimumSeasonIndex = 2;
            }


            //Gathering query values
            NameValueCollection queryValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            string pageString = queryValues.Get("page");

            if (pageString != null)
            {
                bool result = int.TryParse(pageString, out seasonIndex);
                if (!result) //If we could not parse the string then we reset the index.
                {
                    seasonIndex = minimumSeasonIndex;
                }
            }

            if (seasonIndex <= minimumSeasonIndex)
            {
                seasonIndex = minimumSeasonIndex;
                Button_PreviousSeason.Visible = false;
            }

            Panel_Search.Visible          = true;
            Panel_NoSearch.Visible        = false;
            Button_NextSeason.Enabled     = true;
            Button_PreviousSeason.Enabled = true;

            object teamOrderTypeObj   = ViewState["tOrderType"];  //Team order type
            object debateOrderTypeObj = ViewState["dOrderType"];  //Debate order type
            object teamOrderVarObj    = ViewState["teamOrder"];   //The order of teams
            object debateOrderVarObj  = ViewState["debateOrder"]; //The order of debates.
            object dateOrderObj       = ViewState["date"];
            object teamOrderObj       = ViewState["team"];
            object seasonObj          = ViewState["season"];

            if (debateOrderTypeObj != null)
            {
                debateOrderType = (OrderBy)(int.Parse(debateOrderTypeObj.ToString()));
            }
            if (debateOrderVarObj != null)
            {
                debateOrderVar = (DebateOrderVar)(int.Parse(debateOrderVarObj.ToString()));
            }
            if (teamOrderTypeObj != null)
            {
                teamOrderType = (OrderBy)(int.Parse(teamOrderTypeObj.ToString()));
            }
            if (teamOrderVarObj != null)
            {
                teamOrderVar = (TeamOrderVar)(int.Parse(teamOrderVarObj.ToString()));
            }

            if (seasonObj == null)
            {
                List <DebateSeason> debateSeasons = DatabaseHandler.GetDebateSeasons(seasonIndex, seasonIndex);
                if (debateSeasons.Count > 0)
                {
                    debateSeason = debateSeasons[0];
                }
                else  //No debates were found.. ut oh.
                {
                    Button_NextSeason.Visible = false;
                    Panel_PagePanel.Visible   = false;
                    Label_SeasonTitle.Text    = "No debate seasons here!";
                }
            }
            else
            {
                debateSeason = seasonObj as DebateSeason;
            }

            ViewState["season"] = debateSeason;

            if (debateSeason != null) //We do not display the current ongoing debate season.
            {
                if (debateSeason.ID != currentDebateID)
                {
                    //Setting up the enviroment
                    currentDebateSeason = debateSeason.ID;
                    DateTime startDate = debateSeason.StartDate;
                    DateTime endDate   = debateSeason.StartDate.AddDays(debateSeason.Length * 7);
                    Label_SeasonTitle.Text = "Debate Season that started on " + startDate.ToString("MM/dd/yy") + " and ended on " + endDate.ToString("MM/dd/yy");

                    //Generating the team rankings table
                    teams = debateSeason.Teams;
                    teams = Help.RankTeams(teams);

                    teams = Help.OrderTeams(teamOrderType, teamOrderVar, teams);

                    TableRow header = CreateTeamHeaderRow();
                    Table_Rankings.Rows.Add(header);

                    for (int i = 0; i < teams.Count; i++)
                    {
                        TableRow teamRow = Help.CreateTeamRow(teams[i], i, i);
                        Table_Rankings.Rows.Add(teamRow);
                    }

                    //Generating the search options and debate table...
                    if (dateOrderObj != null)
                    {
                        int  val;
                        bool result = int.TryParse(dateOrderObj.ToString(), out val);
                        if (result && val > 0 && val <= debateSeason.Length)
                        {
                            dateOrder = val;
                        }
                    }
                    if (teamOrderObj != null)
                    {
                        int  val;
                        bool result = int.TryParse(teamOrderObj.ToString(), out val);
                        if (result && val > 0 && val <= debateSeason.Teams.Count)
                        {
                            teamOrder = val;
                        }
                    }

                    debates = debateSeason.Debates;

                    debates = Help.OrderDebates(debateOrderType, debateOrderVar, debates);

                    TableRow debateHeader = CreateDebateHeaderRow();
                    TableData.Rows.Add(debateHeader);

                    seasonStart  = debateSeason.StartDate;
                    seasonLength = debateSeason.Length;

                    DateTime currentDate = seasonStart;

                    teams = debateSeason.Teams;

                    if (!IsPostBack) //We do not do this on page post backs (IE: After searches).
                    {
                        //Creating the team drop down
                        for (int i = 0; i < teams.Count; i++)
                        {
                            string   val      = (i + 1).ToString();
                            ListItem teamItem = new ListItem(teams[i].Name, val);
                            DropDownList_TeamName.Items.Add(teamItem);
                        }

                        //Creating the date drop down
                        for (int i = 1; i <= seasonLength; i++)
                        {
                            string   val      = i.ToString();
                            ListItem dateItem = new ListItem(currentDate.ToString("MM/dd/yy"), val);
                            DropDownList_Date.Items.Add(dateItem);
                            currentDate = currentDate.AddDays(7);
                        }
                    }

                    int addedRows = 0;
                    //Adding the debates to the table
                    if ((teamOrder > 0 || dateOrder > 0))
                    {
                        if (teamOrder > 0)
                        {
                            searchName = teams[(teamOrder - 1)].Name;
                            if (!IsPostBack)
                            {
                                DropDownList_TeamName.Items.FindByValue((teamOrder.ToString())).Selected = true;
                            }
                        }
                        if (dateOrder > 0)
                        {
                            searchDate = seasonStart.AddDays((dateOrder - 1) * 7);
                            if (!IsPostBack)
                            {
                                DropDownList_Date.Items.FindByValue((dateOrder.ToString())).Selected = true;
                            }
                        }

                        foreach (Debate d in debates)
                        {
                            if (dateOrder == 0 || (d.Date.Month == searchDate.Month &&
                                                   d.Date.Day == searchDate.Day &&
                                                   d.Date.Year == searchDate.Year))
                            {
                                if (teamOrder == 0 || (d.Team1.Name == searchName ||
                                                       d.Team2.Name == searchName))
                                {
                                    TableRow debateRow = Help.CreateDebateRow(d, includeVs, addedRows);
                                    TableData.Rows.Add(debateRow);
                                    addedRows++;
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (Debate d in debates)
                        {
                            TableRow debateRow = Help.CreateDebateRow(d, includeVs, addedRows);
                            TableData.Rows.Add(debateRow);
                            addedRows++;
                        }
                    }
                    if (addedRows <= 0)
                    {
                        TableData.Visible      = false;
                        Panel_NoSearch.Visible = true;
                        Label_NoSearch.Text    = "No results match your search.";
                    }
                    else
                    {
                        TableData.Visible      = true;
                        Panel_NoSearch.Visible = false;
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int currentDebateID = Help.GetDebateSeasonID(Application);

            Panel_Searching.Visible = true;
            Panel_NoDebate.Visible  = false;

            if (currentDebateID != -1)
            {
                object orderObj       = ViewState["Order"];
                object debateOrderObj = ViewState["dOrder"];
                object dateOrderObj   = ViewState["date"];
                object teamOrderObj   = ViewState["team"];

                if (orderObj != null)
                {
                    order = (OrderBy)(int.Parse(orderObj.ToString()));
                }
                if (debateOrderObj != null)
                {
                    dOrder = (DebateOrderVar)(int.Parse(debateOrderObj.ToString()));
                }

                object savedSeason = ViewState["Season"];

                if (savedSeason == null)
                {
                    debateSeason = DatabaseHandler.GetDebateSeason(currentDebateID);
                }
                else
                {
                    debateSeason = savedSeason as DebateSeason;
                }

                ViewState["Season"] = debateSeason;

                if (dateOrderObj != null)
                {
                    int  val;
                    bool result = int.TryParse(dateOrderObj.ToString(), out val);
                    if (result && val > 0 && val <= debateSeason.Length)
                    {
                        dateOrder = val;
                    }
                }
                if (teamOrderObj != null)
                {
                    int  val;
                    bool result = int.TryParse(teamOrderObj.ToString(), out val);
                    if (result && val > 0 && val <= debateSeason.Teams.Count)
                    {
                        teamOrder = val;
                    }
                }

                debates = debateSeason.Debates;

                debates = Help.OrderDebates(order, dOrder, debates);

                TableRow header = CreateHeaderRow();
                Table1.Rows.Add(header);

                seasonStart  = debateSeason.StartDate;
                seasonLength = debateSeason.Length;

                DateTime currentDate = seasonStart;

                teams = debateSeason.Teams;

                if (!IsPostBack) //We do not do this on page post backs (IE: After searches).
                {
                    //Creating the team drop down
                    for (int i = 0; i < teams.Count; i++)
                    {
                        string   val      = (i + 1).ToString();
                        ListItem teamItem = new ListItem(teams[i].Name, val);
                        DropDownList_TeamName.Items.Add(teamItem);
                    }

                    //Creating the date drop down
                    for (int i = 1; i <= seasonLength; i++)
                    {
                        string   val      = i.ToString();
                        ListItem dateItem = new ListItem(currentDate.ToString("MM/dd/yy"), val);
                        DropDownList_Date.Items.Add(dateItem);
                        currentDate = currentDate.AddDays(7);
                    }
                }

                int addedRows = 0;
                //Adding the debates to the table
                if ((teamOrder > 0 || dateOrder > 0))
                {
                    if (teamOrder > 0)
                    {
                        searchName = teams[(teamOrder - 1)].Name;
                        if (!IsPostBack)
                        {
                            DropDownList_TeamName.Items.FindByValue((teamOrder.ToString())).Selected = true;
                        }
                    }
                    if (dateOrder > 0)
                    {
                        searchDate = seasonStart.AddDays((dateOrder - 1) * 7);
                        if (!IsPostBack)
                        {
                            DropDownList_Date.Items.FindByValue((dateOrder.ToString())).Selected = true;
                        }
                    }

                    foreach (Debate d in debates)
                    {
                        if (dateOrder == 0 || (d.Date.Month == searchDate.Month &&
                                               d.Date.Day == searchDate.Day &&
                                               d.Date.Year == searchDate.Year))
                        {
                            if (teamOrder == 0 || (d.Team1.Name == searchName ||
                                                   d.Team2.Name == searchName))
                            {
                                TableRow debateRow = Help.CreateDebateRow(d, true, addedRows);
                                Table1.Rows.Add(debateRow);
                                addedRows++;
                            }
                        }
                    }
                }
                else
                {
                    foreach (Debate d in debates)
                    {
                        TableRow debateRow = Help.CreateDebateRow(d, true, addedRows);
                        Table1.Rows.Add(debateRow);
                        addedRows++;
                    }
                }
                if (addedRows <= 0)
                {
                    Table1.Visible         = false;
                    Panel_NoDebate.Visible = true;
                    Label_NoSchedule.Text  = "No results match your search.";
                }
                else
                {
                    Table1.Visible         = true;
                    Panel_NoDebate.Visible = false;
                }
            }
            else
            {
                Panel_Searching.Visible = false;
                Panel_NoDebate.Visible  = true;
            }
        }