Skip to content

Commit

Permalink
Record team stats
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-mayank committed Nov 29, 2024
1 parent 1bf099e commit b8e7ee3
Show file tree
Hide file tree
Showing 15 changed files with 805 additions and 692 deletions.
165 changes: 54 additions & 111 deletions data/lib/api/match/match_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -346,128 +346,71 @@ enum MatchGroup {
const MatchGroup(this.value);
}

@freezed
class TeamMatchStatus with _$TeamMatchStatus {
const factory TeamMatchStatus({
@Default(0) int win,
@Default(0) int tie,
@Default(0) int lost,
}) = _TeamMatchStatus;
}

@freezed
class TeamStat with _$TeamStat {
const factory TeamStat({
@Default(0) int played,
@Default(TeamMatchStatus()) TeamMatchStatus status,
@Default(0) int runs,
@Default(0) int wickets,
@Default(0.0) double batting_average,
@Default(0.0) double bowling_average,
@Default(0) int highest_runs,
@Default(0) int lowest_runs,
@Default(0.0) double run_rate,
}) = _TeamStat;
}

extension TeamStatExtension on List<MatchModel> {
TeamStat teamStat(String teamId) {
if (isEmpty) return const TeamStat();

var runs = 0;
var wickets = 0;
var battingAverageTotal = 0.0;
var bowlingAverageTotal = 0.0;
var highestRuns = 0;
var lowestRuns = double.maxFinite.toInt();
extension SingleMatchTeamStatExtension on MatchModel {
TeamStat updateTeamStat(String teamId, TeamStat currentStat) {
final team = _getTeam(teamId);
final opponentTeam = _getOpponentTeam(teamId);

for (final match in this) {
final team = _getTeam(match, teamId);
final opponentTeam = _getOpponentTeam(match, teamId);
final matchRuns = team.run;
final matchWickets = team.wicket;
final battingAverage =
opponentTeam.wicket > 0 ? matchRuns / opponentTeam.wicket : 0.0;
final bowlingAverage =
matchWickets > 0 ? opponentTeam.run / matchWickets : 0.0;

runs += team.run;
wickets += team.wicket;
final result = _matchResult(teamId, team.run, opponentTeam.run);

battingAverageTotal +=
opponentTeam.wicket > 0 ? team.run / opponentTeam.wicket : 0;
if (team.wicket > 0) {
bowlingAverageTotal += opponentTeam.run / team.wicket;
}
final totalOvers = currentStat.run_rate > 0
? _computeOvers(currentStat.runs, currentStat.run_rate)
: 0.0;

if (team.run > highestRuns) highestRuns = team.run;
if (team.run < lowestRuns) lowestRuns = team.run;
}

final bowlingAverage = length > 0.0 ? bowlingAverageTotal / length : 0.0;
final matchOvers = team.over;
final updatedOvers = totalOvers + matchOvers;

return TeamStat(
played: length,
status: _teamMatchStatus(teamId),
run_rate: _runRate(teamId, runs),
runs: runs,
wickets: wickets,
batting_average: battingAverageTotal,
bowling_average: bowlingAverage,
highest_runs: highestRuns,
lowest_runs: lowestRuns == double.maxFinite.toInt() ? 0 : lowestRuns,
);
}

TeamMatchStatus _teamMatchStatus(String teamId) {
return fold<TeamMatchStatus>(
const TeamMatchStatus(),
(status, match) {
final firstTeam = match.teams.firstWhere(
(team) =>
team.team.id ==
(match.toss_decision == TossDecision.bat
? match.toss_winner_id
: match.teams
.firstWhere(
(team) => team.team.id != match.toss_winner_id,
)
.team
.id),
);

final secondTeam =
match.teams.firstWhere((team) => team.team.id != firstTeam.team.id);

if (firstTeam.run == secondTeam.run) {
return TeamMatchStatus(
win: status.win,
lost: status.lost,
tie: status.tie + 1,
);
} else if ((firstTeam.run > secondTeam.run &&
firstTeam.team.id == teamId) ||
(firstTeam.run < secondTeam.run && secondTeam.team.id == teamId)) {
return TeamMatchStatus(
win: status.win + 1,
lost: status.lost,
tie: status.tie,
);
} else {
return TeamMatchStatus(
win: status.win,
lost: status.lost + 1,
tie: status.tie,
);
}
},
played: currentStat.played + 1,
status: TeamMatchStatus(
win: currentStat.status.win + result.win,
lost: currentStat.status.lost + result.lost,
tie: currentStat.status.tie + result.tie,
),
run_rate: updatedOvers > 0
? (currentStat.runs + matchRuns) / updatedOvers
: 0.0,
runs: currentStat.runs + matchRuns,
wickets: currentStat.wickets + matchWickets,
batting_average: currentStat.batting_average + battingAverage,
bowling_average: currentStat.played > 0
? (currentStat.bowling_average + bowlingAverage) / currentStat.played
: 0.0,
highest_runs: matchRuns > currentStat.highest_runs
? matchRuns
: currentStat.highest_runs,
lowest_runs: currentStat.lowest_runs == 0
? matchRuns
: matchRuns < currentStat.lowest_runs
? matchRuns
: currentStat.lowest_runs,
);
}

MatchTeamModel _getTeam(MatchModel match, String teamId) =>
match.teams.firstWhere((team) => team.team.id == teamId);
MatchTeamModel _getTeam(String teamId) =>
teams.firstWhere((team) => team.team.id == teamId);

MatchTeamModel _getOpponentTeam(MatchModel match, String teamId) =>
match.teams.firstWhere((team) => team.team.id != teamId);
MatchTeamModel _getOpponentTeam(String teamId) =>
teams.firstWhere((team) => team.team.id != teamId);

double _runRate(String teamId, int runs) {
final totalOvers = map((match) => _getTeam(match, teamId))
.fold<double>(0.0, (total, team) => total + team.over);
TeamMatchStatus _matchResult(String teamId, int teamRun, int opponentRun) {
if (teamRun == opponentRun) {
return TeamMatchStatus(tie: 1);
} else if (teamRun > opponentRun) {
return TeamMatchStatus(win: 1);
} else {
return TeamMatchStatus(win: 0);
}
}

return totalOvers > 0 ? runs / totalOvers : 0;
double _computeOvers(int runs, double runRate) {
return runRate > 0 ? runs / runRate : 0.0;
}
}
Loading

0 comments on commit b8e7ee3

Please sign in to comment.