LearnGodot/Models/ItemGroup.cs
2025-12-15 18:32:57 +08:00

82 lines
1.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
namespace Learn.Models;
public class ItemGroup(IEnumerable<Item> items)
{
public IEnumerable<string> GetKeys()
{
IEnumerable<string> inter = null;
foreach (var item in items)
{
if (inter == null)
{
inter = item.Info.Keys;
}
else
{
inter = inter.Intersect(item.Info.Keys);
}
}
if(inter == null) return new List<string>();
return inter.ToList();
}
public bool TryGetValue(string key, out string value, out bool isSame)
{
value = null;
isSame = true;
if (!GetKeys().Contains(key)) return false;
bool isFirst = true;
foreach (var item in items)
{
if (isFirst)
{
isFirst = false;
}
else
{
isSame = value == item.Info[key];
if(!isSame) return true;
}
value = item.Info[key];
}
return true;
}
public void SetValue(string key, string value)
{
foreach (var item in items)
{
item.Info[key] = value;
}
}
public bool Remove(string key)
{
if (!GetKeys().Contains(key)) return false;
foreach (var item in items)
{
item.Info.Remove(key);
}
return true;
}
private bool AnyContains(string key)
{
return items.Any(item => item.Info.Keys.Contains(key));
}
public bool TryAdd(string key, string value)
{
if (AnyContains(key)) return false;
foreach (var item in items)
{
item.Info.Add(key, value);
}
return true;
}
}