Skip to content

Commit

Permalink
Added Leaderbaord Functionality (#151)
Browse files Browse the repository at this point in the history
Added Uc for Leaderboards and added the corresponding API-Method
  • Loading branch information
VanKhoiPham authored Jul 1, 2024
2 parents 1c377de + ddc819f commit 3306fa5
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 0 deletions.
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();



}
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;
}
}
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
}
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

}
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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,9 @@ public class PetRestSelfService {
@Inject
protected UcFindPetType ucFindPetType;

@Inject
protected UcLeaderboard ucLeaderboard;

/**
* Get all pets.
*
Expand Down Expand Up @@ -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<PetDTO> getLeaderboard() {
String uuid = SecurityUtil.getExternalUserId(identity);
List<PetEntity> leaderboard = ucLeaderboard.getCompleteLeaderBoard();
return petMapper.mapEntityToTransferObject(leaderboard);
}
}
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

0 comments on commit 3306fa5

Please sign in to comment.