Skip to content

Commit

Permalink
user stats
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed Mar 21, 2024
1 parent 896c040 commit 84164e5
Show file tree
Hide file tree
Showing 25 changed files with 2,507 additions and 157 deletions.
27 changes: 26 additions & 1 deletion data/lib/service/ball_score/ball_score_service.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:data/api/ball_score/ball_score_model.dart';
import 'package:data/storage/app_preferences.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final ballScoreServiceProvider = Provider((ref) {
final service = BallScoreService(
FirebaseFirestore.instance,
ref.read(currentUserPod)?.id,
);

ref.listen(currentUserPod, (_, next) => service._currentUserId = next?.id);
return service;
});

class BallScoreService {
final FirebaseFirestore _firestore;
String? _currentUserId;

final String _collectionName = 'ball_scores';

BallScoreService(this._firestore);
BallScoreService(this._firestore, this._currentUserId);

Future<String> updateBallScore(BallScoreModel score) async {
DocumentReference scoreRef =
Expand Down Expand Up @@ -44,6 +50,25 @@ class BallScoreService {
}).toList();
}

Future<List<BallScoreModel>> getCurrentUserRelatedBalls() async {
if (_currentUserId == null) {
return [];
}
final QuerySnapshot<Map<String, dynamic>> snapshot = await _firestore
.collection(_collectionName)
.where(Filter.or(
Filter("bowler_id", isEqualTo: _currentUserId),
Filter("batsman_id", isEqualTo: _currentUserId),
Filter("wicket_taker_id", isEqualTo: _currentUserId),
Filter("player_out_id", isEqualTo: _currentUserId)))
.get();

return snapshot.docs.map((doc) {
final data = doc.data();
return BallScoreModel.fromJson(data).copyWith(id: doc.id);
}).toList();
}

Future<void> deleteBall(String ballId) async {
await _firestore.collection(_collectionName).doc(ballId).delete();
}
Expand Down
163 changes: 86 additions & 77 deletions data/lib/service/match/match_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,34 @@ import 'package:data/service/team/team_service.dart';
import 'package:data/service/user/user_service.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../storage/app_preferences.dart';

final matchServiceProvider = Provider((ref) {
final service = MatchService(
FirebaseFirestore.instance,
ref.read(teamServiceProvider),
ref.read(userServiceProvider),
ref.read(currentUserPod)?.id,
);

ref.listen(currentUserPod, (_, next) => service._currentUserId = next?.id);
return service;
});

class MatchService {
final FirebaseFirestore _firestore;
final TeamService _teamService;
final UserService _userService;
String? _currentUserId;

final String _collectionName = 'matches';

MatchService(this._firestore, this._teamService, this._userService);
MatchService(
this._firestore,
this._teamService,
this._userService,
this._currentUserId,
);

Future<List<MatchModel>> getMatches() async {
CollectionReference matchCollection =
Expand Down Expand Up @@ -101,6 +113,79 @@ class MatchService {
return matchModel;
}

Future<List<MatchModel>> getCurrentUserMatches() async {
if (_currentUserId == null) {
return [];
}

QuerySnapshot snapshot = await _firestore.collection(_collectionName).get();
List<MatchModel> matches = [];

for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData = mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
if (match.teams
.map((e) => e.squad.map((e) => e.id).contains(_currentUserId))
.contains(true) &&
match.match_status == MatchStatus.finish) {
List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}

return matches;
}

Future<List<MatchModel>> getMatchesByTeamId(String teamId) async {
CollectionReference matchCollection =
_firestore.collection(_collectionName);

QuerySnapshot mainCollectionSnapshot = await matchCollection.get();

List<MatchModel> matches = [];

for (QueryDocumentSnapshot mainDoc in mainCollectionSnapshot.docs) {
Map<String, dynamic> mainDocData = mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
if (match.teams.map((e) => e.team_id).contains(teamId)) {
List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}

return matches;
}

Future<String> updateMatch(AddEditMatchRequest match) async {
DocumentReference matchRef =
_firestore.collection(_collectionName).doc(match.id);
Expand Down Expand Up @@ -233,82 +318,6 @@ class MatchService {
});
}

Future<List<MatchModel>> getMatchesByTeamId(String teamId) async {
CollectionReference matchCollection =
_firestore.collection(_collectionName);

QuerySnapshot mainCollectionSnapshotOne =
await matchCollection.where("teams.0.team_id", isEqualTo: teamId).get();

QuerySnapshot mainCollectionSnapshotTwo =
await matchCollection.where("teams.1.team_id", isEqualTo: teamId).get();

final combinedDocs = mainCollectionSnapshotOne.docs;
combinedDocs.addAll(mainCollectionSnapshotTwo.docs);

List<MatchModel> matches = [];

for (QueryDocumentSnapshot mainDoc in combinedDocs) {
Map<String, dynamic> mainDocData = mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);

List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}

return matches;
}

Future<List<MatchModel>> getMatchesByTeamIdV1(String teamId) async {
CollectionReference matchCollection =
_firestore.collection(_collectionName);

QuerySnapshot mainCollectionSnapshot = await matchCollection.get();

List<MatchModel> matches = [];

for (QueryDocumentSnapshot mainDoc in mainCollectionSnapshot.docs) {
Map<String, dynamic> mainDocData = mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
if (match.teams.map((e) => e.team_id).contains(teamId)) {
List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}

return matches;
}

Future<void> deleteMatch(String matchId) async {
await _firestore.collection(_collectionName).doc(matchId).delete();
}
Expand Down
70 changes: 70 additions & 0 deletions khelo/assets/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@
"score_board_pause_scoring_title" : "Pause Scoring",
"score_board_penalty_run_title" : "Penalty Run",
"score_board_pause_title" : "Pause",
"score_board_power_play_title" : "Power-Play ",
"score_board_pause_scoring_description_text" : "are you sure you want to pause scoring?",
"score_board_need_run_text": "Need {run} in {ball}",
"@score_board_need_run_text": {
Expand Down Expand Up @@ -315,6 +316,75 @@
"team_detail_match_played_title": "\nMatch played",
"team_detail_matches_played_title": "\nMatches played",

"match_status_yet_to_start_title" : "Yet to start",
"match_status_running_title" : "Running",
"match_status_finish_title" : "Finish",

"wicket_type_bowled_title" : "Bowled",
"wicket_type_caught_title" : "Caught",
"wicket_type_caught_behind_title" : "Caught Behind",
"wicket_type_caught_bowled_title" : "Caught And Bowled",
"wicket_type_leg_before_wicket_title" : "Leg Before Wicket",
"wicket_type_stumped_title" : "Stumped",
"wicket_type_run_out_title" : "Run Out",
"wicket_type_hit_wicket_title" : "Hit Wicket",
"wicket_type_hit_ball_twice_title" : "Hit Ball Twice",
"wicket_type_handled_ball_title" : "Handled Ball",
"wicket_type_obstructing_field_title" : "Obstructing the Field",
"wicket_type_timed_out_title" : "Timed Out",
"wicket_type_retired_title" : "Retired",
"wicket_type_retired_hurt_title" : "Retired Hurt",

"my_stat_screen_title": "My Stats",
"my_stat_stats_tab_title": "Stats",
"my_stat_matches_tab_title": "Matches",
"my_stat_stats_batting_statics_title": "Batting Statistics",
"my_stat_stats_run_scored_title": "Run scored",
"my_stat_stats_bowling_title": "Bowling",
"my_stat_stats_batting_title": "Batting",
"my_stat_stats_average_title": "average",
"my_stat_stats_strike_rate_title": "\nStrike rate",
"my_stat_stats_ball_faced_title": " Ball faced",
"my_stat_stats_bowling_statics_title": "Bowling Statistics",
"my_stat_stats_wicket_taken_title": "Wicket taken",
"my_stat_stats_economy_rate_title": " Economy rate",
"my_stat_stats_fielding_statics_title": "Fielding Statistics",
"my_stat_stats_catches_title": "Catches",
"my_stat_stats_run_out_title": " Run out",
"my_stat_stats_stumping_title": " Stumping",

"match_stat_screen_title" : "Match Stat",
"match_stat_wicket_taken_title" : " Wicket taken",
"match_stat_in_over_text" : " (in {count} Over)",
"@match_stat_in_over_text": {
"description": " (in {count} Over)",
"placeholders": {
"count": {
"type": "String"
}
}
},
"match_stat_fours_title" : "\nFours",
"match_stat_sixes_title" : "\nSixes",
"match_stat_played_squad_title" : "Played Squad",
"match_stat_count_player_text" : "({count} player)",
"@match_stat_count_player_text": {
"description": "({count} player)",
"placeholders": {
"count": {
"type": "int"
}
}
},
"match_stat_count_wickets_text" : "{count, plural, =0{{count} Wickets} =1{{count} Wicket} other{{count} Wickets}}",
"@match_stat_count_wickets_text": {
"placeholders": {
"count": {}
}
},
"match_stat_bat_avg_title": "\nBat-Avg",
"match_stat_extra_awarded_title": " Extra awarded",

"my_game_teams_tab_title": "Teams",
"my_game_matches_tab_title": "Matches"
}
26 changes: 26 additions & 0 deletions khelo/lib/components/match_status_tag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:data/api/match/match_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:khelo/domain/extensions/enum_extensions.dart';
import 'package:style/extensions/context_extensions.dart';
import 'package:style/text/app_text_style.dart';

class MatchStatusTag extends StatelessWidget {
final MatchStatus status;
const MatchStatusTag({super.key, required this.status,});

@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
decoration: BoxDecoration(
color: status.getColor(context).withOpacity(0.5),
borderRadius: BorderRadius.circular(16),
),
child: Text(
status.getString(context),
style:
AppTextStyle.body2.copyWith(color: context.colorScheme.textPrimary),
),
);
}
}
Loading

0 comments on commit 84164e5

Please sign in to comment.