From 215d4ecb72b52d30a344345cfae24176bd12a6cd Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Sun, 9 Feb 2025 21:22:52 -0300 Subject: [PATCH] Add action gathering functionality: implement ActionGathered model and repository, update Pet model and services, and enhance GameItemsRepository with item retrieval methods. --- Controllers/PetController.cs | 2 +- Data/ApplicationDbContext.cs | 12 ++ ... 20250209234852_InitialCreate.Designer.cs} | 49 +++++- ...ate.cs => 20250209234852_InitialCreate.cs} | 40 +++++ .../ApplicationDbContextModelSnapshot.cs | 47 ++++++ Models/ActionGathered.cs | 22 +++ Models/Pet.cs | 2 + Models/PetActionGather.cs | 2 + Program.cs | 5 + Repositories/ActionGatheredRepository.cs | 47 ++++++ Repositories/GameItemsRepository.cs | 5 + Repositories/PetRepository.cs | 13 ++ Services/GameItemService.cs | 8 + Services/PetActionBackgroundService.cs | 26 +++ Services/PetActionService.cs | 153 ++++++++++++++++++ Services/PetClassService.cs | 27 ---- Services/PetInventoryService.cs | 6 + Services/PetService.cs | 56 +++++-- 18 files changed, 481 insertions(+), 41 deletions(-) rename Migrations/{20250209011029_InitialCreate.Designer.cs => 20250209234852_InitialCreate.Designer.cs} (94%) rename Migrations/{20250209011029_InitialCreate.cs => 20250209234852_InitialCreate.cs} (90%) create mode 100644 Models/ActionGathered.cs create mode 100644 Repositories/ActionGatheredRepository.cs create mode 100644 Services/PetActionBackgroundService.cs create mode 100644 Services/PetActionService.cs diff --git a/Controllers/PetController.cs b/Controllers/PetController.cs index 1c2aec9..e541fb8 100644 --- a/Controllers/PetController.cs +++ b/Controllers/PetController.cs @@ -69,7 +69,7 @@ namespace PetCompanion.Controllers { try { - var updatedPet = petService.UpdatePetResources(petId, userId.ToString()); + var updatedPet = petService.CollectPetGathered(petId, userId.ToString()); return Ok(updatedPet); } catch (Exception ex) diff --git a/Data/ApplicationDbContext.cs b/Data/ApplicationDbContext.cs index d0527c0..5c85e52 100644 --- a/Data/ApplicationDbContext.cs +++ b/Data/ApplicationDbContext.cs @@ -18,6 +18,7 @@ namespace PetCompanion.Data public DbSet GameItems { get; set; } public DbSet Inventories { get; set; } public DbSet EquippedItems { get; set; } + public DbSet ActionGathered { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -66,6 +67,17 @@ namespace PetCompanion.Data .WithMany() .HasForeignKey(e => e.GameItemId); + modelBuilder.Entity() + .HasOne(ag => ag.Pet) + .WithMany(p => p.ActionGathered) + .HasForeignKey(ag => ag.PetId) + .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasOne(ag => ag.GameItem) + .WithMany() + .HasForeignKey(ag => ag.ItemId); + // Seed initial data var skills = SkillsData.GetInitialSkillsWithoutRelations(); var requirements = SkillsData.GetInitialSkillRequirements(); diff --git a/Migrations/20250209011029_InitialCreate.Designer.cs b/Migrations/20250209234852_InitialCreate.Designer.cs similarity index 94% rename from Migrations/20250209011029_InitialCreate.Designer.cs rename to Migrations/20250209234852_InitialCreate.Designer.cs index 21efb46..9213392 100644 --- a/Migrations/20250209011029_InitialCreate.Designer.cs +++ b/Migrations/20250209234852_InitialCreate.Designer.cs @@ -11,7 +11,7 @@ using PetCompanion.Data; namespace PetCompanion.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20250209011029_InitialCreate")] + [Migration("20250209234852_InitialCreate")] partial class InitialCreate { /// @@ -20,6 +20,34 @@ namespace PetCompanion.Migrations #pragma warning disable 612, 618 modelBuilder.HasAnnotation("ProductVersion", "9.0.1"); + modelBuilder.Entity("PetCompanion.Models.ActionGathered", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Amount") + .HasColumnType("INTEGER"); + + b.Property("ItemId") + .HasColumnType("INTEGER"); + + b.Property("PetId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Resource") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ItemId"); + + b.HasIndex("PetId"); + + b.ToTable("ActionGathered"); + }); + modelBuilder.Entity("PetCompanion.Models.EquippedItem", b => { b.Property("Id") @@ -668,6 +696,23 @@ namespace PetCompanion.Migrations }); }); + modelBuilder.Entity("PetCompanion.Models.ActionGathered", b => + { + b.HasOne("PetCompanion.Models.GameItem", "GameItem") + .WithMany() + .HasForeignKey("ItemId"); + + b.HasOne("PetCompanion.Models.Pet", "Pet") + .WithMany("ActionGathered") + .HasForeignKey("PetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("GameItem"); + + b.Navigation("Pet"); + }); + modelBuilder.Entity("PetCompanion.Models.EquippedItem", b => { b.HasOne("PetCompanion.Models.GameItem", "GameItem") @@ -759,6 +804,8 @@ namespace PetCompanion.Migrations modelBuilder.Entity("PetCompanion.Models.Pet", b => { + b.Navigation("ActionGathered"); + b.Navigation("EquippedItemsList"); b.Navigation("Inventory") diff --git a/Migrations/20250209011029_InitialCreate.cs b/Migrations/20250209234852_InitialCreate.cs similarity index 90% rename from Migrations/20250209011029_InitialCreate.cs rename to Migrations/20250209234852_InitialCreate.cs index f392471..8a72f74 100644 --- a/Migrations/20250209011029_InitialCreate.cs +++ b/Migrations/20250209234852_InitialCreate.cs @@ -71,6 +71,33 @@ namespace PetCompanion.Migrations table.PrimaryKey("PK_Skills", x => x.Id); }); + migrationBuilder.CreateTable( + name: "ActionGathered", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + PetId = table.Column(type: "TEXT", nullable: false), + Resource = table.Column(type: "TEXT", nullable: true), + ItemId = table.Column(type: "INTEGER", nullable: true), + Amount = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ActionGathered", x => x.Id); + table.ForeignKey( + name: "FK_ActionGathered_GameItems_ItemId", + column: x => x.ItemId, + principalTable: "GameItems", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_ActionGathered_Pets_PetId", + column: x => x.PetId, + principalTable: "Pets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "EquippedItems", columns: table => new @@ -301,6 +328,16 @@ namespace PetCompanion.Migrations { 14, 300, "Food", 8 } }); + migrationBuilder.CreateIndex( + name: "IX_ActionGathered_ItemId", + table: "ActionGathered", + column: "ItemId"); + + migrationBuilder.CreateIndex( + name: "IX_ActionGathered_PetId", + table: "ActionGathered", + column: "PetId"); + migrationBuilder.CreateIndex( name: "IX_EquippedItems_GameItemId", table: "EquippedItems", @@ -335,6 +372,9 @@ namespace PetCompanion.Migrations /// protected override void Down(MigrationBuilder migrationBuilder) { + migrationBuilder.DropTable( + name: "ActionGathered"); + migrationBuilder.DropTable( name: "EquippedItems"); diff --git a/Migrations/ApplicationDbContextModelSnapshot.cs b/Migrations/ApplicationDbContextModelSnapshot.cs index c08dfe6..613d189 100644 --- a/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Migrations/ApplicationDbContextModelSnapshot.cs @@ -17,6 +17,34 @@ namespace PetCompanion.Migrations #pragma warning disable 612, 618 modelBuilder.HasAnnotation("ProductVersion", "9.0.1"); + modelBuilder.Entity("PetCompanion.Models.ActionGathered", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Amount") + .HasColumnType("INTEGER"); + + b.Property("ItemId") + .HasColumnType("INTEGER"); + + b.Property("PetId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Resource") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ItemId"); + + b.HasIndex("PetId"); + + b.ToTable("ActionGathered"); + }); + modelBuilder.Entity("PetCompanion.Models.EquippedItem", b => { b.Property("Id") @@ -665,6 +693,23 @@ namespace PetCompanion.Migrations }); }); + modelBuilder.Entity("PetCompanion.Models.ActionGathered", b => + { + b.HasOne("PetCompanion.Models.GameItem", "GameItem") + .WithMany() + .HasForeignKey("ItemId"); + + b.HasOne("PetCompanion.Models.Pet", "Pet") + .WithMany("ActionGathered") + .HasForeignKey("PetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("GameItem"); + + b.Navigation("Pet"); + }); + modelBuilder.Entity("PetCompanion.Models.EquippedItem", b => { b.HasOne("PetCompanion.Models.GameItem", "GameItem") @@ -756,6 +801,8 @@ namespace PetCompanion.Migrations modelBuilder.Entity("PetCompanion.Models.Pet", b => { + b.Navigation("ActionGathered"); + b.Navigation("EquippedItemsList"); b.Navigation("Inventory") diff --git a/Models/ActionGathered.cs b/Models/ActionGathered.cs new file mode 100644 index 0000000..d7630df --- /dev/null +++ b/Models/ActionGathered.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; + +namespace PetCompanion.Models +{ + public class ActionGathered + { + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [Key] + public int Id { get; set; } + [ForeignKey("Pet")] + public string PetId { get; set; } + public string? Resource { get; set; } + [ForeignKey("GameItem")] + public int? ItemId { get; set; } + public int Amount { get; set; } + [JsonIgnore] + public virtual Pet Pet { get; set; } + public virtual GameItem? GameItem { get; set; } + } +} \ No newline at end of file diff --git a/Models/Pet.cs b/Models/Pet.cs index e1e2417..baf20dd 100644 --- a/Models/Pet.cs +++ b/Models/Pet.cs @@ -27,6 +27,8 @@ namespace PetCompanion.Models public virtual Inventory Inventory { get; set; } public virtual ICollection EquippedItemsList { get; set; } = new List(); + public virtual ICollection ActionGathered { get; set; } = new List(); + [NotMapped] public Dictionary EquippedItems { diff --git a/Models/PetActionGather.cs b/Models/PetActionGather.cs index 4391639..626347a 100644 --- a/Models/PetActionGather.cs +++ b/Models/PetActionGather.cs @@ -14,5 +14,7 @@ GATHERING_WISDOM, GATHERING_GOLD, GATHERING_FOOD, + EXPLORE, + BATTLE } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 85b2cf7..1f22f7b 100644 --- a/Program.cs +++ b/Program.cs @@ -31,6 +31,7 @@ namespace PetCompanion builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -40,6 +41,10 @@ namespace PetCompanion builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); + + // Add the background service + builder.Services.AddHostedService(); // Add CORS policy builder.Services.AddCors(options => diff --git a/Repositories/ActionGatheredRepository.cs b/Repositories/ActionGatheredRepository.cs new file mode 100644 index 0000000..63e90ef --- /dev/null +++ b/Repositories/ActionGatheredRepository.cs @@ -0,0 +1,47 @@ +using PetCompanion.Data; +using PetCompanion.Models; +using Microsoft.EntityFrameworkCore; + +namespace PetCompanion.Repositories +{ + public class ActionGatheredRepository + { + private readonly ApplicationDbContext _context; + + public ActionGatheredRepository(ApplicationDbContext context) + { + _context = context; + } + + public IEnumerable GetAllActionGatheredByPetId(string petId) + { + return _context.ActionGathered + .Where(ag => ag.PetId == petId) + .Include(ag => ag.GameItem) + .ToList(); + } + + public ActionGathered CreateActionGathered(ActionGathered actionGathered) + { + var entry = _context.ActionGathered.Add(actionGathered); + _context.SaveChanges(); + return entry.Entity; + } + + public ActionGathered UpdateActionGathered(ActionGathered actionGathered) + { + var entry = _context.ActionGathered.Update(actionGathered); + _context.SaveChanges(); + return entry.Entity; + } + + public void DeleteAllActionGatheredByPetId(string petId) + { + var actionsToDelete = _context.ActionGathered + .Where(ag => ag.PetId == petId); + + _context.ActionGathered.RemoveRange(actionsToDelete); + _context.SaveChanges(); + } + } +} diff --git a/Repositories/GameItemsRepository.cs b/Repositories/GameItemsRepository.cs index d727be7..e2db7c4 100644 --- a/Repositories/GameItemsRepository.cs +++ b/Repositories/GameItemsRepository.cs @@ -17,6 +17,11 @@ namespace PetCompanion.Repositories return _context.GameItems.Find(id); } + public IEnumerable GetAll() + { + return _context.GameItems; + } + public void Add(GameItem item) { _context.GameItems.Add(item); diff --git a/Repositories/PetRepository.cs b/Repositories/PetRepository.cs index ea18e06..a5d266b 100644 --- a/Repositories/PetRepository.cs +++ b/Repositories/PetRepository.cs @@ -21,6 +21,18 @@ namespace PetCompanion.Repositories .Include(p => p.Resources) .Include(p => p.Inventory) .Include(p => p.EquippedItemsList) + .Include(p => p.ActionGathered) + .ToList(); + } + + public IEnumerable GetPetWithActions() + { + return context.Pets + .Where(predicate => predicate.PetGatherAction != PetActionGather.IDLE) + .Include(p => p.Stats) + .Include(p => p.Resources) + .Include(p => p.Inventory) + .Include(p => p.EquippedItemsList) .ToList(); } @@ -32,6 +44,7 @@ namespace PetCompanion.Repositories .Include(p => p.Resources) .Include(p => p.Inventory) .Include(p => p.EquippedItemsList) + .Include(p => p.ActionGathered) .FirstOrDefault(); } diff --git a/Services/GameItemService.cs b/Services/GameItemService.cs index c564d8f..e2defb3 100644 --- a/Services/GameItemService.cs +++ b/Services/GameItemService.cs @@ -35,6 +35,14 @@ namespace PetCompanion.Services } } + public GameItem GetRandomItem() + { + var items = gameItemsRepository.GetAll(); + var random = new Random(); + var index = random.Next(items.Count()); + return items.ElementAt(index); + } + public void ApplyItemEffect(Pet pet, GameItem item) { var effects = item.Effect.Split(';'); diff --git a/Services/PetActionBackgroundService.cs b/Services/PetActionBackgroundService.cs new file mode 100644 index 0000000..b64540d --- /dev/null +++ b/Services/PetActionBackgroundService.cs @@ -0,0 +1,26 @@ +namespace PetCompanion.Services +{ + public class PetActionBackgroundService : BackgroundService + { + private readonly IServiceProvider _serviceProvider; + + public PetActionBackgroundService(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + using (var scope = _serviceProvider.CreateScope()) + { + var petActionService = scope.ServiceProvider.GetRequiredService(); + petActionService.LoopPetActions(); + } + + await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); + } + } + } +} diff --git a/Services/PetActionService.cs b/Services/PetActionService.cs new file mode 100644 index 0000000..abd5ff5 --- /dev/null +++ b/Services/PetActionService.cs @@ -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 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 * 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; + } + } +} diff --git a/Services/PetClassService.cs b/Services/PetClassService.cs index f7c4bfd..394d51f 100644 --- a/Services/PetClassService.cs +++ b/Services/PetClassService.cs @@ -16,32 +16,5 @@ namespace PetCompanion.Services { return _petClassRepository.GetAllPetClassesInfo(); } - - public Resources CalculateGatheredResources(PetStats stats, int petLevel, PetActionGather action, DateTime actionSince) - { - var timeElapsed = (DateTime.UtcNow - actionSince).TotalHours; - var resources = new Resources(); - - if (action == PetActionGather.IDLE) - return resources; - - var baseRate = timeElapsed * 0.5 + petLevel; // Base rate per hour - resources.Junk = (int)(baseRate * 2); - - switch (action) - { - case PetActionGather.GATHERING_WISDOM: - resources.Wisdom = (int)(baseRate * (stats.Intelligence * 2)); - break; - case PetActionGather.GATHERING_GOLD: - resources.Gold = (int)(baseRate * (stats.Charisma * 2)); - break; - case PetActionGather.GATHERING_FOOD: - resources.Food = (int)(baseRate * (stats.Strength * 1.5)); - break; - } - - return resources; - } } } diff --git a/Services/PetInventoryService.cs b/Services/PetInventoryService.cs index 0407f22..e3783a4 100644 --- a/Services/PetInventoryService.cs +++ b/Services/PetInventoryService.cs @@ -146,6 +146,12 @@ namespace PetCompanion.Services if (gameItem == null) throw new Exception("Item not found"); + if (pet == null) + throw new Exception("Pet not found"); + + if (pet.Inventory.Items.Count + quantity > pet.Inventory.Capacity) + throw new Exception("Not enough space in inventory"); + for (int i = 0; i < quantity; i++) { pet.Inventory.Items.Add(itemId); diff --git a/Services/PetService.cs b/Services/PetService.cs index 3fd70e8..5991296 100644 --- a/Services/PetService.cs +++ b/Services/PetService.cs @@ -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 GetAllPets(Guid userId) @@ -123,7 +127,7 @@ namespace PetCompanion.Services } } - public Resources GetGatheredResources(string petId, string userId) + public IEnumerable 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)