import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:webview_flutter/webview_flutter.dart'; import 'package:yumi/app/routes/sc_fluro_navigator.dart'; import 'package:yumi/modules/room_game/bridge/baishun_js_bridge.dart'; import 'package:yumi/modules/room_game/data/models/room_game_models.dart'; import 'package:yumi/modules/room_game/data/room_game_repository.dart'; import 'package:yumi/modules/room_game/views/baishun_loading_view.dart'; import 'package:yumi/modules/wallet/wallet_route.dart'; import 'package:yumi/ui_kit/components/sc_tts.dart'; class BaishunGamePage extends StatefulWidget { const BaishunGamePage({ super.key, required this.roomId, required this.game, required this.launchModel, }); final String roomId; final RoomGameListItemModel game; final BaishunLaunchModel launchModel; @override State createState() => _BaishunGamePageState(); } class _BaishunGamePageState extends State { static const String _logPrefix = '[BaishunGame]'; final RoomGameRepository _repository = RoomGameRepository(); final Set> _webGestureRecognizers = >{ Factory(() => EagerGestureRecognizer()), }; late final WebViewController _controller; Timer? _bridgeBootstrapTimer; Timer? _loadingFallbackTimer; bool _isLoading = true; bool _isClosing = false; bool _didReceiveBridgeMessage = false; bool _didFinishPageLoad = false; bool _hasDeliveredLaunchConfig = false; int _bridgeInjectCount = 0; String? _errorMessage; @override void initState() { super.initState(); _controller = WebViewController() ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setBackgroundColor(Colors.black) ..addJavaScriptChannel( BaishunJsBridge.channelName, onMessageReceived: _handleBridgeMessage, ) ..addJavaScriptChannel( BaishunBridgeActions.getConfig, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.getConfig, message, ), ) ..addJavaScriptChannel( BaishunBridgeActions.destroy, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.destroy, message, ), ) ..addJavaScriptChannel( BaishunBridgeActions.gameRecharge, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.gameRecharge, message, ), ) ..addJavaScriptChannel( BaishunBridgeActions.gameLoaded, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.gameLoaded, message, ), ) ..setNavigationDelegate( NavigationDelegate( onPageStarted: (String url) { _log('page_started url=${_clip(url, 240)}'); _prepareForPageLoad(); }, onPageFinished: (String url) async { _didFinishPageLoad = true; _log('page_finished url=${_clip(url, 240)}'); await _injectBridge(reason: 'page_finished'); }, onWebResourceError: (WebResourceError error) { _log( 'web_resource_error code=${error.errorCode} ' 'type=${error.errorType} desc=${_clip(error.description, 300)}', ); _stopBridgeBootstrap(reason: 'web_resource_error'); if (!mounted) { return; } setState(() { _errorMessage = error.description; _isLoading = false; }); }, ), ); _log('init launch=${_stringifyForLog(_buildLaunchSummary())}'); unawaited(_loadGameEntry()); } @override void dispose() { _log('dispose isClosing=$_isClosing'); _stopBridgeBootstrap(reason: 'dispose'); super.dispose(); } void _prepareForPageLoad() { _log( 'prepare_page_load session=${widget.launchModel.gameSessionId} ' 'entry=${_clip(widget.launchModel.entry.entryUrl, 240)}', ); _didReceiveBridgeMessage = false; _didFinishPageLoad = false; _hasDeliveredLaunchConfig = false; _bridgeInjectCount = 0; _stopBridgeBootstrap(reason: 'prepare_page_load'); _bridgeBootstrapTimer = Timer.periodic(const Duration(milliseconds: 250), ( Timer timer, ) { if (!mounted || _didReceiveBridgeMessage || _errorMessage != null) { _log( 'bootstrap_timer_stop tick=${timer.tick} ' 'didReceiveBridge=$_didReceiveBridgeMessage ' 'hasError=${_errorMessage != null}', ); timer.cancel(); return; } unawaited(_injectBridge(reason: 'bootstrap')); if (_didFinishPageLoad && timer.tick >= 12) { _log( 'bootstrap_timer_stop tick=${timer.tick} reason=page_finished_guard', ); timer.cancel(); } }); _loadingFallbackTimer = Timer(const Duration(seconds: 6), () { if (!mounted || _didReceiveBridgeMessage || _errorMessage != null) { _log( 'loading_fallback_skip didReceiveBridge=$_didReceiveBridgeMessage ' 'hasError=${_errorMessage != null}', ); return; } _log('loading_fallback_fire isLoading=$_isLoading'); setState(() { _isLoading = false; }); }); if (!mounted) { return; } setState(() { _isLoading = true; _errorMessage = null; }); } void _stopBridgeBootstrap({String reason = 'manual'}) { if (_bridgeBootstrapTimer != null || _loadingFallbackTimer != null) { _log('stop_bootstrap reason=$reason'); } _bridgeBootstrapTimer?.cancel(); _bridgeBootstrapTimer = null; _loadingFallbackTimer?.cancel(); _loadingFallbackTimer = null; } Future _loadGameEntry() async { try { _prepareForPageLoad(); final entryUrl = widget.launchModel.entry.entryUrl.trim(); _log( 'load_entry launchMode=${widget.launchModel.entry.launchMode} ' 'entryUrl=${_clip(entryUrl, 240)}', ); if (entryUrl.isEmpty || entryUrl.startsWith('mock://')) { final html = await rootBundle.loadString( 'assets/debug/baishun_mock/index.html', ); _log('load_mock_html baseUrl=https://baishun.mock.local/'); await _controller.loadHtmlString( html, baseUrl: 'https://baishun.mock.local/', ); return; } final uri = Uri.tryParse(entryUrl); if (uri == null) { throw Exception('Invalid game entry url: $entryUrl'); } _log('load_request uri=${_clip(uri.toString(), 240)}'); await _controller.loadRequest(uri); } catch (error) { _log('load_entry_error error=${_clip(error.toString(), 400)}'); if (!mounted) { return; } setState(() { _errorMessage = error.toString(); _isLoading = false; }); } } Future _injectBridge({String reason = 'manual'}) async { _bridgeInjectCount += 1; if (reason != 'bootstrap' || _bridgeInjectCount <= 3 || _bridgeInjectCount % 5 == 0) { _log('inject_bridge reason=$reason count=$_bridgeInjectCount'); } try { await _controller.runJavaScript(BaishunJsBridge.bootstrapScript()); } catch (error) { _log( 'inject_bridge_error reason=$reason error=${_clip(error.toString(), 300)}', ); } } Future _sendConfigToGame({ String? jsCallbackPath, bool force = false, }) async { if (_hasDeliveredLaunchConfig && !force) { _log('skip_send_config reason=already_delivered'); return; } _hasDeliveredLaunchConfig = true; final rawConfig = widget.launchModel.bridgeConfig; final config = _buildEffectiveBridgeConfig(rawConfig); _log( 'send_config jsCallback=${jsCallbackPath ?? ''} ' 'force=$force config=${_stringifyForLog(_buildConfigSummary(config, rawConfig: rawConfig))}', ); try { await _controller.runJavaScript( BaishunJsBridge.buildConfigScript( config, jsCallbackPath: jsCallbackPath, ), ); } catch (error) { _log('send_config_error error=${_clip(error.toString(), 300)}'); } } Future _handleBridgeMessage(JavaScriptMessage message) async { _log('channel_message raw=${_clip(message.message, 600)}'); final bridgeMessage = BaishunBridgeMessage.parse(message.message); await _dispatchBridgeMessage(bridgeMessage); } Future _handleNamedChannelMessage( String action, JavaScriptMessage message, ) async { final bridgeMessage = BaishunBridgeMessage.fromAction( action, message.message, ); _log('named_channel action=$action raw=${_clip(message.message, 600)}'); await _dispatchBridgeMessage(bridgeMessage); } Future _dispatchBridgeMessage( BaishunBridgeMessage bridgeMessage, ) async { if (bridgeMessage.action == BaishunBridgeActions.debugLog) { final tag = bridgeMessage.payload['tag']?.toString().trim(); final message = bridgeMessage.payload['message']?.toString() ?? ''; _log('h5_debug tag=${tag ?? 'unknown'} message=${_clip(message, 800)}'); return; } final callbackPath = _extractCallbackPath(bridgeMessage.payload); _log( 'bridge_action action=${bridgeMessage.action} ' 'callback=${callbackPath.isEmpty ? '-' : callbackPath} ' 'payload=${_stringifyForLog(_sanitizeForLog(bridgeMessage.payload))}', ); if (bridgeMessage.action.isNotEmpty) { _didReceiveBridgeMessage = true; _stopBridgeBootstrap(reason: 'bridge_message_${bridgeMessage.action}'); } switch (bridgeMessage.action) { case BaishunBridgeActions.getConfig: await _sendConfigToGame( jsCallbackPath: callbackPath.isEmpty ? null : callbackPath, ); break; case BaishunBridgeActions.gameLoaded: if (!mounted) { return; } _log('game_loaded received'); setState(() { _isLoading = false; _errorMessage = null; }); break; case BaishunBridgeActions.gameRecharge: _log('game_recharge open_wallet'); await SCNavigatorUtils.push(context, WalletRoute.recharge); await _notifyWalletUpdate(); break; case BaishunBridgeActions.destroy: _log('destroy requested by h5'); await _closeAndExit(reason: 'h5_destroy'); break; default: _log('bridge_action_unhandled action=${bridgeMessage.action}'); break; } } Future _notifyWalletUpdate() async { _log('notify_wallet_update'); try { await _controller.runJavaScript( BaishunJsBridge.buildWalletUpdateScript(), ); } catch (error) { _log('notify_wallet_update_error error=${_clip(error.toString(), 300)}'); } } Future _reload() async { if (!mounted) { return; } _log('reload'); setState(() { _errorMessage = null; _isLoading = true; }); await _loadGameEntry(); } Future _closeAndExit({String reason = 'page_back'}) async { if (_isClosing) { _log('close_skip reason=already_closing source=$reason'); return; } _isClosing = true; _log('close_start reason=$reason'); try { if (!widget.launchModel.gameSessionId.startsWith('bs_mock_')) { final result = await _repository.closeBaishunGame( roomId: widget.roomId, gameSessionId: widget.launchModel.gameSessionId, reason: reason, ); _log( 'close_success result=${_stringifyForLog({'roomId': result.roomId, 'state': result.state, 'gameSessionId': result.gameSessionId, 'currentGameId': result.currentGameId, 'hostUserId': result.hostUserId})}', ); } } catch (error) { _log('close_error error=${_clip(error.toString(), 300)}'); } if (mounted) { _log('close_pop'); Navigator.of(context).pop(); } } String _extractCallbackPath(Map payload) { final candidates = [ payload['jsCallback']?.toString() ?? '', payload['callback']?.toString() ?? '', ]; for (final candidate in candidates) { final trimmed = candidate.trim(); if (trimmed.isNotEmpty) { return trimmed; } } return ''; } void _log(String message) { debugPrint('$_logPrefix $message'); } String _clip(String value, [int limit = 600]) { final trimmed = value.trim(); if (trimmed.length <= limit) { return trimmed; } return '${trimmed.substring(0, limit)}...'; } String _maskValue(String value, {int keepStart = 6, int keepEnd = 4}) { final trimmed = value.trim(); if (trimmed.isEmpty) { return ''; } if (trimmed.length <= keepStart + keepEnd) { return trimmed; } return '${trimmed.substring(0, keepStart)}***${trimmed.substring(trimmed.length - keepEnd)}'; } Object? _sanitizeForLog(Object? value, {String key = ''}) { final lowerKey = key.toLowerCase(); final shouldMask = lowerKey.contains('code') || lowerKey.contains('token') || lowerKey.contains('authorization'); if (value is Map) { final result = {}; for (final MapEntry entry in value.entries) { result[entry.key.toString()] = _sanitizeForLog( entry.value, key: entry.key.toString(), ); } return result; } if (value is List) { return value.map((Object? item) => _sanitizeForLog(item)).toList(); } if (value is String) { final normalized = shouldMask ? _maskValue(value) : value; return _clip(normalized, 600); } return value; } String _stringifyForLog(Object? value) { if (value == null) { return ''; } try { return _clip(jsonEncode(value), 800); } catch (_) { return _clip(value.toString(), 800); } } Map _buildLaunchSummary() { final entry = widget.launchModel.entry; final roomState = widget.launchModel.roomState; return { 'roomId': widget.roomId, 'gameId': widget.game.gameId, 'gameName': widget.game.name, 'vendorType': widget.launchModel.vendorType, 'vendorGameId': widget.launchModel.vendorGameId, 'gameSessionId': widget.launchModel.gameSessionId, 'launchMode': entry.launchMode, 'entryUrl': _clip(entry.entryUrl, 240), 'previewUrl': _clip(entry.previewUrl, 240), 'packageVersion': entry.packageVersion, 'orientation': entry.orientation, 'safeHeight': entry.safeHeight, 'roomState': roomState.state, 'currentGameId': roomState.currentGameId, 'hostUserId': roomState.hostUserId, 'bridgeConfig': _buildConfigSummary(widget.launchModel.bridgeConfig), }; } Map _buildConfigSummary( BaishunBridgeConfigModel config, { BaishunBridgeConfigModel? rawConfig, }) { return { 'appName': config.appName, 'appChannel': config.appChannel, 'appId': config.appId, 'userId': config.userId, 'roomId': config.roomId, 'gameMode': config.gameMode, 'language': config.language, 'rawLanguage': rawConfig?.language ?? config.language, 'gsp': config.gsp, 'code': _maskValue(config.code), 'codeLength': config.code.length, 'sceneMode': config.gameConfig.sceneMode, 'currencyIcon': config.gameConfig.currencyIcon, }; } BaishunBridgeConfigModel _buildEffectiveBridgeConfig( BaishunBridgeConfigModel rawConfig, ) { final normalizedLanguage = _mapBaishunLanguage(rawConfig.language); if (normalizedLanguage == rawConfig.language.trim()) { return rawConfig; } return BaishunBridgeConfigModel( appName: rawConfig.appName, appChannel: rawConfig.appChannel, appId: rawConfig.appId, userId: rawConfig.userId, code: rawConfig.code, roomId: rawConfig.roomId, gameMode: rawConfig.gameMode, language: normalizedLanguage, gsp: rawConfig.gsp, gameConfig: rawConfig.gameConfig, ); } String _mapBaishunLanguage(String rawLanguage) { final trimmed = rawLanguage.trim(); if (trimmed.isEmpty) { return '2'; } final lower = trimmed.toLowerCase(); const supportedCodes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', }; if (supportedCodes.contains(lower)) { return lower; } if (lower.startsWith('zh')) { return '0'; } switch (lower) { case 'en': case 'english': return '2'; case 'id': case 'in': case 'indonesian': return '3'; case 'ms': case 'malay': return '4'; case 'th': case 'thai': return '5'; case 'vi': case 'vietnamese': return '6'; case 'ar': case 'arabic': return '7'; case 'fil': case 'tl': case 'tagalog': return '8'; case 'pt': case 'portuguese': return '9'; case 'tr': case 'turkish': return '10'; case 'ur': case 'urdu': return '11'; default: return '2'; } } @override Widget build(BuildContext context) { final topCrop = 28.w; return PopScope( canPop: false, onPopInvokedWithResult: (bool didPop, Object? result) async { if (didPop) { return; } await _closeAndExit(); }, child: Material( color: Colors.transparent, child: Align( alignment: Alignment.bottomCenter, child: ClipRRect( borderRadius: BorderRadius.only( topLeft: Radius.circular(24.w), topRight: Radius.circular(24.w), ), child: Container( width: ScreenUtil().screenWidth, height: ScreenUtil().screenHeight * 0.5, color: const Color(0xFF081915), child: Stack( children: [ Positioned( top: -topCrop, left: 0, right: 0, bottom: 0, child: WebViewWidget( controller: _controller, gestureRecognizers: _webGestureRecognizers, ), ), if (_errorMessage != null) _buildErrorState(), if (_isLoading && _errorMessage == null) const IgnorePointer( ignoring: true, child: BaishunLoadingView( message: 'Waiting for gameLoaded...', ), ), ], ), ), ), ), ), ); } Widget _buildErrorState() { return Positioned.fill( child: Container( color: Colors.black.withValues(alpha: 0.88), padding: EdgeInsets.symmetric(horizontal: 28.w), alignment: Alignment.center, child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.error_outline, color: Colors.white70, size: 34.w), SizedBox(height: 12.w), Text( 'Game page failed to load', style: TextStyle( color: Colors.white, fontSize: 16.sp, fontWeight: FontWeight.w700, ), ), SizedBox(height: 8.w), Text( _errorMessage ?? '', textAlign: TextAlign.center, style: TextStyle(color: Colors.white70, fontSize: 12.sp), ), SizedBox(height: 16.w), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ TextButton(onPressed: _reload, child: const Text('Retry')), SizedBox(width: 10.w), TextButton( onPressed: () { SCTts.show('Please check game host and package url'); }, child: const Text('Tips'), ), ], ), ], ), ), ); } }