Refactor pet action management: rename PetAction to PetActionGather, update related models and services, and enhance resource gathering logic.
This commit is contained in:
47
Services/PetClassService.cs
Normal file
47
Services/PetClassService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using pet_companion_api.Models;
|
||||
using pet_companion_api.Repositories;
|
||||
|
||||
namespace pet_companion_api.Services
|
||||
{
|
||||
public class PetClassService
|
||||
{
|
||||
private readonly PetClassRepository _petClassRepository;
|
||||
|
||||
public PetClassService(PetClassRepository petClassRepository)
|
||||
{
|
||||
_petClassRepository = petClassRepository;
|
||||
}
|
||||
|
||||
public IEnumerable<PetClassInfo> GetAllPetClasses()
|
||||
{
|
||||
return _petClassRepository.GetAllPetClassesInfo();
|
||||
}
|
||||
|
||||
public Resources CalculateGatheredResources(PetStats stats, int petLevel, PetActionGather action, DateTime actionSince)
|
||||
{
|
||||
var timeElapsed = (DateTime.UtcNow - actionSince).TotalHours;
|
||||
var resources = new Resources();
|
||||
|
||||
if (action == PetActionGather.IDLE)
|
||||
return resources;
|
||||
|
||||
var baseRate = timeElapsed * 0.5 + petLevel; // Base rate per hour
|
||||
resources.Junk = (int)(baseRate * 2);
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case PetActionGather.GATHERING_WISDOM:
|
||||
resources.Wisdom = (int)(baseRate * (stats.Intelligence * 2));
|
||||
break;
|
||||
case PetActionGather.GATHERING_GOLD:
|
||||
resources.Gold = (int)(baseRate * (stats.Charisma * 2));
|
||||
break;
|
||||
case PetActionGather.GATHERING_FOOD:
|
||||
resources.Food = (int)(baseRate * (stats.Strength * 1.5));
|
||||
break;
|
||||
}
|
||||
|
||||
return resources;
|
||||
}
|
||||
}
|
||||
}
|
@@ -6,10 +6,12 @@ namespace pet_companion_api.Services
|
||||
public class PetService
|
||||
{
|
||||
private readonly PetRepository petRepository;
|
||||
private readonly PetClassService _petClassService;
|
||||
|
||||
public PetService(PetRepository petRepository)
|
||||
public PetService(PetRepository petRepository, PetClassService petClassService)
|
||||
{
|
||||
this.petRepository = petRepository;
|
||||
_petClassService = petClassService;
|
||||
}
|
||||
|
||||
public IEnumerable<Pet> GetAllPets(Guid userId)
|
||||
@@ -29,7 +31,7 @@ namespace pet_companion_api.Services
|
||||
Stats = PetStats.BuildFromClass(petRequest.Class),
|
||||
Resources = new Resources(),
|
||||
ActionSince = DateTime.UtcNow,
|
||||
PetAction = PetAction.IDLE,
|
||||
PetAction = PetActionGather.IDLE,
|
||||
IsDead = false
|
||||
};
|
||||
|
||||
@@ -44,10 +46,50 @@ namespace pet_companion_api.Services
|
||||
throw new Exception("Pet not found");
|
||||
}
|
||||
|
||||
pet.PetAction = actionRequest.PetAction;
|
||||
if (actionRequest.Action.HasValue)
|
||||
{
|
||||
// not implemented
|
||||
}
|
||||
else if (actionRequest.PetActionGather.HasValue)
|
||||
{
|
||||
pet.PetAction = actionRequest.PetActionGather.Value;
|
||||
pet.ActionSince = DateTime.UtcNow;
|
||||
|
||||
return petRepository.UpdatePetAction(pet);
|
||||
}
|
||||
|
||||
return pet;
|
||||
}
|
||||
|
||||
public Resources GetGatheredResources(string petId, string userId)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
|
||||
if (pet == null)
|
||||
{
|
||||
throw new Exception("Pet not found");
|
||||
}
|
||||
|
||||
return _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetAction, pet.ActionSince);
|
||||
}
|
||||
|
||||
public Pet UpdatePetResources(string petId, string userId)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
{
|
||||
throw new Exception("Pet not found");
|
||||
}
|
||||
|
||||
var gatheredResources = _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetAction, pet.ActionSince);
|
||||
|
||||
pet.Resources.Wisdom += gatheredResources.Wisdom;
|
||||
pet.Resources.Gold += gatheredResources.Gold;
|
||||
pet.Resources.Food += gatheredResources.Food;
|
||||
pet.Resources.Junk += gatheredResources.Junk;
|
||||
pet.ActionSince = DateTime.UtcNow;
|
||||
|
||||
return petRepository.UpdatePetAction(pet);
|
||||
return petRepository.UpdatePetResources(pet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user