Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Highest Stepper property on team scoreboard #93

Merged
merged 1 commit into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion StepChallenge.Tests/TeamScoreboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,71 @@ public void Test_TeamMembersStepStatus_IsFalseIfStepsTotalIsZero()

Assert.IsFalse(resultFirstParticipantStepsStatus, $"Expected participant to have step status of False but got {resultFirstParticipantStepsStatus}");
}


/// <summary>
/// Tests that if a participant has the most steps, they are returned as the highest stepper
/// </summary>
[Test]
public void Test_TeamMembersStepStatus_ParticipantHasHighestScore()
{
var team = TestData.CreateTeamWithHighestStepper();

var teamService = new TeamService(GetMockStepContext(team));
var result = teamService.GetTeamScoreBoard(1);

var actual = false;
var participant = result.First().ParticipantsStepsStatus.SingleOrDefault(p => p.ParticipantId == 1);
if (participant != null)
{
actual = participant.ParticipantHighestStepper;
}

Assert.IsTrue(actual, $"Expected participant to have highest step status of True but got {actual}");
}

/// <summary>
/// Tests that if two participants have the most steps, they are both returned as highest stepper set to true
/// </summary>
[Test]
public void Test_TeamMembersStepStatus_ParticipantHasHighestScoreIfStepsAreSame()
{
var team = TestData.CreateTeamWithHighestStepper();
team.Participants.ElementAt(1).Steps.First().StepCount = team.Participants.ElementAt(0).Steps.First().StepCount;

var teamService = new TeamService(GetMockStepContext(team));
var result = teamService.GetTeamScoreBoard(1);

var actualOne = false;
var actualTwo = false;

var participantOne = result.First().ParticipantsStepsStatus.SingleOrDefault(p => p.ParticipantId == 1);
var participantTwo = result.First().ParticipantsStepsStatus.SingleOrDefault(p => p.ParticipantId == 1);
if (participantOne != null && participantTwo != null)
{
actualOne = participantOne.ParticipantHighestStepper;
actualTwo = participantTwo.ParticipantHighestStepper;
}

Assert.IsTrue(actualOne && actualTwo, $"Expected participants to both have highest step status of True but got {actualOne} & {actualTwo}");
}

/// <summary>
/// Tests that if all participants have zero steps, there is no highest stepper
/// </summary>
[Test]
public void Test_TeamMembersStepStatus_ParticipantsWithZeroStepsAreHighestStepper()
{
var team = TestData.CreateTeamWithHighestStepper();
team.Participants.ElementAt(0).Steps.First().StepCount = 0;
team.Participants.ElementAt(1).Steps.First().StepCount = 0;
team.Participants.ElementAt(2).Steps.First().StepCount = 0;

var teamService = new TeamService(GetMockStepContext(team));
var result = teamService.GetTeamScoreBoard(1);

Assert.IsTrue(result.All(r => r.ParticipantsStepsStatus.All(s => s.ParticipantHighestStepper == false)), $"Expected all participants to have highest step status of False but got True");
}

private StepContext GetMockStepContext(Team team)
{
var teamSteps = new List<Steps>();
Expand Down
34 changes: 34 additions & 0 deletions StepChallenge.Tests/TeamTests.Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ public static Team CreateTeamForTeamScoreboard()
};
}

public static Team CreateTeamWithHighestStepper()
{
return new Team
{
TeamId = 1,
TeamName = "Team_1",
NumberOfParticipants = 3,
Participants = new List<Participant>
{
new Participant
{
ParticipantName = "B_ParticipantNameOne",
ParticipantId = 1,
Steps = CreateSteps(10, 1, StartDate),
TeamId = 1,
},
new Participant
{
ParticipantName = "A_ParticipantNameTwo",
ParticipantId = 2,
Steps = CreateSteps(5, 2, StartDate),
TeamId = 1,
},
new Participant
{
ParticipantName = "C_ParticipantNameThree",
ParticipantId = 3,
Steps = CreateSteps(1, 3, StartDate),
TeamId = 1,
},
}
};
}

private static IQueryable<Team> CreateThreeTeams()
{
return (new List<Team>
Expand Down
5 changes: 5 additions & 0 deletions StepChallenge/ClientApp/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
font-size: 0.55em;
text-align: center;
font-weight: 700; }

.goldStar {
border: "1px solid red";
text-decoration: underline;
}
2 changes: 1 addition & 1 deletion StepChallenge/ClientApp/src/components/TeamScoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function loadUserSteps(id) {
var apiHelper = new ApiHelper()
var query = `{"query": "query teamQuery( $teamId : ID! )
{ teamSteps( teamId : $teamId )
{stepCount, dateOfSteps, participantsStepsStatus { participantName, participantAddedStepCount} },
{stepCount, dateOfSteps, participantsStepsStatus { participantName, participantAddedStepCount, participantHighestStepper} },
team (teamId: $teamId )
{teamName, numberOfParticipants, participants { participantName }}
} ",
Expand Down
7 changes: 4 additions & 3 deletions StepChallenge/ClientApp/src/components/TeamStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ class TeamStep extends Component {
var status = {
name : "User not registered yet",
initials: "-",
steps: false
steps: false,
highestStepper: false
}
if(this.state.participantsStatus[i] != null ){
status.name = this.state.participantsStatus[i].participantName
status.initials = this.state.participantsStatus[i].participantName.charAt(0).toUpperCase()
status.steps = this.state.participantsStatus[i].participantAddedStepCount
status.highestStepper = this.state.participantsStatus[i].participantHighestStepper
}
info[i] = status
}
Expand All @@ -50,13 +52,12 @@ class TeamStep extends Component {
<br/>
<div style={{"width":"100%"}}>
{info.map(s =>
<span title={this.state.steps != 0 ? s.name : "no steps counted"} className="statusInfo" style={ s.steps ? infoDone : this.state.steps != 0 ? infoMissing : null}>{s.initials}</span>
<span title={this.state.steps != 0 ? s.name + (s.highestStepper ? " - well done on being top stepper!" : "") : "no steps counted"} className={s.highestStepper == true ? "goldStar statusInfo" : "statusInfo"} style={ s.steps ? infoDone : this.state.steps != 0 ? infoMissing : null}>{s.initials}</span>
)}
</div>
</div>
)
}
}


export default TeamStep;
2 changes: 2 additions & 0 deletions StepChallenge/DataModels/TeamScoreBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class ParticipantsStepsStatus
public string ParticipantName { get; set; }
public int ParticipantId { get; set; }
public bool ParticipantAddedStepCount { get; set; }
public int ParticipantStepCount { get; set; }
public bool ParticipantHighestStepper { get; set; }
}
}
1 change: 1 addition & 0 deletions StepChallenge/Model/GraphQL/ParticipantStepStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public ParticipantStepStatusType()

Field(x => x.ParticipantName, type: typeof(StringGraphType)).Description("The Date of the step count");
Field(x => x.ParticipantAddedStepCount, type: typeof(BooleanGraphType)).Description("If the participant has filled in their steps or not");
Field(x => x.ParticipantHighestStepper, type: typeof(BooleanGraphType)).Description("Participants with the highest step count will be set to true");
}

}
Expand Down
3 changes: 2 additions & 1 deletion StepChallenge/Services/StepsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public List<TeamScores> GetTeamScores(IQueryable<Team> teams, DateTime thisMonda
{
TeamId = t.TeamId,
TeamName = t.TeamName,
NumberOfParticipants = t.NumberOfParticipants, //TODO - need to know how many are in a team, if participants haven't registered yet then we can't rely on counting all participants
NumberOfParticipants = t.NumberOfParticipants,
TeamStepCount = t.Participants.Sum(p => p.Steps
.Where(s => s.DateOfSteps >= startDate && s.DateOfSteps < thisMonday
|| t.Participants.All(u => u.Steps.All(x => x.StepCount == 0)))
.Sum(s => s.StepCount))
})
.ToList();

// add a pretend participant with averaged steps to teams with less participants
foreach (var teamScore in sortedTeams.Where(t => t.NumberOfParticipants != averageTeamSize && t.NumberOfParticipants != 0))
{
teamScore.TeamStepCount = ((teamScore.TeamStepCount / teamScore.NumberOfParticipants) *
Expand Down
19 changes: 17 additions & 2 deletions StepChallenge/Services/TeamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,28 @@ public List<TeamScoreBoard> GetTeamScoreBoard(int teamId)

foreach (var teamStep in teamSteps)
{
teamStep.ParticipantsStepsStatus = participants
var stepsStatuses = participants
.Select(p => new ParticipantsStepsStatus
{
ParticipantName = p.ParticipantName,
ParticipantId = p.ParticipantId,
ParticipantAddedStepCount = p.Steps.Any(ps => ps.DateOfSteps == teamStep.DateOfSteps && ps.StepCount != 0)
ParticipantAddedStepCount =
p.Steps.Any(ps => ps.DateOfSteps == teamStep.DateOfSteps && ps.StepCount != 0),
ParticipantStepCount = p.Steps.Where(ps => ps.DateOfSteps == teamStep.DateOfSteps).Sum(ps => ps.StepCount),
})
.OrderByDescending(p => p.ParticipantStepCount)
.ToList();

var highest = stepsStatuses.First().ParticipantStepCount;
if (highest != 0)
{
foreach (var stepStatus in stepsStatuses.Where(p => p.ParticipantStepCount == highest))
{
stepStatus.ParticipantHighestStepper = true;
}
}

teamStep.ParticipantsStepsStatus = stepsStatuses
.OrderBy(p => p.ParticipantName)
.ToList();
}
Expand Down