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:
@@ -42,7 +42,6 @@ namespace PetCompanion.Services
|
||||
Health = 100,
|
||||
MaxHealth = 100,
|
||||
Level = 1,
|
||||
Experience = 0,
|
||||
Stats = PetStats.BuildFromClass(petRequest.Class),
|
||||
Resources = new Resources(),
|
||||
GatherActionSince = DateTime.UtcNow,
|
||||
|
@@ -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;
|
||||
|
@@ -1,84 +1,24 @@
|
||||
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 PetSkill AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
{
|
||||
var pet = _context.Pets
|
||||
.Include(p => p.Skills)
|
||||
.FirstOrDefault(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 = _context.Skills
|
||||
.Include(s => s.Effects)
|
||||
.FirstOrDefault(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);
|
||||
|
||||
_petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
else
|
||||
{
|
||||
var newSkill = new PetSkill
|
||||
{
|
||||
PetId = petId,
|
||||
SkillId = skillId,
|
||||
CurrentTier = SkillTier.I
|
||||
};
|
||||
_petSkillRepository.SavePetSkill(newSkill);
|
||||
|
||||
var effect = skill.Effects.FirstOrDefault(e => e.Tier == SkillTier.I);
|
||||
if (effect != null)
|
||||
effect.PetSkillUpgrade(ref pet);
|
||||
}
|
||||
|
||||
pet.SkillPoints--;
|
||||
_context.SaveChanges();
|
||||
|
||||
return _petSkillRepository.GetPetSkills(petId).First(ps => ps.SkillId == skillId);
|
||||
}
|
||||
|
||||
public IEnumerable<Skill> GetAvailableSkills()
|
||||
{
|
||||
return _context.Skills
|
||||
.Include(s => s.Effects)
|
||||
.ToList();
|
||||
return _petSkillRepository.GetAvailableSkills();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user