/
CalculateLobbyOccupancy.cs
218 lines (176 loc) · 6.56 KB
/
CalculateLobbyOccupancy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
namespace FaFLobbySimClient;
internal delegate Occupancy? CalculateLobbyOccupancy(IReadOnlyList<WordRegion> regions, WidthHeight widthHeight);
internal class CalculateLobbyOccupancyHandler
{
public Occupancy? Calculate(IReadOnlyList<WordRegion> regions, WidthHeight widthHeight)
{
var thresholdY = widthHeight.Width * 0.5f;
var lines = new List<List<WordRegion>>();
var consumedIndices = new HashSet<int>();
for (var i = 0; i < regions.Count; i++)
{
var region = regions[i];
// Don't let right hand side of screen serve as pivot for grouping into lines
if (consumedIndices.Contains(i) || region.Bounds.TopLeft.X >= thresholdY)
{
continue;
}
consumedIndices.Add(i);
var resultList = new List<WordRegion>
{
region
};
for (int j = 0; j < regions.Count; j++)
{
if (consumedIndices.Contains(j))
{
continue;
}
var otherRegion = regions[j];
var dy = Math.Abs(region.Bounds.BottomRight.Y - otherRegion.Bounds.BottomRight.Y);
if (dy <= 5)
{
resultList.Add(otherRegion);
consumedIndices.Add(j);
}
else if (Math.Abs(region.Bounds.TopLeft.Y - otherRegion.Bounds.TopLeft.Y) < 2 && dy < 10)
{
resultList.Add(otherRegion);
consumedIndices.Add(j);
}
else if (otherRegion.Bounds.TopLeft.Y >= region.Bounds.TopLeft.Y
&& otherRegion.Bounds.BottomRight.Y <= region.Bounds.BottomRight.Y)
{
resultList.Add(otherRegion);
consumedIndices.Add(j);
}
}
lines.Add(resultList.OrderBy(x => x.Bounds.TopLeft.X).ToList());
}
lines = lines.OrderBy(x => x[0].Bounds.TopLeft.Y).ToList();
// Table headers are in the top 30% of the screen and occupy more than 50% of the width.
const int topPercent = 30;
const int widthPercent = 45;
var yThreshold = widthHeight.Height * (topPercent / 100f);
var requiredWidth = widthHeight.Width * (widthPercent / 100f);
var mid20Left = widthHeight.Width * 0.4;
var mid20Right = widthHeight.Width * 0.6;
List<WordRegion>? possibleHeaderRow = null;
foreach (var line in lines)
{
var maxY = line.Max(x => x.Bounds.BottomRight.Y);
var minX = line.Min(x => x.Bounds.TopLeft.X);
var maxX = line.Max(x => x.Bounds.BottomRight.X);
var width = maxX - minX;
if (line.Count < 6)
{
continue;
}
var anyInMid20 = line.Any(x => x.Bounds.TopLeft.X >= mid20Left && x.Bounds.BottomRight.X <= mid20Right);
if (!anyInMid20)
{
continue;
}
if (maxY < yThreshold && width > requiredWidth)
{
possibleHeaderRow = line;
break;
}
}
if (possibleHeaderRow == null)
{
return null;
}
// Split the screen into a table under the words, one word per-column.
var foundStart = false;
var columnXs = new List<(int left, int right)>();
for (var i = 0; i < possibleHeaderRow.Count; i++)
{
var region = possibleHeaderRow[i];
// Only select the column starting 'Nickname', a word of length > 50 pixels
if (region.Bounds.Width < 50 && !foundStart)
{
continue;
}
else
{
foundStart = true;
}
// Skip the last 2 columns, they're probably garbage noise
if (possibleHeaderRow.Count > 5 && i >= possibleHeaderRow.Count - 2)
{
continue;
}
var endX = i == possibleHeaderRow.Count - 1
? region.Bounds.BottomRight.X + 10
: possibleHeaderRow[i + 1].Bounds.TopLeft.X - 1;
columnXs.Add((region.Bounds.TopLeft.X - 1, endX));
}
if (columnXs.Count == 0)
{
return null;
}
var rowCount = 0;
var occupied = 0;
var headerIndex = lines.IndexOf(possibleHeaderRow);
var linesWithNicknames = new List<List<WordRegion>>();
var firstCol = columnXs[0];
WordRegion? lastNickname = null;
for (var i = 0; i < lines.Count; i++)
{
if (i <= headerIndex)
{
continue;
}
var line = lines[i];
var nickFirstWord = line.FirstOrDefault(x => Math.Abs(x.Bounds.TopLeft.X - firstCol.left) < 10);
if (nickFirstWord == null)
{
continue;
}
if (lastNickname != null)
{
var gap = Math.Abs(lastNickname.Bounds.TopLeft.Y - nickFirstWord.Bounds.TopLeft.Y);
if (gap > 70)
{
break;
}
}
lastNickname = nickFirstWord;
linesWithNicknames.Add(line);
}
for (var i = 0; i < linesWithNicknames.Count; i++)
{
var line = linesWithNicknames[i];
for (var j = 0; j < columnXs.Count; j++)
{
var column = columnXs[j];
var words = line.Where(x =>
x.Bounds.TopLeft.X >= column.left && x.Bounds.BottomRight.X <= column.right)
.ToList();
// A single word with no chevron, either the word 'Closed' or a single word username.
if (j == 0 && words.Count == 1)
{
rowCount++;
occupied++;
break;
}
if (j == 0 && words.Count == 0)
{
break;
}
if (j == 0 && words.Count > 0)
{
rowCount++;
}
if (j > 0 && words.Count > 0)
{
// A 'word' in another column is team number, faction logo, color block, CPU indicator, etc.
occupied++;
break;
}
}
}
return new Occupancy(rowCount, occupied);
}
}