not working inventory :c
This commit is contained in:
184
Services/PetInventoryService.cs
Normal file
184
Services/PetInventoryService.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using PetCompanion.Models;
|
||||
using PetCompanion.Repositories;
|
||||
|
||||
namespace PetCompanion.Services
|
||||
{
|
||||
public class PetInventoryService
|
||||
{
|
||||
private readonly PetInventoryRepository _inventoryRepository;
|
||||
private readonly PetRepository _petRepository;
|
||||
private readonly GameItemsRepository _gameItemsRepository;
|
||||
|
||||
public PetInventoryService(
|
||||
PetInventoryRepository inventoryRepository,
|
||||
PetRepository petRepository,
|
||||
GameItemsRepository gameItemsRepository)
|
||||
{
|
||||
_inventoryRepository = inventoryRepository;
|
||||
_petRepository = petRepository;
|
||||
_gameItemsRepository = gameItemsRepository;
|
||||
}
|
||||
|
||||
public async Task<Inventory> GetInventory(string petId, string userId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
return await _inventoryRepository.GetPetInventory(petId);
|
||||
}
|
||||
|
||||
public async Task<Inventory> CreateInventory(string petId)
|
||||
{
|
||||
var inventory = new Inventory
|
||||
{
|
||||
PetId = petId,
|
||||
Capacity = 20,
|
||||
Items = new List<InventoryItem>()
|
||||
};
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public async Task<Pet> UseItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
var item = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
|
||||
if (item == null || item.Quantity <= 0)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return _petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> EquipItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
var item = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
|
||||
if (item == null || item.Quantity <= 0)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
if (item.GameItem.Type != ItemType.Equipment)
|
||||
throw new Exception("Item is not equipment");
|
||||
|
||||
var equippedItems = await _inventoryRepository.GetEquippedItems(petId);
|
||||
if (equippedItems.ContainsKey(item.GameItem.EquipTarget))
|
||||
{
|
||||
await UnequipItem(petId, userId, item.GameItem.EquipTarget);
|
||||
}
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return _petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> UnequipItem(string petId, string userId, ItemEquipTarget equipTarget)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var equippedItems = await _inventoryRepository.GetEquippedItems(petId);
|
||||
if (!equippedItems.ContainsKey(equipTarget))
|
||||
return pet;
|
||||
|
||||
var itemId = equippedItems[equipTarget];
|
||||
var item = _gameItemsRepository.GetById(itemId);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
var inventoryItem = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
|
||||
if (inventoryItem != null)
|
||||
inventoryItem.Quantity++;
|
||||
else
|
||||
inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = itemId,
|
||||
Quantity = 1,
|
||||
GameItem = item,
|
||||
InventoryId = pet.Id
|
||||
});
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
}
|
||||
|
||||
return _petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> DropItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
if (inventory == null)
|
||||
throw new Exception("Inventory not found");
|
||||
|
||||
var item = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (item == null || item.Quantity <= 0)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return pet;
|
||||
}
|
||||
|
||||
public async Task<Pet> AddItemToPet(string petId, string userId, int itemId, int quantity)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var gameItem = _gameItemsRepository.GetById(itemId);
|
||||
if (gameItem == null)
|
||||
throw new Exception("Item not found");
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
if (inventory == null)
|
||||
throw new Exception("Inventory not found");
|
||||
|
||||
var existingItem = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (existingItem != null)
|
||||
{
|
||||
existingItem.Quantity += quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = itemId,
|
||||
Quantity = quantity,
|
||||
GameItem = gameItem,
|
||||
InventoryId = inventory.PetId
|
||||
});
|
||||
}
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return pet;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user