-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Leaderbaord Functionality (#151)
Added Uc for Leaderboards and added the corresponding API-Method
- Loading branch information
Showing
7 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PetEntity> getTop10(); | ||
|
||
/** | ||
* Ranked on Happiness and Wellbeing, two are equally ranked the name decides | ||
* @return the leaderboard consisting of all pets | ||
*/ | ||
List<PetEntity> getCompleteLeaderBoard(); | ||
|
||
/** | ||
* Ranked purely on Happiness, when two are equally ranked the name decides | ||
* @return the top 10 pets according to their Happiness | ||
*/ | ||
List<PetEntity> getHappinessTop10(); | ||
|
||
/** | ||
* Ranked purely on Happiness, when two are equally ranked the name decides | ||
* @return the leaderboard consisting of all pets | ||
*/ | ||
List<PetEntity> getCompleteHappinessLeaderBoard(); | ||
|
||
/** | ||
* Ranked purely on Wellbeing, when two are equally ranked the name decides | ||
* @return the top 10 pets according to their Wellbeing | ||
*/ | ||
List<PetEntity> getWellbeingTop10(); | ||
|
||
/** | ||
* Ranked purely on Wellbeing, when two are equally ranked the name decides | ||
* @return the leaderboard consisting of all pets | ||
*/ | ||
List<PetEntity> getCompleteWellbeingLeaderboard(); | ||
|
||
|
||
|
||
} |
74 changes: 74 additions & 0 deletions
74
web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboardImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PetEntity> getTop10() { | ||
List<PetEntity> leaderboard = getCompleteLeaderBoard(); | ||
if(leaderboard.size()>10){ | ||
return leaderboard.subList(0,10); | ||
} | ||
return leaderboard; | ||
} | ||
|
||
@Override | ||
public List<PetEntity> getCompleteLeaderBoard() { | ||
ArrayList<PetEntity> leaderboard = new ArrayList<>(); | ||
leaderboard.addAll(findPet.findAll()); | ||
leaderboard.sort(compDefault); | ||
return leaderboard; | ||
} | ||
|
||
@Override | ||
public List<PetEntity> getHappinessTop10() { | ||
List<PetEntity> leaderboard = getCompleteHappinessLeaderBoard(); | ||
if(leaderboard.size()>10){ | ||
return leaderboard.subList(0,10); | ||
} | ||
return leaderboard; | ||
} | ||
|
||
@Override | ||
public List<PetEntity> getCompleteHappinessLeaderBoard() { | ||
ArrayList<PetEntity> leaderboard = new ArrayList<>(); | ||
leaderboard.addAll(findPet.findAll()); | ||
leaderboard.sort(compHappiness); | ||
return leaderboard; | ||
} | ||
|
||
@Override | ||
public List<PetEntity> getWellbeingTop10() { | ||
List<PetEntity> leaderboard = getCompleteWellbeingLeaderboard(); | ||
if(leaderboard.size()>10){ | ||
return leaderboard.subList(0,10); | ||
} | ||
return leaderboard; | ||
} | ||
|
||
@Override | ||
public List<PetEntity> getCompleteWellbeingLeaderboard() { | ||
ArrayList<PetEntity> leaderboard = new ArrayList<>(); | ||
leaderboard.addAll(findPet.findAll()); | ||
leaderboard.sort(compWellbeing); | ||
return leaderboard; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByHappiness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PetEntity> { | ||
|
||
@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 | ||
} |
20 changes: 20 additions & 0 deletions
20
...backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PetEntity> { | ||
|
||
|
||
@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 | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeingAndHappiness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PetEntity> { | ||
@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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
web_backend/src/test/java/haw/teamagochi/backend/pet/logic/UcLeaderboardTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, PetEntity> 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<PetEntity> 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<PetEntity> 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<PetEntity> 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<PetEntity> 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<PetEntity> 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<PetEntity> 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 |