Skip to content

Commit

Permalink
Merge pull request #257 from softwaremagico/251-new-achievement-a-tea…
Browse files Browse the repository at this point in the history
…m-has-draw-all-fights

251 new achievement a team has draw all fights
  • Loading branch information
softwaremagico authored Nov 14, 2023
2 parents f4463e6 + 5d612bd commit 58b4a44
Show file tree
Hide file tree
Showing 12 changed files with 1,073 additions and 860 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![GitHub commit activity](https://img.shields.io/github/commit-activity/y/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager)
[![GitHub last commit](https://img.shields.io/github/last-commit/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager)
[![CircleCI](https://circleci.com/gh/softwaremagico/KendoTournamentManager.svg?style=shield)](https://circleci.com/gh/softwaremagico/KendoTournamentManager)
[![Time](https://img.shields.io/badge/development-513.5h-blueviolet.svg)]()
[![Time](https://img.shields.io/badge/development-514.5h-blueviolet.svg)]()

[![Powered by](https://img.shields.io/badge/powered%20by%20java-orange.svg?logo=OpenJDK&logoColor=white)]()
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=kendo-tournament-backend&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=kendo-tournament-backend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@
import com.softwaremagico.kt.core.exceptions.TournamentNotFoundException;
import com.softwaremagico.kt.core.providers.AchievementProvider;
import com.softwaremagico.kt.core.providers.DuelProvider;
import com.softwaremagico.kt.core.providers.FightProvider;
import com.softwaremagico.kt.core.providers.ParticipantProvider;
import com.softwaremagico.kt.core.providers.RankingProvider;
import com.softwaremagico.kt.core.providers.RoleProvider;
import com.softwaremagico.kt.core.providers.TeamProvider;
import com.softwaremagico.kt.core.providers.TournamentProvider;
import com.softwaremagico.kt.core.score.ScoreOfCompetitor;
import com.softwaremagico.kt.core.score.ScoreOfTeam;
import com.softwaremagico.kt.persistence.entities.Achievement;
import com.softwaremagico.kt.persistence.entities.Duel;
import com.softwaremagico.kt.persistence.entities.Fight;
import com.softwaremagico.kt.persistence.entities.Participant;
import com.softwaremagico.kt.persistence.entities.Role;
import com.softwaremagico.kt.persistence.entities.Team;
import com.softwaremagico.kt.persistence.entities.Tournament;
import com.softwaremagico.kt.persistence.repositories.AchievementRepository;
import com.softwaremagico.kt.persistence.values.AchievementGrade;
Expand Down Expand Up @@ -120,8 +124,12 @@ public class AchievementController extends BasicInsertableController<Achievement

private final RoleProvider roleProvider;

private final TeamProvider teamProvider;

private final AchievementProvider achievementProvider;

private final FightProvider fightProvider;

private final DuelProvider duelProvider;

private final RankingProvider rankingProvider;
Expand All @@ -134,6 +142,10 @@ public class AchievementController extends BasicInsertableController<Achievement

private List<Duel> duelsFromTournament;

private List<Team> teamsFromTournament;

private List<Fight> fightsFromTournament;

private Map<Participant, List<Score>> scoresByParticipant = null;
private Map<Participant, List<Score>> scoresReceivedByParticipant = null;
private Map<Participant, Long> totalScoreFromParticipant = null;
Expand All @@ -145,15 +157,18 @@ public class AchievementController extends BasicInsertableController<Achievement
protected AchievementController(AchievementProvider provider, AchievementConverter converter,
TournamentConverter tournamentConverter, TournamentProvider tournamentProvider,
ParticipantProvider participantProvider, ParticipantConverter participantConverter,
RoleProvider roleProvider, AchievementProvider achievementProvider, DuelProvider duelProvider,
RoleProvider roleProvider, TeamProvider teamProvider, AchievementProvider achievementProvider,
FightProvider fightProvider, DuelProvider duelProvider,
RankingProvider rankingProvider) {
super(provider, converter);
this.tournamentConverter = tournamentConverter;
this.tournamentProvider = tournamentProvider;
this.participantProvider = participantProvider;
this.participantConverter = participantConverter;
this.roleProvider = roleProvider;
this.teamProvider = teamProvider;
this.achievementProvider = achievementProvider;
this.fightProvider = fightProvider;
this.duelProvider = duelProvider;
this.rankingProvider = rankingProvider;
}
Expand Down Expand Up @@ -184,6 +199,20 @@ private List<Duel> getDuelsFromTournament() {
return duelsFromTournament;
}

private List<Team> getTeamsFromTournament() {
if (teamsFromTournament == null) {
teamsFromTournament = teamProvider.getAll(tournament);
}
return teamsFromTournament;
}

private List<Fight> getFightsFromTournament() {
if (fightsFromTournament == null) {
fightsFromTournament = fightProvider.getFights(tournament);
}
return fightsFromTournament;
}

private Map<Participant, List<Score>> getScoresByParticipant() {
if (scoresByParticipant == null) {
scoresByParticipant = new HashMap<>();
Expand Down Expand Up @@ -324,6 +353,8 @@ public List<AchievementDTO> generateAchievements(TournamentDTO tournamentDTO) {
this.totalScoreFromParticipant = null;
this.totalScoreAgainstParticipant = null;
this.rolesByParticipant = null;
this.fightsFromTournament = null;
this.teamsFromTournament = null;

//Remove any achievement already calculated.
getProvider().delete(tournament);
Expand Down Expand Up @@ -363,6 +394,7 @@ public List<AchievementDTO> generateAchievements(TournamentDTO tournamentDTO) {
achievementsGenerated.addAll(generateTisButAScratchAchievement(tournament));
achievementsGenerated.addAll(generateFirstBloodAchievement(tournament));
achievementsGenerated.addAll(generateDarumaAchievement(tournament));
achievementsGenerated.addAll(generateStormtrooperSyndromeAchievement(tournament));

// Now generate extra grades.
achievementsGenerated.addAll(generateBillyTheKidAchievementBronze(tournament));
Expand Down Expand Up @@ -437,6 +469,9 @@ public List<AchievementDTO> generateAchievements(TournamentDTO tournamentDTO) {
achievementsGenerated.addAll(generateDarumaAchievementBronze(tournament));
achievementsGenerated.addAll(generateDarumaAchievementSilver(tournament));
achievementsGenerated.addAll(generateDarumaAchievementGold(tournament));
achievementsGenerated.addAll(generateStormtrooperSyndromeAchievementBronze(tournament));
achievementsGenerated.addAll(generateStormtrooperSyndromeAchievementSilver(tournament));
achievementsGenerated.addAll(generateStormtrooperSyndromeAchievementGold(tournament));
return convertAll(achievementsGenerated);
}

Expand Down Expand Up @@ -718,15 +753,15 @@ private List<Achievement> generateJuggernautAchievement(Tournament tournament) {
competitors.remove(duel.getCompetitor1());
}
//No hits against him
if (duel.getCompetitor2Score().size() > 0) {
if (!duel.getCompetitor2Score().isEmpty()) {
competitors.remove(duel.getCompetitor1());
}
//Max score competitor 2.
if (duel.getCompetitor2Score().size() < Duel.POINTS_TO_WIN) {
competitors.remove(duel.getCompetitor2());
}
//No hits against him
if (duel.getCompetitor1Score().size() > 0) {
if (!duel.getCompetitor1Score().isEmpty()) {
competitors.remove(duel.getCompetitor2());
}
});
Expand Down Expand Up @@ -1071,10 +1106,10 @@ private List<Achievement> generateLoveSharingAchievementGold(Tournament tourname
private List<Achievement> generateTheCastleAchievement(Tournament tournament) {
final List<Participant> competitors = participantProvider.get(tournament, RoleType.COMPETITOR);
getDuelsFromTournament().forEach(duel -> {
if (duel.getCompetitor2Score().size() > 0) {
if (!duel.getCompetitor2Score().isEmpty()) {
competitors.remove(duel.getCompetitor1());
}
if (duel.getCompetitor1Score().size() > 0) {
if (!duel.getCompetitor1Score().isEmpty()) {
competitors.remove(duel.getCompetitor2());
}
});
Expand Down Expand Up @@ -1122,7 +1157,7 @@ private List<Achievement> generateTheCastleAchievementGold(Tournament tournament
private List<Achievement> generateEntrenchedAchievement(Tournament tournament) {
final List<Participant> competitors = participantProvider.get(tournament, RoleType.COMPETITOR);
getDuelsFromTournament().forEach(duel -> {
if (duel.getCompetitor1Score().size() > 0 || duel.getCompetitor2Score().size() > 0) {
if (!duel.getCompetitor1Score().isEmpty() || !duel.getCompetitor2Score().isEmpty()) {
competitors.remove(duel.getCompetitor1());
competitors.remove(duel.getCompetitor2());
}
Expand Down Expand Up @@ -1810,4 +1845,62 @@ private List<Achievement> generateDarumaAchievementGold(Tournament tournament) {
});
return generateAchievement(AchievementType.DARUMA, AchievementGrade.GOLD, participantsDaruma, tournament);
}

/***
* Be in a team, and all members of the teams does no score in the entire tournament.
* @param tournament The tournament to check.
* @return the generated achievements.
*/
private List<Achievement> generateStormtrooperSyndromeAchievement(Tournament tournament) {
final List<Team> teams = new ArrayList<>(getTeamsFromTournament());
//Ensure that team is in fights.
final Set<Team> teamsWithFights = new HashSet<>();
getFightsFromTournament().forEach(fight -> {
//Remove a tournament if it has some score.
fight.getDuels().forEach(duel -> {
if (!duel.getCompetitor1Score().isEmpty()) {
teams.remove(fight.getTeam1());
}
if (!duel.getCompetitor2Score().isEmpty()) {
teams.remove(fight.getTeam2());
}
});
teamsWithFights.add(fight.getTeam1());
teamsWithFights.add(fight.getTeam2());
});
teams.retainAll(teamsWithFights);
return generateAchievement(AchievementType.STORMTROOPER_SYNDROME, AchievementGrade.NORMAL,
teams.stream().flatMap(team -> team.getMembers().stream()).toList(), tournament);
}

/**
* Achievement for the stormtrooper syndrome in two tournaments.
*
* @param tournament The tournament to check.
* @return a list of new achievements.
*/
private List<Achievement> generateStormtrooperSyndromeAchievementBronze(Tournament tournament) {
return generateConsecutiveGradeAchievements(tournament, DEFAULT_TOURNAMENT_NUMBER_BRONZE,
AchievementType.STORMTROOPER_SYNDROME, AchievementGrade.BRONZE);
}

/***
* Achievement for the stormtrooper syndrome in three tournaments.
* @param tournament The tournament to check.
* @return the generated achievements.
*/
private List<Achievement> generateStormtrooperSyndromeAchievementSilver(Tournament tournament) {
return generateConsecutiveGradeAchievements(tournament, DEFAULT_TOURNAMENT_NUMBER_SILVER,
AchievementType.STORMTROOPER_SYNDROME, AchievementGrade.SILVER);
}

/***
* Achievement for the stormtrooper syndrome in five tournaments.
* @param tournament The tournament to check.
* @return the generated achievements.
*/
private List<Achievement> generateStormtrooperSyndromeAchievementGold(Tournament tournament) {
return generateConsecutiveGradeAchievements(tournament, DEFAULT_TOURNAMENT_NUMBER_GOLD,
AchievementType.STORMTROOPER_SYNDROME, AchievementGrade.GOLD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class ScoreAchievementsTest extends TournamentTestUtils {

private static final String TOURNAMENT3_NAME = "Tournament 3";

private static final String TOURNAMENT4_NAME = "Tournament 4";

@Autowired
private TournamentController tournamentController;

Expand All @@ -78,6 +80,7 @@ public class ScoreAchievementsTest extends TournamentTestUtils {
private TournamentDTO tournament1DTO;
private TournamentDTO tournament2DTO;
private TournamentDTO tournament3DTO;
private TournamentDTO tournament4DTO;

private ParticipantDTO woodCutter;

Expand Down Expand Up @@ -251,6 +254,56 @@ public void prepareTournament3() {
achievementController.generateAchievements(tournament3DTO);
}

@BeforeClass(dependsOnMethods = "prepareTournament3")
public void prepareTournament4() {
tournament4DTO = addTournament(TOURNAMENT4_NAME, MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, 1);
//Create Tournament
List<FightDTO> fightDTOs = fightController.createFights(tournament4DTO.getId(), TeamsOrder.SORTED, 0, null);

//Team1 and Team2 no scores. Team1 has no scores neither against itself.

//Team1 vs Team2
fightDTOs.get(0).getDuels().get(0).setFinished(true);
fightDTOs.get(0).getDuels().get(1).setFinished(true);
fightDTOs.get(0).getDuels().get(2).setFinished(true);
fightDTOs.set(0, fightController.update(fightDTOs.get(0), null));

//Team3 vs Team2
fightDTOs.get(1).getDuels().get(0).addCompetitor1Score(Score.DO);
fightDTOs.get(1).getDuels().get(0).addCompetitor1ScoreTime(68);
fightDTOs.get(1).getDuels().get(0).setFinished(true);
fightDTOs.get(1).getDuels().get(1).setFinished(true);
fightDTOs.get(1).getDuels().get(2).setFinished(true);
fightDTOs.set(1, fightController.update(fightDTOs.get(1), null));

//Team3 vs Team4
fightDTOs.get(2).getDuels().get(0).addCompetitor1Score(Score.DO);
fightDTOs.get(2).getDuels().get(0).addCompetitor1ScoreTime(68);
fightDTOs.get(2).getDuels().get(0).setFinished(true);
fightDTOs.get(2).getDuels().get(1).addCompetitor2Score(Score.MEN);
fightDTOs.get(2).getDuels().get(1).addCompetitor2ScoreTime(68);
fightDTOs.get(2).getDuels().get(1).setFinished(true);
fightDTOs.get(2).getDuels().get(2).setFinished(true);
fightDTOs.set(2, fightController.update(fightDTOs.get(2), null));

fightDTOs.get(3).getDuels().get(0).setFinished(true);
fightDTOs.get(3).getDuels().get(1).setFinished(true);
fightDTOs.get(3).getDuels().get(2).setFinished(true);
fightDTOs.set(3, fightController.update(fightDTOs.get(3), null));

fightDTOs.get(4).getDuels().get(0).setFinished(true);
fightDTOs.get(4).getDuels().get(1).setFinished(true);
fightDTOs.get(4).getDuels().get(2).setFinished(true);
fightDTOs.set(4, fightController.update(fightDTOs.get(4), null));

fightDTOs.get(5).getDuels().get(0).setFinished(true);
fightDTOs.get(5).getDuels().get(1).setFinished(true);
fightDTOs.get(5).getDuels().get(2).setFinished(true);
fightDTOs.set(5, fightController.update(fightDTOs.get(5), null));

achievementController.generateAchievements(tournament4DTO);
}

@Test
public void checkWoodCutterAchievement() {
List<AchievementDTO> achievementsDTOs = achievementController.getAchievements(tournament1DTO, AchievementType.WOODCUTTER);
Expand Down Expand Up @@ -381,6 +434,13 @@ public void firstBloodAchievement() {
Assert.assertEquals(achievementsDTOs.size(), 6);
}

@Test
public void stormtrooperAchievement() {
List<AchievementDTO> achievementsDTOs = achievementController.getAchievements(tournament4DTO, AchievementType.STORMTROOPER_SYNDROME, AchievementGrade.NORMAL);
//Only Participant0, Participant1, Participant2, Participant3, Participant4, Participant5
Assert.assertEquals(achievementsDTOs.size(), 6);
}

@AfterClass
public void wipeOut() {
super.wipeOut();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public enum AchievementType {

FIRST_BLOOD,

DARUMA;
DARUMA,

STORMTROOPER_SYNDROME;


public static AchievementType getType(String name) {
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/models/achievement-type.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export enum AchievementType {

DARUMA = 'DARUMA',

STORMTROOPER_SYNDROME = 'STORMTROOPER_SYNDROME',

}

export namespace AchievementType {
Expand Down
Loading

0 comments on commit 58b4a44

Please sign in to comment.