-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add TrueSkill leaderboard tabs #2
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
Changes from all commits
a158669
118e18a
478f217
a866854
27e1e6a
1aa7eaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import 'package:roboscout_iq/src/models/team_model.dart'; | ||
| import 'package:roboscout_iq/src/services/api_client.dart'; | ||
| import 'package:roboscout_iq/src/services/local_db_service.dart'; | ||
|
|
||
| class LeaderboardRepository { | ||
| final ApiClient _apiClient; | ||
| final LocalDbService _localDb; | ||
|
|
||
| LeaderboardRepository(this._apiClient, this._localDb); | ||
|
|
||
| Future<List<Map<String, dynamic>>> getGlobalSkills(String gradeLevel, | ||
| {bool forceRefresh = false}) async { | ||
| final cacheKey = 'skills_$gradeLevel'; | ||
| final box = _localDb.leaderboardBox; | ||
|
|
||
| if (!forceRefresh && box.containsKey(cacheKey)) { | ||
| try { | ||
| final cachedData = box.get(cacheKey); | ||
| if (cachedData is List) { | ||
| return cachedData | ||
| .map((e) => Map<String, dynamic>.from(e as Map)) | ||
| .toList(); | ||
| } | ||
| } catch (e) { | ||
| print('Error reading skills cache: $e'); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| final data = await _apiClient.getGlobalSkills(gradeLevel: gradeLevel); | ||
| await box.put(cacheKey, data); | ||
| return data; | ||
| } catch (e) { | ||
| if (box.containsKey(cacheKey)) { | ||
| final cachedData = box.get(cacheKey); | ||
| if (cachedData is List) { | ||
| return cachedData | ||
| .map((e) => Map<String, dynamic>.from(e as Map)) | ||
| .toList(); | ||
| } | ||
| } | ||
| rethrow; | ||
| } | ||
| } | ||
|
|
||
| Future<List<Team>> getGlobalTrueSkillRankings( | ||
| {bool forceRefresh = false}) async { | ||
| const cacheKey = 'trueskill_global'; | ||
| final box = _localDb.leaderboardBox; | ||
|
|
||
| if (!forceRefresh && box.containsKey(cacheKey)) { | ||
| try { | ||
| final cachedData = box.get(cacheKey); | ||
| if (cachedData is List) { | ||
| return _deserializeTeams(cachedData); | ||
| } | ||
| } catch (e) { | ||
| print('Error reading trueskill cache: $e'); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| final teams = await _apiClient.getGlobalTrueSkillRankings(); | ||
| final jsonList = teams.map((t) => t.toJson()).toList(); | ||
| await box.put(cacheKey, jsonList); | ||
| return teams; | ||
| } catch (e) { | ||
| if (box.containsKey(cacheKey)) { | ||
| final cachedData = box.get(cacheKey) as List; | ||
| return _deserializeTeams(cachedData); | ||
| } | ||
| rethrow; | ||
| } | ||
| } | ||
|
|
||
| List<Team> _deserializeTeams(List<dynamic> list) { | ||
| return list.map((e) { | ||
| // Hive returns _Map<dynamic, dynamic>, need to cast to Map<String, dynamic> | ||
| // for Team.fromJson | ||
| final json = Map<String, dynamic>.from(e as Map); | ||
| return Team.fromJson(json); | ||
| }).toList(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -305,6 +305,41 @@ class ApiClient { | |
| } | ||
| } | ||
|
|
||
| Future<List<Team>> getGlobalTrueSkillRankings( | ||
| {String gradeLevel = 'Middle School'}) async { | ||
| // RoboStem API uses a different base URL and key | ||
| final token = _settings.roboStemApiKey ?? AppConstants.roboStemApiKey; | ||
| final dio = Dio(BaseOptions( | ||
| baseUrl: AppConstants.roboStemBaseUrl, // https://api.robostem-api.org | ||
| headers: { | ||
| 'x-api-key': token, | ||
| 'accept': 'application/json', | ||
| }, | ||
| connectTimeout: const Duration(seconds: 30), | ||
| receiveTimeout: const Duration(seconds: 30), | ||
| )); | ||
|
Comment on lines
+311
to
+320
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block for creating a |
||
|
|
||
| try { | ||
| final response = await dio.get('/api/rankings/statiq', queryParameters: { | ||
| 'program': 'VIQRC', | ||
| 'limit': 100, // Reduced from 2500 for performance | ||
| 'grade_level': gradeLevel, | ||
| }); | ||
|
|
||
| // RoboStem response structure might differ. Assuming standard list or {data: []} | ||
| List<Map<String, dynamic>> rawList = []; | ||
| if (response.data is List) { | ||
| rawList = (response.data as List).cast<Map<String, dynamic>>(); | ||
| } else if (response.data is Map && response.data['data'] is List) { | ||
| rawList = (response.data['data'] as List).cast<Map<String, dynamic>>(); | ||
| } | ||
|
|
||
| return rawList.map((json) => Team.fromJson(json)).toList(); | ||
| } catch (e) { | ||
| return []; | ||
| } | ||
|
Comment on lines
+338
to
+340
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The } catch (e) {
if (e is DioException) {
print('RoboStem API Error (TrueSkill): ${e.message} ${e.response?.statusCode}');
} else {
print('Error fetching TrueSkill rankings: $e');
}
return [];
}
Comment on lines
+338
to
+340
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This } catch (e) {
print('Error fetching global TrueSkill rankings: $e');
rethrow;
} |
||
| } | ||
|
|
||
| Future<List<Team>> searchTeams( | ||
| {String? number, int? program, int? limit}) async { | ||
| // Strategy: Use RobotEvents API for exact team lookup first, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a new
Dioinstance within thegetGlobalTrueSkillRankingsmethod for every call is inefficient. This can lead to performance issues, such as socket exhaustion under heavy use, and prevents connection reuse. It's better to create a dedicatedDioinstance for the RoboStem API at the class level, similar to how_diois handled for RobotEvents, and reuse it across calls.