77 lines
2.5 KiB
C#
77 lines
2.5 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;
|
|
|
|
public SkillService(ApplicationDbContext context, PetRepository petRepository)
|
|
{
|
|
this.context = context;
|
|
this.petRepository = petRepository;
|
|
}
|
|
|
|
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);
|
|
}
|
|
else
|
|
{
|
|
var newSkill = new PetSkill
|
|
{
|
|
PetId = petId,
|
|
SkillId = skillId,
|
|
CurrentTier = SkillTier.I
|
|
};
|
|
pet.Skills.Add(newSkill);
|
|
var effect = skill.Effects.FirstOrDefault(e => e.Tier == SkillTier.I);
|
|
if (effect != null)
|
|
effect.PetSkillUpgrade(ref pet);
|
|
}
|
|
|
|
pet.SkillPoints--;
|
|
await context.SaveChangesAsync();
|
|
|
|
return pet.Skills.First(ps => ps.SkillId == skillId);
|
|
}
|
|
|
|
public async Task<IEnumerable<Skill>> GetAvailableSkills()
|
|
{
|
|
return await context.Skills
|
|
.Include(s => s.Effects)
|
|
.ToListAsync();
|
|
}
|
|
}
|
|
}
|