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:
153
Services/PetActionService.cs
Normal file
153
Services/PetActionService.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
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<ActionGathered> 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<ActionGathered>? CalculateGatheredResources(Pet pet)
|
||||
{
|
||||
if (pet.PetGatherAction == PetActionGather.IDLE)
|
||||
return null;
|
||||
|
||||
var timeElapsed = (DateTime.UtcNow - pet.GatherActionSince).TotalMinutes;
|
||||
|
||||
var gathered = new List<ActionGathered>();
|
||||
|
||||
var baseRate = timeElapsed + pet.Level * 10; // Base rate per hour
|
||||
gathered.Add(new ActionGathered
|
||||
{
|
||||
Resource = "Junk",
|
||||
Amount = (int)(baseRate * 2)
|
||||
});
|
||||
|
||||
var random = new Random();
|
||||
|
||||
switch (pet.PetGatherAction)
|
||||
{
|
||||
case PetActionGather.GATHERING_WISDOM:
|
||||
gathered.Add(new ActionGathered
|
||||
{
|
||||
Resource = "Wisdom",
|
||||
Amount = (int)(baseRate * (pet.Stats.Intelligence * 2))
|
||||
});
|
||||
break;
|
||||
case PetActionGather.GATHERING_GOLD:
|
||||
gathered.Add(new ActionGathered
|
||||
{
|
||||
Resource = "Gold",
|
||||
Amount = (int)(baseRate * (pet.Stats.Charisma * 2))
|
||||
});
|
||||
break;
|
||||
case PetActionGather.GATHERING_FOOD:
|
||||
gathered.Add(new ActionGathered
|
||||
{
|
||||
Resource = "Food",
|
||||
Amount = (int)(baseRate * (pet.Stats.Strength * 1.5))
|
||||
});
|
||||
break;
|
||||
case PetActionGather.EXPLORE:
|
||||
gathered.Add(new ActionGathered
|
||||
{
|
||||
Resource = "Wisdom",
|
||||
Amount = (int)(baseRate * (pet.Stats.Strength * 3))
|
||||
});
|
||||
|
||||
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 * 3))
|
||||
});
|
||||
|
||||
if (random.Next(0, 100) < 10)
|
||||
{
|
||||
pet.Health -= random.Next(5, 10);
|
||||
}
|
||||
|
||||
if (random.Next(0, 100) < 500)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user