using System; using System.Collections; using System.Collections.Generic; using System.Linq; using GoogleMobileAds.Common; using JetBrains.Annotations; using KwaiAds.Scripts.Api.Interstitial; using KwaiAds.Scripts.Api.Reward; using UnityEngine; using static WZ.KwaiAdsConfigParser; namespace WZ { public class KwaiFloorIvManager : D_MonoSingleton { private int _ivParallelRequests = 3; private List _ivFloorConfigs = new List(); public Dictionary _ivFloorAdControllers = new Dictionary(); private List _sortedFloors = new List(); private int _currentFloorIndex = 0; private bool _isRequestingFloors = false; private List _currentRequestBatch = new List(); private FloorConfig _successfulFloor = null; public float _ivStartLoadTime = 0; public void InitializeWithFloors() { // 对楼层按价格从高到低排序 _sortedFloors = _ivFloorConfigs.OrderByDescending(f => f.price).ToList(); LoggerUtils.Debug("[kwai] floor inter Sorted floors: " + string.Join(", ", _sortedFloors.Select(f => $"{f.id}:{f.price}"))); } public void LoadKwaiBiddingConfig() { _ivParallelRequests = KwaiAdsConfigParser.GetIvParallelRequests(); _ivFloorConfigs = KwaiAdsConfigParser.GetIvFloorConfigs(); LoggerUtils.Debug($"[kwai] floor inter bidding config loaded. FloorOpen: {KwaiAdsConfigParser.GetKwaiIvFloorOpen()}, ParallelRequests: {_ivParallelRequests}, Floors: {_ivFloorConfigs.Count}"); } public void LoadInterstitialWithFloors() { if (_ivFloorConfigs == null || _ivFloorConfigs.Count == 0) { LoggerUtils.Debug("[kwai] floor inter No floor configs available, using standard load"); KwaiAdsManager.Instance.LoadRewardedStandard(); return; } _ivStartLoadTime = Time.realtimeSinceStartup; // 重置状态 _currentFloorIndex = 0; _successfulFloor = null; _isRequestingFloors = true; // 清理之前的广告控制器 foreach (var controller in _ivFloorAdControllers.Values) { controller.Destroy(); } _ivFloorAdControllers.Clear(); // 开始请求楼层广告 RequestNextFloorBatch(); } private void RequestNextFloorBatch() { if (!_isRequestingFloors || _successfulFloor != null) return; // 获取下一批要请求的楼层 _currentRequestBatch = new List(); int count = 0; while (_currentFloorIndex < _sortedFloors.Count && count < _ivParallelRequests) { _currentRequestBatch.Add(_sortedFloors[_currentFloorIndex]); _currentFloorIndex++; count++; } if (_currentRequestBatch.Count == 0) { // 所有楼层都请求完毕,没有填充 LoggerUtils.Debug("[kwai] floor inter All floors requested, no fill"); _isRequestingFloors = false; return; } LoggerUtils.Debug($"[kwai] floor inter Requesting floor batch: {string.Join(", ", _currentRequestBatch.Select(f => $"{f.id}({f.price})"))}"); // 并行请求当前批次的楼层 foreach (var floor in _currentRequestBatch) { RequestFloorAd(floor); } } private void RequestFloorAd(FloorConfig floor) { IInterstitialAdController controller = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getInterstitialAdController();; _ivFloorAdControllers[floor.id] = controller; KwaiInterstitialAdRequest kwaiInterstitialAdRequest = new KwaiInterstitialAdRequest(floor.unite_id); controller.Load(kwaiInterstitialAdRequest, new FloorInterAdListener(this, floor), new FloorInterAdLoadListener(this, floor)); } // 处理楼层广告加载成功 public void OnFloorAdLoaded(FloorConfig floor, IInterstitialAdController controller) { if (!_isRequestingFloors || _successfulFloor != null) return; LoggerUtils.Debug($"[kwai] floor inter Floor ad loaded: {floor.id} with price: {floor.price}"); // 暂停其他并行请求 _successfulFloor = floor; _isRequestingFloors = false; // 取消其他楼层的请求 foreach (var kvp in _ivFloorAdControllers) { if (kvp.Key != floor.id) { kvp.Value.Destroy(); } } } // 处理楼层广告加载失败 public void OnFloorAdFailed(FloorConfig floor, string error) { if (!_isRequestingFloors || _successfulFloor != null) return; LoggerUtils.Debug($"[kwai] floor inter Floor ad failed: {floor.id} with error: {error}"); // 检查当前批次是否全部失败 bool allFailedInBatch = true; foreach (var f in _currentRequestBatch) { if (f.id == floor.id) continue; if (_ivFloorAdControllers.ContainsKey(f.id) && _ivFloorAdControllers[f.id] != null && _ivFloorAdControllers[f.id].IsReady()) { allFailedInBatch = false; break; } } // 如果当前批次全部失败,请求下一批 if (allFailedInBatch) { RequestNextFloorBatch(); } } public bool IsInterstitialAdAvailable() { return _successfulFloor != null && _ivFloorAdControllers.ContainsKey(_successfulFloor.id) && _ivFloorAdControllers[_successfulFloor.id] != null && _ivFloorAdControllers[_successfulFloor.id].IsReady(); } public void ShowInterstitialAd(Action _action) { if (_successfulFloor != null && _ivFloorAdControllers.ContainsKey(_successfulFloor.id) && _ivFloorAdControllers[_successfulFloor.id] != null) { _ivFloorAdControllers[_successfulFloor.id].Show(); } else { _action?.Invoke(); } } } }