85 lines
2.8 KiB
C#
85 lines
2.8 KiB
C#
using PetCompanion.Models;
|
|
using PetCompanion.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PetCompanion.Repositories;
|
|
|
|
namespace PetCompanion.Services
|
|
{
|
|
public class SkillService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly PetRepository _petRepository;
|
|
private readonly PetSkillRepository _petSkillRepository;
|
|
|
|
public SkillService(
|
|
ApplicationDbContext context,
|
|
PetRepository petRepository,
|
|
PetSkillRepository petSkillRepository)
|
|
{
|
|
_context = context;
|
|
_petRepository = petRepository;
|
|
_petSkillRepository = petSkillRepository;
|
|
}
|
|
|
|
public async Task<PetSkill> AllocateSkillPoint(string petId, string userId, int skillId)
|
|
{
|
|
var pet = await _context.Pets
|
|
.Include(p => p.Skills)
|
|
.FirstOrDefaultAsync(p => p.Id == petId && p.UserId == userId);
|
|
|
|
if (pet == null)
|
|
throw new Exception("Pet not found");
|
|
|
|
if (pet.SkillPoints <= 0)
|
|
throw new Exception("No skill points available");
|
|
|
|
var skill = await _context.Skills
|
|
.Include(s => s.Effects)
|
|
.FirstOrDefaultAsync(s => s.Id == skillId);
|
|
|
|
if (skill == null)
|
|
throw new Exception("Skill not found");
|
|
|
|
var existingSkill = pet.Skills.FirstOrDefault(ps => ps.SkillId == skillId);
|
|
if (existingSkill != null)
|
|
{
|
|
if (existingSkill.CurrentTier == SkillTier.III)
|
|
throw new Exception("Skill already at maximum tier");
|
|
|
|
existingSkill.CurrentTier++;
|
|
var effect = skill.Effects.FirstOrDefault(e => e.Tier == existingSkill.CurrentTier);
|
|
if (effect != null)
|
|
effect.PetSkillUpgrade(ref pet);
|
|
|
|
await _petSkillRepository.SavePetSkill(existingSkill);
|
|
}
|
|
else
|
|
{
|
|
var newSkill = new PetSkill
|
|
{
|
|
PetId = petId,
|
|
SkillId = skillId,
|
|
CurrentTier = SkillTier.I
|
|
};
|
|
await _petSkillRepository.SavePetSkill(newSkill);
|
|
|
|
var effect = skill.Effects.FirstOrDefault(e => e.Tier == SkillTier.I);
|
|
if (effect != null)
|
|
effect.PetSkillUpgrade(ref pet);
|
|
}
|
|
|
|
pet.SkillPoints--;
|
|
await _context.SaveChangesAsync();
|
|
|
|
return (await _petSkillRepository.GetPetSkills(petId)).First(ps => ps.SkillId == skillId);
|
|
}
|
|
|
|
public async Task<IEnumerable<Skill>> GetAvailableSkills()
|
|
{
|
|
return await _context.Skills
|
|
.Include(s => s.Effects)
|
|
.ToListAsync();
|
|
}
|
|
}
|
|
}
|