pet-companion-back/Services/PetSkillService.cs

64 lines
2.0 KiB
C#

using PetCompanion.Models;
using PetCompanion.Repositories;
namespace PetCompanion.Services
{
public class PetSkillService
{
private readonly PetSkillRepository _petSkillRepository;
private readonly PetRepository _petRepository;
public PetSkillService(PetSkillRepository petSkillRepository, PetRepository petRepository)
{
_petSkillRepository = petSkillRepository;
_petRepository = petRepository;
}
public IEnumerable<PetSkill> GetPetSkills(string petId, string userId)
{
var pet = _petRepository.GetPetById(petId, userId);
if (pet == null)
throw new Exception("Pet not found");
return _petSkillRepository.GetPetSkills(petId);
}
public PetSkill AllocateSkillPoint(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 skills = _petSkillRepository.GetPetSkills(petId);
var existingSkill = skills.FirstOrDefault(s => s.SkillId == skillId);
if (existingSkill != null)
{
if (existingSkill.CurrentTier == SkillTier.III)
throw new Exception("Skill already at maximum tier");
existingSkill.CurrentTier++;
_petSkillRepository.SavePetSkill(existingSkill);
}
else
{
existingSkill = new PetSkill
{
PetId = petId,
SkillId = skillId,
CurrentTier = SkillTier.I
};
_petSkillRepository.SavePetSkill(existingSkill);
}
pet.SkillPoints--;
_petRepository.UpdatePet(pet);
return existingSkill;
}
}
}