116 lines
3.4 KiB
Dart
116 lines
3.4 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<List<RoomGameShortcutModel>> fetchShortcutGames({
|
|
required String roomId,
|
|
}) {
|
|
return _client.get<List<RoomGameShortcutModel>>(
|
|
'/app/game/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 roomId,
|
|
String category = '',
|
|
}) {
|
|
final query = <String, dynamic>{'roomId': roomId};
|
|
if (category.isNotEmpty) {
|
|
query['category'] = category;
|
|
}
|
|
return _client.get<RoomGameListResponseModel>(
|
|
'/app/game/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 roomId}) {
|
|
return _client.get<BaishunRoomStateModel>(
|
|
'/app/game/baishun/state',
|
|
queryParams: {'roomId': roomId},
|
|
extra: _buildExtra(),
|
|
fromJson:
|
|
(dynamic json) => BaishunRoomStateModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<BaishunLaunchModel> launchBaishunGame({
|
|
required String roomId,
|
|
required String gameId,
|
|
required int sceneMode,
|
|
required String clientOrigin,
|
|
}) {
|
|
return _client.post<BaishunLaunchModel>(
|
|
'/app/game/baishun/launch',
|
|
data: {
|
|
'roomId': roomId,
|
|
'gameId': gameId,
|
|
'sceneMode': sceneMode,
|
|
'clientOrigin': clientOrigin,
|
|
},
|
|
extra: _buildExtra(),
|
|
fromJson:
|
|
(dynamic json) => BaishunLaunchModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<BaishunRoomStateModel> closeBaishunGame({
|
|
required String roomId,
|
|
required String gameSessionId,
|
|
String reason = 'user_exit',
|
|
}) {
|
|
return _client.post<BaishunRoomStateModel>(
|
|
'/app/game/baishun/close',
|
|
data: {
|
|
'roomId': roomId,
|
|
'gameSessionId': gameSessionId,
|
|
'reason': reason,
|
|
},
|
|
extra: _buildExtra(),
|
|
fromJson:
|
|
(dynamic json) => BaishunRoomStateModel.fromJson(
|
|
json as Map<String, dynamic>? ?? const <String, dynamic>{},
|
|
),
|
|
);
|
|
}
|
|
}
|