113 lines
3.7 KiB
C#
113 lines
3.7 KiB
C#
using OECS;
|
|
|
|
namespace Game.CardWars;
|
|
|
|
/// <summary>
|
|
/// Loads card definitions from a CSV and creates card entities in the world.
|
|
/// </summary>
|
|
public static class CardDataLoader
|
|
{
|
|
/// <summary>
|
|
/// Creates all card definition entities in the world from CSV data.
|
|
/// Returns a list of (entity, CardDef) pairs.
|
|
/// </summary>
|
|
public static List<(Entity Entity, CardDef Def)> LoadDefinitions(World world, string csv)
|
|
{
|
|
var results = new List<(Entity, CardDef)>();
|
|
var lines = csv.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
|
|
if (lines.Length < 2)
|
|
return results;
|
|
|
|
// Skip header line.
|
|
for (int i = 1; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i].Trim();
|
|
if (string.IsNullOrEmpty(line)) continue;
|
|
|
|
var def = ParseLine(line);
|
|
if (def.Name == null) continue;
|
|
|
|
var entity = world.CreateEntity();
|
|
world.AddComponent(entity, def);
|
|
results.Add((entity, def));
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
private static CardDef ParseLine(string line)
|
|
{
|
|
// Simple CSV parsing: name,ranks,effect
|
|
// Ranks are semicolon-separated.
|
|
var parts = SplitCsv(line);
|
|
if (parts.Length < 3)
|
|
return default;
|
|
|
|
var name = parts[0].Trim();
|
|
var rankStrs = parts[1].Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
var ranks = new int[rankStrs.Length];
|
|
for (int i = 0; i < rankStrs.Length; i++)
|
|
ranks[i] = int.Parse(rankStrs[i]);
|
|
|
|
var effect = ParseEffect(parts[2].Trim());
|
|
|
|
return new CardDef
|
|
{
|
|
Name = name,
|
|
Ranks = ranks,
|
|
Effect = effect,
|
|
Kind = CardKind.Public
|
|
};
|
|
}
|
|
|
|
private static string[] SplitCsv(string line)
|
|
{
|
|
var result = new List<string>();
|
|
var current = new System.Text.StringBuilder();
|
|
bool inQuotes = false;
|
|
|
|
for (int i = 0; i < line.Length; i++)
|
|
{
|
|
char c = line[i];
|
|
if (c == '"')
|
|
{
|
|
inQuotes = !inQuotes;
|
|
}
|
|
else if (c == ',' && !inQuotes)
|
|
{
|
|
result.Add(current.ToString());
|
|
current.Clear();
|
|
}
|
|
else
|
|
{
|
|
current.Append(c);
|
|
}
|
|
}
|
|
result.Add(current.ToString());
|
|
return result.ToArray();
|
|
}
|
|
|
|
private static CardEffect ParseEffect(string effect)
|
|
{
|
|
return effect switch
|
|
{
|
|
string s when s.Contains("战士") => CardEffect.Warrior,
|
|
string s when s.Contains("弓手") => CardEffect.Archer,
|
|
string s when s.Contains("佣兵") => CardEffect.Mercenary,
|
|
string s when s.Contains("商人") => CardEffect.Merchant,
|
|
string s when s.Contains("舞姬") => CardEffect.Dancer,
|
|
string s when s.Contains("圣骑士") => CardEffect.Paladin,
|
|
string s when s.Contains("飞马") => CardEffect.Pegasus,
|
|
string s when s.Contains("诅咒师") => CardEffect.CurseMaster,
|
|
string s when s.Contains("魔导士") => CardEffect.Mage,
|
|
string s when s.Contains("公主") => CardEffect.Princess,
|
|
string s when s.Contains("决斗家") => CardEffect.Duelist,
|
|
string s when s.Contains("修女") => CardEffect.Nun,
|
|
string s when s.Contains("斥候") => CardEffect.Scout,
|
|
string s when s.Contains("女巫") => CardEffect.Witch,
|
|
string s when s.Contains("盗贼") => CardEffect.Thief,
|
|
_ => CardEffect.None
|
|
};
|
|
}
|
|
} |