/// <summary> /// Calculates the ActivityScore reward that will be awarded to teams in each bracket. /// </summary> /// <param name="contest">Contest to calculate rewards for. Must have teams loaded.</param> /// <returns>List of bracket rewards, with Bronze occupying position 0, working up to Diamond.</returns> public static List<int> CalculateBracketRewards(Contest contest) { const float DIAMOND_POT_ALLOTMENT = 9; const float PLATINUM_POT_ALLOTMENT = 6; const float GOLD_POT_ALLOTMENT = 4; const float SILVER_POT_ALLOTMENT = 2; const float BRONZE_POT_ALLOTMENT = 1; List<int> sizes = ContestDAO.CalculateBracketSizes(contest.Teams.Count); float totalAllotments = 0; totalAllotments += DIAMOND_POT_ALLOTMENT * sizes[(int)ContestBracket.Diamond]; totalAllotments += PLATINUM_POT_ALLOTMENT * sizes[(int)ContestBracket.Platinum]; totalAllotments += GOLD_POT_ALLOTMENT * sizes[(int)ContestBracket.Gold]; totalAllotments += SILVER_POT_ALLOTMENT * sizes[(int)ContestBracket.Silver]; totalAllotments += BRONZE_POT_ALLOTMENT * sizes[(int)ContestBracket.Bronze]; int bronzeReward = (int)Math.Round(contest.Reward * BRONZE_POT_ALLOTMENT / totalAllotments); int silverReward = (int)Math.Round(contest.Reward * SILVER_POT_ALLOTMENT / totalAllotments); int goldReward = (int)Math.Round(contest.Reward * GOLD_POT_ALLOTMENT / totalAllotments); int platinumReward = (int)Math.Round(contest.Reward * PLATINUM_POT_ALLOTMENT / totalAllotments); int diamondReward = (int)Math.Round(contest.Reward * DIAMOND_POT_ALLOTMENT / totalAllotments); return new List<int> { bronzeReward, silverReward, goldReward, platinumReward, diamondReward }; }
/// <summary> /// Saves a contest as a new entry in the DB. /// </summary> /// <param name="contest">Contest object to add to the DB.</param> /// <returns>ID of the created contest on success, 0 on failure.</returns> public static int CreateNewContest(Contest contest) { try { int id; using (SqlConnection connection = ConnectionManager.GetConnection()) { var data = new ActivEarthDataProvidersDataContext(connection); var contestData = new ContestDataProvider { name = contest.Name, description = contest.Description, points = contest.Points, end_mode = (byte)contest.Mode, end_goal = contest.EndCondition.EndValue, end_time = contest.EndCondition.EndTime, start = contest.StartTime, type = (byte)contest.Type, statistic = (byte)contest.StatisticBinding, }; data.ContestDataProviders.InsertOnSubmit(contestData); data.SubmitChanges(); id = contestData.id; } foreach (Team team in contest.Teams) { //TeamDAO.CreateNewTeam(team, contest.ID); } return id; } catch (Exception) { return 0; } }
public void TestGetAllContests() { using (_trans) { Log("Creating contests"); Contest contest1 = new Contest("Test Contest1", "This is a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.Steps); Contest contest2 = new Contest("Test Contest2", "This is also a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.BikeDistance); Contest contest3 = new Contest("Test Contest3", "This is another test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.RunDistance); Log("Adding contests to DB"); ContestDAO.CreateNewContest(contest1); ContestDAO.CreateNewContest(contest2); ContestDAO.CreateNewContest(contest3); Log("Verifying that GetAllContests returns three contests"); Assert.AreEqual(3, ContestDAO.GetAllContests().Count); } }
public void TestContestLockContest() { using (_trans) { InitializeTestDBEntries(); Log("Creating contest"); 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("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); ContestManager.AddGroup(id, _group1); ContestManager.AddGroup(id, _group2); Log("Locking Initialization"); ContestManager.LockContest(id); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id, true, true); Log("Verifying that contest participants have been locked"); Assert.IsTrue(retrieved.Teams[0].Members[0].Initialized); Assert.IsTrue(retrieved.Teams[0].Members[1].Initialized); Assert.IsTrue(retrieved.Teams[1].Members[0].Initialized); Assert.IsTrue(retrieved.Teams[1].Members[1].Initialized); } }
public void TestUpdateContestChangeName() { using (_trans) { InitializeTestDBEntries(); int id; string newName = "New Name"; Log("Creating contest"); Contest contest = new Contest() { Name = "Test Contest1", Description = "This is a test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.Steps, CreatorId = _user1.UserID }; Log("Adding contest to the database"); Assert.IsTrue((id = ContestDAO.CreateNewContest(contest)) > 0); Log("Loading contest from the database"); Contest retrieved = ContestDAO.GetContestFromContestId(id, false, false); Log("Verifying that a matching contest was found"); Assert.IsNotNull(retrieved); Log("Renaming the Contest"); retrieved.Name = newName; Log("Updating the contest in the DB"); Assert.IsTrue(ContestDAO.UpdateContest(retrieved)); Log("Reloading contest from the database"); Contest retrieved2 = ContestDAO.GetContestFromContestId(id, false, false); Log("Verifying that a matching contest was found"); Assert.IsNotNull(retrieved2); Log("Verifying that the contest has the new name"); Assert.AreEqual(newName, retrieved2.Name); } }
public void TestUpdateContestAddTeamMembers() { using (_trans) { InitializeTestDBEntries(); Log("Creating contest"); Contest contest = new Contest() { Name = "Test Contest1", Description = "This is a test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.Steps, CreatorId = _user1.UserID }; Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); ContestManager.AddGroup(id, _group1); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id, true, true); Log("Verifying that the two added members are found"); Assert.IsTrue(retrieved.Teams[0].ContainsMember(_user1.UserID)); Assert.IsTrue(retrieved.Teams[0].ContainsMember(_user2.UserID)); Assert.IsFalse(retrieved.Teams[0].ContainsMember(_user3.UserID)); Assert.IsFalse(retrieved.Teams[0].ContainsMember(_user4.UserID)); Log("Adding two more members to the team"); retrieved.Teams[0].Members.Add(new ContestTeamMember() { UserId = _user3.UserID }); retrieved.Teams[0].Members.Add(new ContestTeamMember() { UserId = _user4.UserID }); ContestDAO.UpdateContest(retrieved); Log("Re-loading contest from DB"); Contest retrieved2 = ContestDAO.GetContestFromContestId(id, true, true); Log("Verifying the correct number of teams"); Assert.AreEqual(retrieved.Teams.Count, retrieved2.Teams.Count); Log("Verifying that each member is found"); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(_user1.UserID)); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(_user2.UserID)); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(_user3.UserID)); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(_user4.UserID)); } }
public void TestGetTeamByTeamId() { using (_trans) { Log("Creating contest to put the team in"); Contest contest = new Contest("Test Contest1", "This is a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.Steps); Log("Adding the contest to the DB"); int contestId = ContestDAO.CreateNewContest(contest); Log("Creating team"); Team team = new Team("Test Team"); team.Add(new User("Test", "Subject1")); team.Add(new User("Test", "Subject2")); Log("Adding team to DB"); int id = TeamDAO.CreateNewTeam(team, contestId); Log("Retrieving team from DB"); Assert.IsNotNull(TeamDAO.GetTeamFromTeamId(id)); } }
/// <summary> /// Loads content specific to contest Signup. /// </summary> /// <param name="contest">The contest.</param> /// <param name="isCompeting">Whether the team is currently competeing or not.</param> private void LoadContestSignupData(Contest contest, bool isCompeting) { if (contest.Type == ContestType.Group) { if (!isCompeting) { btnLeaveContest.Visible = false; if (GroupSelection.Items.Count == 0) { List<Group> groups = GroupDAO.GetAllGroupsByOwner(user); foreach (Group group in groups) { GroupSelection.Items.Add(group.Name); } } if (GroupSelection.Items.Count != 0) { btnJoinContest.Visible = true; GroupSelection.Visible = true; } else { SignUpErrorMessage.Text = "This is a group contest. In order to join a group contest, you must " + "be a group leader of at least one group. If you are part of a group and not the leader, ask your group leader to add " + "the group to this contest."; SignUpErrorMessage.Visible = true; } } else { if (contest.Type == ContestType.Group) { ContestTeam team = TeamDAO.GetTeamFromUserIdAndContestId(user.UserID, contestId, true); Group group = GroupDAO.GetGroupFromGroupId(team.GroupId ?? -1); if (group.Owner.UserID == user.UserID) { btnLeaveContest.Visible = true; } else { btnLeaveContest.Visible = false; } } else { btnLeaveContest.Visible = true; } btnJoinContest.Visible = false; GroupSelection.Visible = false; } } else { btnJoinContest.Visible = !isCompeting; btnLeaveContest.Visible = isCompeting; } ContestSignUpPanel.Visible = true; Color[] backColors = { Color.FromArgb(34, 139, 34), Color.White }; Color[] textColors = { Color.White, Color.Black }; if (contest.Teams.Count == 0) { CurrentTeamsSignedUp.Visible = false; if (contest.Type == ContestType.Group) { NoTeamsMessage.Text = "No groups have signed up for this contest."; } else { NoTeamsMessage.Text = "No one has signed up for this contest."; } NoTeamsMessage.Visible = true; } else { CurrentTeamsSignedUp.Visible = true; NoTeamsMessage.Visible = false; CurrentTeamsSignedUp.PopulateTeamTable(contest.Teams, backColors, textColors); } }
public void TestGetContestFromContestIdPresentWithTeams() { using (_trans) { InitializeTestDBEntries(); Log("Creating contest"); Contest contest = new Contest() { Name = "Test Contest1", Description = "This is a test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.Steps, CreatorId = _user1.UserID }; Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); Contest retrieved = ContestDAO.GetContestFromContestId(id, false, false); Assert.AreEqual(0, retrieved.Reward); Log("Adding groups to the contest"); ContestManager.AddGroup(id, _group1); ContestManager.AddGroup(id, _group2); Log("Reading back from DB"); retrieved = ContestDAO.GetContestFromContestId(id, true, true); Log(String.Format("Verifying nonzero reward (Actual = {0})", retrieved.Reward)); Assert.AreNotEqual(0, retrieved.Reward); Log("Verifying the correct number of teams"); Assert.AreEqual(2, retrieved.Teams.Count); Log("Verifying that each member is found"); Assert.IsTrue(retrieved.Teams[0].ContainsMember(_user1.UserID) || retrieved.Teams[1].ContainsMember(_user1.UserID)); Assert.IsTrue(retrieved.Teams[0].ContainsMember(_user2.UserID) || retrieved.Teams[1].ContainsMember(_user2.UserID)); Assert.IsTrue(retrieved.Teams[1].ContainsMember(_user3.UserID) || retrieved.Teams[0].ContainsMember(_user3.UserID)); Assert.IsTrue(retrieved.Teams[1].ContainsMember(_user4.UserID) || retrieved.Teams[0].ContainsMember(_user4.UserID)); } }
/// <summary> /// Creates a new goal-based Contest /// </summary> /// <param name="type">Determines whether the contest in Individual or Group-based.</param> /// <param name="name">Contest Name.</param> /// <param name="description">Contest Description.</param> /// <param name="start">Time to start the contest.</param> /// <param name="end">Score at which to end the contest.</param> /// <param name="searchable">True if the Contest can be found by searching (public), false if private.</param> /// <param name="statistic">Statistic on which the Contest is based.</param> /// <param name="creatorId">UserID of the creator of the contest.</param> /// <returns>ID of the newly created Contest.</returns> public static int CreateContest(ContestType type, string name, string description, DateTime start, float end, bool searchable, Statistic statistic, int creatorId) { Contest newContest = new Contest { Name = name, Description = description, Mode = ContestEndMode.GoalBased, Type = type, StartTime = start, EndValue = end, StatisticBinding = statistic, IsSearchable = searchable, DeactivatedTime = null, IsActive = true, CreatorId = creatorId }; return ContestDAO.CreateNewContest(newContest); }
public void TestUpdateContestLockInitialization() { using (_trans) { Log("Creating contest"); Contest contest = new Contest("Test Contest1", "This is a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.Steps); Log("Creating Members"); User member1 = new User("Test", "Subject1"); User member2 = new User("Test", "Subject2"); User member3 = new User("Test", "Subject3"); User member4 = new User("Test", "Subject4"); Log("Creating Groups"); Group group1 = new Group(0, "Group 1", member1, string.Empty, new List<string>()); group1.Members.Add(member2); Group group2 = new Group(0, "Group 2", member3, string.Empty, new List<string>()); group2.Members.Add(member4); Log("Adding groups to the contest"); contest.AddGroup(group1); contest.AddGroup(group2); Log("Adding Members to DB"); Assert.Fail("Not yet implemented"); /*UserDAO.CreateNewUser(member1); UserDAO.CreateNewUser(member2); UserDAO.CreateNewUser(member3); UserDAO.CreateNewUser(member4);*/ Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id); Log("Locking Initialization"); retrieved.LockInitialValues(); Log("Updating contest entry in DB"); Assert.IsTrue(ContestDAO.UpdateContest(retrieved)); Log("Reading back from DB"); Contest retrieved2 = ContestDAO.GetContestFromContestId(id); Log("Verifying that contest participants have been locked"); Assert.IsTrue(retrieved2.Teams[0].Members[0].Initialized); Assert.IsTrue(retrieved2.Teams[0].Members[1].Initialized); Assert.IsTrue(retrieved2.Teams[1].Members[0].Initialized); Assert.IsTrue(retrieved2.Teams[1].Members[1].Initialized); } }
public void TestUpdateContestChangeName() { using (_trans) { int id; string newName = "New Name"; Log("Creating contest"); Contest contest = new Contest("Test Contest", "This is a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.Steps); Log("Adding contest to the database"); Assert.IsTrue((id = ContestDAO.CreateNewContest(contest)) > 0); Log("Loading contest from the database"); Contest retrieved = ContestDAO.GetContestFromContestId(id); Log("Verifying that a matching contest was found"); Assert.IsNotNull(retrieved); Log("Renaming the Contest"); retrieved.Name = newName; Log("Updating the contest in the DB"); Assert.IsTrue(ContestDAO.UpdateContest(retrieved)); Log("Reloading contest from the database"); Contest retrieved2 = ContestDAO.GetContestFromContestId(id); Log("Verifying that a matching contest was found"); Assert.IsNotNull(retrieved2); Log("Verifying that the contest has the new name"); Assert.AreEqual(newName, retrieved2.Name); } }
public void TestUpdateContestAddTeamMembers() { using (_trans) { Log("Creating contest"); Contest contest = new Contest("Test Contest1", "This is a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.Steps); Log("Creating Members"); User member1 = new User("Test", "Subject1"); User member2 = new User("Test", "Subject2"); User member3 = new User("Test", "Subject3"); User member4 = new User("Test", "Subject4"); Log("Creating Groups"); Group group1 = new Group(0, "Group 1", member1, string.Empty, new List<string>()); group1.Members.Add(member2); Log("Adding group to the contest"); contest.AddGroup(group1); Log("Adding Members to DB"); Assert.Fail("Not yet implemented"); /*UserDAO.CreateNewUser(member1); UserDAO.CreateNewUser(member2); UserDAO.CreateNewUser(member3); UserDAO.CreateNewUser(member4);*/ Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id); Log("Adding two more members to the team"); retrieved.Teams[0].Members.Add(new TeamMember(member3)); retrieved.Teams[0].Members.Add(new TeamMember(member4)); Log("Updating Contest in DB"); Assert.IsTrue(ContestDAO.UpdateContest(retrieved)); Log("Re-loading contest from DB"); Contest retrieved2 = ContestDAO.GetContestFromContestId(id); Log("Verifying the correct number of teams"); Assert.AreEqual(retrieved.Teams.Count, retrieved2.Teams.Count); Log("Verifying that each member is found"); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(member1)); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(member2)); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(member3)); Assert.IsTrue(retrieved2.Teams[0].ContainsMember(member4)); } }
public void TestRemoveContestFromContestId() { using (_trans) { Log("Creating contest"); Contest contest = new Contest("Test Contest1", "This is a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.Steps); Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id); Log("Verifying that the contest was retrieved"); Assert.IsNotNull(retrieved); Log("Removing contest from db"); Assert.IsTrue(ContestDAO.RemoveContestFromContestId(id)); Log("Attempting to read contest back from DB"); Contest retrieved2 = ContestDAO.GetContestFromContestId(id); Log("Verifying that the contest was not found"); Assert.IsNull(retrieved2); } }
public void TestGetAllContests() { using (_trans) { InitializeTestDBEntries(); Log("Getting pre-existing Contest count"); int contestsBefore = ContestDAO.GetActiveContests(false, false).Count; Log("Creating contests"); Contest contest1 = new Contest() { Name = "Test Contest1", Description = "This is a test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.Steps, CreatorId = _user1.UserID }; Contest contest2 = new Contest() { Name = "Test Contest2", Description = "This is also a test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.BikeDistance, CreatorId = _user1.UserID }; Contest contest3 = new Contest() { Name = "Test Contest3", Description = "This is another test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.RunDistance, CreatorId = _user1.UserID }; Log("Adding contests to DB"); int id1 = ContestDAO.CreateNewContest(contest1); int id2 = ContestDAO.CreateNewContest(contest2); int id3 = ContestDAO.CreateNewContest(contest3); Log("Verifying that GetAllContests returns three contests"); Assert.AreEqual(contestsBefore + 3, ContestDAO.GetActiveContests(false, false).Count); } }
public void TestGetContestFromContestIdPresentNoTeams() { using (_trans) { InitializeTestDBEntries(); Log("Creating contests"); Contest contest = new Contest() { Name = "Test Contest1", Description = "This is a test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.Steps, CreatorId = _user1.UserID }; Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id, false, false); Log("Verifying the integrity of the contest fields"); Assert.AreEqual(contest.Name, retrieved.Name); Assert.AreEqual(contest.Description, retrieved.Description); Assert.AreEqual(contest.Reward, retrieved.Reward); Assert.AreEqual(contest.Teams.Count, retrieved.Teams.Count); Assert.AreEqual(contest.StatisticBinding, retrieved.StatisticBinding); } }
/// <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; }
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); } }
/// <summary> /// Returns the length of a contest of a time-based contest, and estimates the length of a goal-based contest. /// </summary> /// <param name="contest">Contest to calculate the length of.</param> /// <returns>Estimated length of the contest.</returns> private static int CalculateEstimatedLengthInDays(Contest contest) { if (contest.Mode == ContestEndMode.TimeBased) { return contest.EndTime.Value.Subtract(contest.StartTime).Days; } else // ContestEndMode.GoalBased { List<float> expectedPerDay = new List<float> { 7000, 2, 4, 1, 0.6f, 1, 7, 1.25f, 0.5f, 0.5f, 0.25f }; return (int)Math.Round(contest.EndValue.Value / expectedPerDay[(int)contest.StatisticBinding]); } }
/// <summary> /// Saves a contest as a new entry in the DB. /// </summary> /// <param name="contest">Contest object to add to the DB.</param> /// <returns>ID of the created contest on success, 0 on failure.</returns> public static int CreateNewContest(Contest contest) { try { int id; using (SqlConnection connection = ConnectionManager.GetConnection()) { var data = new ActivEarthDataProvidersDataContext(connection); var contestData = new ContestDataProvider { name = contest.Name, description = contest.Description, reward = contest.Reward, end_mode = (byte)contest.Mode, end_goal = contest.EndValue, end_time = contest.EndTime, start = contest.StartTime, type = (byte)contest.Type, statistic = (byte)contest.StatisticBinding, searchable = contest.IsSearchable, active = contest.IsActive, deactivated = contest.DeactivatedTime, creator_id = contest.CreatorId }; data.ContestDataProviders.InsertOnSubmit(contestData); data.SubmitChanges(); id = contestData.id; } foreach (ContestTeam team in contest.Teams) { TeamDAO.CreateNewTeam(team); } return id; } catch (Exception) { return 0; } }
public void TestRemoveContestFromContestId() { using (_trans) { InitializeTestDBEntries(); Log("Creating contest"); Contest contest = new Contest() { Name = "Test Contest1", Description = "This is a test contest", Mode = ContestEndMode.GoalBased, Type = ContestType.Group, StartTime = DateTime.Today, EndValue = 500, IsActive = true, IsSearchable = true, StatisticBinding = Statistic.Steps, CreatorId = _user1.UserID }; Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id, false, false); Log("Verifying that the contest was retrieved"); Assert.IsNotNull(retrieved); Log("Removing contest from db"); Assert.IsTrue(ContestDAO.RemoveContestFromContestId(id)); Log("Attempting to read contest back from DB"); Contest retrieved2 = ContestDAO.GetContestFromContestId(id, false, false); Log("Verifying that the contest was not found"); Assert.IsNull(retrieved2); } }
/// <summary> /// Updates an existing Contest in the DB. /// </summary> /// <param name="contest">Contest whose record needs updating.</param> /// <returns>True on success, false on failure.</returns> public static bool UpdateContest(Contest contest) { try { using (SqlConnection connection = ConnectionManager.GetConnection()) { var data = new ActivEarthDataProvidersDataContext(connection); ContestDataProvider dbContest = (from c in data.ContestDataProviders where c.id == contest.ID select c).FirstOrDefault(); if (dbContest != null) { dbContest.name = contest.Name; dbContest.description = contest.Description; dbContest.reward = contest.Reward; dbContest.end_mode = (byte)contest.Mode; dbContest.end_goal = contest.EndValue; dbContest.end_time = contest.EndTime; dbContest.start = contest.StartTime; dbContest.type = (byte)contest.Type; dbContest.statistic = (byte)contest.StatisticBinding; dbContest.searchable = contest.IsSearchable; dbContest.active = contest.IsActive; dbContest.deactivated = contest.DeactivatedTime; dbContest.creator_id = contest.CreatorId; data.SubmitChanges(); } else { return false; } } if (contest != null) { foreach (ContestTeam team in contest.Teams) { TeamDAO.UpdateTeam(team); } } return true; } catch (Exception) { return false; } }
public void TestGetContestFromContestIdPresentNoTeams() { using (_trans) { Log("Creating contests"); Contest contest = new Contest("Test Contest1", "This is a test contest", 30, ContestEndMode.GoalBased, ContestType.Group, DateTime.Today, new EndCondition(500), Statistic.Steps); Log("Saving to DB"); int id = ContestDAO.CreateNewContest(contest); Log("Reading back from DB"); Contest retrieved = ContestDAO.GetContestFromContestId(id); Log("Verifying the integrity of the contest fields"); Assert.AreEqual(contest.Name, retrieved.Name); Assert.AreEqual(contest.Description, retrieved.Description); Assert.AreEqual(contest.Points, retrieved.Points); Assert.AreEqual(contest.Teams.Count, retrieved.Teams.Count); Assert.AreEqual(contest.StatisticBinding, retrieved.StatisticBinding); } }