From e743d027b3b29d91dbf8cd72a0195da566ca1b74 Mon Sep 17 00:00:00 2001 From: "Trefflich, Merlin" Date: Mon, 1 Jul 2024 12:21:20 +0200 Subject: [PATCH 1/5] Added Uc for Pet Leaderboards and the corresponding Comparators for the pets --- .../backend/pet/logic/UcLeaderboard.java | 46 +++++++++++++ .../backend/pet/logic/UcLeaderboardImpl.java | 69 +++++++++++++++++++ .../logic/comparator/PetSortByHappiness.java | 18 +++++ .../logic/comparator/PetSortByWellbeing.java | 20 ++++++ .../PetSortByWellbeingAndHappiness.java | 20 ++++++ 5 files changed, 173 insertions(+) create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboard.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboardImpl.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByHappiness.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeing.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/comparator/PetSortByWellbeingAndHappiness.java 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..f47b5712 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcLeaderboardImpl.java @@ -0,0 +1,69 @@ +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.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,9); + } + return leaderboard; + } + + @Override + public List getCompleteLeaderBoard() { + List leaderboard = findPet.findAll(); + leaderboard.sort(compDefault); + return leaderboard; + } + + @Override + public List getHappinessTop10() { + List leaderboard = getCompleteHappinessLeaderBoard(); + if(leaderboard.size()>10){ + return leaderboard.subList(0,9); + } + return leaderboard; + } + + @Override + public List getCompleteHappinessLeaderBoard() { + List leaderboard = findPet.findAll(); + leaderboard.sort(compHappiness); + return leaderboard; + } + + @Override + public List getWellbeingTop10() { + List leaderboard = getCompleteWellbeingLeaderboard(); + if(leaderboard.size()>10){ + return leaderboard.subList(0,9); + } + return leaderboard; + } + + @Override + public List getCompleteWellbeingLeaderboard() { + List leaderboard = 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..8bf11945 --- /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..712c586a --- /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..be69681c --- /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 + +} From 8a1525f8b64abb604ae3c0d6758e6b8d2a43c64e Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 1 Jul 2024 14:39:00 +0200 Subject: [PATCH 2/5] modified settings and pet modal --- frontend/src/Components/CreatePetModal.tsx | 34 ++--- frontend/src/Components/Settings.tsx | 142 ++++++++++++++------- 2 files changed, 114 insertions(+), 62 deletions(-) diff --git a/frontend/src/Components/CreatePetModal.tsx b/frontend/src/Components/CreatePetModal.tsx index 29159d93..970e0912 100644 --- a/frontend/src/Components/CreatePetModal.tsx +++ b/frontend/src/Components/CreatePetModal.tsx @@ -22,19 +22,19 @@ const CreatePetModal = () => { const handleInputPetType = (e: { target: { value: SetStateAction; }; }) => { setPetType(e.target.value); - }; + }; const handleSubmit = (e: { preventDefault: () => void; }) => { e.preventDefault(); // Prevent form from reloading the page - if (petName.trim() === "" || petType === "") { + if (petName.trim() === "" || petName.trim() === "DEAD" || petType === "") { return; } // TODO: Save the device ID or perform any action with the input value - console.log("Pet Name:", petName," Pet Type:", petType); + console.log("Pet Name:", petName, " Pet Type:", petType); setOpen(false); // Close the popup after submission }; - return ( + return ( <>
@@ -44,20 +44,22 @@ const CreatePetModal = () => { onClose={handleClose} overlayStyle={{ background: 'rgba(0, 0, 0, 0.5)' }} contentStyle={{ borderRadius: '10px', border: '2px solid grey', width: '50%' }} - > -
- + >

- Enter your Pet Name below and choose it's Type: + Enter your Pet Name below and choose it's Type: +

Pet name cannot be "DEAD"

+

@@ -82,21 +84,21 @@ const CreatePetModal = () => {
@ - +
ok
-
- + - -
@@ -108,7 +110,7 @@ const CreatePetModal = () => {
- ); + ); }; export default CreatePetModal; diff --git a/frontend/src/Components/Settings.tsx b/frontend/src/Components/Settings.tsx index 863b7983..fb6171db 100644 --- a/frontend/src/Components/Settings.tsx +++ b/frontend/src/Components/Settings.tsx @@ -1,60 +1,110 @@ -import { useState } from 'react'; +import React, { useState } from 'react'; import profile_pic1 from '../Misc/8-bit-dog-nobg.png'; import 'bootstrap/dist/css/bootstrap.min.css'; import LinkDevice from "../Components/LinkDevice"; interface SettingsProps { - username: string; + username: string; } function Settings( - { username }: SettingsProps + { username }: SettingsProps ) { - const [items, setItems] = useState(['Device 1', 'Device 2', 'Device 3']); - const [showLinkDevice, setShowLinkDevice] = useState(true); - - const removeItem = (index: number) => { - setItems(items.filter((_, i) => i !== index)); - }; - - // const resetItem = (index: number) => { - // setItems(items.map((item, i) => (i === index ? `Item ${index + 1}` : item))); - // }; - - return ( -
-
- profile picture -

PROFILE

-
-
-
-
@
+ const [items, setItems] = useState(['Device 1', 'Device 2', 'Device 3']); + const [showLinkDevice, setShowLinkDevice] = useState(true); + const [selectedIndex, setSelectedIndex] = useState(null); + const [showModal, setShowModal] = useState(false); + const [itemToRemove, setItemToRemove] = useState(null); + + const handleRemoveItem = (index: number) => { + setShowModal(true); + setItemToRemove(index); + }; + + const confirmRemoveItem = () => { + if (itemToRemove !== null) { + setItems(items.filter((_, i) => i !== itemToRemove)); + if (selectedIndex === itemToRemove) { + setSelectedIndex(null); + } + setItemToRemove(null); + } + setShowModal(false); + }; + + const cancelRemoveItem = () => { + setShowModal(false); + setItemToRemove(null); + }; + + const resetItem = (index: number) => { + setItems(items.map((item, i) => (i === index ? `Item ${index + 1}` : item))); + }; + + const selectItem = (index: number) => { + setSelectedIndex(index); + }; + + return ( +
+
+ profile picture +

PROFILE

+
+
+
+
@
+
+ +
+
+ +
+
+
    + {items.map((item, index) => ( +
  • + {item} +
    + + +
    +
  • + ))} +
+
+
- -
-
- -
-
-
    - {items.map((item, index) => ( -
  • - {item} -
    - -
    -
  • - ))} -
-
+ {showLinkDevice === false && ( + setShowLinkDevice(true)} /> + )} + + {showModal && ( +
+
+
+
+
Confirm Removal
+
+
+

Are you sure you want to remove this pet?

+
+
+ + +
+
+
+
+ )}
-
- {showLinkDevice==false && ( - setShowLinkDevice(true)} /> - )} -
- ); + ); } export default Settings; From f4f22e74b8158889670e73909deeb2168c777f45 Mon Sep 17 00:00:00 2001 From: "Trefflich, Merlin" Date: Mon, 1 Jul 2024 14:55:06 +0200 Subject: [PATCH 3/5] Added Tests, Changed Comparators to sort descending, Fix for List Problem in Leadderboard UC --- .../backend/pet/logic/UcLeaderboardImpl.java | 17 +- .../logic/comparator/PetSortByHappiness.java | 4 +- .../logic/comparator/PetSortByWellbeing.java | 4 +- .../PetSortByWellbeingAndHappiness.java | 4 +- .../backend/pet/logic/UcLeaderboardTest.java | 151 ++++++++++++++++++ 5 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 web_backend/src/test/java/haw/teamagochi/backend/pet/logic/UcLeaderboardTest.java 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 index f47b5712..c2925c70 100644 --- 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 @@ -7,6 +7,8 @@ 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 @@ -23,14 +25,15 @@ public class UcLeaderboardImpl implements UcLeaderboard { public List getTop10() { List leaderboard = getCompleteLeaderBoard(); if(leaderboard.size()>10){ - return leaderboard.subList(0,9); + return leaderboard.subList(0,10); } return leaderboard; } @Override public List getCompleteLeaderBoard() { - List leaderboard = findPet.findAll(); + ArrayList leaderboard = new ArrayList<>(); + leaderboard.addAll(findPet.findAll()); leaderboard.sort(compDefault); return leaderboard; } @@ -39,14 +42,15 @@ public List getCompleteLeaderBoard() { public List getHappinessTop10() { List leaderboard = getCompleteHappinessLeaderBoard(); if(leaderboard.size()>10){ - return leaderboard.subList(0,9); + return leaderboard.subList(0,10); } return leaderboard; } @Override public List getCompleteHappinessLeaderBoard() { - List leaderboard = findPet.findAll(); + ArrayList leaderboard = new ArrayList<>(); + leaderboard.addAll(findPet.findAll()); leaderboard.sort(compHappiness); return leaderboard; } @@ -55,14 +59,15 @@ public List getCompleteHappinessLeaderBoard() { public List getWellbeingTop10() { List leaderboard = getCompleteWellbeingLeaderboard(); if(leaderboard.size()>10){ - return leaderboard.subList(0,9); + return leaderboard.subList(0,10); } return leaderboard; } @Override public List getCompleteWellbeingLeaderboard() { - List leaderboard = findPet.findAll(); + 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 index 8bf11945..93ea4e63 100644 --- 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 @@ -11,8 +11,8 @@ public int compare(PetEntity o1, PetEntity o2) { if(res == 0){ return(o1.getName().compareTo(o2.getName())); } else if (res < 0) { - return -1; + return 1; } - 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 index 712c586a..7147d545 100644 --- 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 @@ -12,9 +12,9 @@ public int compare(PetEntity o1, PetEntity o2) { if(res == 0){ return(o1.getName().compareTo(o2.getName())); } else if (res < 0) { - return -1; + return 1; } - 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 index be69681c..bb9753d6 100644 --- 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 @@ -12,9 +12,9 @@ public int compare(PetEntity o1, PetEntity o2) { if(res == 0){ return(o1.getName().compareTo(o2.getName())); } else if (res < 0) { - return -1; + return 1; } - return 1; + return -1; }//method } 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 From 52d5bde94f657cad1da424a32f544a31499c4978 Mon Sep 17 00:00:00 2001 From: "Trefflich, Merlin" Date: Mon, 1 Jul 2024 15:02:58 +0200 Subject: [PATCH 4/5] Added API-Method for Leaderboard --- .../pet/service/rest/v1/PetRestSelfService.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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); + } } From 847719bd28ef5fa4f71933f9ab8e223d4b4320c4 Mon Sep 17 00:00:00 2001 From: VanKHoiPham Date: Mon, 1 Jul 2024 16:10:53 +0200 Subject: [PATCH 5/5] update reset endpoint --- .../service/rest/v1/DeviceRestService.java | 25 +++++++++++++++++++ .../backend/pet/logic/UcManagePetImpl.java | 1 + .../pet/logic/gameCycle/GameCycleImpl.java | 17 +++++++++++++ .../pet/service/rest/v1/PetRestService.java | 15 +++++++++++ 4 files changed, 58 insertions(+) diff --git a/web_backend/src/main/java/haw/teamagochi/backend/device/service/rest/v1/DeviceRestService.java b/web_backend/src/main/java/haw/teamagochi/backend/device/service/rest/v1/DeviceRestService.java index c6a31827..b0f8c3dd 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/device/service/rest/v1/DeviceRestService.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/device/service/rest/v1/DeviceRestService.java @@ -5,6 +5,8 @@ import haw.teamagochi.backend.device.logic.UcManageDevice; import haw.teamagochi.backend.device.service.rest.v1.mapper.DeviceMapper; import haw.teamagochi.backend.device.service.rest.v1.model.DeviceDTO; +import haw.teamagochi.backend.pet.logic.UcManagePet; +import haw.teamagochi.backend.pet.logic.gameCycle.GameCycleImpl; import jakarta.inject.Inject; import jakarta.ws.rs.DELETE; import jakarta.ws.rs.GET; @@ -34,6 +36,13 @@ public class DeviceRestService { @Inject protected UcManageDevice ucManageDevice; + @Inject + protected UcManagePet ucManagePet; + + @Inject + protected GameCycleImpl gameCycle; + + /** * Get all devices. * @@ -84,4 +93,20 @@ public DeviceDTO deleteDeviceById(@PathParam("deviceId") long deviceId) { } throw new NotFoundException(); } + @DELETE + @Path("/reset") + @Operation(summary = "delete all Devices and Pets") + @APIResponse(responseCode = "200") + @APIResponse(responseCode = "404", description = "Not Found") + public List deleteAllPet() { + List entities = ucFindDevice.findAll(); + gameCycle.setStopRequested(true); + while (!gameCycle.isStopped()){ + //waiting for stopped + } + ucManageDevice.deleteAll(); + ucManagePet.deleteAll(); + gameCycle.setStopRequested(false); + return deviceMapper.mapEntityToTransferObject(entities); + } } diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcManagePetImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcManagePetImpl.java index f4133f89..b157f2b3 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcManagePetImpl.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/UcManagePetImpl.java @@ -90,6 +90,7 @@ public void changeDevice(long petId, long deviceId) { * {@inheritDoc} */ @Override + @Transactional public void deleteAll() { petRepository.deleteAll(); } diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/gameCycle/GameCycleImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/gameCycle/GameCycleImpl.java index 241c58f6..3c2063e5 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/gameCycle/GameCycleImpl.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/gameCycle/GameCycleImpl.java @@ -14,6 +14,8 @@ import java.util.Random; import jakarta.transaction.Transactional; +import lombok.Getter; +import lombok.Setter; import org.eclipse.microprofile.config.inject.ConfigProperty; @@ -45,15 +47,30 @@ public class GameCycleImpl implements GameCycle{ HappinessVO happinessVO; + @Setter + private volatile boolean stopRequested = false; + + @Getter + private volatile boolean stopped = false; + + @Override @Scheduled(every = "{GameCycle.interval}") @Transactional public void petGameCycle() { + if (stopRequested) { + // Exit the method if stop is requested + stopped = true; + }else { + stopped = false; for(DeviceEntity device: findDevice.findAll()){//only pets currently on a device + PetEntity pet = device.getPet(); if(pet != null) { deteriorate(pet); } + } + }//method } diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestService.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestService.java index 649d43bb..18bf88bd 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestService.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestService.java @@ -84,4 +84,19 @@ public PetDTO deletePetById(@PathParam("petId") long petId) { } throw new NotFoundException(); } + +// @DELETE +// @Path("/deleteAllPet") +// @Operation(summary = "Delete all pet") +// @APIResponse(responseCode = "200") +// @APIResponse(responseCode = "404", description = "Not Found") +// public List deleteAllPet() { +// List entities = ucFindPet.findAll(); +// try { +// ucManagePet.deleteAll(); +// } catch (Exception e) { +// throw new NotFoundException(e); +// } +// return petMapper.mapEntityToTransferObject(entities); +// } }