diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboard.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboard.java new file mode 100644 index 00000000..98fa60c5 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboard.java @@ -0,0 +1,46 @@ +package haw.teamagochi.backend.pet.logic; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import java.util.List; + +public interface UcLeaderboard { + + /** + * Ranked on Happiness and Wellbeing, two are equally ranked the name decides + * @return the top 10 Pets + */ + List getTop10(); + + /** + * Ranked on Happiness and Wellbeing, two are equally ranked the name decides + * @return the leaderboard consisting of all pets + */ + List getCompleteLeaderBoard(); + + /** + * Ranked purely on Happiness, when two are equally ranked the name decides + * @return the top 10 pets according to their Happiness + */ + List getHappinessTop10(); + + /** + * Ranked purely on Happiness, when two are equally ranked the name decides + * @return the leaderboard consisting of all pets + */ + List getCompleteHappinessLeaderBoard(); + + /** + * Ranked purely on Wellbeing, when two are equally ranked the name decides + * @return the top 10 pets according to their Wellbeing + */ + List getWellbeingTop10(); + + /** + * Ranked purely on Wellbeing, when two are equally ranked the name decides + * @return the leaderboard consisting of all pets + */ + List getCompleteWellbeingLeaderboard(); + + + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboardImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboardImpl.java new file mode 100644 index 00000000..c2925c70 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboardImpl.java @@ -0,0 +1,74 @@ +package haw.teamagochi.backend.pet.logic; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import haw.teamagochi.backend.pet.logic.UcLeaderboard; +import haw.teamagochi.backend.pet.logic.comparator.PetSortByHappiness; +import haw.teamagochi.backend.pet.logic.comparator.PetSortByWellbeing; +import haw.teamagochi.backend.pet.logic.comparator.PetSortByWellbeingAndHappiness; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +@ApplicationScoped +public class UcLeaderboardImpl implements UcLeaderboard { + + @Inject + UcFindPet findPet; + + PetSortByHappiness compHappiness = new PetSortByHappiness(); + PetSortByWellbeing compWellbeing = new PetSortByWellbeing(); + PetSortByWellbeingAndHappiness compDefault = new PetSortByWellbeingAndHappiness(); + + @Override + public List getTop10() { + List leaderboard = getCompleteLeaderBoard(); + if(leaderboard.size()>10){ + return leaderboard.subList(0,10); + } + return leaderboard; + } + + @Override + public List getCompleteLeaderBoard() { + ArrayList leaderboard = new ArrayList<>(); + leaderboard.addAll(findPet.findAll()); + leaderboard.sort(compDefault); + return leaderboard; + } + + @Override + public List getHappinessTop10() { + List leaderboard = getCompleteHappinessLeaderBoard(); + if(leaderboard.size()>10){ + return leaderboard.subList(0,10); + } + return leaderboard; + } + + @Override + public List getCompleteHappinessLeaderBoard() { + ArrayList leaderboard = new ArrayList<>(); + leaderboard.addAll(findPet.findAll()); + leaderboard.sort(compHappiness); + return leaderboard; + } + + @Override + public List getWellbeingTop10() { + List leaderboard = getCompleteWellbeingLeaderboard(); + if(leaderboard.size()>10){ + return leaderboard.subList(0,10); + } + return leaderboard; + } + + @Override + public List getCompleteWellbeingLeaderboard() { + ArrayList leaderboard = new ArrayList<>(); + leaderboard.addAll(findPet.findAll()); + leaderboard.sort(compWellbeing); + return leaderboard; + } +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByHappiness.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByHappiness.java new file mode 100644 index 00000000..93ea4e63 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByHappiness.java @@ -0,0 +1,18 @@ +package haw.teamagochi.backend.pet.logic.comparator; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import java.util.Comparator; + +public class PetSortByHappiness implements Comparator { + + @Override + public int compare(PetEntity o1, PetEntity o2) { + int res = o1.getHappiness() - o2.getHappiness(); + if(res == 0){ + return(o1.getName().compareTo(o2.getName())); + } else if (res < 0) { + return 1; + } + return -1; + }//metthod +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeing.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeing.java new file mode 100644 index 00000000..7147d545 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeing.java @@ -0,0 +1,20 @@ +package haw.teamagochi.backend.pet.logic.comparator; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import java.util.Comparator; + +public class PetSortByWellbeing implements Comparator { + + + @Override + public int compare(PetEntity o1, PetEntity o2) { + int res = o1.getWellbeing() - o2.getWellbeing(); + if(res == 0){ + return(o1.getName().compareTo(o2.getName())); + } else if (res < 0) { + return 1; + } + return -1; + }//metthod + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeingAndHappiness.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeingAndHappiness.java new file mode 100644 index 00000000..bb9753d6 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeingAndHappiness.java @@ -0,0 +1,20 @@ +package haw.teamagochi.backend.pet.logic.comparator; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import java.util.Comparator; + +public class PetSortByWellbeingAndHappiness implements Comparator { + @Override + public int compare(PetEntity o1, PetEntity o2) { + int o1SumHappWell = o1.getWellbeing() + o1.getHappiness(); + int o2SumHappWell = o2.getWellbeing() + o2.getHappiness(); + int res = o1SumHappWell - o2SumHappWell; + if(res == 0){ + return(o1.getName().compareTo(o2.getName())); + } else if (res < 0) { + return 1; + } + return -1; + }//method + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java index 7c3b6a80..e579724a 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java @@ -4,6 +4,7 @@ import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; import haw.teamagochi.backend.pet.logic.UcFindPet; import haw.teamagochi.backend.pet.logic.UcFindPetType; +import haw.teamagochi.backend.pet.logic.UcLeaderboard; import haw.teamagochi.backend.pet.logic.UcManagePet; import haw.teamagochi.backend.pet.service.rest.v1.mapper.PetMapper; import haw.teamagochi.backend.pet.service.rest.v1.model.PetDTO; @@ -54,6 +55,9 @@ public class PetRestSelfService { @Inject protected UcFindPetType ucFindPetType; + @Inject + protected UcLeaderboard ucLeaderboard; + /** * Get all pets. * @@ -112,4 +116,14 @@ public PetDTO getPetById(@PathParam("petId") long petId) { } throw new NotFoundException(); } + + @GET + @Path("/leaderboard") + @Operation(summary = "Get the current Leaderboard") + @APIResponse(responseCode = "200") + public List getLeaderboard() { + String uuid = SecurityUtil.getExternalUserId(identity); + List leaderboard = ucLeaderboard.getCompleteLeaderBoard(); + return petMapper.mapEntityToTransferObject(leaderboard); + } } diff --git a/web_backend/src/test/java/haw/teamagochi/backend/pet/logic/UcLeaderboardTest.java b/web_backend/src/test/java/haw/teamagochi/backend/pet/logic/UcLeaderboardTest.java new file mode 100644 index 00000000..0958e26b --- /dev/null +++ b/web_backend/src/test/java/haw/teamagochi/backend/pet/logic/UcLeaderboardTest.java @@ -0,0 +1,151 @@ +package haw.teamagochi.backend.pet.logic; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; +import haw.teamagochi.backend.pet.dataaccess.repository.PetRepository; +import haw.teamagochi.backend.user.dataaccess.model.UserEntity; +import haw.teamagochi.backend.user.logic.UcManageUser; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class UcLeaderboardTest { + + @Inject + PetRepository repo; + @Inject + UcLeaderboard leaderbaords; + @Inject + UcManagePetType ucManagePetType; + @Inject + UcManagePet petManger; + @Inject + UcManageUser userManger; + @Inject UcFindPet findPet; + + HashMap petEntities; + + @BeforeEach + @Transactional + void beforeEach() { + petManger.deleteAll(); + UserEntity owner = userManger.create(UUID.randomUUID()); + PetTypeEntity petType = ucManagePetType.createPetType("Frog"); + + petEntities = new HashMap<>(); + PetEntity pet1 = petManger.create(owner.getId(), "ABC", petType.getId()); + pet1.setHappiness(100); + pet1.setWellbeing(100); + + PetEntity pet2 = petManger.create(owner.getId(), "Muffl", petType.getId()); + pet2.setHappiness(90); + pet2.setWellbeing(90); + + PetEntity pet3 = petManger.create(owner.getId(), "BCD", petType.getId()); + pet3.setHappiness(100); + pet3.setWellbeing(100); + + repo.persist(pet1); + repo.persist(pet2); + repo.persist(pet3); + petEntities.put("pet1", pet1); + petEntities.put("pet2", pet2); + petEntities.put("pet3", pet3); + + int statsModifyer = 1; + for (int i = 15; i >= 4; i--) { + //add some more pets + PetEntity tmpPet = petManger.create(owner.getId(), "petewbv" + i, petType.getId()); + tmpPet.setWellbeing(10 + statsModifyer*2); + tmpPet.setHappiness(10 + statsModifyer * 2); + repo.persist(tmpPet); + petEntities.put("pet" + i, tmpPet); + statsModifyer++; + } + } + + @Test + void testGetTop10() { + Assertions.assertEquals(15, petEntities.size()); + Assertions.assertEquals(15, findPet.findAll().size()); + List leaderboard = leaderbaords.getTop10(); + Assertions.assertEquals(10, leaderboard.size()); + Assertions.assertEquals(petEntities.get("pet1"), leaderboard.get(0)); + Assertions.assertEquals(petEntities.get("pet3"), leaderboard.get(1)); + Assertions.assertEquals(petEntities.get("pet2"), leaderboard.get(2)); + for (int i = 4; i <= 10; i++) { + Assertions.assertEquals(petEntities.get("pet" + i), leaderboard.get(i - 1)); + } + }//method + + @Test + void testgetCompleteLeaderBoard() { + List leaderboard = leaderbaords.getCompleteLeaderBoard(); + Assertions.assertEquals(15, leaderboard.size()); + Assertions.assertEquals(petEntities.get("pet1"), leaderboard.get(0)); + Assertions.assertEquals(petEntities.get("pet3"), leaderboard.get(1)); + Assertions.assertEquals(petEntities.get("pet2"), leaderboard.get(2)); + for (int i = 4; i <= 15; i++) { + Assertions.assertEquals(petEntities.get("pet" + i), leaderboard.get(i - 1)); + } + }//method + + @Test + void testGetHappinessTop10() { + Assertions.assertEquals(15, petEntities.size()); + Assertions.assertEquals(15, findPet.findAll().size()); + List leaderboard = leaderbaords.getHappinessTop10(); + Assertions.assertEquals(10, leaderboard.size()); + Assertions.assertEquals(petEntities.get("pet1"), leaderboard.get(0)); + Assertions.assertEquals(petEntities.get("pet3"), leaderboard.get(1)); + Assertions.assertEquals(petEntities.get("pet2"), leaderboard.get(2)); + for (int i = 4; i <= 10; i++) { + Assertions.assertEquals(petEntities.get("pet" + i), leaderboard.get(i - 1)); + } + }//method + + @Test + void testGetCompleteHappinessLeaderBoard() { + List leaderboard = leaderbaords.getCompleteHappinessLeaderBoard(); + Assertions.assertEquals(15, leaderboard.size()); + Assertions.assertEquals(petEntities.get("pet1"), leaderboard.get(0)); + Assertions.assertEquals(petEntities.get("pet3"), leaderboard.get(1)); + Assertions.assertEquals(petEntities.get("pet2"), leaderboard.get(2)); + for (int i = 4; i <= 15; i++) { + Assertions.assertEquals(petEntities.get("pet" + i), leaderboard.get(i - 1)); + } + }//method + + @Test + void testGetWellbeingTop10() { + Assertions.assertEquals(15, petEntities.size()); + Assertions.assertEquals(15, findPet.findAll().size()); + List leaderboard = leaderbaords.getWellbeingTop10(); + Assertions.assertEquals(10, leaderboard.size()); + Assertions.assertEquals(petEntities.get("pet1"), leaderboard.get(0)); + Assertions.assertEquals(petEntities.get("pet3"), leaderboard.get(1)); + Assertions.assertEquals(petEntities.get("pet2"), leaderboard.get(2)); + for (int i = 4; i <= 10; i++) { + Assertions.assertEquals(petEntities.get("pet" + i), leaderboard.get(i - 1)); + } + }//method + + @Test + void testGetCompleteWellbeingLeaderBoard() { + List leaderboard = leaderbaords.getCompleteWellbeingLeaderboard(); + Assertions.assertEquals(15, leaderboard.size()); + Assertions.assertEquals(petEntities.get("pet1"), leaderboard.get(0)); + Assertions.assertEquals(petEntities.get("pet3"), leaderboard.get(1)); + Assertions.assertEquals(petEntities.get("pet2"), leaderboard.get(2)); + for (int i = 4; i <= 15; i++) { + Assertions.assertEquals(petEntities.get("pet" + i), leaderboard.get(i - 1)); + } + }//method +}//class