From f6b381894c86abfc3d1365eecff498acf3732abb Mon Sep 17 00:00:00 2001 From: Software Magico Date: Thu, 25 Apr 2024 15:24:58 +0200 Subject: [PATCH] Generating achievement and test --- README.md | 2 +- .../controller/AchievementController.java | 62 ++- .../kt/core/providers/DuelProvider.java | 4 + .../SithKillsMasterAchievementsTest.java | 441 ++++++++++++++++++ .../src/test/resources/testng.xml | 2 + .../kt/persistence/entities/Duel.java | 9 + .../repositories/DuelRepository.java | 4 + 7 files changed, 518 insertions(+), 6 deletions(-) create mode 100644 backend/kendo-tournament-core/src/test/java/com/softwaremagico/kt/core/tests/achievements/SithKillsMasterAchievementsTest.java diff --git a/README.md b/README.md index 20d6904dd..107915260 100644 --- a/README.md +++ b/README.md @@ -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-597.5h-blueviolet.svg)]() +[![Time](https://img.shields.io/badge/development-599h-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) diff --git a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/AchievementController.java b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/AchievementController.java index aff0c04b7..5706af169 100644 --- a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/AchievementController.java +++ b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/AchievementController.java @@ -101,6 +101,11 @@ public class AchievementController extends BasicInsertableController getAchievements(TournamentDTO tournamentDTO, Achieve return convertAll(getProvider().get(tournamentConverter.reverse(tournamentDTO), achievementType)); } + public List getAchievements(TournamentDTO tournamentDTO) { + return convertAll(getProvider().get(tournamentConverter.reverse(tournamentDTO))); + } + public List getAchievements(TournamentDTO tournamentDTO, AchievementType achievementType, AchievementGrade achievementGrade) { return convertAll(getProvider().get(tournamentConverter.reverse(tournamentDTO), achievementType, achievementGrade)); } @@ -1981,14 +1990,57 @@ private List generateVendettaAchievement(Tournament tournament) { participants, tournament); } + /** + * Somebody wins a fight against other participant that is always winning him. + * + * @param tournament + * @return + */ private List generateSithApprenticesAlwaysKillTheirMasterAchievement(Tournament tournament) { - getFightsFromTournament().forEach(fight -> fight.getDuels().forEach(duel -> { - if (duel.getWinner() < 0) { - - } else if (duel.getWinner() > 0) { + final List achievements = new ArrayList<>(); + getFightsFromTournament().forEach(fight -> { + for (Duel duel : fight.getDuels()) { + final List previousDuels = duelProvider.getWhenBothAreInvolved(duel.getCompetitor1(), duel.getCompetitor2()); + boolean isApprentice = true; + int numberOfPreviousDuels = 0; + for (Duel previousDuel : previousDuels) { + if (previousDuel.getCreatedAt().isBefore(tournament.getCreatedAt())) { + numberOfPreviousDuels++; + //Check if he has already won vs the master + if (Objects.equals(duel.getCompetitorWinner(), previousDuel.getCompetitorWinner()) || previousDuel.getWinner() == 0) { + isApprentice = false; + break; + } + } + } + if (isApprentice) { + //Generate achievement depending on the number of fights. + switch (numberOfPreviousDuels) { + case 0: + case 1: + //At least, the other participant must have won you two times. + break; + case MINIMUM_LOST_SITH_NORMAL: + achievements.add(new Achievement(duel.getCompetitorWinner(), tournament, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER, + AchievementGrade.NORMAL)); + break; + case MINIMUM_LOST_SITH_BRONZE: + achievements.add(new Achievement(duel.getCompetitorWinner(), tournament, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER, + AchievementGrade.BRONZE)); + break; + case MINIMUM_LOST_SITH_SILVER: + achievements.add(new Achievement(duel.getCompetitorWinner(), tournament, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER, + AchievementGrade.SILVER)); + break; + default: + achievements.add(new Achievement(duel.getCompetitorWinner(), tournament, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER, + AchievementGrade.GOLD)); + } + } } - } + }); + return achievementProvider.saveAll(achievements); } diff --git a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/DuelProvider.java b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/DuelProvider.java index 4aa596af8..9615fe660 100644 --- a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/DuelProvider.java +++ b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/DuelProvider.java @@ -71,6 +71,10 @@ public List get(Participant participant) { return getRepository().findByParticipant(participant); } + public List getWhenBothAreInvolved(Participant participant1, Participant participant2) { + return getRepository().findByParticipants(participant1, participant2); + } + public List get(Tournament tournament) { return getRepository().findByTournament(tournament); } diff --git a/backend/kendo-tournament-core/src/test/java/com/softwaremagico/kt/core/tests/achievements/SithKillsMasterAchievementsTest.java b/backend/kendo-tournament-core/src/test/java/com/softwaremagico/kt/core/tests/achievements/SithKillsMasterAchievementsTest.java new file mode 100644 index 000000000..310b9a8c6 --- /dev/null +++ b/backend/kendo-tournament-core/src/test/java/com/softwaremagico/kt/core/tests/achievements/SithKillsMasterAchievementsTest.java @@ -0,0 +1,441 @@ +package com.softwaremagico.kt.core.tests.achievements; + +/*- + * #%L + * Kendo Tournament Manager (Core) + * %% + * Copyright (C) 2021 - 2023 Softwaremagico + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * #L% + */ + +import com.softwaremagico.kt.core.TournamentTestUtils; +import com.softwaremagico.kt.core.controller.AchievementController; +import com.softwaremagico.kt.core.controller.DuelController; +import com.softwaremagico.kt.core.controller.FightController; +import com.softwaremagico.kt.core.controller.models.AchievementDTO; +import com.softwaremagico.kt.core.controller.models.FightDTO; +import com.softwaremagico.kt.core.controller.models.TournamentDTO; +import com.softwaremagico.kt.core.managers.TeamsOrder; +import com.softwaremagico.kt.persistence.values.AchievementGrade; +import com.softwaremagico.kt.persistence.values.AchievementType; +import com.softwaremagico.kt.persistence.values.Score; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.time.LocalDateTime; +import java.util.List; + +@SpringBootTest +@Test(groups = {"sithKillsMasterAchievementsTest"}) +public class SithKillsMasterAchievementsTest extends TournamentTestUtils { + private static final int MEMBERS = 3; + private static final int TEAMS = 3; + + private static final int REFEREES = 0; + + private static final int ORGANIZER = 0; + + private static final int VOLUNTEER = 0; + + private static final int PRESS = 0; + + private static final String TOURNAMENT1_NAME = "Tournament 1"; + + private static final String TOURNAMENT2_NAME = "Tournament 2"; + + private static final String TOURNAMENT3_NAME = "Tournament 3"; + + private static final String TOURNAMENT4_NAME = "Tournament 4"; + private static final String TOURNAMENT5_NAME = "Tournament 5"; + private static final String TOURNAMENT6_NAME = "Tournament 6"; + + private static final int TOURNAMENT1_DELAY = 10; + + private static final int TOURNAMENT2_DELAY = 9; + + private static final int TOURNAMENT3_DELAY = 8; + + private static final int TOURNAMENT4_DELAY = 7; + private static final int TOURNAMENT5_DELAY = 6; + private static final int TOURNAMENT6_DELAY = 5; + + @Autowired + private DuelController duelController; + + @Autowired + private FightController fightController; + + @Autowired + private AchievementController achievementController; + + private TournamentDTO tournament1DTO; + private TournamentDTO tournament2DTO; + private TournamentDTO tournament3DTO; + private TournamentDTO tournament4DTO; + private TournamentDTO tournament5DTO; + private TournamentDTO tournament6DTO; + + + @BeforeClass + public void prepareData() { + addParticipants(MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, 0); + } + + @BeforeClass(dependsOnMethods = "prepareData") + public void prepareTournament1() { + //Create Tournament + tournament1DTO = addTournament(TOURNAMENT1_NAME, MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, TOURNAMENT1_DELAY); + List fightDTOs = fightController.createFights(tournament1DTO.getId(), TeamsOrder.SORTED, 0, null); + + //P1 vs P4 Win P1 + //P2 vs P5 Win P2 + //P3 vs P6 Win P3 + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(0).setFinished(true); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(1).setFinished(true); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(2).setFinished(true); + fightDTOs.set(0, fightController.update(fightDTOs.get(0), null)); + + //P7 vs P4 Win P4 + //P8 vs P5 Win P5 + //P9 vs P6 Win P6 + fightDTOs.get(1).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(0).setFinished(true); + fightDTOs.get(1).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(1).setFinished(true); + fightDTOs.get(1).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(2).setFinished(true); + fightDTOs.set(1, fightController.update(fightDTOs.get(1), null)); + + //P7 vs P1 Win P1 + //P8 vs P2 Win P2 + //P9 vs P3 Win P3 + fightDTOs.get(2).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(0).setFinished(true); + fightDTOs.get(2).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(1).setFinished(true); + fightDTOs.get(2).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(2).setFinished(true); + fightDTOs.set(2, fightController.update(fightDTOs.get(2), null)); + + + //Set Time + fightDTOs.forEach(fightDTO -> { + fightDTO.getDuels().forEach(duelDTO -> duelDTO.setCreatedAt(LocalDateTime.now().minusMinutes(TOURNAMENT1_DELAY))); + duelController.updateAll(fightDTO.getDuels(), null); + }); + + + achievementController.generateAchievements(tournament1DTO); + } + + @BeforeClass(dependsOnMethods = "prepareTournament1") + public void prepareTournament2() { + //Create Tournament + tournament2DTO = addTournament(TOURNAMENT2_NAME, MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, TOURNAMENT2_DELAY); + List fightDTOs = fightController.createFights(tournament2DTO.getId(), TeamsOrder.SORTED, 0, null); + + //P1 vs P4 Win P1 + //P2 vs P5 Win P2 + //P3 vs P6 Win P3 + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(0).setFinished(true); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(1).setFinished(true); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(2).setFinished(true); + fightDTOs.set(0, fightController.update(fightDTOs.get(0), null)); + + //P7 vs P4 Win P4 + //P8 vs P5 Win P5 + //P9 vs P6 Win P6 + fightDTOs.get(1).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(0).setFinished(true); + fightDTOs.get(1).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(1).setFinished(true); + fightDTOs.get(1).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(2).setFinished(true); + fightDTOs.set(1, fightController.update(fightDTOs.get(1), null)); + + //P7 vs P1 Win P1 + //P8 vs P2 Win P2 + //P9 vs P3 Win P3 + fightDTOs.get(2).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(0).setFinished(true); + fightDTOs.get(2).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(1).setFinished(true); + fightDTOs.get(2).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(2).setFinished(true); + fightDTOs.set(2, fightController.update(fightDTOs.get(2), null)); + + //Set Time + fightDTOs.forEach(fightDTO -> { + fightDTO.getDuels().forEach(duelDTO -> duelDTO.setCreatedAt(LocalDateTime.now().minusMinutes(TOURNAMENT2_DELAY))); + duelController.updateAll(fightDTO.getDuels(), null); + }); + + + achievementController.generateAchievements(tournament2DTO); + } + + @BeforeClass(dependsOnMethods = "prepareTournament2") + public void prepareTournament3() { + tournament3DTO = addTournament(TOURNAMENT3_NAME, MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, TOURNAMENT3_DELAY); + //Create Tournament + List fightDTOs = fightController.createFights(tournament3DTO.getId(), TeamsOrder.SORTED, 0, null); + //P1 vs P4 Win P4 <--- + //P2 vs P5 Win P2 + //P3 vs P6 Win P3 + fightDTOs.get(0).getDuels().get(0).addCompetitor2Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(0).addCompetitor2Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(0).setFinished(true); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(1).setFinished(true); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(2).setFinished(true); + fightDTOs.set(0, fightController.update(fightDTOs.get(0), null)); + + //P7 vs P4 Win P4 + //P8 vs P5 Win P5 + //P9 vs P6 Win P6 + fightDTOs.get(1).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(0).setFinished(true); + fightDTOs.get(1).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(1).setFinished(true); + fightDTOs.get(1).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(2).setFinished(true); + fightDTOs.set(1, fightController.update(fightDTOs.get(1), null)); + + //P7 vs P1 Win P1 + //P8 vs P2 Win P2 + //P9 vs P3 Win P3 + fightDTOs.get(2).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(0).setFinished(true); + fightDTOs.get(2).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(1).setFinished(true); + fightDTOs.get(2).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(2).setFinished(true); + fightDTOs.set(2, fightController.update(fightDTOs.get(2), null)); + + //Set Time + fightDTOs.forEach(fightDTO -> { + fightDTO.getDuels().forEach(duelDTO -> duelDTO.setCreatedAt(LocalDateTime.now().minusMinutes(TOURNAMENT3_DELAY))); + duelController.updateAll(fightDTO.getDuels(), null); + }); + + achievementController.generateAchievements(tournament3DTO); + } + + @BeforeClass(dependsOnMethods = "prepareTournament3") + public void prepareTournament4() { + tournament4DTO = addTournament(TOURNAMENT4_NAME, MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, TOURNAMENT4_DELAY); + //Create Tournament + List fightDTOs = fightController.createFights(tournament4DTO.getId(), TeamsOrder.SORTED, 0, null); + //P1 vs P4 Win P1 + //P2 vs P5 Win P5 <--- + //P3 vs P6 Win P6 + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(0).setFinished(true); + fightDTOs.get(0).getDuels().get(1).addCompetitor2Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(1).addCompetitor2Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(1).setFinished(true); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(2).setFinished(true); + fightDTOs.set(0, fightController.update(fightDTOs.get(0), null)); + + //P7 vs P4 Win P4 + //P8 vs P5 Win P5 + //P9 vs P6 Win P6 + fightDTOs.get(1).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(0).setFinished(true); + fightDTOs.get(1).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(1).setFinished(true); + fightDTOs.get(1).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(2).setFinished(true); + fightDTOs.set(1, fightController.update(fightDTOs.get(1), null)); + + //P7 vs P1 Win P1 + //P8 vs P2 Win P2 + //P9 vs P3 Win P3 + fightDTOs.get(2).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(0).setFinished(true); + fightDTOs.get(2).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(1).setFinished(true); + fightDTOs.get(2).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(2).setFinished(true); + fightDTOs.set(2, fightController.update(fightDTOs.get(2), null)); + + //Set Time + fightDTOs.forEach(fightDTO -> { + fightDTO.getDuels().forEach(duelDTO -> duelDTO.setCreatedAt(LocalDateTime.now().minusMinutes(TOURNAMENT4_DELAY))); + duelController.updateAll(fightDTO.getDuels(), null); + }); + + achievementController.generateAchievements(tournament4DTO); + } + + @BeforeClass(dependsOnMethods = "prepareTournament4") + public void prepareTournament5() { + tournament5DTO = addTournament(TOURNAMENT5_NAME, MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, TOURNAMENT5_DELAY); + //Create Tournament + List fightDTOs = fightController.createFights(tournament5DTO.getId(), TeamsOrder.SORTED, 0, null); + //P1 vs P4 Win P4 + //P2 vs P5 Win P2 + //P3 vs P6 Win P6 <-- + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(0).setFinished(true); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(1).setFinished(true); + fightDTOs.get(0).getDuels().get(2).addCompetitor2Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(2).addCompetitor2Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(2).setFinished(true); + fightDTOs.set(0, fightController.update(fightDTOs.get(0), null)); + + //P7 vs P4 Win P4 + //P8 vs P5 Win P5 + //P9 vs P6 Win P6 + fightDTOs.get(1).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(0).setFinished(true); + fightDTOs.get(1).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(1).setFinished(true); + fightDTOs.get(1).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(2).setFinished(true); + fightDTOs.set(1, fightController.update(fightDTOs.get(1), null)); + + //P7 vs P1 Win P1 + //P8 vs P2 Win P2 + //P9 vs P3 Win P3 + fightDTOs.get(2).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(0).setFinished(true); + fightDTOs.get(2).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(1).setFinished(true); + fightDTOs.get(2).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(2).setFinished(true); + fightDTOs.set(2, fightController.update(fightDTOs.get(2), null)); + + //Set Time + fightDTOs.forEach(fightDTO -> { + fightDTO.getDuels().forEach(duelDTO -> duelDTO.setCreatedAt(LocalDateTime.now().minusMinutes(TOURNAMENT5_DELAY))); + duelController.updateAll(fightDTO.getDuels(), null); + }); + + achievementController.generateAchievements(tournament5DTO); + } + + @BeforeClass(dependsOnMethods = "prepareTournament5") + public void prepareTournament6() { + tournament6DTO = addTournament(TOURNAMENT6_NAME, MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, TOURNAMENT6_DELAY); + //Create Tournament + List fightDTOs = fightController.createFights(tournament6DTO.getId(), TeamsOrder.SORTED, 0, null); + //P1 vs P4 Win P4 <-- (Again!) + //P2 vs P5 Win P5 + //P3 vs P6 Win P6 + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(0).addCompetitor1Score(Score.TSUKI); + fightDTOs.get(0).getDuels().get(0).setFinished(true); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(1).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(1).setFinished(true); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.DO); + fightDTOs.get(0).getDuels().get(2).addCompetitor1Score(Score.KOTE); + fightDTOs.get(0).getDuels().get(2).setFinished(true); + fightDTOs.set(0, fightController.update(fightDTOs.get(0), null)); + + //P7 vs P4 Win P7 <-- + //P8 vs P5 Win P5 + //P9 vs P6 Win P6 + fightDTOs.get(1).getDuels().get(0).addCompetitor1Score(Score.TSUKI); + fightDTOs.get(1).getDuels().get(0).setFinished(true); + fightDTOs.get(1).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(1).setFinished(true); + fightDTOs.get(1).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(1).getDuels().get(2).setFinished(true); + fightDTOs.set(1, fightController.update(fightDTOs.get(1), null)); + + //P7 vs P1 Win P1 + //P8 vs P2 Win P2 + //P9 vs P3 Win P3 + fightDTOs.get(2).getDuels().get(0).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(0).setFinished(true); + fightDTOs.get(2).getDuels().get(1).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(1).setFinished(true); + fightDTOs.get(2).getDuels().get(2).addCompetitor2Score(Score.MEN); + fightDTOs.get(2).getDuels().get(2).setFinished(true); + fightDTOs.set(2, fightController.update(fightDTOs.get(2), null)); + + //Set Time + fightDTOs.forEach(fightDTO -> { + fightDTO.getDuels().forEach(duelDTO -> duelDTO.setCreatedAt(LocalDateTime.now().minusMinutes(TOURNAMENT6_DELAY))); + duelController.updateAll(fightDTO.getDuels(), null); + }); + + achievementController.generateAchievements(tournament6DTO); + } + + @Test + public void checkSithKillsMasterAchievementsTest() { + List achievementsDTOs = achievementController.getAchievements(tournament1DTO, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER); + Assert.assertEquals(achievementsDTOs.size(), 0); + + achievementsDTOs = achievementController.getAchievements(tournament2DTO, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER); + Assert.assertEquals(achievementsDTOs.size(), 0); + + achievementsDTOs = achievementController.getAchievements(tournament3DTO, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER); + Assert.assertEquals(achievementsDTOs.size(), 1); + Assert.assertEquals(achievementsDTOs.get(0).getParticipant().getLastname(), "Lastname3"); //P4 -> Lastname 3 + Assert.assertEquals(achievementsDTOs.get(0).getAchievementGrade(), AchievementGrade.NORMAL); + + achievementsDTOs = achievementController.getAchievements(tournament4DTO, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER); + Assert.assertEquals(achievementsDTOs.size(), 1); + Assert.assertEquals(achievementsDTOs.get(0).getParticipant().getLastname(), "Lastname4"); //P5 -> Lastname 4 + Assert.assertEquals(achievementsDTOs.get(0).getAchievementGrade(), AchievementGrade.BRONZE); + + achievementsDTOs = achievementController.getAchievements(tournament5DTO, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER); + Assert.assertEquals(achievementsDTOs.size(), 1); + Assert.assertEquals(achievementsDTOs.get(0).getParticipant().getLastname(), "Lastname5"); //P6 -> Lastname 5 + Assert.assertEquals(achievementsDTOs.get(0).getAchievementGrade(), AchievementGrade.SILVER); + + achievementsDTOs = achievementController.getAchievements(tournament6DTO, AchievementType.SITH_APPRENTICES_ALWAYS_KILL_THEIR_MASTER); + //P6 has defeated the master, P4 has lost, lost, win, lost, lost win. On Tournament6 win again, but is not the master as has been already defeated. + Assert.assertEquals(achievementsDTOs.size(), 1); + Assert.assertEquals(achievementsDTOs.get(0).getParticipant().getLastname(), "Lastname6"); //P7 -> Lastname 6 + Assert.assertEquals(achievementsDTOs.get(0).getAchievementGrade(), AchievementGrade.GOLD); + } + + + @AfterClass(alwaysRun = true) + public void wipeOut() { + super.wipeOut(); + } +} diff --git a/backend/kendo-tournament-core/src/test/resources/testng.xml b/backend/kendo-tournament-core/src/test/resources/testng.xml index e6ee8d9df..b06929a18 100644 --- a/backend/kendo-tournament-core/src/test/resources/testng.xml +++ b/backend/kendo-tournament-core/src/test/resources/testng.xml @@ -18,6 +18,7 @@ + @@ -48,6 +49,7 @@ + diff --git a/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/entities/Duel.java b/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/entities/Duel.java index 11b6fcbd6..4a567bd23 100644 --- a/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/entities/Duel.java +++ b/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/entities/Duel.java @@ -233,6 +233,15 @@ public int getWinner() { return Integer.compare(getCompetitor2ScoreValue(), getCompetitor1ScoreValue()); } + public Participant getCompetitorWinner() { + if (getWinner() < 0) { + return getCompetitor1(); + } else if (getWinner() > 0) { + return getCompetitor2(); + } + return null; + } + public Integer getCompetitor1ScoreValue() { return (int) competitor1Score.stream().filter(Score::isValidPoint).count(); } diff --git a/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/DuelRepository.java b/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/DuelRepository.java index c26a35dae..591c949a7 100644 --- a/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/DuelRepository.java +++ b/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/DuelRepository.java @@ -117,4 +117,8 @@ List findByTournamentAndCompetitor1ScoreTimeLessThanEqualOrCompetitor2Scor @Query("SELECT d FROM Duel d WHERE d.competitor1=:participant OR d.competitor2=:participant") List findByParticipant(@Param("participant") Participant participant); + + @Query("SELECT d FROM Duel d WHERE (d.competitor1=:participant1 AND d.competitor2=:participant2) " + + "OR (d.competitor2=:participant1 AND d.competitor1=:participant2)") + List findByParticipants(@Param("participant1") Participant participant1, @Param("participant2") Participant participant2); }