Skip to content

Commit b9b873e

Browse files
Implement pet/shop/health system
1 parent e646ec4 commit b9b873e

File tree

16 files changed

+1145
-36
lines changed

16 files changed

+1145
-36
lines changed

src/main/java/com/backend/controller/PetController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.springframework.web.bind.annotation.RequestParam;
1212
import org.springframework.web.bind.annotation.RestController;
1313

14-
import java.util.ArrayList;
1514

1615
@RestController
1716
public class PetController {
@@ -35,11 +34,11 @@ public ResponseEntity<Object> getPet(@RequestParam SessionID sessionID){
3534
/**
3635
* Post request to replace the current pet with the same pet with an updated outfit from the database
3736
* @param sessionID string that represents the current session and verifies the action
38-
* @param newOutfit ArrayList of ShopItems that will be for the updated outfit
37+
* @param shopItem ShopItem that will be for the updated outfit
3938
*/
4039
@PostMapping("/updatePetOutfit")
41-
public ResponseEntity<Object> updatePetOutfit(@RequestParam SessionID sessionID, ArrayList<ShopItem> newOutfit){
42-
return this.petSystemFacade.updateCurrentOutfit(sessionID, newOutfit);
40+
public ResponseEntity<Object> updatePetOutfit(@RequestParam SessionID sessionID, ShopItem shopItem){
41+
return this.petSystemFacade.updateCurrentOutfit(sessionID, shopItem);
4342
}
4443

4544
/**

src/main/java/com/backend/entities/ShopItem.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ public class ShopItem {
1919
private final Double cost;
2020
private final String name;
2121
private final String description;
22+
private final String imageLink;
23+
private final int location;
2224

2325
// Contructor
2426
@PersistenceCreator
25-
public ShopItem(String ID, Double cost, String name, String description){
27+
public ShopItem(String ID, Double cost, String name, String description, String imageLink, int location){
2628
this.itemID = new ItemID(ID);
2729
this.ID = ID;
2830
this.cost = cost;
2931
this.name = name;
3032
this.description = description;
33+
this.imageLink = imageLink;
34+
this.location = location;
3135
}
3236

3337
// Getters
@@ -58,4 +62,18 @@ public String getName(){
5862
public String getDescription(){
5963
return description;
6064
}
65+
66+
/**
67+
* @return the link for the image
68+
*/
69+
public String getImageLink(){
70+
return imageLink;
71+
}
72+
73+
/**
74+
* @return the location
75+
*/
76+
public int getLocation(){
77+
return location;
78+
}
6179
}

src/main/java/com/backend/usecases/facades/PetSystemFacade.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.springframework.http.ResponseEntity;
1313
import org.springframework.stereotype.Service;
1414

15-
import java.util.ArrayList;
1615

1716
@Service
1817
@Configurable
@@ -24,6 +23,14 @@ public class PetSystemFacade {
2423
private final BalanceManager balanceManager;
2524
private final IErrorHandler errorHandler;
2625

26+
/**
27+
* Spring Boot Dependency Injection of the accountManager
28+
* @param shopManager the dependency to be injected
29+
* @param accountManager the dependency to be injected
30+
* @param petManager the dependency to be injected
31+
* @param balanceManager the dependency to be injected
32+
* @param errorHandler the dependency to be injected
33+
*/
2734
@Autowired
2835
public PetSystemFacade(ShopManager shopManager, AccountManager accountManager, PetManager petManager, BalanceManager balanceManager, IErrorHandler errorHandler){
2936
this.shopManager = shopManager;
@@ -34,7 +41,7 @@ public PetSystemFacade(ShopManager shopManager, AccountManager accountManager, P
3441
}
3542

3643
/**
37-
* Post request of the pet owning the item and subtracting from its balance the cost of the item
44+
* Post request of the allowing the pet owning the item and subtracting from its balance the cost of the item if it fulfills the requirements
3845
* @param sessionID string that represents the current session and verifies the action
3946
* @param itemID string that represents which item the pet is attempting to access
4047
*/
@@ -69,19 +76,28 @@ public ResponseEntity<Object> purchaseItem(String sessionID, String itemID) {
6976
}
7077
}
7178

72-
public ResponseEntity<Object> updateCurrentOutfit(SessionID sessionID, ArrayList<ShopItem> newOutfit){
79+
/**
80+
* Post request of the allowing the pet to update the outfit
81+
* @param sessionID string that represents the current session and verifies the action
82+
* @param shopItem ShopItem of shopItem of outfit that the pet will wear
83+
*/
84+
public ResponseEntity<Object> updateCurrentOutfit(SessionID sessionID, ShopItem shopItem){
7385
AccountID accountID = this.accountManager.verifySession(sessionID);
7486
if (accountID == null){
7587
return this.errorHandler.logError(new SessionException("Account ID is null since sessionID was invalid"), HttpStatus.BAD_REQUEST);
7688
}
77-
boolean result = this.shopManager.updateCurrentOutfit(accountID, newOutfit);
89+
boolean result = this.shopManager.updateCurrentOutfit(accountID, shopItem);
7890
if (!result){
7991
return this.errorHandler.logError(new SessionException("Account ID is null since sessionID was invalid"), HttpStatus.BAD_REQUEST);
8092
}
8193
return new ResponseEntity<>("Outfit successfully updated", HttpStatus.OK);
8294
}
8395

8496

97+
/**
98+
* Get request of the pet by the sessionID from the database
99+
* @param sessionID string that represents the current session and verifies the action
100+
*/
85101
public ResponseEntity<Object> getPet(SessionID sessionID){
86102
AccountID accountID = this.accountManager.verifySession(sessionID);
87103
if (accountID == null){
@@ -90,6 +106,10 @@ public ResponseEntity<Object> getPet(SessionID sessionID){
90106
return new ResponseEntity<>(petManager.getPet(accountID.getID()), HttpStatus.OK);
91107
}
92108

109+
/**
110+
* Get request of the pet's balance by the sessionID from the database
111+
* @param sessionID string that represents the current session and verifies the action
112+
*/
93113
public ResponseEntity<Object> getBalance(SessionID sessionID){
94114
AccountID accountID = this.accountManager.verifySession(sessionID);
95115
if (accountID == null){
@@ -100,6 +120,9 @@ public ResponseEntity<Object> getBalance(SessionID sessionID){
100120
return new ResponseEntity<>(balance, HttpStatus.OK);
101121
}
102122

123+
/**
124+
* Get request of all the shopItems from the database
125+
*/
103126
public ResponseEntity<Object> getShopItems(){
104127
return new ResponseEntity<>(this.shopManager.getShopItems(), HttpStatus.OK);
105128
}

src/main/java/com/backend/usecases/managers/BalanceManager.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ public class BalanceManager {
1515
private final PetRepo petRepo;
1616
private final IErrorHandler errorHandler;
1717

18+
/**
19+
* Spring Boot Dependency Injection of the Accounts Repository
20+
* @param petRepo the dependency to be injected
21+
* @param errorHandler the dependency to be injected
22+
*/
1823
@Autowired
1924
public BalanceManager(PetRepo petRepo, IErrorHandler errorHandler) {
2025
this.petRepo = petRepo;
2126
this.errorHandler = errorHandler;
2227
}
2328

2429
/**
25-
* Get request of balance from the database through the pet object
30+
* Get the balance from the database through the pet object by the accountID
2631
* @param accountID string that represents the current session and verifies the action
2732
*/
2833
public Double getBalance(String accountID){
@@ -37,7 +42,7 @@ public Double getBalance(String accountID){
3742
}
3843

3944
/**
40-
* Post request of balance changed with the parameter amount
45+
* Update balance changed with the parameter amount for the corresponding account
4146
* @param accountID string that represents the current session and verifies the action
4247
* @param amount the double that represents the amount added to the pet's balance
4348
*/

src/main/java/com/backend/usecases/managers/HealthManager.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ public class HealthManager {
2020
private final PetRepo petRepo;
2121
private final IErrorHandler errorHandler;
2222

23+
/**
24+
* Spring Boot Dependency Injection of the Accounts Repository
25+
* @param petRepo the dependency to be injected
26+
* @param errorHandler the dependency to be injected
27+
*/
2328
@Autowired
2429
public HealthManager(PetRepo petRepo, IErrorHandler errorHandler) {
2530
this.petRepo = petRepo;
2631
this.errorHandler = errorHandler;
2732
}
2833

2934
/**
30-
* Post request of health added with the parameter amount
35+
* Changing health with the parameter amount for the corresponding account
3136
* @param accountID string that represents the current session and verifies the action
3237
* @param amount the double that represents the amount added to the pet's balance
3338
*/
@@ -37,16 +42,17 @@ public void updateHealth(String accountID, double amount){
3742
this.errorHandler.logError(new SessionException("Pet is null since accountID was invalid"));
3843
return;
3944
}
40-
4145
double updatedHealth = pet.get().getHealth() + amount;
46+
if (updatedHealth > 100){
47+
updatedHealth = 100;
48+
}
4249

4350
Pet updatedPet = new Pet(accountID, updatedHealth, pet.get().getBalance(), pet.get().getInventory(), pet.get().getCurrentOutfit());
4451
this.petRepo.save(updatedPet);
4552
}
4653

4754
/**
4855
* Decay of health overtime if task have not been completed
49-
*
5056
* @param accountID string that represents the current session and verifies the action
5157
*/
5258
public void healthDecay(String accountID, List<TaskCompletionRecord> completionRecords){

src/main/java/com/backend/usecases/managers/PetManager.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,45 @@ public class PetManager {
1818
private final PetRepo petRepo;
1919
private final IErrorHandler errorHandler;
2020

21+
/**
22+
* Spring Boot Dependency Injection of the Accounts Repository
23+
* @param petRepo the dependency to be injected
24+
* @param errorHandler the dependency to be injected
25+
*/
2126
@Autowired
2227
public PetManager(PetRepo petRepo, IErrorHandler errorHandler) {
2328
this.petRepo = petRepo;
2429
this.errorHandler = errorHandler;
2530
}
2631

2732
/**
28-
* Get request to return the pet object and it's attributes
33+
* Return the pet object and it's attributes
2934
* @param accountID string that represents the current session and verifies the action
3035
*/
3136
public Pet getPet(String accountID){
3237
Optional<Pet> pet = petRepo.findById(accountID);
33-
return pet.orElse(null);
38+
39+
if (pet.isEmpty()){
40+
return null;
41+
}
42+
return pet.get();
3443
}
3544

3645

3746
/**
38-
* Adds the default pet for an account
47+
* Adds the default pet for an account into the database
3948
* @param id AccountId string that represents the pet linking to the account
4049
*/
4150
public Pet initializePet(String id){
4251
if (id.isEmpty()){
4352
return null;
4453
}
4554
ArrayList<ShopItem> curInventory = new ArrayList<>();
46-
curInventory.add(new ShopItem("1480775928", 19.9, "brown pants", "a pair of classic fit brown pants"));
47-
Pet pet = new Pet(id, 85.00, 25.4, curInventory, new ArrayList<>());
55+
ArrayList<ShopItem> curOutfit = new ArrayList<>();
56+
ShopItem shopItem = new ShopItem("1480775928", 19.9, "brown pants", "a pair of classic fit brown pants", "", 2);
57+
curInventory.add(shopItem);
58+
curOutfit.add(shopItem);
59+
Pet pet = new Pet(id, 85.00, 25.4, curInventory, curOutfit);
4860
this.petRepo.save(pet);
4961
return pet;
5062
}

src/main/java/com/backend/usecases/managers/ShopManager.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public class ShopManager {
2424
private final PetRepo petRepo;
2525
private final IErrorHandler errorHandler;
2626

27+
/**
28+
* Spring Boot Dependency Injection of the Accounts Repository
29+
* @param petRepo the dependency to be injected
30+
* @param errorHandler the dependency to be injected
31+
*/
2732
@Autowired
2833
public ShopManager(ShopItemRepo shopRepo, PetRepo petRepo, IErrorHandler errorHandler) {
2934
this.shopRepo = shopRepo;
@@ -33,15 +38,13 @@ public ShopManager(ShopItemRepo shopRepo, PetRepo petRepo, IErrorHandler errorHa
3338

3439
/**
3540
* Returns all the shop items from the database through petRepo
36-
*
3741
*/
3842
public List<ShopItem> getShopItems() {
3943
return this.shopRepo.findAll();
4044
}
4145

4246
/**
4347
* Returns a shop items from the database through petRepo
44-
*
4548
* @param shopItemID string that represents the id of the shopItem
4649
*/
4750
public ShopItem getShopItem(String shopItemID) {
@@ -54,17 +57,36 @@ public ShopItem getShopItem(String shopItemID) {
5457

5558

5659
/**
57-
* Post request of the pet object after modifying it with the newOutfit
60+
* Update the pet object after modifying it with the newOutfit
5861
* @param accountID string that represents the user and the pet's identity
59-
* @param newOutfit ArrayList of shopItem that would represent which items the pet is wearing
62+
* @param shopItem ShopItem that would represent which items the pet is wearing
6063
*/
61-
public boolean updateCurrentOutfit(AccountID accountID, ArrayList<ShopItem> newOutfit){
64+
public boolean updateCurrentOutfit(AccountID accountID, ShopItem shopItem){
6265
Optional<Pet> pet = this.petRepo.findById(accountID.getID());
6366
if (pet.isEmpty()) {
6467
return false;
6568
}
6669

67-
Pet updatedPet = new Pet(accountID.getID(), pet.get().getHealth(), 0.0, pet.get().getInventory(), newOutfit);
70+
ArrayList<ShopItem> curOutfit = pet.get().getCurrentOutfit();
71+
ArrayList<ShopItem> newOutfit = new ArrayList<>();
72+
73+
boolean alreadyEquipped = false;
74+
75+
for(ShopItem s: curOutfit){
76+
if (shopItem.getLocation() == s.getLocation()){
77+
newOutfit.add(shopItem);
78+
alreadyEquipped = true;
79+
}
80+
else{
81+
newOutfit.add(s);
82+
}
83+
}
84+
85+
if(!alreadyEquipped){
86+
newOutfit.add(shopItem);
87+
}
88+
89+
Pet updatedPet = new Pet(accountID.getID(), pet.get().getHealth(), pet.get().getBalance(), pet.get().getInventory(), newOutfit);
6890
this.petRepo.save(updatedPet);
6991
return true;
7092
}

0 commit comments

Comments
 (0)