Add action gathering functionality: implement ActionGathered model and repository, update Pet model and services, and enhance GameItemsRepository with item retrieval methods.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using PetCompanion.Models;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using PetCompanion.Models;
|
||||
using PetCompanion.Repositories;
|
||||
|
||||
namespace PetCompanion.Services
|
||||
@@ -10,19 +11,22 @@ namespace PetCompanion.Services
|
||||
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)
|
||||
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(Guid userId)
|
||||
@@ -123,7 +127,7 @@ namespace PetCompanion.Services
|
||||
}
|
||||
}
|
||||
|
||||
public Resources GetGatheredResources(string petId, string userId)
|
||||
public IEnumerable<ActionGathered> GetGatheredResources(string petId, string userId)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
|
||||
@@ -132,10 +136,10 @@ namespace PetCompanion.Services
|
||||
throw new Exception("Pet not found");
|
||||
}
|
||||
|
||||
return petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
|
||||
return petActionService.GetGatheredByPet(petId);
|
||||
}
|
||||
|
||||
public Pet UpdatePetResources(string petId, string userId)
|
||||
public Pet CollectPetGathered(string petId, string userId)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
@@ -143,15 +147,43 @@ namespace PetCompanion.Services
|
||||
throw new Exception("Pet not found");
|
||||
}
|
||||
|
||||
var gatheredResources = petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
|
||||
var petGathered = petActionService.GetGatheredByPet(petId);
|
||||
|
||||
pet.Resources.Wisdom += gatheredResources.Wisdom;
|
||||
pet.Resources.Gold += gatheredResources.Gold;
|
||||
pet.Resources.Food += gatheredResources.Food;
|
||||
pet.Resources.Junk += gatheredResources.Junk;
|
||||
pet.GatherActionSince = DateTime.UtcNow;
|
||||
if (petGathered == null)
|
||||
{
|
||||
throw new Exception("No resources to collect");
|
||||
}
|
||||
|
||||
return petRepository.UpdatePetResources(pet);
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user