inventory working :D
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,6 +19,8 @@ namespace PetCompanion.Repositories
|
||||
.Where(p => p.UserId == userId)
|
||||
.Include(p => p.Stats)
|
||||
.Include(p => p.Resources)
|
||||
.Include(p => p.Inventory)
|
||||
.Include(p => p.EquippedItemsList)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@@ -28,6 +30,8 @@ namespace PetCompanion.Repositories
|
||||
.Where(p => p.Id == petId && p.UserId == userId)
|
||||
.Include(p => p.Stats)
|
||||
.Include(p => p.Resources)
|
||||
.Include(p => p.Inventory)
|
||||
.Include(p => p.EquippedItemsList)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
@@ -41,6 +45,12 @@ namespace PetCompanion.Repositories
|
||||
public Pet UpdatePet(Pet pet)
|
||||
{
|
||||
context.Pets.Update(pet);
|
||||
|
||||
if (pet.Inventory != null)
|
||||
{
|
||||
context.Inventories.Update(pet.Inventory);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
return pet;
|
||||
}
|
||||
|
@@ -13,16 +13,16 @@ namespace PetCompanion.Repositories
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PetSkill>> GetPetSkills(string petId)
|
||||
public IEnumerable<PetSkill> GetPetSkills(string petId)
|
||||
{
|
||||
return await _context.PetSkills
|
||||
return _context.PetSkills
|
||||
.Include(ps => ps.Skill)
|
||||
.ThenInclude(s => s.Effects)
|
||||
.Where(ps => ps.PetId == petId)
|
||||
.ToListAsync();
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<PetSkill> SavePetSkill(PetSkill petSkill)
|
||||
public PetSkill SavePetSkill(PetSkill petSkill)
|
||||
{
|
||||
if (petSkill.Id == 0)
|
||||
{
|
||||
@@ -32,7 +32,7 @@ namespace PetCompanion.Repositories
|
||||
{
|
||||
_context.PetSkills.Update(petSkill);
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
_context.SaveChanges();
|
||||
return petSkill;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user