Skip to content

Commit 6950375

Browse files
committed
fen base
1 parent 1a17703 commit 6950375

File tree

1 file changed

+47
-128
lines changed

1 file changed

+47
-128
lines changed

src/dsstats.shared/DsFen/DsFen.cs

Lines changed: 47 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace dsstats.shared.DsFen;
1+
using System.Text;
2+
3+
namespace dsstats.shared.DsFen;
24

35
public static partial class DsFen
46
{
@@ -7,158 +9,75 @@ public static partial class DsFen
79

810
public static string GetFen(SpawnDto spawn, Commander cmdr, int team)
911
{
12+
StringBuilder sb = new();
1013
var build = CmdrBuildFactory.Create(cmdr);
11-
if (build == null) return string.Empty;
12-
14+
if (build is null)
15+
{
16+
return string.Empty;
17+
}
1318
var polygon = team == 1 ? polygon1 : polygon2;
1419

15-
var groundUnits = new Dictionary<DsPoint, char>();
16-
var airUnits = new Dictionary<DsPoint, char>();
17-
1820
foreach (var unit in spawn.Units)
1921
{
2022
var buildOption = build.GetUnitBuildOption(unit.Unit.Name);
21-
if (buildOption == null) continue;
22-
23-
char key = buildOption.RequiresToggle && !buildOption.IsActive
24-
? char.ToUpper(buildOption.Key)
25-
: buildOption.Key;
26-
27-
var points = GetPoints(unit.Poss);
28-
foreach (var p in points)
23+
if (buildOption is null)
2924
{
30-
if (!polygon.IsPointInside(p))
31-
{
32-
continue;
33-
}
34-
var grid = polygon.GetNormalizedPoint(p);
35-
if (buildOption.IsAir)
36-
airUnits[grid] = key;
37-
else
38-
groundUnits[grid] = key;
25+
continue;
3926
}
40-
}
41-
42-
string EncodeLayer(Dictionary<DsPoint, char> layer)
43-
{
44-
if (layer.Count == 0) return "";
45-
46-
int minX = layer.Keys.Min(p => p.X);
47-
int maxX = layer.Keys.Max(p => p.X);
48-
int minY = layer.Keys.Min(p => p.Y);
49-
int maxY = layer.Keys.Max(p => p.Y);
50-
51-
var rows = new List<string>();
52-
for (int y = minY; y <= maxY; y++)
27+
char unitChar = buildOption.Key;
28+
if (buildOption.RequiresToggle && !buildOption.IsActive)
5329
{
54-
string row = "";
55-
int empty = 0;
56-
for (int x = minX; x <= maxX; x++)
30+
unitChar = char.ToUpper(unitChar);
31+
}
32+
var points = GetPoints(unit.Poss);
33+
foreach (var point in points)
34+
{
35+
if (!polygon.IsPointInside(point))
5736
{
58-
if (layer.TryGetValue(new(x, y), out char key))
59-
{
60-
if (empty > 0)
61-
{
62-
row += empty;
63-
empty = 0;
64-
}
65-
row += key;
66-
}
67-
else
68-
{
69-
empty++;
70-
}
37+
continue;
38+
// apply fen like string
7139
}
72-
if (empty > 0) row += empty;
73-
rows.Add(row);
40+
// Normalized point to a 25,17 (width,height) rectangle with bottom left at (0, 0)
41+
var normalizedPoint = polygon.GetNormalizedPoint(point);
7442
}
75-
76-
return string.Join("/", rows);
7743
}
7844

79-
var groundFen = EncodeLayer(groundUnits);
80-
var airFen = EncodeLayer(airUnits);
8145

82-
return $"{team}:{cmdr};{groundFen}|{airFen}";
46+
return sb.ToString();
8347
}
8448

8549
public static void ApplyFen(string fen, SpawnDto spawn, out Commander cmdr, out int team)
8650
{
8751
cmdr = Commander.None;
8852
team = 0;
89-
spawn.Units.Clear();
90-
91-
if (string.IsNullOrWhiteSpace(fen)) return;
92-
93-
var parts = fen.Split(';');
94-
if (parts.Length < 2) return;
95-
96-
var headerParts = parts[0].Split(':');
97-
if (headerParts.Length != 2) return;
98-
if (!int.TryParse(headerParts[0], out team)) return;
99-
if (!Enum.TryParse(headerParts[1], out cmdr)) return;
100-
101-
var build = CmdrBuildFactory.Create(cmdr);
102-
if (build == null) return;
103-
53+
// identify team
54+
// identify commander
55+
// getNormalizedPoints
10456
var polygon = team == 1 ? polygon1 : polygon2;
105-
106-
var layers = parts[1].Split('|');
107-
string groundLayer = layers.Length > 0 ? layers[0] : "";
108-
string airLayer = layers.Length > 1 ? layers[1] : "";
109-
110-
ParseLayer(groundLayer, build, false, spawn, polygon);
111-
ParseLayer(airLayer, build, true, spawn, polygon);
112-
}
113-
114-
private static void ParseLayer(string layer, CmdrBuild build, bool isAir, SpawnDto spawn, Polygon polygon)
115-
{
116-
if (string.IsNullOrWhiteSpace(layer)) return;
117-
118-
var rows = layer.Split('/');
119-
for (int y = 0; y < rows.Length; y++)
57+
var build = CmdrBuildFactory.Create(cmdr);
58+
if (build is null)
12059
{
121-
int x = 0;
122-
var row = rows[y];
123-
for (int i = 0; i < row.Length; i++)
124-
{
125-
if (char.IsDigit(row[i]))
126-
{
127-
string num = "";
128-
while (i < row.Length && char.IsDigit(row[i]))
129-
num += row[i++];
130-
i--;
131-
x += int.Parse(num);
132-
}
133-
else
134-
{
135-
char ch = row[i];
136-
bool isUpper = char.IsUpper(ch);
137-
char key = char.ToLower(ch);
138-
139-
var name = build.GetUnitNameFromKey(key, isAir, isUpper);
140-
if (name != null)
141-
{
142-
var gridPos = new DsPoint(x, y);
143-
var world = polygon.GetDeNormalizedPoint(gridPos);
144-
145-
var unit = spawn.Units.FirstOrDefault(u => u.Unit.Name == name);
146-
if (unit == null)
147-
{
148-
unit = new SpawnUnitDto { Unit = new UnitDto { Name = name }, Poss = "", Count = 0 };
149-
spawn.Units.Add(unit);
150-
}
151-
152-
if (!string.IsNullOrEmpty(unit.Poss))
153-
unit.Poss += ",";
154-
unit.Poss += $"{world.X},{world.Y}";
155-
unit.Count++;
156-
}
60+
return;
61+
}
15762

158-
x++;
159-
}
160-
}
63+
// DeNormalize sample
64+
var point = new DsPoint(0, 0);
65+
char unitChar = 'a';
66+
bool isAir = false;
67+
bool isToggle = false;
68+
var unitString = build.GetUnitNameFromKey(unitChar, isAir, isToggle);
69+
if (string.IsNullOrEmpty(unitString))
70+
{
71+
return;
16172
}
73+
var deNormalizedPoint = polygon.GetDeNormalizedPoint(point);
74+
SpawnUnitDto spawnUnit = new()
75+
{
76+
Unit = new UnitDto { Name = unitString },
77+
Poss = $"{deNormalizedPoint.X},{deNormalizedPoint.Y}",
78+
Count = 1
79+
};
80+
spawn.Units.Add(spawnUnit);
16281
}
16382

16483
public static List<DsPoint> GetPoints(string possString)

0 commit comments

Comments
 (0)