not working inventory :c
This commit is contained in:
121
Services/GameItemService.cs
Normal file
121
Services/GameItemService.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using CsvHelper;
|
||||
using System.Globalization;
|
||||
using PetCompanion.Models;
|
||||
using PetCompanion.Repositories;
|
||||
|
||||
namespace PetCompanion.Services
|
||||
{
|
||||
public class GameItemService
|
||||
{
|
||||
private readonly GameItemsRepository gameItemsRepository;
|
||||
|
||||
public GameItemService(GameItemsRepository gameItemsRepository)
|
||||
{
|
||||
this.gameItemsRepository = gameItemsRepository;
|
||||
}
|
||||
|
||||
public void LoadItemsFromCsv(string filePath)
|
||||
{
|
||||
using var reader = new StreamReader(filePath);
|
||||
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
|
||||
|
||||
var items = csv.GetRecords<GameItem>().ToList();
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var existingItem = gameItemsRepository.GetById(item.Id);
|
||||
if (existingItem == null)
|
||||
{
|
||||
gameItemsRepository.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
//gameItemsRepository.Update(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyItemEffect(Pet pet, GameItem item)
|
||||
{
|
||||
var effects = item.Effect.Split(';');
|
||||
foreach (var effect in effects)
|
||||
{
|
||||
ApplySingleEffect(pet, effect.Trim());
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveItemEffect(Pet pet, GameItem item)
|
||||
{
|
||||
var effects = item.Effect.Split(';');
|
||||
foreach (var effect in effects)
|
||||
{
|
||||
RemoveSingleEffect(pet, effect.Trim());
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplySingleEffect(Pet pet, string effect)
|
||||
{
|
||||
if (effect == "Nothing") return;
|
||||
|
||||
var parts = effect.Split('_');
|
||||
var action = parts[0];
|
||||
var target = parts[1];
|
||||
var value = parts.Length > 2 ? int.Parse(parts[2]) : 0;
|
||||
|
||||
switch ($"{action}_{target}")
|
||||
{
|
||||
case "ADD_FOOD_RESOURCES":
|
||||
pet.Resources.Food += value;
|
||||
break;
|
||||
case "ADD_INTELLIGENCE":
|
||||
pet.Stats.Intelligence += value;
|
||||
break;
|
||||
case "ADD_HEALTH":
|
||||
pet.Health = Math.Min(pet.Health + value, pet.MaxHealth);
|
||||
break;
|
||||
case "ADD_MAX_HEALTH":
|
||||
pet.MaxHealth += value;
|
||||
break;
|
||||
// Add more effect handlers as needed
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveSingleEffect(Pet pet, string effect)
|
||||
{
|
||||
if (effect == "Nothing") return;
|
||||
|
||||
var parts = effect.Split('_');
|
||||
var action = parts[0];
|
||||
var target = parts[1];
|
||||
var value = parts.Length > 2 ? int.Parse(parts[2]) : 0;
|
||||
|
||||
switch ($"{action}_{target}")
|
||||
{
|
||||
case "ADD_MAX_HEALTH":
|
||||
pet.MaxHealth -= value;
|
||||
pet.Health = Math.Min(pet.Health, pet.MaxHealth);
|
||||
break;
|
||||
case "ADD_MAX_INTELLIGENCE":
|
||||
pet.Stats.MaxIntelligence -= value;
|
||||
pet.Stats.Intelligence = Math.Min(pet.Stats.Intelligence, pet.Stats.MaxIntelligence);
|
||||
break;
|
||||
case "ADD_MAX_STRENGTH":
|
||||
pet.Stats.MaxStrength -= value;
|
||||
pet.Stats.Strength = Math.Min(pet.Stats.Strength, pet.Stats.MaxStrength);
|
||||
break;
|
||||
case "ADD_MAX_CHARISMA":
|
||||
pet.Stats.MaxCharisma -= value;
|
||||
pet.Stats.Charisma = Math.Min(pet.Stats.Charisma, pet.Stats.MaxCharisma);
|
||||
break;
|
||||
case "ADD_MAX_ALL_STATS":
|
||||
pet.Stats.MaxIntelligence -= value;
|
||||
pet.Stats.MaxStrength -= value;
|
||||
pet.Stats.MaxCharisma -= value;
|
||||
pet.Stats.Intelligence = Math.Min(pet.Stats.Intelligence, pet.Stats.MaxIntelligence);
|
||||
pet.Stats.Strength = Math.Min(pet.Stats.Strength, pet.Stats.MaxStrength);
|
||||
pet.Stats.Charisma = Math.Min(pet.Stats.Charisma, pet.Stats.MaxCharisma);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
184
Services/PetInventoryService.cs
Normal file
184
Services/PetInventoryService.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using PetCompanion.Models;
|
||||
using PetCompanion.Repositories;
|
||||
|
||||
namespace PetCompanion.Services
|
||||
{
|
||||
public class PetInventoryService
|
||||
{
|
||||
private readonly PetInventoryRepository _inventoryRepository;
|
||||
private readonly PetRepository _petRepository;
|
||||
private readonly GameItemsRepository _gameItemsRepository;
|
||||
|
||||
public PetInventoryService(
|
||||
PetInventoryRepository inventoryRepository,
|
||||
PetRepository petRepository,
|
||||
GameItemsRepository gameItemsRepository)
|
||||
{
|
||||
_inventoryRepository = inventoryRepository;
|
||||
_petRepository = petRepository;
|
||||
_gameItemsRepository = gameItemsRepository;
|
||||
}
|
||||
|
||||
public async Task<Inventory> GetInventory(string petId, string userId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
return await _inventoryRepository.GetPetInventory(petId);
|
||||
}
|
||||
|
||||
public async Task<Inventory> CreateInventory(string petId)
|
||||
{
|
||||
var inventory = new Inventory
|
||||
{
|
||||
PetId = petId,
|
||||
Capacity = 20,
|
||||
Items = new List<InventoryItem>()
|
||||
};
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public async Task<Pet> UseItem(string petId, string userId, int itemId)
|
||||
{
|
||||
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)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return _petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> EquipItem(string petId, string userId, int itemId)
|
||||
{
|
||||
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)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
if (item.GameItem.Type != ItemType.Equipment)
|
||||
throw new Exception("Item is not equipment");
|
||||
|
||||
var equippedItems = await _inventoryRepository.GetEquippedItems(petId);
|
||||
if (equippedItems.ContainsKey(item.GameItem.EquipTarget))
|
||||
{
|
||||
await UnequipItem(petId, userId, item.GameItem.EquipTarget);
|
||||
}
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return _petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> UnequipItem(string petId, string userId, ItemEquipTarget equipTarget)
|
||||
{
|
||||
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;
|
||||
|
||||
var itemId = equippedItems[equipTarget];
|
||||
var item = _gameItemsRepository.GetById(itemId);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
var inventory = await _inventoryRepository.GetPetInventory(petId);
|
||||
var inventoryItem = inventory.Items.FirstOrDefault(i => i.GameItemId == itemId);
|
||||
|
||||
if (inventoryItem != null)
|
||||
inventoryItem.Quantity++;
|
||||
else
|
||||
inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = itemId,
|
||||
Quantity = 1,
|
||||
GameItem = item,
|
||||
InventoryId = pet.Id
|
||||
});
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
}
|
||||
|
||||
return _petRepository.UpdatePet(pet);
|
||||
}
|
||||
|
||||
public async Task<Pet> DropItem(string petId, string userId, int itemId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
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)
|
||||
throw new Exception("Item not found in inventory");
|
||||
|
||||
item.Quantity--;
|
||||
if (item.Quantity <= 0)
|
||||
inventory.Items.Remove(item);
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return pet;
|
||||
}
|
||||
|
||||
public async Task<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 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)
|
||||
{
|
||||
existingItem.Quantity += quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
inventory.Items.Add(new InventoryItem
|
||||
{
|
||||
GameItemId = itemId,
|
||||
Quantity = quantity,
|
||||
GameItem = gameItem,
|
||||
InventoryId = inventory.PetId
|
||||
});
|
||||
}
|
||||
|
||||
await _inventoryRepository.SaveInventory(inventory);
|
||||
return pet;
|
||||
}
|
||||
}
|
||||
}
|
@@ -6,12 +6,23 @@ namespace PetCompanion.Services
|
||||
public class PetService
|
||||
{
|
||||
private readonly PetRepository petRepository;
|
||||
private readonly PetClassService _petClassService;
|
||||
private readonly PetClassService petClassService;
|
||||
private readonly GameItemService gameItemService;
|
||||
private readonly GameItemsRepository gameItemsRepository;
|
||||
private readonly PetInventoryService petInventoryService;
|
||||
|
||||
public PetService(PetRepository petRepository, PetClassService petClassService)
|
||||
public PetService(
|
||||
PetRepository petRepository,
|
||||
PetClassService petClassService,
|
||||
GameItemService gameItemService,
|
||||
GameItemsRepository gameItemsRepository,
|
||||
PetInventoryService petInventoryService)
|
||||
{
|
||||
this.petRepository = petRepository;
|
||||
_petClassService = petClassService;
|
||||
this.petClassService = petClassService;
|
||||
this.gameItemService = gameItemService;
|
||||
this.gameItemsRepository = gameItemsRepository;
|
||||
this.petInventoryService = petInventoryService;
|
||||
}
|
||||
|
||||
public IEnumerable<Pet> GetAllPets(Guid userId)
|
||||
@@ -39,7 +50,11 @@ namespace PetCompanion.Services
|
||||
IsDead = false
|
||||
};
|
||||
|
||||
return petRepository.CreatePet(pet);
|
||||
var createdPet = petRepository.CreatePet(pet);
|
||||
|
||||
var inventory = petInventoryService.CreateInventory(petId).Result;
|
||||
|
||||
return createdPet;
|
||||
}
|
||||
|
||||
public Pet UpdatePetAction(string petId, string userId, PetUpdateActionRequest actionRequest)
|
||||
@@ -118,7 +133,7 @@ namespace PetCompanion.Services
|
||||
throw new Exception("Pet not found");
|
||||
}
|
||||
|
||||
return _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
|
||||
return petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
|
||||
}
|
||||
|
||||
public Pet UpdatePetResources(string petId, string userId)
|
||||
@@ -129,7 +144,7 @@ namespace PetCompanion.Services
|
||||
throw new Exception("Pet not found");
|
||||
}
|
||||
|
||||
var gatheredResources = _petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
|
||||
var gatheredResources = petClassService.CalculateGatheredResources(pet.Stats, pet.Level, pet.PetGatherAction, pet.GatherActionSince);
|
||||
|
||||
pet.Resources.Wisdom += gatheredResources.Wisdom;
|
||||
pet.Resources.Gold += gatheredResources.Gold;
|
||||
@@ -139,5 +154,147 @@ 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);
|
||||
if (pet == null)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
Services/PetSkillService.cs
Normal file
63
Services/PetSkillService.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using PetCompanion.Models;
|
||||
using PetCompanion.Repositories;
|
||||
|
||||
namespace PetCompanion.Services
|
||||
{
|
||||
public class PetSkillService
|
||||
{
|
||||
private readonly PetSkillRepository _petSkillRepository;
|
||||
private readonly PetRepository _petRepository;
|
||||
|
||||
public PetSkillService(PetSkillRepository petSkillRepository, PetRepository petRepository)
|
||||
{
|
||||
_petSkillRepository = petSkillRepository;
|
||||
_petRepository = petRepository;
|
||||
}
|
||||
|
||||
public async Task<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);
|
||||
}
|
||||
|
||||
public async Task<PetSkill> AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
{
|
||||
var pet = _petRepository.GetPetById(petId, userId);
|
||||
if (pet == null)
|
||||
throw new Exception("Pet not found");
|
||||
|
||||
if (pet.SkillPoints <= 0)
|
||||
throw new Exception("No skill points available");
|
||||
|
||||
var skills = await _petSkillRepository.GetPetSkills(petId);
|
||||
var existingSkill = skills.FirstOrDefault(s => s.SkillId == skillId);
|
||||
|
||||
if (existingSkill != null)
|
||||
{
|
||||
if (existingSkill.CurrentTier == SkillTier.III)
|
||||
throw new Exception("Skill already at maximum tier");
|
||||
|
||||
existingSkill.CurrentTier++;
|
||||
await _petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
else
|
||||
{
|
||||
existingSkill = new PetSkill
|
||||
{
|
||||
PetId = petId,
|
||||
SkillId = skillId,
|
||||
CurrentTier = SkillTier.I
|
||||
};
|
||||
await _petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
|
||||
pet.SkillPoints--;
|
||||
_petRepository.UpdatePet(pet);
|
||||
|
||||
return existingSkill;
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,18 +7,23 @@ namespace PetCompanion.Services
|
||||
{
|
||||
public class SkillService
|
||||
{
|
||||
private readonly ApplicationDbContext context;
|
||||
private readonly PetRepository petRepository;
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly PetRepository _petRepository;
|
||||
private readonly PetSkillRepository _petSkillRepository;
|
||||
|
||||
public SkillService(ApplicationDbContext context, PetRepository petRepository)
|
||||
public SkillService(
|
||||
ApplicationDbContext context,
|
||||
PetRepository petRepository,
|
||||
PetSkillRepository petSkillRepository)
|
||||
{
|
||||
this.context = context;
|
||||
this.petRepository = petRepository;
|
||||
_context = context;
|
||||
_petRepository = petRepository;
|
||||
_petSkillRepository = petSkillRepository;
|
||||
}
|
||||
|
||||
public async Task<PetSkill> AllocateSkillPoint(string petId, string userId, int skillId)
|
||||
{
|
||||
var pet = await context.Pets
|
||||
var pet = await _context.Pets
|
||||
.Include(p => p.Skills)
|
||||
.FirstOrDefaultAsync(p => p.Id == petId && p.UserId == userId);
|
||||
|
||||
@@ -28,7 +33,7 @@ namespace PetCompanion.Services
|
||||
if (pet.SkillPoints <= 0)
|
||||
throw new Exception("No skill points available");
|
||||
|
||||
var skill = await context.Skills
|
||||
var skill = await _context.Skills
|
||||
.Include(s => s.Effects)
|
||||
.FirstOrDefaultAsync(s => s.Id == skillId);
|
||||
|
||||
@@ -45,6 +50,8 @@ namespace PetCompanion.Services
|
||||
var effect = skill.Effects.FirstOrDefault(e => e.Tier == existingSkill.CurrentTier);
|
||||
if (effect != null)
|
||||
effect.PetSkillUpgrade(ref pet);
|
||||
|
||||
await _petSkillRepository.SavePetSkill(existingSkill);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -54,21 +61,22 @@ namespace PetCompanion.Services
|
||||
SkillId = skillId,
|
||||
CurrentTier = SkillTier.I
|
||||
};
|
||||
pet.Skills.Add(newSkill);
|
||||
await _petSkillRepository.SavePetSkill(newSkill);
|
||||
|
||||
var effect = skill.Effects.FirstOrDefault(e => e.Tier == SkillTier.I);
|
||||
if (effect != null)
|
||||
effect.PetSkillUpgrade(ref pet);
|
||||
}
|
||||
|
||||
pet.SkillPoints--;
|
||||
await context.SaveChangesAsync();
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return pet.Skills.First(ps => ps.SkillId == skillId);
|
||||
return (await _petSkillRepository.GetPetSkills(petId)).First(ps => ps.SkillId == skillId);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Skill>> GetAvailableSkills()
|
||||
{
|
||||
return await context.Skills
|
||||
return await _context.Skills
|
||||
.Include(s => s.Effects)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
Reference in New Issue
Block a user