| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  | using System; | 
					
						
							|  |  |  | using System.Collections; | 
					
						
							|  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  | using System.Linq; | 
					
						
							|  |  |  | using JetBrains.Annotations; | 
					
						
							|  |  |  | using KwaiAds.Scripts.Api.Reward; | 
					
						
							|  |  |  | using UnityEngine; | 
					
						
							|  |  |  | using static WZ.KwaiAdsConfigParser; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace WZ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |     public class KwaiFloorRvManager : D_MonoSingleton<KwaiFloorRvManager> | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         private int _rvParallelRequests = 3; | 
					
						
							|  |  |  |         private List<FloorConfig> _rvFloorConfigs = new List<FloorConfig>(); | 
					
						
							|  |  |  |         public Dictionary<string, IRewardAdController> _rvFloorAdControllers = new Dictionary<string, IRewardAdController>(); | 
					
						
							|  |  |  |         private List<FloorConfig> _sortedFloors = new List<FloorConfig>(); | 
					
						
							|  |  |  |         private int _currentFloorIndex = 0; | 
					
						
							|  |  |  |         private bool _isRequestingFloors = false; | 
					
						
							|  |  |  |         private List<FloorConfig> _currentRequestBatch = new List<FloorConfig>(); | 
					
						
							|  |  |  |         private FloorConfig _successfulFloor = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |         public float _rvStartLoadTime = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  |         public void InitializeWithFloors() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // 对楼层按价格从高到低排序 | 
					
						
							|  |  |  |             _sortedFloors = _rvFloorConfigs.OrderByDescending(f => f.price).ToList(); | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |             LoggerUtils.Debug("[kwai] floor reward Sorted floors: " + string.Join(", ", _sortedFloors.Select(f => $"{f.id}:{f.price}"))); | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public void LoadKwaiBiddingConfig() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _rvParallelRequests = KwaiAdsConfigParser.GetRvParallelRequests(); | 
					
						
							|  |  |  |             _rvFloorConfigs = KwaiAdsConfigParser.GetRvFloorConfigs(); | 
					
						
							|  |  |  |             KwaiAdsManager.Instance._appId = KwaiAdsConfigParser.GetKwaiAppId(); | 
					
						
							|  |  |  |             KwaiAdsManager.Instance._token = KwaiAdsConfigParser.GetKwaiAppToken(); | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |             LoggerUtils.Debug($"[kwai] floor reward bidding config loaded. FloorOpen: {KwaiAdsConfigParser.GetKwaiRvFloorOpen()}, ParallelRequests: {_rvParallelRequests}, Floors: {_rvFloorConfigs.Count}"); | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public void LoadRewardedWithFloors() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (_rvFloorConfigs == null || _rvFloorConfigs.Count == 0) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |                 LoggerUtils.Debug("[kwai] floor reward No floor configs available, using standard load"); | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  |                 KwaiAdsManager.Instance.LoadRewardedStandard(); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |             _rvStartLoadTime = Time.realtimeSinceStartup; | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  |             // 重置状态 | 
					
						
							|  |  |  |             _currentFloorIndex = 0; | 
					
						
							|  |  |  |             _successfulFloor = null; | 
					
						
							|  |  |  |             _isRequestingFloors = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 清理之前的广告控制器 | 
					
						
							|  |  |  |             foreach (var controller in _rvFloorAdControllers.Values) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 controller.Destroy(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             _rvFloorAdControllers.Clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 开始请求楼层广告 | 
					
						
							|  |  |  |             RequestNextFloorBatch(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         private void RequestNextFloorBatch() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (!_isRequestingFloors || _successfulFloor != null) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 获取下一批要请求的楼层 | 
					
						
							|  |  |  |             _currentRequestBatch = new List<FloorConfig>(); | 
					
						
							|  |  |  |             int count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             while (_currentFloorIndex < _sortedFloors.Count && count < _rvParallelRequests) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 _currentRequestBatch.Add(_sortedFloors[_currentFloorIndex]); | 
					
						
							|  |  |  |                 _currentFloorIndex++; | 
					
						
							|  |  |  |                 count++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (_currentRequestBatch.Count == 0) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 // 所有楼层都请求完毕,没有填充 | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |                 LoggerUtils.Debug("[kwai] floor reward All floors requested, no fill"); | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  |                 _isRequestingFloors = false; | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |             LoggerUtils.Debug($"[kwai] floor reward Requesting floor batch: {string.Join(", ", _currentRequestBatch.Select(f => $"{f.id}({f.price})"))}"); | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // 并行请求当前批次的楼层 | 
					
						
							|  |  |  |             foreach (var floor in _currentRequestBatch) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 RequestFloorAd(floor); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         private void RequestFloorAd(FloorConfig floor) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             IRewardAdController controller = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getRewardAdController(); | 
					
						
							|  |  |  |             _rvFloorAdControllers[floor.id] = controller; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             KwaiRewardAdRequest kwaiRewardAdRequest = new KwaiRewardAdRequest(floor.unite_id); | 
					
						
							|  |  |  |             controller.Load(kwaiRewardAdRequest, | 
					
						
							|  |  |  |                 new FloorRewardAdListener(this, floor), | 
					
						
							|  |  |  |                 new FloorRewardAdLoadListener(this, floor)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 处理楼层广告加载成功 | 
					
						
							|  |  |  |         public void OnFloorAdLoaded(FloorConfig floor, IRewardAdController controller) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (!_isRequestingFloors || _successfulFloor != null) return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |             LoggerUtils.Debug($"[kwai] floor reward ad loaded: {floor.id} with price: {floor.price}"); | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // 暂停其他并行请求 | 
					
						
							|  |  |  |             _successfulFloor = floor; | 
					
						
							|  |  |  |             _isRequestingFloors = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 取消其他楼层的请求 | 
					
						
							|  |  |  |             foreach (var kvp in _rvFloorAdControllers) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (kvp.Key != floor.id) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     kvp.Value.Destroy(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 处理楼层广告加载失败 | 
					
						
							|  |  |  |         public void OnFloorAdFailed(FloorConfig floor, string error) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (!_isRequestingFloors || _successfulFloor != null) return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-15 05:58:43 +00:00
										 |  |  |             LoggerUtils.Debug($"[kwai] floor reward ad failed: {floor.id} with error: {error}"); | 
					
						
							| 
									
										
										
										
											2025-09-15 02:19:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // 检查当前批次是否全部失败 | 
					
						
							|  |  |  |             bool allFailedInBatch = true; | 
					
						
							|  |  |  |             foreach (var f in _currentRequestBatch) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (f.id == floor.id) continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (_rvFloorAdControllers.ContainsKey(f.id) && | 
					
						
							|  |  |  |                     _rvFloorAdControllers[f.id] != null && | 
					
						
							|  |  |  |                     _rvFloorAdControllers[f.id].IsReady()) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     allFailedInBatch = false; | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 如果当前批次全部失败,请求下一批 | 
					
						
							|  |  |  |             if (allFailedInBatch) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 RequestNextFloorBatch(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public bool IsRewardedAvailable() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return _successfulFloor != null && | 
					
						
							|  |  |  |                     _rvFloorAdControllers.ContainsKey(_successfulFloor.id) && | 
					
						
							|  |  |  |                     _rvFloorAdControllers[_successfulFloor.id] != null && | 
					
						
							|  |  |  |                     _rvFloorAdControllers[_successfulFloor.id].IsReady(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public void ShowRewarded(Action _action) | 
					
						
							|  |  |  |         {  | 
					
						
							|  |  |  |             if (_successfulFloor != null && | 
					
						
							|  |  |  |                     _rvFloorAdControllers.ContainsKey(_successfulFloor.id) && | 
					
						
							|  |  |  |                     _rvFloorAdControllers[_successfulFloor.id] != null) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 _rvFloorAdControllers[_successfulFloor.id].Show(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 _action?.Invoke(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |