104 lines
3.0 KiB
C#
104 lines
3.0 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace DDD
|
|
{
|
|
public static class CraftingHelper
|
|
{
|
|
public static List<IngredientEntry> ExtractIngredients(params string[] values)
|
|
{
|
|
var list = new List<IngredientEntry>();
|
|
|
|
for (int i = 0; i < values.Length; i += 2)
|
|
{
|
|
var key = values[i];
|
|
var amountStr = values[i + 1];
|
|
|
|
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(amountStr))
|
|
continue;
|
|
|
|
if (int.TryParse(amountStr, out int amount))
|
|
{
|
|
list.Add(new IngredientEntry
|
|
{
|
|
IngredientId = key,
|
|
Amount = amount
|
|
});
|
|
}
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public static List<TasteData> ResolveTasteDatas(string[] tasteKeys, TasteDataSo tasteSo)
|
|
{
|
|
var result = new List<TasteData>();
|
|
|
|
foreach (var key in tasteKeys)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key)) continue;
|
|
|
|
if (tasteSo.TryGetDataById(key, out var taste))
|
|
{
|
|
result.Add(taste);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static int GetCraftableCount(List<IngredientEntry> ingredients)
|
|
{
|
|
if (ingredients == null || ingredients.Count == 0) return 0;
|
|
|
|
int min = int.MaxValue;
|
|
foreach (var entry in ingredients)
|
|
{
|
|
int owned = InventoryManager.Instance.GetItemCount(entry.IngredientId);
|
|
int possible = owned / entry.Amount;
|
|
if (possible < min)
|
|
min = possible;
|
|
}
|
|
|
|
return min;
|
|
}
|
|
|
|
public static bool TryConsumeIngredients(List<IngredientEntry> ingredients, int count)
|
|
{
|
|
if (count <= 0) return false;
|
|
|
|
foreach (var entry in ingredients)
|
|
{
|
|
int owned = InventoryManager.Instance.GetItemCount(entry.IngredientId);
|
|
if (owned < entry.Amount * count)
|
|
return false;
|
|
}
|
|
|
|
foreach (var entry in ingredients)
|
|
{
|
|
InventoryManager.Instance.RemoveItem(entry.IngredientId, entry.Amount * count);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static int ConsumeAll(List<IngredientEntry> ingredients, int maxCount)
|
|
{
|
|
if (maxCount <= 0) return 0;
|
|
|
|
foreach (var entry in ingredients)
|
|
{
|
|
InventoryManager.Instance.RemoveItem(entry.IngredientId, entry.Amount * maxCount);
|
|
}
|
|
|
|
return maxCount;
|
|
}
|
|
|
|
public static void RefundIngredients(List<IngredientEntry> ingredients, int count)
|
|
{
|
|
foreach (var entry in ingredients)
|
|
{
|
|
InventoryManager.Instance.AddItem(entry.IngredientId, entry.Amount * count);
|
|
}
|
|
}
|
|
}
|
|
} |