Refactor pet action management: rename PetAction to PetBasicAction, update related properties and methods, and enhance pet stats with maximum values.

This commit is contained in:
2025-02-01 23:08:25 -03:00
parent d6d3dc9f44
commit df62710b9a
14 changed files with 173 additions and 239 deletions

View File

@@ -21,17 +21,18 @@ namespace pet_companion_api.Services
public Pet CreatePet(Guid userId, PetCreationRequest petRequest)
{
var petId = Guid.NewGuid().ToString();
var pet = new Pet
{
Id = Guid.NewGuid().ToString(),
Id = petId,
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,
GatherActionSince = DateTime.UtcNow,
PetGatherAction = PetActionGather.IDLE,
IsDead = false
};
@@ -46,21 +47,63 @@ namespace pet_companion_api.Services
throw new Exception("Pet not found");
}
if (actionRequest.Action.HasValue)
if (actionRequest.BasicAction.HasValue)
{
// not implemented
}
else if (actionRequest.PetActionGather.HasValue)
{
pet.PetAction = actionRequest.PetActionGather.Value;
pet.ActionSince = DateTime.UtcNow;
var currentTime = DateTime.UtcNow;
if (pet.PetBasicAction != PetBasicAction.UNKNOWN &&
currentTime < pet.BasicActionCooldown.ToUniversalTime().AddSeconds(10))
{
throw new Exception("Pet is still on cooldown");
}
return petRepository.UpdatePetAction(pet);
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);
@@ -70,7 +113,7 @@ namespace pet_companion_api.Services
throw new Exception("Pet not found");
}
return _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetAction, pet.ActionSince);
return _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
}
public Pet UpdatePetResources(string petId, string userId)
@@ -81,13 +124,13 @@ namespace pet_companion_api.Services
throw new Exception("Pet not found");
}
var gatheredResources = _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetAction, pet.ActionSince);
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.ActionSince = DateTime.UtcNow;
pet.GatherActionSince = DateTime.UtcNow;
return petRepository.UpdatePetResources(pet);
}