inventory working :D
This commit is contained in:
parent
7a2c3d2f67
commit
5289910270
@ -8,24 +8,24 @@ namespace PetCompanion.Controllers
|
||||
[Route("api/v1/[controller]")]
|
||||
public class InventoryController : ControllerBase
|
||||
{
|
||||
private readonly PetInventoryService _inventoryService;
|
||||
private readonly ILogger<InventoryController> _logger;
|
||||
private readonly PetInventoryService inventoryService;
|
||||
private readonly ILogger<InventoryController> logger;
|
||||
private readonly Guid userId = Guid.Parse("f5f4b3b3-3b7b-4b7b-8b7b-7b7b7b7b7b7b");
|
||||
|
||||
public InventoryController(
|
||||
ILogger<InventoryController> logger,
|
||||
PetInventoryService inventoryService)
|
||||
{
|
||||
_logger = logger;
|
||||
_inventoryService = inventoryService;
|
||||
this.logger = logger;
|
||||
this.inventoryService = inventoryService;
|
||||
}
|
||||
|
||||
[HttpGet("{petId}")]
|
||||
public async Task<IActionResult> GetInventory(string petId)
|
||||
public IActionResult GetInventory(string petId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var inventory = await _inventoryService.GetInventory(petId, userId.ToString());
|
||||
var inventory = inventoryService.GetInventory(petId, userId.ToString());
|
||||
return Ok(inventory);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -35,11 +35,11 @@ namespace PetCompanion.Controllers
|
||||
}
|
||||
|
||||
[HttpPut("{petId}/{itemId}/use")]
|
||||
public async Task<IActionResult> UseItem(string petId, int itemId)
|
||||
public IActionResult UseItem(string petId, int itemId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var updatedPet = await _inventoryService.UseItem(petId, userId.ToString(), itemId);
|
||||
var updatedPet = inventoryService.UseItem(petId, userId.ToString(), itemId);
|
||||
return Ok(updatedPet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -53,7 +53,7 @@ namespace PetCompanion.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
var updatedPet = _inventoryService.EquipItem(petId, userId.ToString(), itemId);
|
||||
var updatedPet = inventoryService.EquipItem(petId, userId.ToString(), itemId);
|
||||
return Ok(updatedPet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -67,7 +67,7 @@ namespace PetCompanion.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
var updatedPet = _inventoryService.UnequipItem(petId, userId.ToString(), equipTarget);
|
||||
var updatedPet = inventoryService.UnequipItem(petId, userId.ToString(), equipTarget);
|
||||
return Ok(updatedPet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -81,7 +81,7 @@ namespace PetCompanion.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
var updatedPet = _inventoryService.DropItem(petId, userId.ToString(), itemId);
|
||||
var updatedPet = inventoryService.DropItem(petId, userId.ToString(), itemId);
|
||||
return Ok(updatedPet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -95,7 +95,7 @@ namespace PetCompanion.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
var updatedPet = _inventoryService.AddItemToPet(petId, userId.ToString(), request.ItemId, request.Quantity);
|
||||
var updatedPet = inventoryService.AddItemToPet(petId, userId.ToString(), request.ItemId, request.Quantity);
|
||||
return Ok(updatedPet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -23,18 +23,18 @@ namespace PetCompanion.Controllers
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAvailableSkills()
|
||||
public IActionResult GetAvailableSkills()
|
||||
{
|
||||
var skills = await _skillService.GetAvailableSkills();
|
||||
var skills = _skillService.GetAvailableSkills();
|
||||
return Ok(skills);
|
||||
}
|
||||
|
||||
[HttpGet("{petId}/skills")]
|
||||
public async Task<IActionResult> GetPetSkills(string petId)
|
||||
public IActionResult GetPetSkills(string petId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var skills = await _petSkillService.GetPetSkills(petId, userId.ToString());
|
||||
var skills = _petSkillService.GetPetSkills(petId, userId.ToString());
|
||||
return Ok(skills);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -44,11 +44,11 @@ namespace PetCompanion.Controllers
|
||||
}
|
||||
|
||||
[HttpPost("{petId}/allocate/{skillId}")]
|
||||
public async Task<IActionResult> AllocateSkillPoint(string petId, int skillId)
|
||||
public IActionResult AllocateSkillPoint(string petId, int skillId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _petSkillService.AllocateSkillPoint(petId, userId.ToString(), skillId);
|
||||
var result = _petSkillService.AllocateSkillPoint(petId, userId.ToString(), skillId);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -17,7 +17,6 @@ namespace PetCompanion.Data
|
||||
public DbSet<PetSkill> PetSkills { get; set; }
|
||||
public DbSet<GameItem> GameItems { get; set; }
|
||||
public DbSet<Inventory> Inventories { get; set; }
|
||||
public DbSet<InventoryItem> InventoryItems { get; set; }
|
||||
public DbSet<EquippedItem> EquippedItems { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
@ -52,14 +51,6 @@ namespace PetCompanion.Data
|
||||
.WithMany(s => s.Effects)
|
||||
.HasForeignKey(se => se.SkillId);
|
||||
|
||||
modelBuilder.Entity<InventoryItem>()
|
||||
.HasOne<Inventory>()
|
||||
.WithMany(i => i.Items)
|
||||
.HasForeignKey(ii => ii.InventoryId);
|
||||
|
||||
modelBuilder.Entity<InventoryItem>()
|
||||
.HasOne(ii => ii.GameItem);
|
||||
|
||||
modelBuilder.Entity<EquippedItem>()
|
||||
.HasOne(e => e.Pet)
|
||||
.WithMany(p => p.EquippedItemsList)
|
||||
|
@ -11,7 +11,7 @@ using PetCompanion.Data;
|
||||
namespace PetCompanion.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20250202234056_InitialCreate")]
|
||||
[Migration("20250204213845_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@ -88,34 +88,13 @@ namespace PetCompanion.Migrations
|
||||
b.Property<int>("Capacity")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("PetId");
|
||||
|
||||
b.ToTable("Inventories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.InventoryItem", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("GameItemId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("InventoryId")
|
||||
b.PrimitiveCollection<string>("Items")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Quantity")
|
||||
.HasColumnType("INTEGER");
|
||||
b.HasKey("PetId");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("GameItemId");
|
||||
|
||||
b.HasIndex("InventoryId");
|
||||
|
||||
b.ToTable("InventoryItems");
|
||||
b.ToTable("Inventories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.Pet", b =>
|
||||
@ -579,23 +558,6 @@ namespace PetCompanion.Migrations
|
||||
b.Navigation("Pet");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.InventoryItem", b =>
|
||||
{
|
||||
b.HasOne("PetCompanion.Models.GameItem", "GameItem")
|
||||
.WithMany()
|
||||
.HasForeignKey("GameItemId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("PetCompanion.Models.Inventory", null)
|
||||
.WithMany("Items")
|
||||
.HasForeignKey("InventoryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("GameItem");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.PetSkill", b =>
|
||||
{
|
||||
b.HasOne("PetCompanion.Models.Pet", "Pet")
|
||||
@ -644,11 +606,6 @@ namespace PetCompanion.Migrations
|
||||
b.Navigation("Skill");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.Inventory", b =>
|
||||
{
|
||||
b.Navigation("Items");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.Pet", b =>
|
||||
{
|
||||
b.Navigation("EquippedItemsList");
|
@ -106,6 +106,7 @@ namespace PetCompanion.Migrations
|
||||
columns: table => new
|
||||
{
|
||||
PetId = table.Column<string>(type: "TEXT", nullable: false),
|
||||
Items = table.Column<string>(type: "TEXT", nullable: false),
|
||||
Capacity = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
@ -215,33 +216,6 @@ namespace PetCompanion.Migrations
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "InventoryItems",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
InventoryId = table.Column<string>(type: "TEXT", nullable: false),
|
||||
GameItemId = table.Column<int>(type: "INTEGER", nullable: false),
|
||||
Quantity = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_InventoryItems", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_InventoryItems_GameItems_GameItemId",
|
||||
column: x => x.GameItemId,
|
||||
principalTable: "GameItems",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_InventoryItems_Inventories_InventoryId",
|
||||
column: x => x.InventoryId,
|
||||
principalTable: "Inventories",
|
||||
principalColumn: "PetId",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
table: "Skills",
|
||||
columns: new[] { "Id", "Description", "Icon", "Name", "PointsCost", "SkillsIdRequired", "Type" },
|
||||
@ -294,16 +268,6 @@ namespace PetCompanion.Migrations
|
||||
table: "EquippedItems",
|
||||
column: "PetId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_InventoryItems_GameItemId",
|
||||
table: "InventoryItems",
|
||||
column: "GameItemId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_InventoryItems_InventoryId",
|
||||
table: "InventoryItems",
|
||||
column: "InventoryId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_PetSkills_PetId",
|
||||
table: "PetSkills",
|
||||
@ -327,7 +291,7 @@ namespace PetCompanion.Migrations
|
||||
name: "EquippedItems");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "InventoryItems");
|
||||
name: "Inventories");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "PetSkills");
|
||||
@ -345,13 +309,10 @@ namespace PetCompanion.Migrations
|
||||
name: "GameItems");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Inventories");
|
||||
name: "Pets");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Skills");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Pets");
|
||||
}
|
||||
}
|
||||
}
|
@ -85,34 +85,13 @@ namespace PetCompanion.Migrations
|
||||
b.Property<int>("Capacity")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("PetId");
|
||||
|
||||
b.ToTable("Inventories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.InventoryItem", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("GameItemId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("InventoryId")
|
||||
b.PrimitiveCollection<string>("Items")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Quantity")
|
||||
.HasColumnType("INTEGER");
|
||||
b.HasKey("PetId");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("GameItemId");
|
||||
|
||||
b.HasIndex("InventoryId");
|
||||
|
||||
b.ToTable("InventoryItems");
|
||||
b.ToTable("Inventories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.Pet", b =>
|
||||
@ -576,23 +555,6 @@ namespace PetCompanion.Migrations
|
||||
b.Navigation("Pet");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.InventoryItem", b =>
|
||||
{
|
||||
b.HasOne("PetCompanion.Models.GameItem", "GameItem")
|
||||
.WithMany()
|
||||
.HasForeignKey("GameItemId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("PetCompanion.Models.Inventory", null)
|
||||
.WithMany("Items")
|
||||
.HasForeignKey("InventoryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("GameItem");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.PetSkill", b =>
|
||||
{
|
||||
b.HasOne("PetCompanion.Models.Pet", "Pet")
|
||||
@ -641,11 +603,6 @@ namespace PetCompanion.Migrations
|
||||
b.Navigation("Skill");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.Inventory", b =>
|
||||
{
|
||||
b.Navigation("Items");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PetCompanion.Models.Pet", b =>
|
||||
{
|
||||
b.Navigation("EquippedItemsList");
|
||||
|
@ -10,17 +10,7 @@ namespace PetCompanion.Models
|
||||
public string PetId { get; set; }
|
||||
[JsonIgnore]
|
||||
public virtual Pet Pet { get; set; }
|
||||
public virtual ICollection<InventoryItem> Items { get; set; } = new List<InventoryItem>();
|
||||
public List<int> Items { get; set; } = new List<int>();
|
||||
public int Capacity { get; set; } = 20;
|
||||
}
|
||||
|
||||
public class InventoryItem
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
public string InventoryId { get; set; }
|
||||
public int GameItemId { get; set; }
|
||||
public virtual GameItem GameItem { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -25,9 +25,10 @@ namespace PetCompanion.Models
|
||||
public DateTime BasicActionCooldown { get; set; }
|
||||
|
||||
public int SkillPoints { get; set; } = 2;
|
||||
public virtual ICollection<PetSkill> Skills { get; set; } = new List<PetSkill>();
|
||||
|
||||
public virtual ICollection<PetSkill> Skills { get; set; } = new List<PetSkill>();
|
||||
public virtual Inventory Inventory { get; set; }
|
||||
public virtual ICollection<EquippedItem> EquippedItemsList { get; set; } = new List<EquippedItem>();
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<ItemEquipTarget, int> EquippedItems
|
||||
@ -44,8 +45,6 @@ namespace PetCompanion.Models
|
||||
}
|
||||
}
|
||||
|
||||
public virtual ICollection<EquippedItem> EquippedItemsList { get; set; } = new List<EquippedItem>();
|
||||
|
||||
public Pet() { }
|
||||
|
||||
public void IncrementIntelligence(int amount)
|
||||
|
@ -13,55 +13,59 @@ namespace PetCompanion.Repositories
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Inventory> GetPetInventory(string petId)
|
||||
public Inventory GetPetInventory(string petId)
|
||||
{
|
||||
return await _context.Inventories
|
||||
.Include(i => i.Items)
|
||||
.ThenInclude(ii => ii.GameItem)
|
||||
.FirstOrDefaultAsync(i => i.PetId == petId);
|
||||
return _context.Inventories
|
||||
.FirstOrDefault(i => i.PetId == petId);
|
||||
}
|
||||
|
||||
public async Task<Dictionary<ItemEquipTarget, int>> GetEquippedItems(string petId)
|
||||
public Dictionary<ItemEquipTarget, int> GetEquippedItems(string petId)
|
||||
{
|
||||
var equippedItems = await _context.EquippedItems
|
||||
var equippedItems = _context.EquippedItems
|
||||
.Where(e => e.PetId == petId)
|
||||
.ToDictionaryAsync(e => e.EquipTarget, e => e.GameItemId);
|
||||
.ToDictionary(e => e.EquipTarget, e => e.GameItemId);
|
||||
|
||||
return equippedItems;
|
||||
}
|
||||
|
||||
public async Task SaveInventory(Inventory inventory)
|
||||
public void UpdateEquippedItems(string petId, Dictionary<ItemEquipTarget, int> equippedItems)
|
||||
{
|
||||
var existingInventory = await _context.Inventories
|
||||
.Include(i => i.Items)
|
||||
.FirstOrDefaultAsync(i => i.PetId == inventory.PetId);
|
||||
var existingItems = _context.EquippedItems
|
||||
.Where(e => e.PetId == petId)
|
||||
.ToList();
|
||||
|
||||
if (existingInventory == null)
|
||||
_context.EquippedItems.RemoveRange(existingItems);
|
||||
|
||||
var newEquippedItems = equippedItems.Select(kvp => new EquippedItem
|
||||
{
|
||||
_context.Inventories.Add(inventory);
|
||||
PetId = petId,
|
||||
EquipTarget = kvp.Key,
|
||||
GameItemId = kvp.Value
|
||||
});
|
||||
|
||||
_context.EquippedItems.AddRangeAsync(newEquippedItems);
|
||||
_context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Inventory SavePetInventory(Inventory inventory)
|
||||
{
|
||||
if (_context.Inventories.Any(i => i.PetId == inventory.PetId))
|
||||
{
|
||||
_context.Inventories.Update(inventory);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clear existing items
|
||||
_context.InventoryItems.RemoveRange(existingInventory.Items);
|
||||
|
||||
// Update inventory properties
|
||||
// existingInventory.Capacity = inventory.Capacity;
|
||||
|
||||
// Add new items
|
||||
foreach (var item in inventory.Items)
|
||||
{
|
||||
existingInventory.Items.Add(new InventoryItem
|
||||
{
|
||||
InventoryId = existingInventory.PetId,
|
||||
GameItemId = item.GameItemId,
|
||||
Quantity = item.Quantity,
|
||||
GameItem = item.GameItem
|
||||
});
|
||||
}
|
||||
_context.Inventories.Add(inventory);
|
||||
}
|
||||
_context.SaveChanges();
|
||||
return inventory;
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
public Inventory UpdatePetInventory(Inventory inventory)
|
||||
{
|
||||
_context.Inventories.Update(inventory);
|
||||
_context.SaveChanges();
|
||||
return inventory;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ namespace PetCompanion.Repositories
|
||||
.Where(p => p.UserId == userId)
|
||||
.Include(p => p.Stats)
|
||||
.Include(p => p.Resources)
|
||||
.Include(p => p.Inventory)
|
||||
.Include(p => p.EquippedItemsList)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@ -28,6 +30,8 @@ namespace PetCompanion.Repositories
|
||||
.Where(p => p.Id == petId && p.UserId == userId)
|
||||
.Include(p => p.Stats)
|
||||
.Include(p => p.Resources)
|
||||
.Include(p => p.Inventory)
|
||||
.Include(p => p.EquippedItemsList)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
@ -41,6 +45,12 @@ namespace PetCompanion.Repositories
|
||||
public Pet UpdatePet(Pet pet)
|
||||
{
|
||||
context.Pets.Update(pet);
|
||||
|
||||
if (pet.Inventory != null)
|
||||
{
|
||||
context.Inventories.Update(pet.Inventory);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
return pet;
|
||||
}
|
||||
|
@ -13,16 +13,16 @@ namespace PetCompanion.Repositories
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PetSkill>> GetPetSkills(string petId)
|
||||
public IEnumerable<PetSkill> GetPetSkills(string petId)
|
||||
{
|
||||
return await _context.PetSkills
|
||||
return _context.PetSkills
|
||||
.Include(ps => ps.Skill)
|
||||
.ThenInclude(s => s.Effects)
|
||||
.Where(ps => ps.PetId == petId)
|
||||
.ToListAsync();
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<PetSkill> SavePetSkill(PetSkill petSkill)
|
||||
public PetSkill SavePetSkill(PetSkill petSkill)
|
||||
{
|
||||
if (petSkill.Id == 0)
|
||||
{
|
||||
@ -32,7 +32,7 @@ namespace PetCompanion.Repositories
|
||||
{
|
||||
_context.PetSkills.Update(petSkill);
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
_context.SaveChanges();
|
||||
return petSkill;
|
||||
}
|
||||
}
|
||||
|
@ -5,180 +5,151 @@ namespace PetCompanion.Services
|
||||
{
|
||||
public class PetInventoryService
|
||||
{
|
||||
private readonly PetInventoryRepository _inventoryRepository;
|
||||
private readonly PetRepository _petRepository;
|
||||
private readonly GameItemsRepository _gameItemsRepository;
|
||||
private readonly PetInventoryRepository petInventoryRepository;
|
||||
private readonly PetRepository petRepository;
|
||||
private readonly GameItemsRepository gameItemsRepository;
|
||||
private readonly GameItemService gameItemService;
|
||||
|
||||
public PetInventoryService(
|
||||
PetInventoryRepository inventoryRepository,
|
||||
PetInventoryRepository petInventoryRepository,
|
||||
PetRepository petRepository,
|
||||
GameItemsRepository gameItemsRepository)
|
||||
GameItemsRepository gameItemsRepository,
|
||||
GameItemService gameItemService)
|
||||
{
|
||||
_inventoryRepository = inventoryRepository;
|
||||
_petRepository = petRepository;
|
||||
_gameItemsRepository = gameItemsRepository;
|
||||
this.petInventoryRepository = petInventoryRepository;
|
||||
this.petRepository = petRepository;
|
||||
this.gameItemsRepository = gameItemsRepository;
|
||||
this.gameItemService = gameItemService;
|
||||
}
|
||||
|
||||
public async Task<Inventory> GetInventory(string petId, string userId)
|
||||
public Inventory GetInventory(string petId, string userId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
return await _inventoryRepository.GetPetInventory(petId);
|
||||
return petInventoryRepository.GetPetInventory(petId);
|
||||
}
|
||||
|
||||
public async Task<Inventory> CreateInventory(string petId)
|
||||
public Inventory CreateInventory(string petId)
|
||||
{
|
||||
var inventory = new Inventory
|
||||
{
|
||||
PetId = petId,
|
||||
Capacity = 20,
|
||||
Items = new List<InventoryItem>()
|
||||
Items = new List<int>()
|
||||
};
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
petInventoryRepository.SavePetInventory(inventory);
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public async Task<Pet> UseItem(string petId, string userId, int itemId)
|
||||
public Pet UseItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
var item = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
|
||||
if (item == null || item.Quantity <= 0)
|
||||
if (!pet.Inventory.Items.Any(i => i == itemId))
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
var gameItem = gameItemsRepository.GetById(itemId);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return _petRepository.UpdatePet(pet);
|
||||
gameItemService.ApplyItemEffect(pet, gameItem);
|
||||
|
||||
pet.Inventory.Items.Remove(itemId);
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> EquipItem(string petId, string userId, int itemId)
|
||||
public Pet EquipItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
var item = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
|
||||
if (item == null || item.Quantity <= 0)
|
||||
if (!pet.Inventory.Items.Any(i => i == itemId))
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
if (item.GameItem.Type != ItemType.Equipment)
|
||||
var gameItem = gameItemsRepository.GetById(itemId);
|
||||
|
||||
if (gameItem.Type != ItemType.Equipment)
|
||||
throw new Exception("Item is not equipment");
|
||||
|
||||
var equippedItems = await _inventoryRepository.GetEquippedItems(petId);
|
||||
if (equippedItems.ContainsKey(item.GameItem.EquipTarget))
|
||||
// If there's already an item equipped in that slot, unequip it first
|
||||
if (pet.EquippedItems.ContainsKey(gameItem.EquipTarget))
|
||||
{
|
||||
await UnequipItem(petId, userId, item.GameItem.EquipTarget);
|
||||
UnequipItem(petId, userId, gameItem.EquipTarget);
|
||||
}
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
// Apply equipment effects
|
||||
gameItemService.ApplyItemEffect(pet, gameItem);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return _petRepository.UpdatePet(pet);
|
||||
// Mark item as equipped
|
||||
pet.EquippedItems[gameItem.EquipTarget] = itemId;
|
||||
|
||||
// Remove from inventory
|
||||
pet.Inventory.Items.Remove(itemId);
|
||||
|
||||
petInventoryRepository.UpdateEquippedItems(petId, pet.EquippedItems);
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> UnequipItem(string petId, string userId, ItemEquipTarget equipTarget)
|
||||
public Pet UnequipItem(string petId, string userId, ItemEquipTarget equipTarget)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var equippedItems = await _inventoryRepository.GetEquippedItems(petId);
|
||||
if (!equippedItems.ContainsKey(equipTarget))
|
||||
return pet;
|
||||
if (!pet.EquippedItems.ContainsKey(equipTarget))
|
||||
throw new Exception("No item equipped in that slot");
|
||||
|
||||
var itemId = equippedItems[equipTarget];
|
||||
var item = _gameItemsRepository.GetById(itemId);
|
||||
var equippedItemId = pet.EquippedItems[equipTarget];
|
||||
var equippedItem = gameItemsRepository.GetById(equippedItemId);
|
||||
|
||||
if (item != null)
|
||||
if (equippedItem != null)
|
||||
{
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
var inventoryItem = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
// Remove equipment effects
|
||||
gameItemService.RemoveItemEffect(pet, equippedItem);
|
||||
|
||||
if (inventoryItem != null)
|
||||
inventoryItem.Quantity++;
|
||||
else
|
||||
inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = itemId,
|
||||
Quantity = 1,
|
||||
GameItem = item,
|
||||
InventoryId = pet.Id
|
||||
});
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
// Add item back to inventory
|
||||
pet.Inventory.Items.Add(equippedItemId);
|
||||
}
|
||||
|
||||
return _petRepository.UpdatePet(pet);
|
||||
pet.EquippedItems.Remove(equipTarget);
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> DropItem(string petId, string userId, int itemId)
|
||||
public Pet DropItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
if (inventory == null)
|
||||
throw new Exception("Inventory not found");
|
||||
|
||||
var item = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (item == null || item.Quantity <= 0)
|
||||
if (!pet.Inventory.Items.Any(i => i == itemId))
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
var gameItem = gameItemsRepository.GetById(itemId);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return pet;
|
||||
pet.Inventory.Items.Remove(itemId);
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> AddItemToPet(string petId, string userId, int itemId, int quantity)
|
||||
public Pet AddItemToPet(string petId, string userId, int itemId, int quantity)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
var gameItem = gameItemsRepository.GetById(itemId);
|
||||
|
||||
var gameItem = _gameItemsRepository.GetById(itemId);
|
||||
if (gameItem == null)
|
||||
throw new Exception("Item not found");
|
||||
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
if (inventory == null)
|
||||
throw new Exception("Inventory not found");
|
||||
|
||||
var existingItem = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (existingItem != null)
|
||||
for (int i = 0; i < quantity; i++)
|
||||
{
|
||||
existingItem.Quantity += quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = itemId,
|
||||
Quantity = quantity,
|
||||
GameItem = gameItem,
|
||||
InventoryId = inventory.PetId
|
||||
});
|
||||
pet.Inventory.Items.Add(itemId);
|
||||
}
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return pet;
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ namespace PetCompanion.Services
|
||||
|
||||
var createdPet = petRepository.CreatePet(pet);
|
||||
|
||||
var inventory = petInventoryService.CreateInventory(petId).Result;
|
||||
var inventory = petInventoryService.CreateInventory(petId);
|
||||
|
||||
return createdPet;
|
||||
}
|
||||
@ -155,98 +155,6 @@ namespace PetCompanion.Services
|
||||
return petRepository.UpdatePetResources(pet);
|
||||
}
|
||||
|
||||
public Pet UseItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (inventoryItem == null || inventoryItem.Quantity <= 0)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
gameItemService.ApplyItemEffect(pet, inventoryItem.GameItem);
|
||||
|
||||
inventoryItem.Quantity--;
|
||||
if (inventoryItem.Quantity <= 0)
|
||||
pet.Inventory.Items.Remove(inventoryItem);
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public Pet EquipItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (inventoryItem == null || inventoryItem.Quantity <= 0)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
if (inventoryItem.GameItem.Type != ItemType.Equipment)
|
||||
throw new Exception("Item is not equipment");
|
||||
|
||||
// If there's already an item equipped in that slot, unequip it first
|
||||
if (pet.EquippedItems.ContainsKey(inventoryItem.GameItem.EquipTarget))
|
||||
{
|
||||
UnequipItem(pet, inventoryItem.GameItem.EquipTarget);
|
||||
}
|
||||
|
||||
// Apply equipment effects
|
||||
gameItemService.ApplyItemEffect(pet, inventoryItem.GameItem);
|
||||
|
||||
// Mark item as equipped
|
||||
pet.EquippedItems[inventoryItem.GameItem.EquipTarget] = itemId;
|
||||
|
||||
// Remove from inventory
|
||||
inventoryItem.Quantity--;
|
||||
if (inventoryItem.Quantity <= 0)
|
||||
pet.Inventory.Items.Remove(inventoryItem);
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public Pet UnequipItem(string petId, string userId, ItemEquipTarget equipTarget)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
UnequipItem(pet, equipTarget);
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
private void UnequipItem(Pet pet, ItemEquipTarget equipTarget)
|
||||
{
|
||||
if (!pet.EquippedItems.ContainsKey(equipTarget))
|
||||
return;
|
||||
|
||||
var equippedItemId = pet.EquippedItems[equipTarget];
|
||||
var equippedItem = gameItemsRepository.GetById(equippedItemId);
|
||||
|
||||
if (equippedItem != null)
|
||||
{
|
||||
// Remove equipment effects
|
||||
gameItemService.RemoveItemEffect(pet, equippedItem);
|
||||
|
||||
// Add item back to inventory
|
||||
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == equippedItemId);
|
||||
if (inventoryItem != null)
|
||||
inventoryItem.Quantity++;
|
||||
else
|
||||
pet.Inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = equippedItemId,
|
||||
Quantity = 1,
|
||||
GameItem = equippedItem,
|
||||
InventoryId = pet.Id
|
||||
});
|
||||
}
|
||||
|
||||
pet.EquippedItems.Remove(equipTarget);
|
||||
}
|
||||
|
||||
public Pet GetPet(string petId, string userId)
|
||||
{
|
||||
var pet = petRepository.GetPetById(petId, userId);
|
||||
@ -254,47 +162,5 @@ namespace PetCompanion.Services
|
||||
throw new Exception("Pet not found");
|
||||
return pet;
|
||||
}
|
||||
|
||||
public Pet DropItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = GetPet(petId, userId);
|
||||
|
||||
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (inventoryItem == null || inventoryItem.Quantity <= 0)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
inventoryItem.Quantity--;
|
||||
if (inventoryItem.Quantity <= 0)
|
||||
pet.Inventory.Items.Remove(inventoryItem);
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public Pet AddItemToPet(string petId, string userId, int itemId, int quantity)
|
||||
{
|
||||
var pet = GetPet(petId, userId);
|
||||
var gameItem = gameItemsRepository.GetById(itemId);
|
||||
|
||||
if (gameItem == null)
|
||||
throw new Exception("Item not found");
|
||||
|
||||
var inventoryItem = pet.Inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
if (inventoryItem != null)
|
||||
{
|
||||
inventoryItem.Quantity += quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
pet.Inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = itemId,
|
||||
Quantity = quantity,
|
||||
GameItem = gameItem,
|
||||
InventoryId = pet.Id
|
||||
});
|
||||
}
|
||||
|
||||
return petRepository.UpdatePet(pet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,16 +14,16 @@ namespace PetCompanion.Services
|
||||
_petRepository = petRepository;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PetSkill>> GetPetSkills(string petId, string userId)
|
||||
public IEnumerable<PetSkill> GetPetSkills(string petId, string userId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
return await _petSkillRepository.GetPetSkills(petId);
|
||||
return _petSkillRepository.GetPetSkills(petId);
|
||||
}
|
||||
|
||||
public async Task<PetSkill> AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
public PetSkill AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
@ -32,7 +32,7 @@ namespace PetCompanion.Services
|
||||
if (pet.SkillPoints <= 0)
|
||||
throw new Exception("No skill points available");
|
||||
|
||||
var skills = await _petSkillRepository.GetPetSkills(petId);
|
||||
var skills = _petSkillRepository.GetPetSkills(petId);
|
||||
var existingSkill = skills.FirstOrDefault(s => s.SkillId == skillId);
|
||||
|
||||
if (existingSkill != null)
|
||||
@ -41,7 +41,7 @@ namespace PetCompanion.Services
|
||||
throw new Exception("Skill already at maximum tier");
|
||||
|
||||
existingSkill.CurrentTier++;
|
||||
await _petSkillRepository.SavePetSkill(existingSkill);
|
||||
_petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -51,7 +51,7 @@ namespace PetCompanion.Services
|
||||
SkillId = skillId,
|
||||
CurrentTier = SkillTier.I
|
||||
};
|
||||
await _petSkillRepository.SavePetSkill(existingSkill);
|
||||
_petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
|
||||
pet.SkillPoints--;
|
||||
|
@ -21,11 +21,11 @@ namespace PetCompanion.Services
|
||||
_petSkillRepository = petSkillRepository;
|
||||
}
|
||||
|
||||
public async Task<PetSkill> AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
public PetSkill AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
{
|
||||
var pet = await _context.Pets
|
||||
var pet = _context.Pets
|
||||
.Include(p => p.Skills)
|
||||
.FirstOrDefaultAsync(p => p.Id == petId && p.UserId == userId);
|
||||
.FirstOrDefault(p => p.Id == petId && p.UserId == userId);
|
||||
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
@ -33,9 +33,9 @@ namespace PetCompanion.Services
|
||||
if (pet.SkillPoints <= 0)
|
||||
throw new Exception("No skill points available");
|
||||
|
||||
var skill = await _context.Skills
|
||||
var skill = _context.Skills
|
||||
.Include(s => s.Effects)
|
||||
.FirstOrDefaultAsync(s => s.Id == skillId);
|
||||
.FirstOrDefault(s => s.Id == skillId);
|
||||
|
||||
if (skill == null)
|
||||
throw new Exception("Skill not found");
|
||||
@ -51,7 +51,7 @@ namespace PetCompanion.Services
|
||||
if (effect != null)
|
||||
effect.PetSkillUpgrade(ref pet);
|
||||
|
||||
await _petSkillRepository.SavePetSkill(existingSkill);
|
||||
_petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -61,7 +61,7 @@ namespace PetCompanion.Services
|
||||
SkillId = skillId,
|
||||
CurrentTier = SkillTier.I
|
||||
};
|
||||
await _petSkillRepository.SavePetSkill(newSkill);
|
||||
_petSkillRepository.SavePetSkill(newSkill);
|
||||
|
||||
var effect = skill.Effects.FirstOrDefault(e => e.Tier == SkillTier.I);
|
||||
if (effect != null)
|
||||
@ -69,16 +69,16 @@ namespace PetCompanion.Services
|
||||
}
|
||||
|
||||
pet.SkillPoints--;
|
||||
await _context.SaveChangesAsync();
|
||||
_context.SaveChanges();
|
||||
|
||||
return (await _petSkillRepository.GetPetSkills(petId)).First(ps => ps.SkillId == skillId);
|
||||
return _petSkillRepository.GetPetSkills(petId).First(ps => ps.SkillId == skillId);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Skill>> GetAvailableSkills()
|
||||
public IEnumerable<Skill> GetAvailableSkills()
|
||||
{
|
||||
return await _context.Skills
|
||||
return _context.Skills
|
||||
.Include(s => s.Effects)
|
||||
.ToListAsync();
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user