Skip to content

Commit

Permalink
fix(playermatches): does not recalc. playerStreak after fetching
Browse files Browse the repository at this point in the history
resolves #437
  • Loading branch information
tusharlock10 committed Feb 24, 2024
1 parent a44c085 commit 646e9ed
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
8 changes: 8 additions & 0 deletions lib/providers/matches.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class _MatchesNotifier extends ChangeNotifier {
bool isPlayerMatchesLoading = false;
String? combinedMatchesPlayerId;
List<data_classes.CombinedMatch>? combinedMatches;
int? playerStreak;

/// Common matches
bool isCommonMatchesLoading = false;
Expand All @@ -40,6 +41,7 @@ class _MatchesNotifier extends ChangeNotifier {
isPlayerMatchesLoading = true;
combinedMatches = null;
combinedMatchesPlayerId = null;
playerStreak = null;
utilities.postFrameCallback(notifyListeners);
}

Expand Down Expand Up @@ -84,6 +86,12 @@ class _MatchesNotifier extends ChangeNotifier {
);
}

// get the playerStreak
playerStreak = utilities.getPlayerStreak(
combinedMatches,
combinedMatchesPlayerId,
);

if (!forceUpdate) isPlayerMatchesLoading = false;

notifyListeners();
Expand Down
1 change: 1 addition & 0 deletions lib/screens/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Home extends HookConsumerWidget {
providers.auth.select((_) => _.user?.favouriteFriends),
);
final isGuest = ref.watch(providers.auth.select((_) => _.isGuest));

// State
final isRefreshing = useState(false);

Expand Down
2 changes: 1 addition & 1 deletion lib/screens/main/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Main extends HookConsumerWidget {
return Scaffold(
drawer: const AppDrawer(),
drawerEnableOpenDragGesture: constants.isMobile,
drawerEdgeDragWidth: MediaQuery.of(context).size.width / 2,
drawerEdgeDragWidth: MediaQuery.of(context).size.width / 3,
onDrawerChanged: onDrawerChanged,
body: MainPagesStack(selectedIndex: bottomTabIndex),
bottomNavigationBar: MainBottomTabs(
Expand Down
32 changes: 2 additions & 30 deletions lib/screens/player_detail/player_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import "package:paladinsedge/screens/index.dart" as screens;
import "package:paladinsedge/screens/player_detail/player_detail_header.dart";
import "package:paladinsedge/screens/player_detail/player_detail_matches.dart";
import "package:paladinsedge/screens/player_detail/player_detail_menu.dart";
import "package:paladinsedge/utilities/index.dart" as utilities;
import "package:paladinsedge/widgets/index.dart" as widgets;

class PlayerDetail extends HookConsumerWidget {
Expand Down Expand Up @@ -51,8 +50,8 @@ class PlayerDetail extends HookConsumerWidget {
final playerInferred = ref.watch(
providers.players.select((_) => _.playerInferred),
);
final combinedMatches = ref.watch(
providers.matches.select((_) => _.combinedMatches),
final playerStreak = ref.watch(
providers.matches.select((_) => _.playerStreak),
);

// Variables
Expand All @@ -65,33 +64,6 @@ class PlayerDetail extends HookConsumerWidget {
// State
final isRefreshing = useState(false);

// Hooks
final playerStreak = useMemoized(
() {
if (combinedMatches == null) return null;
if (combinedMatches.length < 5) return null;
if (combinedMatchesPlayerId == null) return null;

final playerId = combinedMatchesPlayerId;
final firstCombinedMatch = combinedMatches.first;
final isFirstWin = utilities.didPlayerWin(firstCombinedMatch, playerId);
int streak = 1;

for (final combinedMatch in combinedMatches.skip(1)) {
final isWin = utilities.didPlayerWin(combinedMatch, playerId);
if (isFirstWin == isWin) {
streak++;
} else {
break;
}
}
if (streak < 3) return null;

return isFirstWin ? streak : -streak;
},
[combinedMatches, combinedMatchesPlayerId],
);

// Methods
final getPlayerDetails = useCallback(
({bool forceUpdate = false}) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class _RecentMatchesBar extends HookConsumerWidget {
}

for (final combinedMatch in filteredCombinedMatches) {
if (combinedMatch.match.queue.contains("Training")) continue;
if (utilities.isTrainingMatch(combinedMatch.match)) continue;

final matchPlayer = utilities.getMatchPlayerFromPlayerId(
combinedMatch.matchPlayers,
Expand Down
32 changes: 32 additions & 0 deletions lib/utilities/match.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import "package:dartx/dartx.dart";
import "package:paladinsedge/data_classes/index.dart" as data_classes;
import "package:paladinsedge/models/index.dart" as models;

bool isTrainingMatch(models.Match match) {
return match.queue.contains("Training");
}

models.MatchPlayer? getMatchPlayerFromPlayerId(
List<models.MatchPlayer> matchPlayers,
String? playerId,
Expand All @@ -21,3 +25,31 @@ bool didPlayerWin(

return matchPlayer.team == combinedMatch.match.winningTeam;
}

int? getPlayerStreak(
List<data_classes.CombinedMatch>? combinedMatches,
String? combinedMatchesPlayerId,
) {
if (combinedMatches == null) return null;
if (combinedMatches.length < 5) return null;
if (combinedMatchesPlayerId == null) return null;

final playerId = combinedMatchesPlayerId;
final firstCombinedMatch = combinedMatches.first;
final isFirstWin = didPlayerWin(firstCombinedMatch, playerId);
int streak = 1;

for (final combinedMatch in combinedMatches.skip(1)) {
if (isTrainingMatch(combinedMatch.match)) continue;

final isWin = didPlayerWin(combinedMatch, playerId);
if (isFirstWin == isWin) {
streak++;
} else {
break;
}
}
if (streak < 3) return null;

return isFirstWin ? streak : -streak;
}
6 changes: 3 additions & 3 deletions lib/widgets/text_chip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class TextChip extends StatelessWidget {
this.width,
this.height,
this.onTap,
this.textSize = 10,
this.textSize = 8,
this.color = Colors.grey,
this.spacing = 0,
this.trailingIcon,
this.iconSize = 12,
this.trailingIconSize = 12,
this.iconSize = 10,
this.trailingIconSize = 10,
this.highlightText,
Key? key,
}) : super(key: key);
Expand Down

0 comments on commit 646e9ed

Please sign in to comment.