retroachievements 0.1.0
retroachievements: ^0.1.0 copied to clipboard
A typed Dart client library for the RetroAchievements.org API.
import 'dart:io';
import 'package:retroachievements/retroachievements.dart';
void main() async {
final username = Platform.environment['RA_USERNAME'] ?? 'example_user';
final webApiKey = Platform.environment['RA_APIKEY'] ?? 'example_api_key';
// 1. Build authorization object
final auth = buildAuthorization(username: username, webApiKey: webApiKey);
print('--- Offline Embedded Cache (Zero network calls) ---');
final cachedSystems =
getCachedConsoleIds(shouldOnlyRetrieveActiveSystems: true);
print('Embedded active systems: ${cachedSystems.length}');
final cachedGames32x = getCachedGameList(consoleId: 10);
print('Embedded 32X games: ${cachedGames32x.length}');
final doom = findCachedGameByHash('49134106e611839a28caf94a1ddd783d');
print(
'Identified ROM hash -> ${doom?.title} (Console: ${doom?.consoleName})');
print('\n--- RetroAchievements Online API ---');
if (username == 'example_user' || webApiKey == 'example_api_key') {
print(
'Note: Set RA_USERNAME and RA_APIKEY environment variables to make live API calls.');
return;
}
try {
// 2. Fetch active console systems
print('Fetching active consoles...');
final consoles = await getConsoleIds(
auth,
shouldOnlyRetrieveActiveSystems: true,
);
print('Found ${consoles.length} active consoles.');
if (consoles.isNotEmpty) {
print('First console: ${consoles.first.name} (ID: ${consoles.first.id})');
}
// 3. Fetch Achievement of the Week
print('\nFetching Achievement of the Week...');
final aotw = await getAchievementOfTheWeek(auth);
print('AOTW: "${aotw.achievement.title}" for "${aotw.game.title}"');
print('Description: ${aotw.achievement.description}');
print('Total players attempted: ${aotw.totalPlayers}');
// 4. Fetch Top Ten Users
print('\nFetching Top 10 Users...');
final topTen = await getTopTenUsers(auth);
for (var i = 0; i < topTen.length; i++) {
final user = topTen[i];
print('#${i + 1}: ${user.username} (${user.totalPoints} pts)');
}
// 5. Fetch Game Metadata (e.g. Sonic The Hedgehog - ID: 1)
print('\nFetching Game Info (Sonic The Hedgehog - ID 1)...');
final game = await getGame(auth, gameId: 1);
print('Game: ${game.title} (${game.consoleName})');
print('Developer: ${game.developer}, Publisher: ${game.publisher}');
} on RetroAchievementsApiException catch (e) {
print('API Error: ${e.statusCode} ${e.statusText} (${e.endpointUrl})');
} catch (e) {
print('Error: $e');
}
}