Refactor namespaces to follow new naming convention and add item model classes
This commit is contained in:
121
Repositories/ItemRepository.cs
Normal file
121
Repositories/ItemRepository.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Pet.Companion.Models;
|
||||
using Pet.Companion.Models.Enums;
|
||||
|
||||
namespace Pet.Companion.Repositories
|
||||
{
|
||||
public class ItemRepository
|
||||
{
|
||||
private readonly Dictionary<string, Item> _itemTemplates = new();
|
||||
|
||||
public ItemRepository()
|
||||
{
|
||||
LoadItemsFromCsv();
|
||||
}
|
||||
|
||||
private void LoadItemsFromCsv()
|
||||
{
|
||||
var lines = File.ReadAllLines("Items.csv").Skip(1); // Skip header
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var parts = line.Split(',');
|
||||
var name = parts[0];
|
||||
var type = Enum.Parse<ItemType>(parts[1]);
|
||||
var rarity = Enum.Parse<ItemRarity>(parts[2]);
|
||||
var description = parts[3];
|
||||
|
||||
Item item = type switch
|
||||
{
|
||||
ItemType.Consumable => CreateConsumableItem(name, description, rarity),
|
||||
ItemType.Equipment => CreateEquipmentItem(name, description, rarity),
|
||||
ItemType.Material => CreateMaterialItem(name, description, rarity),
|
||||
_ => throw new ArgumentException($"Unknown item type: {type}")
|
||||
};
|
||||
|
||||
_itemTemplates.Add(name, item);
|
||||
}
|
||||
}
|
||||
|
||||
private Item CreateConsumableItem(string name, string description, ItemRarity rarity)
|
||||
{
|
||||
var item = new ConsumableItem
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Name = name,
|
||||
Description = description,
|
||||
Type = ItemType.Consumable,
|
||||
Rarity = rarity
|
||||
};
|
||||
|
||||
// Parse effects from description
|
||||
if (description.Contains("food resources"))
|
||||
{
|
||||
var foodAmount = int.Parse(Regex.Match(description, @"\+(\d+) food").Groups[1].Value);
|
||||
item.Effect = (pet) => pet.Resources.Food += foodAmount;
|
||||
}
|
||||
else if (description.Contains("Intelligence"))
|
||||
{
|
||||
var amount = int.Parse(Regex.Match(description, @"\+(\d+)").Groups[1].Value);
|
||||
item.Effect = (pet) => pet.IncrementIntelligence(amount);
|
||||
}
|
||||
// Add more effect parsing as needed
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private Item CreateEquipmentItem(string name, string description, ItemRarity rarity)
|
||||
{
|
||||
var equipment = new StatBoostEquipment
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Name = name,
|
||||
Description = description,
|
||||
Type = ItemType.Equipment,
|
||||
Rarity = rarity
|
||||
};
|
||||
|
||||
// Parse slot and stats from description
|
||||
var slotMatch = Regex.Match(description, @"(Helmet|Chest|Legging|Weapon):");
|
||||
if (slotMatch.Success)
|
||||
{
|
||||
equipment.Slot = Enum.Parse<EquipmentSlot>(slotMatch.Groups[1].Value);
|
||||
}
|
||||
|
||||
var statMatch = Regex.Match(description, @"\+(\d+) Max (\w+)");
|
||||
if (statMatch.Success)
|
||||
{
|
||||
var amount = int.Parse(statMatch.Groups[1].Value);
|
||||
var stat = statMatch.Groups[2].Value;
|
||||
equipment.StatBoosts.Add(stat, amount);
|
||||
}
|
||||
|
||||
return equipment;
|
||||
}
|
||||
|
||||
private Item CreateMaterialItem(string name, string description, ItemRarity rarity)
|
||||
{
|
||||
return new MaterialItem
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Name = name,
|
||||
Description = description,
|
||||
Type = ItemType.Material,
|
||||
Rarity = rarity
|
||||
};
|
||||
}
|
||||
|
||||
public Item CreateItem(string itemName)
|
||||
{
|
||||
if (_itemTemplates.TryGetValue(itemName, out var template))
|
||||
{
|
||||
// Create a new instance with a new ID but same properties
|
||||
var json = System.Text.Json.JsonSerializer.Serialize(template);
|
||||
var newItem = System.Text.Json.JsonSerializer.Deserialize<Item>(json);
|
||||
newItem.Id = Guid.NewGuid().ToString();
|
||||
return newItem;
|
||||
}
|
||||
|
||||
throw new KeyNotFoundException($"Item template not found: {itemName}");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using pet_companion_api.Data;
|
||||
using pet_companion_api.Models;
|
||||
using Pet.Companion.Data;
|
||||
using Pet.Companion.Models;
|
||||
|
||||
namespace pet_companion_api.Repositories
|
||||
namespace Pet.Companion.Repositories
|
||||
{
|
||||
public class PetClassRepository
|
||||
{
|
||||
|
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using pet_companion_api.Data;
|
||||
using pet_companion_api.Models;
|
||||
using Pet.Companion.Data;
|
||||
using Pet.Companion.Models;
|
||||
|
||||
namespace pet_companion_api.Repositories
|
||||
namespace Pet.Companion.Repositories
|
||||
{
|
||||
public class PetRepository
|
||||
{
|
||||
|
Reference in New Issue
Block a user