/// <summary> /// Saves a Team as a new entry in the DB. /// </summary> /// <param name="team">Team object to add to the DB.</param> /// <returns>ID of the created team on success, 0 on failure.</returns> public static int CreateNewTeam(ContestTeam team) { try { int id; using (SqlConnection connection = ConnectionManager.GetConnection()) { var data = new ActivEarthDataProvidersDataContext(connection); var teamData = new TeamDataProvider { name = team.Name, score = team.Score, contest_id = team.ContestId, locked = team.IsLocked, group_id = team.GroupId, bracket = team.Bracket }; data.TeamDataProviders.InsertOnSubmit(teamData); data.SubmitChanges(); id = teamData.id; } TeamDAO.UpdateTeamMembers(team); return id; } catch (Exception) { return 0; } }
/// <summary> /// Makes a bracket cell for a row. /// </summary> /// <param name="team">The team corressponding to the row the cell is being /// placed in.</param> /// <returns>The Bracket cell for the row.</returns> private TableCell MakeBracketCell(ContestTeam team) { TableCell bracketCell = new TableCell(); bracketCell.HorizontalAlign = HorizontalAlign.Center; System.Web.UI.WebControls.Image bracketImage = new System.Web.UI.WebControls.Image(); bracketImage.Width = new Unit("25px"); bracketImage.Height = new Unit("25px"); bracketImage.ImageAlign = ImageAlign.Middle; if (team.Bracket == (int)ContestBracket.Bronze) { bracketImage.ImageUrl = "~/Images/Competition/Contests/BronzeBracket.png"; } else if (team.Bracket == (int)ContestBracket.Silver) { bracketImage.ImageUrl = "~/Images/Competition/Contests/SilverBracket.png"; } else if (team.Bracket == (int)ContestBracket.Gold) { bracketImage.ImageUrl = "~/Images/Competition/Contests/GoldBracket.png"; } else if (team.Bracket == (int)ContestBracket.Platinum) { bracketImage.ImageUrl = "~/Images/Competition/Contests/PlatinumBracket.png"; } else { bracketImage.ImageUrl = "~/Images/Competition/Contests/DiamondBracket.png"; } bracketCell.Controls.Add(bracketImage);; return bracketCell; }
/// <summary> /// Adds a group to a group Contest; will fail if a group is added to an individual contest. /// </summary> /// <param name="contestId">ID for the contest the group should be added to.</param> /// <param name="group">Group to be added.</param> /// <returns>True on success, false on failure.</returns> public static bool AddGroup(int contestId, Group group) { Contest contest = ContestDAO.GetContestFromContestId(contestId, false, false); if (contest.Type == ContestType.Group) { string teamName = group.Name; ContestTeam newTeam = new ContestTeam { ContestId = contestId, Name = teamName, GroupId = group.ID }; int teamId = TeamDAO.CreateNewTeam(newTeam); foreach (User user in group.Members) { TeamDAO.CreateNewTeamMember(user.UserID, teamId); } contest.Reward = ContestManager.CalculateContestReward( ContestManager.CalculateEstimatedLengthInDays(contest), TeamDAO.GetTeamsFromContestId(contestId, false).Count); ContestDAO.UpdateContest(contest); return (teamId > 0); } else { return false; } }
/// <summary> /// Adds a user to the Contest. /// </summary> /// <param name="user">User to be added.</param> public static void AddUser(int contestId, User user) { if (!TeamDAO.UserCompetingInContest(user.UserID, contestId)) { string teamName = String.Format("{0} {1}", user.FirstName, user.LastName); ContestTeam newTeam = new ContestTeam { ContestId = contestId, Name = teamName }; int teamId = TeamDAO.CreateNewTeam(newTeam); TeamDAO.CreateNewTeamMember(user.UserID, teamId); } Contest contest = ContestDAO.GetContestFromContestId(contestId, false, false); contest.Reward = ContestManager.CalculateContestReward( ContestManager.CalculateEstimatedLengthInDays(contest), TeamDAO.GetTeamsFromContestId(contestId, false).Count); ContestDAO.UpdateContest(contest); }
/// <summary> /// Updates the entry of any members already existing on the team, and creates /// new entries for new members of the team. /// </summary> /// <param name="team">Team whose members must be updated.</param> /// <returns>True on success, false on failure.</returns> public static bool UpdateTeamMembers(ContestTeam team) { try { foreach (ContestTeamMember user in team.Members) { using (SqlConnection connection = ConnectionManager.GetConnection()) { var data = new ActivEarthDataProvidersDataContext(connection); var dbUser = (from u in data.TeamMemberDataProviders where u.team_id == team.ID && u.user_id == user.UserId select u).FirstOrDefault(); if (dbUser != null) { dbUser.score = user.Score; dbUser.initial_score = user.InitialScore; dbUser.initialized = user.Initialized; data.SubmitChanges(); } else { if (CreateNewTeamMember(user, team.ID) == 0) { return false; } } } } return true; } catch (Exception) { return false; } }
/// <summary> /// Updates an existing Team in the DB. /// </summary> /// <param name="team">Team whose record needs updating.</param> /// <returns>True on success, false on failure.</returns> public static bool UpdateTeam(ContestTeam team) { try { using (SqlConnection connection = ConnectionManager.GetConnection()) { var data = new ActivEarthDataProvidersDataContext(connection); TeamDataProvider dbTeam = (from c in data.TeamDataProviders where c.id == team.ID select c).FirstOrDefault(); if (dbTeam != null) { dbTeam.name = team.Name; dbTeam.score = team.Score; dbTeam.locked = team.IsLocked; dbTeam.group_id = team.GroupId; dbTeam.contest_id = team.ContestId; dbTeam.bracket = team.Bracket; data.SubmitChanges(); UpdateTeamMembers(team); return true; } else { CreateNewTeam(team); return true; } } } catch (Exception) { return false; } }
/// <summary> /// Notes each user's state at the beginning of a contest so that /// the delta score can be calculated. /// /// Sets the initialized flag to true, allowing the calculation of /// delta scores. /// </summary> public static void LockTeam(ContestTeam team) { Statistic statistic = ContestDAO.GetStatisticFromContestId(team.ContestId); foreach (ContestTeamMember user in team.Members) { UserStatistic userStat = UserStatisticDAO.GetStatisticFromUserIdAndStatType(user.UserId, statistic); user.InitialScore = (userStat != null ? userStat.Value : 0); user.Initialized = true; } team.IsLocked = true; TeamDAO.UpdateTeam(team); }
/// <summary> /// Sets the prgress towards the goal the top contributor has made. /// </summary> /// <param name="firstPlace">The team in first.</param> /// <param name="goal">The goal of the contest.</param> /// <param name="format">The format string the goal value is.</param> public void PopulateProgressGraph(ContestTeam firstPlace, float goal, string format) { _ContestProgress.Value = (firstPlace != null ? (int)((firstPlace.Score / goal) * 100) : 0); _StartLabel.Text = 0.ToString(format); _EndLabel.Text = goal.ToString(format); }
/// <summary> /// Makes a Team cell for the row. /// </summary> /// <param name="team">The name of the team to display.</param> /// <returns>A Table Cell containing the name of the team.</returns> private TableCell MakeTeamCell(ContestTeam team) { TableCell teamCell = new TableCell(); Label teamNameLabel = new Label(); teamNameLabel.Text = team.Name; teamCell.Controls.Add(teamNameLabel); return teamCell; }
/// <summary> /// Makes a Table Cell containing the team's score. /// </summary> /// <param name="team">The team to get the score from.</param> /// <param name="scoreFormat">The format of the score.</param> /// <returns>A Table Cell containing the score.</returns> private TableCell MakeScoreCell(ContestTeam team, string scoreFormat) { TableCell scoreCell = new TableCell(); scoreCell.HorizontalAlign = HorizontalAlign.Right; Label scoreLabel = new Label(); scoreLabel.Text = team.Score.ToString(scoreFormat); scoreCell.Controls.Add(scoreLabel); return scoreCell; }
/// <summary> /// Creates a new row for the leaderboard table. /// </summary> /// <param name="team">The team corressponding to the row.</param> /// <param name="backColor">The backcolor of the row.</param> /// <param name="textColor">The text color of the row.</param> /// <param name="scoreFormat">The score format for the row.</param> /// <param name="rewards">The rewards of each bracket.</param> /// <returns>A new row for the leaderboard table.</returns> private TableRow MakeRowForTable(ContestTeam team, Color backColor, Color textColor, string scoreFormat, List<int> rewards) { TableRow newRow = new TableRow(); newRow.BackColor = backColor; newRow.ForeColor = textColor; newRow.Cells.Add(MakeBracketCell(team)); newRow.Cells.Add(MakeTeamCell(team)); newRow.Cells.Add(MakeScoreCell(team, scoreFormat)); newRow.Cells.Add(MakeRewardCell(team, rewards)); return newRow; }
/// <summary> /// Makes a table cell containing the reward the team /// will receive. /// </summary> /// <param name="team">The Team to retrieve the bracket from.</param> /// <param name="rewards">The rewards for each bracket.</param> /// <returns>A Table Cell containing the reward.</returns> private TableCell MakeRewardCell(ContestTeam team, List<int> rewards) { TableCell rewardCell = new TableCell(); rewardCell.HorizontalAlign = HorizontalAlign.Right; Label rewardLabel = new Label(); rewardLabel.Style.Add("margin-right", "5px"); if (team.Bracket == (int)ContestBracket.Bronze) { rewardLabel.Text = rewards[0].ToString(); } else if (team.Bracket == (int)ContestBracket.Silver) { rewardLabel.Text = rewards[1].ToString(); } else if (team.Bracket == (int)ContestBracket.Gold) { rewardLabel.Text = rewards[2].ToString(); } else if (team.Bracket == (int)ContestBracket.Platinum) { rewardLabel.Text = rewards[3].ToString(); } else { rewardLabel.Text = rewards[4].ToString(); } System.Web.UI.WebControls.Image activityScoreImage = new System.Web.UI.WebControls.Image(); activityScoreImage.Width = new Unit("20px"); activityScoreImage.Height = new Unit("20px"); activityScoreImage.ImageAlign = ImageAlign.Middle; activityScoreImage.ImageUrl = "~/Images/Competition/Activity_Score.png"; rewardCell.Controls.Add(rewardLabel); rewardCell.Controls.Add(activityScoreImage); return rewardCell; }
public void TestGetTeamByTeamId() { using (_trans) { InitializeTestDBEntries(); Log("Creating contest to put the team in"); Contest contest = new Contest() { Name = "Test Contest1", Description = "This is a test contest", Reward = 30, Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.Steps, CreatorId = _user1.UserID }; Log("Adding the contest to the DB"); int contestId = ContestDAO.CreateNewContest(contest); Log("Creating team"); ContestTeam team = new ContestTeam() { ContestId = contestId, Name = "Test Team" }; Log("Adding team to DB"); int teamId = TeamDAO.CreateNewTeam(team); int i = TeamDAO.CreateNewTeamMember(_user1.UserID, teamId); int j = TeamDAO.CreateNewTeamMember(_user2.UserID, teamId); Log("Retrieving team from DB"); ContestTeam notFound = TeamDAO.GetTeamFromTeamId(-1, true); ContestTeam retrieved = TeamDAO.GetTeamFromTeamId(teamId, true); Assert.IsNull(notFound); Assert.IsNotNull(retrieved); Assert.AreEqual(2, retrieved.Members.Count); } }
public void TestContestTeamScoreCalculation() { using (_trans) { InitializeTestDBEntries(); Log("Creating group contest"); int id = ContestManager.CreateContest(ContestType.Group, "Test Contest 1", "This is a test time-based contest.", DateTime.Now, DateTime.Now.AddDays(1), true, Statistic.Steps, _user1.UserID); ContestTeam team = new ContestTeam() { Name = "Team1", ContestId = id }; int teamId = TeamDAO.CreateNewTeam(team); TeamDAO.CreateNewTeamMember(_user1.UserID, teamId); TeamDAO.CreateNewTeamMember(_user2.UserID, teamId); TeamDAO.CreateNewTeamMember(_user3.UserID, teamId); TeamDAO.CreateNewTeamMember(_user4.UserID, teamId); Log("Setting individual initial statistics"); StatisticManager.SetUserStatistic(_user1.UserID, Statistic.Steps, 0); StatisticManager.SetUserStatistic(_user2.UserID, Statistic.Steps, 50); StatisticManager.SetUserStatistic(_user3.UserID, Statistic.Steps, 100); StatisticManager.SetUserStatistic(_user4.UserID, Statistic.Steps, 150); Log("Locking initial values"); ContestManager.LockContest(id); Log("Adding 50 steps to each user"); StatisticManager.SetUserStatistic(_user1.UserID, Statistic.Steps, 50); StatisticManager.SetUserStatistic(_user2.UserID, Statistic.Steps, 100); StatisticManager.SetUserStatistic(_user3.UserID, Statistic.Steps, 150); StatisticManager.SetUserStatistic(_user4.UserID, Statistic.Steps, 200); Log("Retrieving Contest from DB"); Contest contest = ContestManager.GetContest(id, true, false); Log("Verifying team aggregate score"); Assert.AreEqual(200, contest.Teams[0].Score); } }
/// <summary> /// Finds the list of teams that should be displayed to the user as part of /// the contest standings report. For the user's current bracket, the user's team /// will be shown. For brackets better than the user's current standing, the lowest /// team in the bracket is shown (the team that the user needs to pass to enter that bracket). /// For brackets worse than the user's current standing, the top team in the bracket is shown /// (the team that the user needs to stay ahead of to avoid falling into that bracket). /// </summary> /// <param name="userTeam">Team that the current user is on.</param> /// <param name="contest">Contest to be analyzed.</param> /// <returns>List of teams to display in the contest standings table.</returns> public static List<ContestTeam> GetTeamsToDisplay(ContestTeam userTeam, Contest contest) { int userBracket; List<ContestTeam> toDisplay = new List<ContestTeam>(); if (userTeam == null) { userBracket = (int)ContestBracket.Diamond + 1; } else { userBracket = userTeam.Bracket; } List<ContestTeam> diamond = contest.Teams.Where(t => t.Bracket == (int)ContestBracket.Diamond).ToList(); List<ContestTeam> platinum = contest.Teams.Where(t => t.Bracket == (int)ContestBracket.Platinum).ToList(); List<ContestTeam> gold = contest.Teams.Where(t => t.Bracket == (int)ContestBracket.Gold).ToList(); List<ContestTeam> silver = contest.Teams.Where(t => t.Bracket == (int)ContestBracket.Silver).ToList(); List<ContestTeam> bronze = contest.Teams.Where(t => t.Bracket == (int)ContestBracket.Bronze).ToList(); if ((int)ContestBracket.Diamond > userBracket) { toDisplay.Add(diamond.LastOrDefault()); } else if ((int)ContestBracket.Diamond == userBracket) { toDisplay.Add(userTeam); } else { toDisplay.Add(diamond.FirstOrDefault()); } if ((int)ContestBracket.Platinum > userBracket) { toDisplay.Add(platinum.LastOrDefault()); } else if ((int)ContestBracket.Platinum == userBracket) { toDisplay.Add(userTeam); } else { toDisplay.Add(platinum.FirstOrDefault()); } if ((int)ContestBracket.Gold > userBracket) { toDisplay.Add(gold.LastOrDefault()); } else if ((int)ContestBracket.Gold == userBracket) { toDisplay.Add(userTeam); } else { toDisplay.Add(gold.FirstOrDefault()); } if ((int)ContestBracket.Silver > userBracket) { toDisplay.Add(silver.LastOrDefault()); } else if ((int)ContestBracket.Silver == userBracket) { toDisplay.Add(userTeam); } else { toDisplay.Add(silver.FirstOrDefault()); } if ((int)ContestBracket.Bronze > userBracket) { toDisplay.Add(bronze.LastOrDefault()); } else if ((int)ContestBracket.Bronze == userBracket) { toDisplay.Add(userTeam); } else { toDisplay.Add(bronze.FirstOrDefault()); } return toDisplay; }