Refactor inventory unequip logic to use item ID instead of equip target; update README for inventory and skill tree progress

This commit is contained in:
Jose Henrique 2025-02-05 20:37:12 -03:00
parent 5289910270
commit f553196ca0
5 changed files with 32 additions and 17 deletions

View File

@ -62,12 +62,12 @@ namespace PetCompanion.Controllers
} }
} }
[HttpPut("{petId}/{equipTarget}/unequip")] [HttpPut("{petId}/{itemId}/unequip")]
public IActionResult UnequipItem(string petId, ItemEquipTarget equipTarget) public IActionResult UnequipItem(string petId, int itemId)
{ {
try try
{ {
var updatedPet = inventoryService.UnequipItem(petId, userId.ToString(), equipTarget); var updatedPet = inventoryService.UnequipItem(petId, userId.ToString(), itemId);
return Ok(updatedPet); return Ok(updatedPet);
} }
catch (Exception ex) catch (Exception ex)

View File

@ -15,15 +15,15 @@
## Inventory system ## Inventory system
- [ ] Inventory UI - [ ] Inventory UI
- [ ] Inventory database - [x] Inventory database
- [ ] Inventory item database (items, consumables, equipment) - [x] Inventory item database (items, consumables, equipment)
- [ ] Inventory item interactions (use, equip, unequip, drop) - [x] Inventory item interactions (use, equip, unequip, drop)
- [ ] Inventory item stacking - [/] Inventory item stacking
- [ ] Inventory item sorting (UI) - [ ] Inventory item sorting (UI)
## Skill tree system ## Skill tree system
- [ ] Skill tree UI - [ ] Skill tree UI
- [ ] Skill tree database - [x] Skill tree database
- [ ] Skill tree interactions (learn, unlearn, upgrade) - [/] Skill tree interactions (learn, unlearn, upgrade)
- [ ] Skill tree requirements (level, skill points, other skills) - [/] Skill tree requirements (level, skill points, other skills)
- [ ] Skill tree effects (TBD) - [x] Skill tree effects (TBD)

View File

@ -13,6 +13,13 @@ namespace PetCompanion.Repositories
_context = context; _context = context;
} }
public Skill GetSkill(int id)
{
return _context.Skills
.Include(s => s.Effects)
.FirstOrDefault(s => s.Id == id);
}
public IEnumerable<PetSkill> GetPetSkills(string petId) public IEnumerable<PetSkill> GetPetSkills(string petId)
{ {
return _context.PetSkills return _context.PetSkills

View File

@ -79,7 +79,8 @@ namespace PetCompanion.Services
// If there's already an item equipped in that slot, unequip it first // If there's already an item equipped in that slot, unequip it first
if (pet.EquippedItems.ContainsKey(gameItem.EquipTarget)) if (pet.EquippedItems.ContainsKey(gameItem.EquipTarget))
{ {
UnequipItem(petId, userId, gameItem.EquipTarget); var equippedItemId = pet.EquippedItems[gameItem.EquipTarget];
UnequipItem(petId, userId, equippedItemId);
} }
// Apply equipment effects // Apply equipment effects
@ -96,17 +97,18 @@ namespace PetCompanion.Services
return petRepository.UpdatePet(pet); return petRepository.UpdatePet(pet);
} }
public Pet UnequipItem(string petId, string userId, ItemEquipTarget equipTarget) public Pet UnequipItem(string petId, string userId, int itemId)
{ {
var pet = petRepository.GetPetById(petId, userId); var pet = petRepository.GetPetById(petId, userId);
if (pet == null) if (pet == null)
throw new Exception("Pet not found"); throw new Exception("Pet not found");
if (!pet.EquippedItems.ContainsKey(equipTarget)) var equipTarget = pet.EquippedItems.FirstOrDefault(kvp => kvp.Value == itemId).Key;
if (equipTarget == ItemEquipTarget.None)
throw new Exception("No item equipped in that slot"); throw new Exception("No item equipped in that slot");
var equippedItemId = pet.EquippedItems[equipTarget]; var equippedItem = gameItemsRepository.GetById(itemId);
var equippedItem = gameItemsRepository.GetById(equippedItemId);
if (equippedItem != null) if (equippedItem != null)
{ {
@ -114,7 +116,7 @@ namespace PetCompanion.Services
gameItemService.RemoveItemEffect(pet, equippedItem); gameItemService.RemoveItemEffect(pet, equippedItem);
// Add item back to inventory // Add item back to inventory
pet.Inventory.Items.Add(equippedItemId); pet.Inventory.Items.Add(itemId);
} }
pet.EquippedItems.Remove(equipTarget); pet.EquippedItems.Remove(equipTarget);

View File

@ -32,6 +32,7 @@ namespace PetCompanion.Services
if (pet.SkillPoints <= 0) if (pet.SkillPoints <= 0)
throw new Exception("No skill points available"); throw new Exception("No skill points available");
var skill = _petSkillRepository.GetSkill(skillId);
var skills = _petSkillRepository.GetPetSkills(petId); var skills = _petSkillRepository.GetPetSkills(petId);
var existingSkill = skills.FirstOrDefault(s => s.SkillId == skillId); var existingSkill = skills.FirstOrDefault(s => s.SkillId == skillId);
@ -45,6 +46,11 @@ namespace PetCompanion.Services
} }
else else
{ {
if (!skill.SkillsIdRequired.TrueForAll(ni => pet.Skills.Any(s => s.SkillId == ni)))
{
throw new Exception("Missing required skill");
}
existingSkill = new PetSkill existingSkill = new PetSkill
{ {
PetId = petId, PetId = petId,