138 lines
4.1 KiB
Dart
138 lines
4.1 KiB
Dart
import 'package:yumi/modules/room_game/data/models/room_game_models.dart';
|
|
import 'package:yumi/shared/data_sources/sources/remote/net/api.dart';
|
|
import 'package:yumi/shared/data_sources/sources/remote/net/network_client.dart';
|
|
|
|
const String _gameApiHost = String.fromEnvironment(
|
|
'GAME_API_HOST',
|
|
defaultValue: '',
|
|
);
|
|
|
|
class RoomGameApi {
|
|
RoomGameApi({NetworkClient? client}) : _client = client ?? http;
|
|
|
|
final NetworkClient _client;
|
|
|
|
Map<String, dynamic>? _buildExtra([Map<String, dynamic>? extra]) {
|
|
final merged = <String, dynamic>{...?extra};
|
|
final trimmedGameApiHost = _gameApiHost.trim();
|
|
if (trimmedGameApiHost.isNotEmpty) {
|
|
merged[BaseNetworkClient.baseUrlOverrideKey] = trimmedGameApiHost;
|
|
}
|
|
return merged.isEmpty ? null : merged;
|
|
}
|
|
|
|
Future<RoomGameProviderListResponseModel> fetchProviders() {
|
|
return _client.get<RoomGameProviderListResponseModel>(
|
|
'/app/game/providers',
|
|
extra: _buildExtra(),
|
|
fromJson:
|
|
(dynamic json) => RoomGameProviderListResponseModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<List<RoomGameShortcutModel>> fetchShortcutGames({
|
|
required String provider,
|
|
required String roomId,
|
|
}) {
|
|
return _client.get<List<RoomGameShortcutModel>>(
|
|
'/app/game/providers/$provider/room/shortcut',
|
|
queryParams: {'roomId': roomId},
|
|
extra: _buildExtra(),
|
|
fromJson: (dynamic json) {
|
|
final list = json as List<dynamic>? ?? const <dynamic>[];
|
|
return list
|
|
.whereType<Map<String, dynamic>>()
|
|
.map(RoomGameShortcutModel.fromJson)
|
|
.toList();
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<RoomGameListResponseModel> fetchRoomGames({
|
|
required String provider,
|
|
required String roomId,
|
|
String category = '',
|
|
}) {
|
|
final query = <String, dynamic>{'roomId': roomId};
|
|
if (category.isNotEmpty) {
|
|
query['category'] = category;
|
|
}
|
|
return _client.get<RoomGameListResponseModel>(
|
|
'/app/game/providers/$provider/room/list',
|
|
queryParams: query,
|
|
extra: _buildExtra(const <String, dynamic>{
|
|
BaseNetworkClient.silentErrorToastKey: true,
|
|
}),
|
|
fromJson:
|
|
(dynamic json) => RoomGameListResponseModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<BaishunRoomStateModel> fetchRoomState({
|
|
required String provider,
|
|
required String roomId,
|
|
}) {
|
|
return _client.get<BaishunRoomStateModel>(
|
|
'/app/game/providers/$provider/state',
|
|
queryParams: {'roomId': roomId},
|
|
extra: _buildExtra(),
|
|
fromJson:
|
|
(dynamic json) => BaishunRoomStateModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<BaishunLaunchModel> launchGame({
|
|
required String provider,
|
|
required String roomId,
|
|
required String gameId,
|
|
required int sceneMode,
|
|
required String clientOrigin,
|
|
required Map<String, dynamic> params,
|
|
}) {
|
|
return _client.post<BaishunLaunchModel>(
|
|
'/app/game/providers/$provider/launch',
|
|
data: {
|
|
'roomId': roomId,
|
|
'gameId': gameId,
|
|
'sceneMode': sceneMode,
|
|
'clientOrigin': clientOrigin,
|
|
'params': params,
|
|
},
|
|
extra: _buildExtra(),
|
|
fromJson:
|
|
(dynamic json) => BaishunLaunchModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<BaishunRoomStateModel> closeGame({
|
|
required String provider,
|
|
required String roomId,
|
|
required String gameSessionId,
|
|
String reason = 'user_exit',
|
|
Map<String, dynamic> params = const <String, dynamic>{},
|
|
}) {
|
|
return _client.post<BaishunRoomStateModel>(
|
|
'/app/game/providers/$provider/close',
|
|
data: {
|
|
'roomId': roomId,
|
|
'gameSessionId': gameSessionId,
|
|
'reason': reason,
|
|
'params': params,
|
|
},
|
|
extra: _buildExtra(),
|
|
fromJson:
|
|
(dynamic json) => BaishunRoomStateModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
}
|