198 lines
6.8 KiB
C#
198 lines
6.8 KiB
C#
using Microsoft.EntityFrameworkCore.Query;
|
|
using PetCompanion.Models;
|
|
using PetCompanion.Repositories;
|
|
|
|
namespace PetCompanion.Services
|
|
{
|
|
public class PetService
|
|
{
|
|
private readonly PetRepository petRepository;
|
|
private readonly PetClassService petClassService;
|
|
private readonly GameItemService gameItemService;
|
|
private readonly GameItemsRepository gameItemsRepository;
|
|
private readonly PetInventoryService petInventoryService;
|
|
private readonly PetActionService petActionService;
|
|
|
|
public PetService(
|
|
PetRepository petRepository,
|
|
PetClassService petClassService,
|
|
GameItemService gameItemService,
|
|
GameItemsRepository gameItemsRepository,
|
|
PetInventoryService petInventoryService,
|
|
PetActionService petActionService)
|
|
{
|
|
this.petRepository = petRepository;
|
|
this.petClassService = petClassService;
|
|
this.gameItemService = gameItemService;
|
|
this.gameItemsRepository = gameItemsRepository;
|
|
this.petInventoryService = petInventoryService;
|
|
this.petActionService = petActionService;
|
|
}
|
|
|
|
public IEnumerable<Pet> GetAllPets(string userId)
|
|
{
|
|
return petRepository.GetPetsByUserId(userId);
|
|
}
|
|
|
|
public Pet CreatePet(string userId, PetCreationRequest petRequest)
|
|
{
|
|
var petId = Guid.NewGuid().ToString();
|
|
var pet = new Pet
|
|
{
|
|
Id = petId,
|
|
UserId = userId,
|
|
Name = petRequest.Name,
|
|
Class = petRequest.Class,
|
|
Health = 100,
|
|
MaxHealth = 100,
|
|
Level = 1,
|
|
Stats = PetStats.BuildFromClass(petRequest.Class),
|
|
Resources = new Resources(),
|
|
GatherActionSince = DateTime.UtcNow,
|
|
PetGatherAction = PetActionGather.IDLE,
|
|
IsDead = false
|
|
};
|
|
|
|
var createdPet = petRepository.CreatePet(pet);
|
|
|
|
var inventory = petInventoryService.CreateInventory(petId);
|
|
|
|
return createdPet;
|
|
}
|
|
|
|
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);
|
|
pet.Health = Math.Min(pet.Health + 5, pet.MaxHealth);
|
|
break;
|
|
case PetBasicAction.SLEEP:
|
|
pet.IncrementIntelligence(1);
|
|
pet.IncrementStrength(1);
|
|
pet.Health = Math.Min(pet.Health + 15, pet.MaxHealth);
|
|
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 IEnumerable<ActionGathered> GetGatheredResources(string petId, string userId)
|
|
{
|
|
var pet = petRepository.GetPetById(petId, userId);
|
|
|
|
if (pet == null)
|
|
{
|
|
throw new Exception("Pet not found");
|
|
}
|
|
|
|
return petActionService.GetGatheredByPet(petId);
|
|
}
|
|
|
|
public Pet CollectPetGathered(string petId, string userId)
|
|
{
|
|
var pet = petRepository.GetPetById(petId, userId);
|
|
if (pet == null)
|
|
{
|
|
throw new Exception("Pet not found");
|
|
}
|
|
|
|
var petGathered = petActionService.GetGatheredByPet(petId);
|
|
|
|
if (petGathered == null)
|
|
{
|
|
throw new Exception("No resources to collect");
|
|
}
|
|
|
|
foreach (var resource in petGathered)
|
|
{
|
|
if (resource.Resource != null && resource.Resource != string.Empty)
|
|
{
|
|
switch (resource.Resource)
|
|
{
|
|
case "Junk":
|
|
pet.Resources.Junk += resource.Amount;
|
|
break;
|
|
case "Food":
|
|
pet.Resources.Food += resource.Amount;
|
|
break;
|
|
case "Gold":
|
|
pet.Resources.Gold += resource.Amount;
|
|
break;
|
|
case "Wisdom":
|
|
pet.Resources.Wisdom += resource.Amount;
|
|
break;
|
|
}
|
|
}
|
|
else if (resource.ItemId != null && resource.ItemId > 0)
|
|
{
|
|
petInventoryService.AddItemToPet(petId, userId, resource.ItemId ?? 1, resource.Amount);
|
|
}
|
|
}
|
|
|
|
var updatedPet = petRepository.UpdatePet(pet);
|
|
petActionService.DeleteAllActionGatheredByPetId(petId);
|
|
|
|
return updatedPet;
|
|
}
|
|
|
|
public Pet GetPet(string petId, string userId)
|
|
{
|
|
var pet = petRepository.GetPetById(petId, userId);
|
|
if (pet == null)
|
|
throw new Exception("Pet not found");
|
|
return pet;
|
|
}
|
|
}
|
|
}
|