Refactor skill allocation to upgrade; implement skill requirements and resource checks; remove experience from Pet model; update README for skill tree progress
This commit is contained in:
@@ -23,16 +23,29 @@ namespace PetCompanion.Services
|
||||
return _petSkillRepository.GetPetSkills(petId);
|
||||
}
|
||||
|
||||
public PetSkill AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
public PetSkill UpgradeSkill(string petId, string userId, int skillId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
if (pet.SkillPoints <= 0)
|
||||
throw new Exception("No skill points available");
|
||||
|
||||
var skill = _petSkillRepository.GetSkill(skillId);
|
||||
|
||||
if (skill == null)
|
||||
throw new Exception("Skill not found");
|
||||
|
||||
foreach (var req in skill.SkillRequirements)
|
||||
{
|
||||
if (req.Resource.ToLower() == "wisdom" && pet.Resources.Wisdom < req.Cost)
|
||||
throw new Exception("Insufficient resources");
|
||||
if (req.Resource.ToLower() == "food" && pet.Resources.Food < req.Cost)
|
||||
throw new Exception("Insufficient resources");
|
||||
if (req.Resource.ToLower() == "gold" && pet.Resources.Gold < req.Cost)
|
||||
throw new Exception("Insufficient resources");
|
||||
if (req.Resource.ToLower() == "junk" && pet.Resources.Junk < req.Cost)
|
||||
throw new Exception("Insufficient resources");
|
||||
}
|
||||
|
||||
var skills = _petSkillRepository.GetPetSkills(petId);
|
||||
var existingSkill = skills.FirstOrDefault(s => s.SkillId == skillId);
|
||||
|
||||
@@ -46,7 +59,7 @@ namespace PetCompanion.Services
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!skill.SkillsIdRequired.TrueForAll(ni => pet.Skills.Any(s => s.SkillId == ni)))
|
||||
if (!skill.SkillsIdRequired?.TrueForAll(ni => pet.Skills.Any(s => s.SkillId == ni)) ?? false)
|
||||
{
|
||||
throw new Exception("Missing required skill");
|
||||
}
|
||||
@@ -60,7 +73,14 @@ namespace PetCompanion.Services
|
||||
_petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
|
||||
pet.SkillPoints--;
|
||||
foreach (var req in skill.SkillRequirements)
|
||||
{
|
||||
if (req.Resource.ToLower() == "wisdom") pet.Resources.Wisdom -= req.Cost;
|
||||
if (req.Resource.ToLower() == "food") pet.Resources.Food -= req.Cost;
|
||||
if (req.Resource.ToLower() == "gold") pet.Resources.Gold -= req.Cost;
|
||||
if (req.Resource.ToLower() == "junk") pet.Resources.Junk -= req.Cost;
|
||||
}
|
||||
|
||||
_petRepository.UpdatePet(pet);
|
||||
|
||||
return existingSkill;
|
||||
|
||||
Reference in New Issue
Block a user