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 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 skill = _petSkillRepository.GetSkill(skillId); 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 { if (!skill.SkillsIdRequired.TrueForAll(ni => pet.Skills.Any(s => s.SkillId == ni))) { throw new Exception("Missing required skill"); } existingSkill = new PetSkill { PetId = petId, SkillId = skillId, CurrentTier = SkillTier.I }; _petSkillRepository.SavePetSkill(existingSkill); } pet.SkillPoints--; _petRepository.UpdatePet(pet); return existingSkill; } } }