not working inventory :c

This commit is contained in:
2025-02-04 17:47:18 -03:00
parent f234b5d84d
commit 7a2c3d2f67
22 changed files with 2713 additions and 49 deletions

View File

@@ -6,12 +6,23 @@ namespace PetCompanion.Services
public class PetService
{
private readonly PetRepository petRepository;
private readonly PetClassService _petClassService;
private readonly PetClassService petClassService;
private readonly GameItemService gameItemService;
private readonly GameItemsRepository gameItemsRepository;
private readonly PetInventoryService petInventoryService;
public PetService(PetRepository petRepository, PetClassService petClassService)
public PetService(
PetRepository petRepository,
PetClassService petClassService,
GameItemService gameItemService,
GameItemsRepository gameItemsRepository,
PetInventoryService petInventoryService)
{
this.petRepository = petRepository;
_petClassService = petClassService;
this.petClassService = petClassService;
this.gameItemService = gameItemService;
this.gameItemsRepository = gameItemsRepository;
this.petInventoryService = petInventoryService;
}
public IEnumerable<Pet> GetAllPets(Guid userId)
@@ -39,7 +50,11 @@ namespace PetCompanion.Services
IsDead = false
};
return petRepository.CreatePet(pet);
var createdPet = petRepository.CreatePet(pet);
var inventory = petInventoryService.CreateInventory(petId).Result;
return createdPet;
}
public Pet UpdatePetAction(string petId, string userId, PetUpdateActionRequest actionRequest)
@@ -118,7 +133,7 @@ namespace PetCompanion.Services
throw new Exception("Pet not found");
}
return _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
return petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
}
public Pet UpdatePetResources(string petId, string userId)
@@ -129,7 +144,7 @@ namespace PetCompanion.Services
throw new Exception("Pet not found");
}
var gatheredResources = _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
var gatheredResources = petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
pet.Resources.Wisdom += gatheredResources.Wisdom;
pet.Resources.Gold += gatheredResources.Gold;
@@ -139,5 +154,147 @@ namespace PetCompanion.Services
return petRepository.UpdatePetResources(pet);
}
public Pet UseItem(string petId, string userId, int itemId)
{
var pet = petRepository.GetPetById(petId, userId);
if (pet == null)
throw new Exception("Pet not found");
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
if (inventoryItem == null || inventoryItem.Quantity <= 0)
throw new Exception("Item not found in inventory");
gameItemService.ApplyItemEffect(pet, inventoryItem.GameItem);
inventoryItem.Quantity--;
if (inventoryItem.Quantity <= 0)
pet.Inventory.Items.Remove(inventoryItem);
return petRepository.UpdatePet(pet);
}
public Pet EquipItem(string petId, string userId, int itemId)
{
var pet = petRepository.GetPetById(petId, userId);
if (pet == null)
throw new Exception("Pet not found");
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
if (inventoryItem == null || inventoryItem.Quantity <= 0)
throw new Exception("Item not found in inventory");
if (inventoryItem.GameItem.Type != ItemType.Equipment)
throw new Exception("Item is not equipment");
// If there's already an item equipped in that slot, unequip it first
if (pet.EquippedItems.ContainsKey(inventoryItem.GameItem.EquipTarget))
{
UnequipItem(pet, inventoryItem.GameItem.EquipTarget);
}
// Apply equipment effects
gameItemService.ApplyItemEffect(pet, inventoryItem.GameItem);
// Mark item as equipped
pet.EquippedItems[inventoryItem.GameItem.EquipTarget] = itemId;
// Remove from inventory
inventoryItem.Quantity--;
if (inventoryItem.Quantity <= 0)
pet.Inventory.Items.Remove(inventoryItem);
return petRepository.UpdatePet(pet);
}
public Pet UnequipItem(string petId, string userId, ItemEquipTarget equipTarget)
{
var pet = petRepository.GetPetById(petId, userId);
if (pet == null)
throw new Exception("Pet not found");
UnequipItem(pet, equipTarget);
return petRepository.UpdatePet(pet);
}
private void UnequipItem(Pet pet, ItemEquipTarget equipTarget)
{
if (!pet.EquippedItems.ContainsKey(equipTarget))
return;
var equippedItemId = pet.EquippedItems[equipTarget];
var equippedItem = gameItemsRepository.GetById(equippedItemId);
if (equippedItem != null)
{
// Remove equipment effects
gameItemService.RemoveItemEffect(pet, equippedItem);
// Add item back to inventory
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == equippedItemId);
if (inventoryItem != null)
inventoryItem.Quantity++;
else
pet.Inventory.Items.Add(new InventoryItem
{
GameItemId = equippedItemId,
Quantity = 1,
GameItem = equippedItem,
InventoryId = pet.Id
});
}
pet.EquippedItems.Remove(equipTarget);
}
public Pet GetPet(string petId, string userId)
{
var pet = petRepository.GetPetById(petId, userId);
if (pet == null)
throw new Exception("Pet not found");
return pet;
}
public Pet DropItem(string petId, string userId, int itemId)
{
var pet = GetPet(petId, userId);
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
if (inventoryItem == null || inventoryItem.Quantity <= 0)
throw new Exception("Item not found in inventory");
inventoryItem.Quantity--;
if (inventoryItem.Quantity <= 0)
pet.Inventory.Items.Remove(inventoryItem);
return petRepository.UpdatePet(pet);
}
public Pet AddItemToPet(string petId, string userId, int itemId, int quantity)
{
var pet = GetPet(petId, userId);
var gameItem = gameItemsRepository.GetById(itemId);
if (gameItem == null)
throw new Exception("Item not found");
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
if (inventoryItem != null)
{
inventoryItem.Quantity += quantity;
}
else
{
pet.Inventory.Items.Add(new InventoryItem
{
GameItemId = itemId,
Quantity = quantity,
GameItem = gameItem,
InventoryId = pet.Id
});
}
return petRepository.UpdatePet(pet);
}
}
}