139 lines
4.7 KiB
C#
139 lines
4.7 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 petId = Guid.NewGuid().ToString();
|
|
var pet = new Pet
|
|
{
|
|
Id = petId,
|
|
UserId = userId.ToString(),
|
|
Name = petRequest.Name,
|
|
Class = petRequest.Class,
|
|
Level = 1,
|
|
Stats = PetStats.BuildFromClass(petRequest.Class),
|
|
Resources = new Resources(),
|
|
GatherActionSince = DateTime.UtcNow,
|
|
PetGatherAction = 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.BasicAction.HasValue)
|
|
{
|
|
var currentTime = DateTime.UtcNow;
|
|
if (pet.PetBasicAction != PetBasicAction.UNKNOWN &&
|
|
currentTime < pet.BasicActionCooldown.ToUniversalTime().AddSeconds(10))
|
|
{
|
|
throw new Exception("Pet is still on cooldown");
|
|
}
|
|
|
|
pet.BasicActionCooldown = DateTime.UtcNow.AddMinutes(GetCooldownForBasicAction(actionRequest.BasicAction.Value));
|
|
pet.PetBasicAction = actionRequest.BasicAction.Value;
|
|
|
|
switch (actionRequest.BasicAction.Value)
|
|
{
|
|
case PetBasicAction.FEED:
|
|
pet.Resources.Food -= 1;
|
|
pet.IncrementStrength(1);
|
|
break;
|
|
case PetBasicAction.SLEEP:
|
|
pet.IncrementIntelligence(1);
|
|
pet.IncrementStrength(1);
|
|
break;
|
|
case PetBasicAction.PLAY:
|
|
pet.Resources.Junk -= 1;
|
|
pet.IncrementCharisma(1);
|
|
break;
|
|
}
|
|
|
|
return petRepository.UpdatePetBasicAction(pet);
|
|
}
|
|
else if (actionRequest.GatherAction.HasValue)
|
|
{
|
|
pet.PetGatherAction = actionRequest.GatherAction.Value;
|
|
pet.GatherActionSince = DateTime.UtcNow;
|
|
|
|
return petRepository.UpdatePetGatherAction(pet);
|
|
}
|
|
|
|
return pet;
|
|
}
|
|
|
|
// returns in minutes
|
|
private int GetCooldownForBasicAction(PetBasicAction value)
|
|
{
|
|
switch (value)
|
|
{
|
|
case PetBasicAction.FEED:
|
|
return 5;
|
|
case PetBasicAction.PLAY:
|
|
return 10;
|
|
case PetBasicAction.SLEEP:
|
|
return 15;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
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.PetGatherAction, pet.GatherActionSince);
|
|
}
|
|
|
|
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.PetGatherAction, pet.GatherActionSince);
|
|
|
|
pet.Resources.Wisdom += gatheredResources.Wisdom;
|
|
pet.Resources.Gold += gatheredResources.Gold;
|
|
pet.Resources.Food += gatheredResources.Food;
|
|
pet.Resources.Junk += gatheredResources.Junk;
|
|
pet.GatherActionSince = DateTime.UtcNow;
|
|
|
|
return petRepository.UpdatePetResources(pet);
|
|
}
|
|
}
|
|
}
|