96 lines
3.0 KiB
C#
96 lines
3.0 KiB
C#
using pet_companion_api.Models;
|
|
using pet_companion_api.Repositories;
|
|
|
|
namespace pet_companion_api.Services
|
|
{
|
|
public class PetService
|
|
{
|
|
private readonly PetRepository petRepository;
|
|
private readonly PetClassService _petClassService;
|
|
|
|
public PetService(PetRepository petRepository, PetClassService petClassService)
|
|
{
|
|
this.petRepository = petRepository;
|
|
_petClassService = petClassService;
|
|
}
|
|
|
|
public IEnumerable<Pet> GetAllPets(Guid userId)
|
|
{
|
|
return petRepository.GetPetsByUserId(userId.ToString());
|
|
}
|
|
|
|
public Pet CreatePet(Guid userId, PetCreationRequest petRequest)
|
|
{
|
|
var pet = new Pet
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
UserId = userId.ToString(),
|
|
Name = petRequest.Name,
|
|
Class = petRequest.Class,
|
|
Level = 1,
|
|
Stats = PetStats.BuildFromClass(petRequest.Class),
|
|
Resources = new Resources(),
|
|
ActionSince = DateTime.UtcNow,
|
|
PetAction = PetActionGather.IDLE,
|
|
IsDead = false
|
|
};
|
|
|
|
return petRepository.CreatePet(pet);
|
|
}
|
|
|
|
public Pet UpdatePetAction(string petId, string userId, PetUpdateActionRequest actionRequest)
|
|
{
|
|
var pet = petRepository.GetPetById(petId, userId);
|
|
if (pet == null)
|
|
{
|
|
throw new Exception("Pet not found");
|
|
}
|
|
|
|
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.UpdatePetResources(pet);
|
|
}
|
|
}
|
|
}
|