Skip to content

Commit

Permalink
Fix the bug in finding ownerRecord in the leaderboard_entity
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Sep 20, 2024
1 parent 13e4587 commit 81aa1e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 20 additions & 9 deletions lib/domain/entities/leaderboard_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@ import 'package:nakama/nakama.dart';
class LeaderboardEntity with EquatableMixin {
final LeaderboardRecordList _recordList;
final Map<String, User> _userProfiles;
final String _currentUserId;

LeaderboardRecord? get ownerRecord => _recordList.records.firstOrNull;
LeaderboardRecord? get ownerRecord {
if (_recordList.records == null) {
return null;
}
for (final record in _recordList.records!) {
if (record.ownerId == _currentUserId) {
return record;
}
}
return null;
}

int get length => _recordList.records.length;
int get length => _recordList.records!.length;

(LeaderboardRecord record, String name) operator [](int index) {
final record = _recordList.records[index];
final record = _recordList.records![index];
final user = _userProfiles[record.ownerId!]!;
return (record, user.showingName);
}

LeaderboardEntity({
required LeaderboardRecordList recordList,
required Map<String, User> userProfiles,
}) : _userProfiles = userProfiles,
_recordList = recordList,
assert(recordList.records.length == userProfiles.length);
LeaderboardEntity(
this._recordList,
this._userProfiles,
this._currentUserId,
) : assert(_recordList.records!.length == _userProfiles.length);

@override
List<Object?> get props => [
_recordList,
_userProfiles,
_currentUserId,
];
}
6 changes: 4 additions & 2 deletions lib/domain/repositories/game_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ class GameRepository {
final usersMap = Map.fromEntries(
users.map((user) => MapEntry(user.id, user)),
);
final currentUserId = await getCurrentUserId();
return LeaderboardEntity(
recordList: recordList,
userProfiles: usersMap,
recordList,
usersMap,
currentUserId,
);
}

Expand Down

0 comments on commit 81aa1e3

Please sign in to comment.