Skip to content

Commit

Permalink
#151 First draft implementation for team score claulation in backend
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Strittmatter <[email protected]>
  • Loading branch information
Weltraumschaf committed Nov 22, 2022
1 parent 08ac7ab commit 059f8b2
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.iteratec.teamdojo.service.dto.*;
import com.iteratec.teamdojo.service.dto.custom.TeamScoreDTO;
import java.util.List;
import lombok.NonNull;

/**
* This service calculates the team-score for given teams
Expand All @@ -20,23 +19,4 @@ public interface CustomTeamScoreService {
* @return never {@code null}
*/
TeamScoreDTO calculateTeamScore(TeamDTO t);

/**
* Calculates the score for given team
*
* @param t must not be {@code null}
* @param teamSkills skills already achieved by the team
* @param skills all available skills
* @param badges all available badges
* @return never {@code null}
*/
TeamScoreDTO calculateTeamScore(
TeamDTO t,
List<TeamSkillDTO> teamSkills,
List<SkillDTO> skills,
List<BadgeDTO> badges,
List<BadgeSkillDTO> badgeSkills,
List<LevelDTO> levels,
List<LevelSkillDTO> levelSkills
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ public class TeamScoreDTO {

public static final TeamScoreDTO NULL = new TeamScoreDTO();

private TeamDTO team;
private Long score;
// TODO Define API based on UI requirements. Seems that we need the properties from Progress and irrelevancy.

/**
* TODO: Please refine.
* Number of achieved skills?
* Not negative?
*/
private int achieved;
/**
* TODO: Please refine.
* How many skills are required?
* Not negative?
*/
private int required;
/**
* TODO: Please refine.
* Not negative?
*/
private int totalScore;
private float progressInPercent;
private boolean completed;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
package com.iteratec.teamdojo.service.impl.custom;

import com.iteratec.teamdojo.service.custom.CustomTeamScoreService;
import com.iteratec.teamdojo.service.dto.*;
import com.iteratec.teamdojo.service.dto.TeamDTO;
import com.iteratec.teamdojo.service.dto.custom.TeamScoreDTO;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

Expand All @@ -18,65 +16,32 @@
public class CustomTeamScoreServiceImpl implements CustomTeamScoreService {

@Override
public TeamScoreDTO calculateTeamScore(TeamDTO t) {
return TeamScoreDTO.NULL;
public TeamScoreDTO calculateTeamScore(@NonNull final TeamDTO t) {
final var achieved = calculateAchieved();
final var required = calculateRequired();
final var totalScore = calculateTotalScore();
final var progressInPercent = calculateProgressInPercent();
final var completed = calculateCompleted(achieved, required);
return new TeamScoreDTO(achieved, required, totalScore, progressInPercent, completed);
}

@Override
public TeamScoreDTO calculateTeamScore(
TeamDTO t,
List<TeamSkillDTO> teamSkills,
List<SkillDTO> skills,
List<BadgeDTO> badges,
List<BadgeSkillDTO> badgeSkills,
List<LevelDTO> levels,
List<LevelSkillDTO> levelSkills
) {
final var groupedBadgeSkills = this.groupBadgeSkillsByBadgeId(badgeSkills);
final var groupedLevelskills = this.groupLevelSkillsByLevelId(levelSkills);

final var skillScore = this.calcSkillScore(t, teamSkills, skills);
final var levelBonus = this.calcLevelBonus(t, teamSkills, skills, groupedLevelskills);
final var badgeBonus = this.calcBadgeBonus(t, teamSkills, skills, badges, groupedBadgeSkills);
final var totalScore = skillScore + levelBonus + badgeBonus;
return new TeamScoreDTO(t, totalScore);
}

private Long calcSkillScore(TeamDTO t, List<TeamSkillDTO> teamSkills, List<SkillDTO> skills) {
var score = skills.stream().filter(skill -> this.isSkillCompleted(teamSkills, skill)).mapToLong(SkillDTO::getScore).sum();
return score;
}

private Long calcLevelBonus(
TeamDTO t,
List<TeamSkillDTO> teamSkills,
List<SkillDTO> skills,
Map<Long, List<LevelSkillDTO>> groupedLevelSkills
) {
var score = t.getParticipations();

return 0L;
int calculateTotalScore() {
return 0;
}

private Long calcBadgeBonus(
TeamDTO t,
List<TeamSkillDTO> teamSkills,
List<SkillDTO> skills,
List<BadgeDTO> badges,
Map<Long, List<BadgeSkillDTO>> groupedBadgeSkills
) {
return 0L;
int calculateRequired() {
return 0;
}

private boolean isSkillCompleted(List<TeamSkillDTO> teamSkills, SkillDTO skill) {
return false;
int calculateAchieved() {
return 0;
}

private Map<Long, List<LevelSkillDTO>> groupLevelSkillsByLevelId(List<LevelSkillDTO> levelSkills) {
return levelSkills.stream().collect(Collectors.groupingBy(levelSkill -> levelSkill.getLevel().getId()));
float calculateProgressInPercent() {
return 0.0f;
}

private Map<Long, List<BadgeSkillDTO>> groupBadgeSkillsByBadgeId(List<BadgeSkillDTO> badgeSkills) {
return badgeSkills.stream().collect(Collectors.groupingBy(badgeSkill -> badgeSkill.getBadge().getId()));
boolean calculateCompleted(final int achieved, final int required) {
return achieved >= required;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.iteratec.teamdojo.service.impl.custom;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import org.junit.jupiter.api.Test;

/**
*
*/
class CustomTeamScoreServiceImplTest {

private final CustomTeamScoreServiceImpl sut = new CustomTeamScoreServiceImpl();

@Test
void calculateProgress() {}

@Test
void calculateTotalScore() {}

@Test
void calculateRequired() {}

@Test
void calculateAchieved() {}

@Test
void calculateProgressInPercent() {}

@Test
void calculateCompleted() {
assertAll(
() -> assertThat(sut.calculateCompleted(-1, 0)).isFalse(),
() -> assertThat(sut.calculateCompleted(0, -1)).isTrue(),
() -> assertThat(sut.calculateCompleted(-1, -1)).isTrue(),
() -> assertThat(sut.calculateCompleted(0, 0)).isTrue(),
() -> assertThat(sut.calculateCompleted(1, 0)).isTrue(),
() -> assertThat(sut.calculateCompleted(2, 0)).isTrue(),
() -> assertThat(sut.calculateCompleted(3, 0)).isTrue(),
() -> assertThat(sut.calculateCompleted(3, 1)).isTrue(),
() -> assertThat(sut.calculateCompleted(3, 2)).isTrue(),
() -> assertThat(sut.calculateCompleted(3, 3)).isTrue(),
() -> assertThat(sut.calculateCompleted(3, 4)).isFalse(),
() -> assertThat(sut.calculateCompleted(3, 5)).isFalse(),
() -> assertThat(sut.calculateCompleted(Integer.MAX_VALUE, Integer.MAX_VALUE)).isTrue(),
() -> assertThat(sut.calculateCompleted(Integer.MAX_VALUE, Integer.MIN_VALUE)).isTrue(),
() -> assertThat(sut.calculateCompleted(Integer.MIN_VALUE, Integer.MAX_VALUE)).isFalse(),
() -> assertThat(sut.calculateCompleted(Integer.MIN_VALUE, Integer.MIN_VALUE)).isTrue()
);
}
}

0 comments on commit 059f8b2

Please sign in to comment.