Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor score board #7

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/lib/api/ball_score/ball_score_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BallScoreModel with _$BallScoreModel {
required int ball_number,
required String bowler_id,
required String batsman_id,
required String non_striker_id,
int? runs_scored,
ExtrasType? extras_type,
int? extras_awarded,
Expand Down
24 changes: 23 additions & 1 deletion data/lib/api/ball_score/ball_score_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mixin _$BallScoreModel {
int get ball_number => throw _privateConstructorUsedError;
String get bowler_id => throw _privateConstructorUsedError;
String get batsman_id => throw _privateConstructorUsedError;
String get non_striker_id => throw _privateConstructorUsedError;
int? get runs_scored => throw _privateConstructorUsedError;
ExtrasType? get extras_type => throw _privateConstructorUsedError;
int? get extras_awarded => throw _privateConstructorUsedError;
Expand Down Expand Up @@ -55,6 +56,7 @@ abstract class $BallScoreModelCopyWith<$Res> {
int ball_number,
String bowler_id,
String batsman_id,
String non_striker_id,
int? runs_scored,
ExtrasType? extras_type,
int? extras_awarded,
Expand Down Expand Up @@ -85,6 +87,7 @@ class _$BallScoreModelCopyWithImpl<$Res, $Val extends BallScoreModel>
Object? ball_number = null,
Object? bowler_id = null,
Object? batsman_id = null,
Object? non_striker_id = null,
Object? runs_scored = freezed,
Object? extras_type = freezed,
Object? extras_awarded = freezed,
Expand Down Expand Up @@ -120,6 +123,10 @@ class _$BallScoreModelCopyWithImpl<$Res, $Val extends BallScoreModel>
? _value.batsman_id
: batsman_id // ignore: cast_nullable_to_non_nullable
as String,
non_striker_id: null == non_striker_id
? _value.non_striker_id
: non_striker_id // ignore: cast_nullable_to_non_nullable
as String,
runs_scored: freezed == runs_scored
? _value.runs_scored
: runs_scored // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -175,6 +182,7 @@ abstract class _$$BallScoreModelImplCopyWith<$Res>
int ball_number,
String bowler_id,
String batsman_id,
String non_striker_id,
int? runs_scored,
ExtrasType? extras_type,
int? extras_awarded,
Expand Down Expand Up @@ -203,6 +211,7 @@ class __$$BallScoreModelImplCopyWithImpl<$Res>
Object? ball_number = null,
Object? bowler_id = null,
Object? batsman_id = null,
Object? non_striker_id = null,
Object? runs_scored = freezed,
Object? extras_type = freezed,
Object? extras_awarded = freezed,
Expand Down Expand Up @@ -238,6 +247,10 @@ class __$$BallScoreModelImplCopyWithImpl<$Res>
? _value.batsman_id
: batsman_id // ignore: cast_nullable_to_non_nullable
as String,
non_striker_id: null == non_striker_id
? _value.non_striker_id
: non_striker_id // ignore: cast_nullable_to_non_nullable
as String,
runs_scored: freezed == runs_scored
? _value.runs_scored
: runs_scored // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -288,6 +301,7 @@ class _$BallScoreModelImpl implements _BallScoreModel {
required this.ball_number,
required this.bowler_id,
required this.batsman_id,
required this.non_striker_id,
this.runs_scored,
this.extras_type,
this.extras_awarded,
Expand All @@ -314,6 +328,8 @@ class _$BallScoreModelImpl implements _BallScoreModel {
@override
final String batsman_id;
@override
final String non_striker_id;
@override
final int? runs_scored;
@override
final ExtrasType? extras_type;
Expand All @@ -334,7 +350,7 @@ class _$BallScoreModelImpl implements _BallScoreModel {

@override
String toString() {
return 'BallScoreModel(id: $id, inning_id: $inning_id, over_number: $over_number, ball_number: $ball_number, bowler_id: $bowler_id, batsman_id: $batsman_id, runs_scored: $runs_scored, extras_type: $extras_type, extras_awarded: $extras_awarded, wicket_type: $wicket_type, player_out_id: $player_out_id, wicket_taker_id: $wicket_taker_id, is_four: $is_four, is_six: $is_six, time: $time)';
return 'BallScoreModel(id: $id, inning_id: $inning_id, over_number: $over_number, ball_number: $ball_number, bowler_id: $bowler_id, batsman_id: $batsman_id, non_striker_id: $non_striker_id, runs_scored: $runs_scored, extras_type: $extras_type, extras_awarded: $extras_awarded, wicket_type: $wicket_type, player_out_id: $player_out_id, wicket_taker_id: $wicket_taker_id, is_four: $is_four, is_six: $is_six, time: $time)';
}

@override
Expand All @@ -353,6 +369,8 @@ class _$BallScoreModelImpl implements _BallScoreModel {
other.bowler_id == bowler_id) &&
(identical(other.batsman_id, batsman_id) ||
other.batsman_id == batsman_id) &&
(identical(other.non_striker_id, non_striker_id) ||
other.non_striker_id == non_striker_id) &&
(identical(other.runs_scored, runs_scored) ||
other.runs_scored == runs_scored) &&
(identical(other.extras_type, extras_type) ||
Expand Down Expand Up @@ -380,6 +398,7 @@ class _$BallScoreModelImpl implements _BallScoreModel {
ball_number,
bowler_id,
batsman_id,
non_striker_id,
runs_scored,
extras_type,
extras_awarded,
Expand Down Expand Up @@ -413,6 +432,7 @@ abstract class _BallScoreModel implements BallScoreModel {
required final int ball_number,
required final String bowler_id,
required final String batsman_id,
required final String non_striker_id,
final int? runs_scored,
final ExtrasType? extras_type,
final int? extras_awarded,
Expand All @@ -439,6 +459,8 @@ abstract class _BallScoreModel implements BallScoreModel {
@override
String get batsman_id;
@override
String get non_striker_id;
@override
int? get runs_scored;
@override
ExtrasType? get extras_type;
Expand Down
2 changes: 2 additions & 0 deletions data/lib/api/ball_score/ball_score_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions data/lib/service/team/team_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class TeamService {
TeamService(this._currentUserId, this._firestore, this._userService);

Future<String> updateTeam(AddTeamRequestModel team) async {
DocumentReference teamRef =
_firestore.collection(_collectionName).doc(team.id);
DocumentReference teamRef = _firestore.collection(_collectionName).doc(team.id);
WriteBatch batch = _firestore.batch();

batch.set(teamRef, team.toJson(), SetOptions(merge: true));
Expand Down Expand Up @@ -129,7 +128,8 @@ class TeamService {
DocumentReference teamRef =
_firestore.collection(_collectionName).doc(teamId);

await teamRef.update({'players': FieldValue.arrayUnion(players)});
await teamRef.set(
{'players': FieldValue.arrayUnion(players)}, SetOptions(merge: true));
}

Future<void> removePlayersFromTeam(
Expand Down
24 changes: 18 additions & 6 deletions khelo/assets/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"common_next_title": "Next",
"common_yes_title": "Yes",
"common_no_title": "No",
"common_runs_dot_title": " Runs.",
"common_wickets_dot_title": " Wickets.",
"common_wicket_taken_title": " Wicket taken",
"common_not_specified_title": "Not Specified",
"common_obscure_phone_number_text": "{countryCode} ***** ***{lastDigits}",
Expand Down Expand Up @@ -73,6 +75,8 @@
"edit_profile_save_title": "SAVE",

"home_screen_title": "Home",
"home_screen_no_matches_title": "No matches are running currently",
"home_screen_no_matches_description_text": "Running matches will be shown here, Stay tuned for some intense matches.",

"image_picker_choose_option_title": "Choose an option.",
"image_picker_crop_image_title": "Crop Image",
Expand Down Expand Up @@ -258,6 +262,7 @@
"score_board_match_complete_title": "Match Complete",
"score_board_end_match_title": "End Match",
"score_board_start_next_inning_title": "Start Next Inning",
"score_board_end_inning_title": "End Inning",
"score_board_current_team_title": "Current Team",
"score_board_team_title": "Team",
"score_board_r_title": "R",
Expand All @@ -266,7 +271,16 @@
"score_board_won_by_title": " Won By ",
"score_board_select_player_title": "Select Player",
"score_board_select_title": "select",
"score_board_choose_batsman_title": "choose Batsman",
"score_board_choose_batsman_title": "choose Batsman at position {pos}",
"@score_board_choose_bowler_for_over_title": {
"description": "choose Batsman at position {pos}",
"placeholders": {
"pos": {
"type": "int"
}
}
},
"score_board_choose_opening_batsmen_title": "choose opening Batsmen",
"score_board_choose_bowler_for_over_title": "choose Bowler for over {over}",
"@score_board_choose_bowler_for_over_title": {
"description": "choose Bowler for over {over}",
Expand All @@ -280,23 +294,21 @@
"score_board_injured_tag_title": "Injured",
"score_board_who_on_strike_title": "Who's on Strike?",
"score_board_who_got_out_title": "Who got out",
"score_board_runs_dot_title": " Runs.",
"score_board_wickets_dot_title": " Wickets.",
"score_board_end_of_over_title": "End Of Over ",
"score_board_start_next_over_title": "Start Next Over",
"score_board_batsman_title": "batsMan",
"score_board_bowler_title": "bowler",
"score_board_change_striker_title": "Change striker",
"score_board_change_striker_title": "Change Striker",
"score_board_add_substitute_title": "Add substitute",
"score_board_pause_scoring_title": "Pause scoring",
"score_board_by_title": " by ",
"score_board_can_undo_till_running_over_title": "Undo is only allowed till running over",
"score_board_undo_last_ball_title": "Undo Last Ball",
"score_board_undo_last_ball_description_text": "are you sure you want to undo last ball?",
"score_board_pause_scoring_title": "Pause Scoring",
"score_board_penalty_run_title": "Penalty Run",
"score_board_continue_with_injured_player_title": "Continue With Injured Player",
"score_board_pause_title": "Pause",
"score_board_power_play_title": "Power-Play ",
"score_board_more_options_title": "More Options",
"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
37 changes: 37 additions & 0 deletions khelo/lib/components/won_by_message_text.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flutter/cupertino.dart';
import 'package:khelo/domain/extensions/context_extensions.dart';
import 'package:style/extensions/context_extensions.dart';
import 'package:style/text/app_text_style.dart';

class WonByMessageText extends StatelessWidget {
final String teamName;
final int difference;
final String trailingText;

const WonByMessageText({
super.key,
required this.teamName,
required this.difference,
required this.trailingText,
});

@override
Widget build(BuildContext context) {
return Text.rich(TextSpan(
text: teamName,
style:
AppTextStyle.header4.copyWith(color: context.colorScheme.primary),
children: [
TextSpan(
text: context.l10n.score_board_won_by_title,
style: AppTextStyle.subtitle2
.copyWith(color: context.colorScheme.textSecondary)),
TextSpan(
text: "$difference",
),
TextSpan(
text: trailingText,
),
]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:data/api/ball_score/ball_score_model.dart';

extension BallScoreModelBoolean on BallScoreModel? {
bool? isLegalDelivery() {
if (this == null) {
return null;
}
return this!.extras_type != ExtrasType.penaltyRun &&
this!.extras_type != ExtrasType.noBall &&
this!.extras_type != ExtrasType.wide &&
this!.wicket_type != WicketType.timedOut &&
this!.wicket_type != WicketType.retired &&
this!.wicket_type != WicketType.retiredHurt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:data/api/match/match_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:khelo/domain/extensions/context_extensions.dart';

extension MatchModelString on MatchModel {
({String teamName, int difference, String wonByText})? getWinnerSummary(
BuildContext context) {
if (match_status != MatchStatus.finish) {
return null;
}

final firstTeam = toss_decision == TossDecision.bat
? teams.firstWhere((element) => element.team.id == toss_winner_id)
: teams.firstWhere((element) => element.team.id != toss_winner_id);
final secondTeam =
teams.firstWhere((element) => element.team.id != firstTeam.team.id);

if (firstTeam.run! > secondTeam.run!) {
// first batting team won
final teamName = firstTeam.team.name;

final runDifference = firstTeam.run! - secondTeam.run!;

return (
teamName: teamName,
difference: runDifference,
wonByText: context.l10n.common_runs_dot_title,
);
} else {
// second batting team won
final teamName = secondTeam.team.name;

final wicketDifference =
secondTeam.squad.length - (firstTeam.wicket ?? 0);

return (
teamName: teamName,
difference: wicketDifference,
wonByText: context.l10n.common_wickets_dot_title,
);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor to improve readability and reduce complexity.

The getWinnerSummary method could be refactored to improve readability and reduce complexity. Consider breaking down the logic into smaller, more focused methods (e.g., calculateRunDifference, calculateWicketDifference). This will make the code easier to understand and maintain.

}
Loading
Loading