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