117 lines
2.9 KiB
Dart
117 lines
2.9 KiB
Dart
import 'package:yumi/modules/room_game/data/models/room_game_models.dart';
|
|
import 'package:yumi/modules/room_game/data/room_game_api.dart';
|
|
|
|
class RoomGameRepository {
|
|
RoomGameRepository({RoomGameApi? api}) : _api = api ?? RoomGameApi();
|
|
|
|
final RoomGameApi _api;
|
|
|
|
Future<List<RoomGameProviderModel>> fetchProviders() async {
|
|
final response = await _api.fetchProviders();
|
|
return response.items;
|
|
}
|
|
|
|
Future<List<RoomGameShortcutModel>> fetchShortcutGames({
|
|
required String roomId,
|
|
}) async {
|
|
final providers = await fetchProviders();
|
|
if (providers.isEmpty) {
|
|
return const <RoomGameShortcutModel>[];
|
|
}
|
|
|
|
final items = <RoomGameShortcutModel>[];
|
|
Object? lastError;
|
|
for (final provider in providers) {
|
|
try {
|
|
final result = await _api.fetchShortcutGames(
|
|
provider: provider.key,
|
|
roomId: roomId,
|
|
);
|
|
items.addAll(result);
|
|
} catch (error) {
|
|
lastError = error;
|
|
}
|
|
}
|
|
|
|
if (items.isEmpty && lastError != null) {
|
|
throw lastError;
|
|
}
|
|
|
|
items.sort((a, b) => a.sort.compareTo(b.sort));
|
|
return items;
|
|
}
|
|
|
|
Future<List<RoomGameListItemModel>> fetchRoomGames({
|
|
required String roomId,
|
|
String category = '',
|
|
}) async {
|
|
final providers = await fetchProviders();
|
|
if (providers.isEmpty) {
|
|
return const <RoomGameListItemModel>[];
|
|
}
|
|
|
|
final items = <RoomGameListItemModel>[];
|
|
Object? lastError;
|
|
for (final provider in providers) {
|
|
try {
|
|
final response = await _api.fetchRoomGames(
|
|
provider: provider.key,
|
|
roomId: roomId,
|
|
category: category,
|
|
);
|
|
items.addAll(response.items);
|
|
} catch (error) {
|
|
lastError = error;
|
|
}
|
|
}
|
|
|
|
if (items.isEmpty && lastError != null) {
|
|
throw lastError;
|
|
}
|
|
|
|
items.sort((a, b) => a.sort.compareTo(b.sort));
|
|
return items;
|
|
}
|
|
|
|
Future<BaishunRoomStateModel> fetchRoomState({
|
|
required String provider,
|
|
required String roomId,
|
|
}) {
|
|
return _api.fetchRoomState(provider: provider, roomId: roomId);
|
|
}
|
|
|
|
Future<BaishunLaunchModel> launchGame({
|
|
required String provider,
|
|
required String roomId,
|
|
required String gameId,
|
|
int sceneMode = 1,
|
|
required String clientOrigin,
|
|
Map<String, dynamic> params = const <String, dynamic>{},
|
|
}) {
|
|
return _api.launchGame(
|
|
provider: provider,
|
|
roomId: roomId,
|
|
gameId: gameId,
|
|
sceneMode: sceneMode,
|
|
clientOrigin: clientOrigin,
|
|
params: params,
|
|
);
|
|
}
|
|
|
|
Future<BaishunRoomStateModel> closeGame({
|
|
required String provider,
|
|
required String roomId,
|
|
required String gameSessionId,
|
|
String reason = 'user_exit',
|
|
Map<String, dynamic> params = const <String, dynamic>{},
|
|
}) {
|
|
return _api.closeGame(
|
|
provider: provider,
|
|
roomId: roomId,
|
|
gameSessionId: gameSessionId,
|
|
reason: reason,
|
|
params: params,
|
|
);
|
|
}
|
|
}
|