2026-04-22 20:08:45 +08:00

494 lines
14 KiB
Dart

class RoomGameProviderModel {
const RoomGameProviderModel({required this.key, required this.displayName});
final String key;
final String displayName;
factory RoomGameProviderModel.fromJson(Map<String, dynamic> json) {
return RoomGameProviderModel(
key: _asString(json['key']),
displayName: _asString(json['displayName']),
);
}
}
class RoomGameProviderListResponseModel {
const RoomGameProviderListResponseModel({required this.items});
final List<RoomGameProviderModel> items;
factory RoomGameProviderListResponseModel.fromJson(
Map<String, dynamic> json,
) {
final list = json['items'] as List<dynamic>? ?? const <dynamic>[];
return RoomGameProviderListResponseModel(
items:
list
.whereType<Map<String, dynamic>>()
.map(RoomGameProviderModel.fromJson)
.toList(),
);
}
}
class RoomGameShortcutModel {
const RoomGameShortcutModel({
required this.gameId,
required this.provider,
required this.gameType,
required this.providerGameId,
required this.name,
required this.cover,
required this.launchMode,
required this.gameMode,
required this.sort,
required this.launchParams,
});
final String gameId;
final String provider;
final String gameType;
final String providerGameId;
final String name;
final String cover;
final String launchMode;
final int gameMode;
final int sort;
final Map<String, dynamic> launchParams;
String get vendorType => provider;
String get vendorGameId => providerGameId;
factory RoomGameShortcutModel.fromJson(Map<String, dynamic> json) {
return RoomGameShortcutModel(
gameId: _asString(json['gameId']),
provider: _asString(json['provider'] ?? json['vendorType']),
gameType:
_asString(json['gameType']).isNotEmpty
? _asString(json['gameType'])
: _asString(json['provider'] ?? json['vendorType']),
providerGameId: _asString(json['providerGameId'] ?? json['vendorGameId']),
name: _asString(json['name']),
cover: _asString(json['cover']),
launchMode: _asString(json['launchMode']),
gameMode: _asInt(json['gameMode']),
sort: _asInt(json['sort']),
launchParams: _asMap(json['launchParams']),
);
}
}
class RoomGameListItemModel {
const RoomGameListItemModel({
required this.gameId,
required this.provider,
required this.gameType,
required this.providerGameId,
required this.name,
required this.cover,
required this.category,
required this.sort,
required this.launchMode,
required this.fullScreen,
required this.gameMode,
required this.safeHeight,
required this.orientation,
required this.packageVersion,
required this.status,
required this.launchParams,
});
final String gameId;
final String provider;
final String gameType;
final String providerGameId;
final String name;
final String cover;
final String category;
final int sort;
final String launchMode;
final bool fullScreen;
final int gameMode;
final int safeHeight;
final int orientation;
final String packageVersion;
final String status;
final Map<String, dynamic> launchParams;
String get vendorType => provider;
String get vendorGameId => providerGameId;
bool get isBaishun =>
provider.toUpperCase() == 'BAISHUN' ||
gameType.toUpperCase() == 'BAISHUN';
bool get isLeader =>
provider.toUpperCase() == 'LEADER' || gameType.toUpperCase() == 'LEADER';
factory RoomGameListItemModel.fromJson(Map<String, dynamic> json) {
return RoomGameListItemModel(
gameId: _asString(json['gameId']),
provider: _asString(json['provider'] ?? json['vendorType']),
gameType:
_asString(json['gameType']).isNotEmpty
? _asString(json['gameType'])
: _asString(json['provider'] ?? json['vendorType']),
providerGameId: _asString(json['providerGameId'] ?? json['vendorGameId']),
name: _asString(json['name']),
cover: _asString(json['cover']),
category: _asString(json['category']),
sort: _asInt(json['sort']),
launchMode: _asString(json['launchMode']),
fullScreen: _asBool(json['fullScreen']),
gameMode: _asInt(json['gameMode']),
safeHeight: _asInt(json['safeHeight']),
orientation: _asInt(json['orientation']),
packageVersion: _asString(json['packageVersion']),
status: _asString(json['status']),
launchParams: _asMap(json['launchParams']),
);
}
factory RoomGameListItemModel.debugMock() {
return const RoomGameListItemModel(
gameId: 'bs_mock',
provider: 'BAISHUN',
gameType: 'BAISHUN',
providerGameId: '999001',
name: 'BAISHUN Mock',
cover: '',
category: 'CHAT_ROOM',
sort: 999999,
launchMode: 'H5_REMOTE',
fullScreen: true,
gameMode: 3,
safeHeight: 800,
orientation: 1,
packageVersion: 'debug',
status: 'DEBUG',
launchParams: <String, dynamic>{'gameType': 'BAISHUN'},
);
}
}
class RoomGameListResponseModel {
const RoomGameListResponseModel({required this.items});
final List<RoomGameListItemModel> items;
factory RoomGameListResponseModel.fromJson(Map<String, dynamic> json) {
final list = json['items'] as List<dynamic>? ?? const <dynamic>[];
return RoomGameListResponseModel(
items:
list
.whereType<Map<String, dynamic>>()
.map(RoomGameListItemModel.fromJson)
.toList(),
);
}
}
class BaishunGameConfigModel {
const BaishunGameConfigModel({
required this.sceneMode,
required this.currencyIcon,
this.rawJson = const <String, dynamic>{},
});
final int sceneMode;
final String currencyIcon;
final Map<String, dynamic> rawJson;
factory BaishunGameConfigModel.fromJson(Map<String, dynamic> json) {
return BaishunGameConfigModel(
sceneMode: _asInt(json['sceneMode']),
currencyIcon: _asString(json['currencyIcon']),
rawJson: _asMap(json),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
...rawJson,
'sceneMode': sceneMode,
'currencyIcon': currencyIcon,
};
}
}
class BaishunBridgeConfigModel {
const BaishunBridgeConfigModel({
required this.appName,
required this.appChannel,
required this.appId,
required this.userId,
required this.code,
required this.roomId,
required this.gameMode,
required this.language,
required this.gsp,
required this.gameConfig,
this.rawJson = const <String, dynamic>{},
});
final String appName;
final String appChannel;
final int appId;
final String userId;
final String code;
final String roomId;
final String gameMode;
final String language;
final int gsp;
final BaishunGameConfigModel gameConfig;
final Map<String, dynamic> rawJson;
factory BaishunBridgeConfigModel.fromJson(Map<String, dynamic> json) {
return BaishunBridgeConfigModel(
appName: _asString(json['appName']),
appChannel: _asString(json['appChannel']),
appId: _asInt(json['appId']),
userId: _asString(json['userId']),
code: _asString(json['code']),
roomId: _asString(json['roomId']),
gameMode: _asString(json['gameMode']),
language: _asString(json['language']),
gsp: _asInt(json['gsp']),
gameConfig: BaishunGameConfigModel.fromJson(
json['gameConfig'] as Map<String, dynamic>? ??
const <String, dynamic>{},
),
rawJson: _asMap(json),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
...rawJson,
'appName': appName,
'appChannel': appChannel,
'appId': appId,
'userId': userId,
'code': code,
'roomId': roomId,
'gameMode': gameMode,
'language': language,
'gsp': gsp,
'gameConfig': gameConfig.toJson(),
};
}
}
class BaishunLaunchEntryModel {
const BaishunLaunchEntryModel({
required this.launchMode,
required this.entryUrl,
required this.previewUrl,
required this.downloadUrl,
required this.packageVersion,
required this.orientation,
required this.safeHeight,
this.rawJson = const <String, dynamic>{},
});
final String launchMode;
final String entryUrl;
final String previewUrl;
final String downloadUrl;
final String packageVersion;
final int orientation;
final int safeHeight;
final Map<String, dynamic> rawJson;
factory BaishunLaunchEntryModel.fromJson(Map<String, dynamic> json) {
return BaishunLaunchEntryModel(
launchMode: _asString(json['launchMode']),
entryUrl: _asString(json['entryUrl']),
previewUrl: _asString(json['previewUrl']),
downloadUrl: _asString(json['downloadUrl']),
packageVersion: _asString(json['packageVersion']),
orientation: _asInt(json['orientation']),
safeHeight: _asInt(json['safeHeight']),
rawJson: _asMap(json),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
...rawJson,
'launchMode': launchMode,
'entryUrl': entryUrl,
'previewUrl': previewUrl,
'downloadUrl': downloadUrl,
'packageVersion': packageVersion,
'orientation': orientation,
'safeHeight': safeHeight,
};
}
}
class BaishunRoomStateModel {
const BaishunRoomStateModel({
required this.roomId,
required this.state,
required this.provider,
required this.gameSessionId,
required this.currentGameId,
required this.currentProviderGameId,
required this.currentGameName,
required this.currentGameCover,
required this.hostUserId,
this.rawJson = const <String, dynamic>{},
});
final String roomId;
final String state;
final String provider;
final String gameSessionId;
final String currentGameId;
final String currentProviderGameId;
final String currentGameName;
final String currentGameCover;
final int hostUserId;
final Map<String, dynamic> rawJson;
String get currentVendorGameId => currentProviderGameId;
factory BaishunRoomStateModel.fromJson(Map<String, dynamic> json) {
return BaishunRoomStateModel(
roomId: _asString(json['roomId']),
state: _asString(json['state']),
provider: _asString(json['provider'] ?? json['vendorType']),
gameSessionId: _asString(json['gameSessionId']),
currentGameId: _asString(json['currentGameId'] ?? json['currentGameID']),
currentProviderGameId: _asString(
json['currentProviderGameId'] ?? json['currentVendorGameId'],
),
currentGameName: _asString(json['currentGameName']),
currentGameCover: _asString(json['currentGameCover']),
hostUserId: _asInt(json['hostUserId']),
rawJson: _asMap(json),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
...rawJson,
'roomId': roomId,
'state': state,
'provider': provider,
'gameSessionId': gameSessionId,
'currentGameId': currentGameId,
'currentProviderGameId': currentProviderGameId,
'currentGameName': currentGameName,
'currentGameCover': currentGameCover,
'hostUserId': hostUserId,
};
}
}
class BaishunLaunchModel {
const BaishunLaunchModel({
required this.gameSessionId,
required this.provider,
required this.gameId,
required this.providerGameId,
required this.entry,
required this.launchConfig,
required this.roomState,
});
final String gameSessionId;
final String provider;
final String gameId;
final String providerGameId;
final BaishunLaunchEntryModel entry;
final BaishunBridgeConfigModel launchConfig;
final BaishunRoomStateModel roomState;
String get vendorType => provider;
String get vendorGameId => providerGameId;
BaishunBridgeConfigModel get bridgeConfig => launchConfig;
factory BaishunLaunchModel.fromJson(Map<String, dynamic> json) {
return BaishunLaunchModel(
gameSessionId: _asString(json['gameSessionId']),
provider: _asString(json['provider'] ?? json['vendorType']),
gameId: _asString(json['gameId']),
providerGameId: _asString(json['providerGameId'] ?? json['vendorGameId']),
entry: BaishunLaunchEntryModel.fromJson(
json['entry'] as Map<String, dynamic>? ?? const <String, dynamic>{},
),
launchConfig: BaishunBridgeConfigModel.fromJson(
(json['launchConfig'] ?? json['bridgeConfig'])
as Map<String, dynamic>? ??
const <String, dynamic>{},
),
roomState: BaishunRoomStateModel.fromJson(
json['roomState'] as Map<String, dynamic>? ?? const <String, dynamic>{},
),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'gameSessionId': gameSessionId,
'provider': provider,
'gameId': gameId,
'providerGameId': providerGameId,
'entry': entry.toJson(),
'launchConfig': launchConfig.toJson(),
'roomState': roomState.toJson(),
};
}
}
String _asString(dynamic value) {
if (value == null) {
return '';
}
return value.toString().trim();
}
int _asInt(dynamic value) {
if (value == null) {
return 0;
}
if (value is int) {
return value;
}
if (value is num) {
return value.toInt();
}
return int.tryParse(value.toString()) ?? 0;
}
bool _asBool(dynamic value) {
if (value is bool) {
return value;
}
if (value is num) {
return value != 0;
}
if (value is String) {
return value.toLowerCase() == 'true' || value == '1';
}
return false;
}
Map<String, dynamic> _asMap(dynamic value) {
if (value is Map<String, dynamic>) {
return value;
}
if (value is Map) {
return value.map(
(dynamic key, dynamic item) => MapEntry(key.toString(), item),
);
}
return const <String, dynamic>{};
}