inventory working :D

This commit is contained in:
2025-02-05 19:38:25 -03:00
parent 7a2c3d2f67
commit 5289910270
15 changed files with 174 additions and 468 deletions

View File

@@ -13,55 +13,59 @@ namespace PetCompanion.Repositories
_context = context;
}
public async Task<Inventory> GetPetInventory(string petId)
public Inventory GetPetInventory(string petId)
{
return await _context.Inventories
.Include(i => i.Items)
.ThenInclude(ii => ii.GameItem)
.FirstOrDefaultAsync(i => i.PetId == petId);
return _context.Inventories
.FirstOrDefault(i => i.PetId == petId);
}
public async Task<Dictionary<ItemEquipTarget, int>> GetEquippedItems(string petId)
public Dictionary<ItemEquipTarget, int> GetEquippedItems(string petId)
{
var equippedItems = await _context.EquippedItems
var equippedItems = _context.EquippedItems
.Where(e => e.PetId == petId)
.ToDictionaryAsync(e => e.EquipTarget, e => e.GameItemId);
.ToDictionary(e => e.EquipTarget, e => e.GameItemId);
return equippedItems;
}
public async Task SaveInventory(Inventory inventory)
public void UpdateEquippedItems(string petId, Dictionary<ItemEquipTarget, int> equippedItems)
{
var existingInventory = await _context.Inventories
.Include(i => i.Items)
.FirstOrDefaultAsync(i => i.PetId == inventory.PetId);
var existingItems = _context.EquippedItems
.Where(e => e.PetId == petId)
.ToList();
if (existingInventory == null)
_context.EquippedItems.RemoveRange(existingItems);
var newEquippedItems = equippedItems.Select(kvp => new EquippedItem
{
_context.Inventories.Add(inventory);
PetId = petId,
EquipTarget = kvp.Key,
GameItemId = kvp.Value
});
_context.EquippedItems.AddRangeAsync(newEquippedItems);
_context.SaveChangesAsync();
}
public Inventory SavePetInventory(Inventory inventory)
{
if (_context.Inventories.Any(i => i.PetId == inventory.PetId))
{
_context.Inventories.Update(inventory);
}
else
{
// Clear existing items
_context.InventoryItems.RemoveRange(existingInventory.Items);
// Update inventory properties
// existingInventory.Capacity = inventory.Capacity;
// Add new items
foreach (var item in inventory.Items)
{
existingInventory.Items.Add(new InventoryItem
{
InventoryId = existingInventory.PetId,
GameItemId = item.GameItemId,
Quantity = item.Quantity,
GameItem = item.GameItem
});
}
_context.Inventories.Add(inventory);
}
_context.SaveChanges();
return inventory;
}
await _context.SaveChangesAsync();
public Inventory UpdatePetInventory(Inventory inventory)
{
_context.Inventories.Update(inventory);
_context.SaveChanges();
return inventory;
}
}
}