Skip to content

Commit

Permalink
Generated Stormtrooper Syndrome logic
Browse files Browse the repository at this point in the history
  • Loading branch information
softwaremagico committed Nov 13, 2023
1 parent 7720bcb commit 5d612bd
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 13 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-514h-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
6 changes: 5 additions & 1 deletion frontend/src/assets/i18n/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,11 @@
},
"stormtrooperSyndrome": {
"title": "Efecte stormtrooper",
"description": "Un equip falla tots els punts en un torneig."
"description": "Un equip falla tots els punts en un torneig.",
"normal": "Aconseguit en un torneig.",
"bronze": "Aconseguit en almenys dos tornejos successius.",
"silver": "Aconseguit en almenys tres tornejos successius.",
"gold": "Aconseguit en almenys cinc tornejos successius."
}
},
"removeTeams": "Treure Equips",
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,11 @@
},
"stormtrooperSyndrome": {
"title": "Stormtrooper-Effekt",
"description": "Einem Team fehlen in einem Turnier alle Punkte."
"description": "Einem Team fehlen in einem Turnier alle Punkte.",
"normal": "Für ein Turnier erreicht.",
"bronze": "Erreicht in mindestens zwei aufeinanderfolgenden Turnieren erreicht.",
"silver": "Erreicht in mindestens drei aufeinanderfolgenden Turnieren erreicht.",
"gold": "Während mindestens fünf aufeinanderfolgenden Turnieren erreicht."
}
},
"removeTeams": "Teams entfernenn",
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,11 @@
},
"stormtrooperSyndrome": {
"title": "Stormtrooper Syndrome",
"description": "A team miss all points on a tournament."
"description": "A team miss all points on a tournament.",
"normal": "Achieved for one tournament.",
"bronze": "Achieved for at least two consecutive tournaments.",
"silver": "Achieved for at least three consecutive tournaments.",
"gold": "Achieved for at least five consecutive tournaments."
}
},
"removeTeams": "Remove Teams",
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/assets/i18n/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,11 @@
},
"stormtrooperSyndrome": {
"title": "Efecto stormtrooper",
"description": "Un equipo falla todos los puntos en un torneo."
"description": "Un equipo falla todos los puntos en un torneo.",
"normal": "Conseguido para un torneo.",
"bronze": "Logrado durante al menos dos torneos.",
"silver": "Logrado durante al menos tres torneos.",
"gold": "Logrado durante al menos cinco torneos."
}
},
"removeTeams": "Quitar equipos",
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/assets/i18n/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,11 @@
},
"stormtrooperSyndrome": {
"title": "Effetto Stormtrooper",
"description": "Una squadra sbaglia tutti i punti in un torneo."
"description": "Una squadra sbaglia tutti i punti in un torneo.",
"normal": "Ottenuto per un torneo.",
"bronze": "Ottenuto durante almeno due tornei consecutivi.",
"silver": "Ottenuto durante almeno tre tornei consecutivi.",
"gold": "Ottenuto durante almeno cinque tornei consecutivi."
}
},
"removeTeams": "Rimuovi squadre",
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/assets/i18n/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,11 @@
},
"stormtrooperSyndrome": {
"title": "Stormtrooper-effect",
"description": "Een team mist alle punten in een toernooi."
"description": "Een team mist alle punten in een toernooi.",
"normal": "Bereikt voor één toernooi.",
"bronze": "Behaald tijdens ten minste twee opeenvolgende toernooien.",
"silver": "Behaald tijdens ten minste drie opeenvolgende toernooien.",
"gold": "Behaald tijdens ten minste vijf opeenvolgende toernooien."
}
},
"removeTeams": "Teams verwijderen",
Expand Down

0 comments on commit 5d612bd

Please sign in to comment.