using PetCompanion.Models; using PetCompanion.Repositories; namespace PetCompanion.Services { public class PetActionService { private readonly ActionGatheredRepository actionGatheredRepository; private readonly PetRepository petRepository; private readonly GameItemService gameItemService; public PetActionService(ActionGatheredRepository actionGatheredRepository, GameItemService gameItemService, PetRepository petRepository) { this.actionGatheredRepository = actionGatheredRepository; this.gameItemService = gameItemService; this.petRepository = petRepository; } public IEnumerable GetGatheredByPet(string petId) { return actionGatheredRepository.GetAllActionGatheredByPetId(petId); } public void DeleteAllActionGatheredByPetId(string petId) { actionGatheredRepository.DeleteAllActionGatheredByPetId(petId); } public void LoopPetActions() { var pets = petRepository.GetPetWithActions(); foreach (var pet in pets) { var gatheredResources = CalculateGatheredResources(pet); if (gatheredResources != null) { var gatheredResourcesDb = actionGatheredRepository.GetAllActionGatheredByPetId(pet.Id); foreach (var resource in gatheredResources) { if (gatheredResourcesDb.Any(ag => ag.Resource == resource.Resource)) { var existingResource = gatheredResourcesDb.First(ag => ag.Resource == resource.Resource); existingResource.Amount += resource.Amount; actionGatheredRepository.UpdateActionGathered(existingResource); } else { actionGatheredRepository.CreateActionGathered(resource); } } } } } public ICollection? CalculateGatheredResources(Pet pet) { if (pet.PetGatherAction == PetActionGather.IDLE) return null; var timeElapsed = (DateTime.UtcNow - pet.GatherActionSince).TotalMinutes; var gathered = new List(); var baseRate = (timeElapsed + pet.Level) / 10; // Base rate per hour gathered.Add(new ActionGathered { Resource = "Junk", Amount = (int)(baseRate * 0.2) }); var random = new Random(); switch (pet.PetGatherAction) { case PetActionGather.GATHERING_WISDOM: gathered.Add(new ActionGathered { Resource = "Wisdom", Amount = (int)(baseRate * (pet.Stats.Intelligence * 0.5)) }); break; case PetActionGather.GATHERING_GOLD: gathered.Add(new ActionGathered { Resource = "Gold", Amount = (int)(baseRate * (pet.Stats.Charisma * 0.5)) }); break; case PetActionGather.GATHERING_FOOD: gathered.Add(new ActionGathered { Resource = "Food", Amount = (int)(baseRate * (pet.Stats.Strength * 0.5)) }); break; case PetActionGather.EXPLORE: gathered.Add(new ActionGathered { Resource = "Wisdom", Amount = (int)(baseRate * (pet.Stats.Strength * 0.25)) }); if (random.Next(0, 100) < 5) { pet.Health -= 5; } if (random.Next(0, 100) < 5) { gathered.Add(new ActionGathered { ItemId = gameItemService.GetRandomItem().Id, Amount = 1 }); } break; case PetActionGather.BATTLE: gathered.Add(new ActionGathered { Resource = "Gold", Amount = (int)(baseRate * (pet.Stats.Strength * 0.25)) }); if (random.Next(0, 100) < 10) { pet.Health -= random.Next(5, 10); } if (random.Next(0, 100) < 10) { gathered.Add(new ActionGathered { ItemId = gameItemService.GetRandomItem().Id, Amount = 1 }); } break; } for (int i = 0; i < gathered.Count; i++) { gathered[i].PetId = pet.Id; } return gathered; } } }