Skip to content

Commit c58bf1b

Browse files
committed
Split CalculateAll into smaller specific methods
1 parent c218f1e commit c58bf1b

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

SportsAnalyzer/Controllers/Common.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,8 @@ public static void CalcStats(this Statistics statistics,
162162
{
163163
statistics.SetMatches(xmlSocDB.LeagueMatches.ToList());
164164
statistics.SetRoundsRange(startRound, endRound);
165-
statistics.CalculateAll();
166-
}
167-
168-
public static void CalcStatsForRounds(this Statistics stats,
169-
XmlSocDB xmlSocDB,
170-
IEnumerable<int> selectedRounds)
171-
{
172-
stats.SetMatches(xmlSocDB.LeagueMatches.ToList());
173-
stats.SetRounds(selectedRounds);
174-
stats.CalculateAll();
165+
statistics.CalculateBasicStats();
166+
statistics.CalculateGoalsInIntervals();
175167
}
176168
}
177169
}

SportsAnalyzer/Controllers/StatsController.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public IHttpActionResult GoalsIntervals(StatsRequestModel statsRequest)
4646
var stats = new Statistics(statsRequest.SeasonYear,
4747
statsRequest.LeagueName,
4848
statsRequest.TeamName);
49-
stats.CalcStatsForRounds(db, statsRequest.Rounds.ToList());
49+
stats.SetMatches(db.LeagueMatches.ToList());
50+
stats.SetRounds(statsRequest.Rounds.ToList());
51+
stats.CalculateGoalsInIntervals();
5052

5153
return Ok(stats.GoalsInIntervalsPercent);
5254
}
@@ -64,7 +66,9 @@ public IHttpActionResult MatchGoals(StatsRequestModel statsRequest)
6466
var stats = new Statistics(statsRequest.SeasonYear,
6567
statsRequest.LeagueName,
6668
statsRequest.TeamName);
67-
stats.CalcStatsForRounds(db, statsRequest.Rounds.ToList());
69+
stats.SetMatches(db.LeagueMatches.ToList());
70+
stats.SetRounds(statsRequest.Rounds.ToList());
71+
stats.CalculateMatchGoals();
6872

6973
return Ok(stats.MatchGoalsPct);
7074
}

SportsAnalyzer/Models/Statistics.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static IDictionary<string, Dictionary<int, int>> TablePositions
131131

132132
/* Methods */
133133

134-
public void CalculateAll()
134+
public void CalculateBasicStats()
135135
{
136136
if (SelectedMatches == null || SelectedMatches.Count == 0)
137137
{
@@ -144,6 +144,14 @@ public void CalculateAll()
144144
GoalsAvg = Round(GoalsSum / MatchesNumber, 2);
145145
GoalsAvgHome = Round(SelectedMatches.Average((match) => match.HomeGoals ?? 0), 2);
146146
GoalsAvgAway = Round(SelectedMatches.Average((match) => match.AwayGoals ?? 0), 2);
147+
}
148+
149+
public void CalculateGoalsInIntervals()
150+
{
151+
if (SelectedMatches == null || SelectedMatches.Count == 0)
152+
{
153+
return;
154+
}
147155

148156
var regexGoalTime = new Regex("\\d{1,}");
149157
double scoredGoalsSum = 0;
@@ -179,26 +187,37 @@ public void CalculateAll()
179187
}
180188
}
181189
}
190+
}
191+
192+
for (int i = 0; i < NumberOfMatchIntervals; i++)
193+
{
194+
GoalsInIntervalsPercent[i] = Round((GoalsInIntervals[i] / scoredGoalsSum) * 100, 2);
195+
}
196+
}
182197

198+
public void CalculateMatchGoals()
199+
{
200+
if (SelectedMatches == null || SelectedMatches.Count == 0)
201+
{
202+
return;
203+
}
204+
205+
foreach (var match in SelectedMatches)
206+
{
183207
int matchGoals = match.HomeGoals ?? 0;
184208
matchGoals += match.AwayGoals ?? 0;
185209
if (!MatchGoals.ContainsKey(matchGoals))
186210
MatchGoals.Add(matchGoals, 0);
187211
MatchGoals[matchGoals]++;
188212
}
189213

190-
for (int i = 0; i < NumberOfMatchIntervals; i++)
191-
{
192-
GoalsInIntervalsPercent[i] = Round((GoalsInIntervals[i] / scoredGoalsSum) * 100, 2);
193-
}
194-
195214
int numberOfGoals = 0;
196215
double numberOfMatches = 0;
197216
foreach (var item in MatchGoals)
198217
{
199218
numberOfGoals = item.Key;
200219
numberOfMatches = item.Value;
201-
MatchGoalsPct[numberOfGoals] = Round((numberOfMatches / MatchesNumber) * 100, 2);
220+
MatchGoalsPct[numberOfGoals] = Round((numberOfMatches / SelectedMatches.Count) * 100, 2);
202221
}
203222
}
204223

0 commit comments

Comments
 (0)