139 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			139 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace WZ
 | |
| {
 | |
|     [Serializable]
 | |
|     public class AdTypeBidState
 | |
|     {
 | |
|         public AdsType AdType;
 | |
|         public DateTime LastBidSuccessTime;
 | |
|         
 | |
|         public AdTypeBidState(AdsType adType)
 | |
|         {
 | |
|             AdType = adType;
 | |
|             LastBidSuccessTime = DateTime.MinValue;
 | |
|         }
 | |
|         
 | |
|         public bool IsBidExpired(int expireInSec)
 | |
|         {
 | |
|             // 如果功能关闭,则不视为过期
 | |
|             if (expireInSec == -1) return false;
 | |
|             
 | |
|             // 如果从未成功竞价过,则视为过期
 | |
|             // if (LastBidSuccessTime == DateTime.MinValue) return true;
 | |
|             
 | |
|             // 检查是否超过过期时间
 | |
|             TimeSpan timeSinceLastBid = DateTime.Now - LastBidSuccessTime;
 | |
|             return timeSinceLastBid.TotalSeconds > expireInSec;
 | |
|         }
 | |
|     }
 | |
|     
 | |
|     [Serializable]
 | |
|     public class BidPlatformState
 | |
|     {
 | |
|         public PlatformType PlatformName;
 | |
|         public bool IsEnabled = true;
 | |
|         
 | |
|         // 每种广告类型的竞价状态
 | |
|         public Dictionary<AdsType, AdTypeBidState> AdTypeStates = new Dictionary<AdsType, AdTypeBidState>();
 | |
|         
 | |
|         public BidPlatformState(PlatformType platformName)
 | |
|         {
 | |
|             PlatformName = platformName;
 | |
|             
 | |
|             // 初始化所有广告类型的状态
 | |
|             foreach (AdsType adType in Enum.GetValues(typeof(AdsType)))
 | |
|             {
 | |
|                 AdTypeStates.Add(adType, new AdTypeBidState(adType));
 | |
|             }
 | |
|         }
 | |
|         
 | |
|         // 记录特定广告类型的竞价成功
 | |
|         public void RecordBidSuccess(AdsType adType)
 | |
|         {
 | |
|             if (AdTypeStates.TryGetValue(adType, out var state))
 | |
|             {
 | |
|                 state.LastBidSuccessTime = DateTime.Now;
 | |
|                 Debug.Log($"{PlatformName} 平台 {adType} 广告竞价成功,记录时间: {state.LastBidSuccessTime}");
 | |
|             }
 | |
|         }
 | |
|         
 | |
|         // 检查特定广告类型的竞价是否过期
 | |
|         public bool IsBidExpired(AdsType adType, int expireInSec)
 | |
|         {
 | |
|             if (!IsEnabled || expireInSec == -1) return false;
 | |
|             
 | |
|             if (AdTypeStates.TryGetValue(adType, out var state))
 | |
|             {
 | |
|                 return state.IsBidExpired(expireInSec);
 | |
|             }
 | |
|             
 | |
|             return false;
 | |
|         }
 | |
|         
 | |
|         // 获取所有过期的广告类型
 | |
|         public List<AdsType> GetExpiredAdTypes(int expireInSec)
 | |
|         {
 | |
|             List<AdsType> expiredAdTypes = new List<AdsType>();
 | |
|             
 | |
|             if (!IsEnabled || expireInSec == -1) return expiredAdTypes;
 | |
|             
 | |
|             foreach (var kvp in AdTypeStates)
 | |
|             {
 | |
|                 if (kvp.Value.IsBidExpired(expireInSec))
 | |
|                 {
 | |
|                     expiredAdTypes.Add(kvp.Key);
 | |
|                 }
 | |
|             }
 | |
|             
 | |
|             return expiredAdTypes;
 | |
|         }
 | |
|     }
 | |
|     public class BidPlatformManager : NormalSingleton<BidPlatformManager>
 | |
|     {
 | |
|         private Dictionary<PlatformType, BidPlatformState> _platformStates = new Dictionary<PlatformType, BidPlatformState>();
 | |
|         private int _bidExpireInSec = -1;
 | |
| 
 | |
|         public void InitializePlatformStates(int expireInSec)
 | |
|         {
 | |
|             _bidExpireInSec = expireInSec;
 | |
|             _platformStates.Clear();
 | |
|             _platformStates.Add(PlatformType.Admob, new BidPlatformState(PlatformType.Admob));
 | |
|             _platformStates.Add(PlatformType.Topon, new BidPlatformState(PlatformType.Topon));
 | |
|             _platformStates.Add(PlatformType.AppLovin, new BidPlatformState(PlatformType.AppLovin));
 | |
|             _platformStates.Add(PlatformType.Bigo, new BidPlatformState(PlatformType.Bigo));
 | |
|             _platformStates.Add(PlatformType.Kwai, new BidPlatformState(PlatformType.Kwai));
 | |
|         }
 | |
| 
 | |
|         // 记录平台竞价成功
 | |
|         public void RecordBidSuccess(PlatformType platformName, AdsType adType)
 | |
|         {
 | |
|             if (_platformStates.TryGetValue(platformName, out var state))
 | |
|             {
 | |
|                 state.RecordBidSuccess(adType);
 | |
|             }
 | |
|         }
 | |
|         // 获取所有过期平台
 | |
|         public Dictionary<PlatformType, List<AdsType>> GetExpiredBids()
 | |
|         {
 | |
|             Dictionary<PlatformType, List<AdsType>> expiredBids = new Dictionary<PlatformType, List<AdsType>>();
 | |
| 
 | |
|             if (_bidExpireInSec == -1) return expiredBids;
 | |
| 
 | |
|             foreach (var kvp in _platformStates)
 | |
|             {
 | |
|                 List<AdsType> expiredAdTypes = kvp.Value.GetExpiredAdTypes(_bidExpireInSec);
 | |
|                 if (expiredAdTypes.Count > 0)
 | |
|                 {
 | |
|                     expiredBids.Add(kvp.Key, expiredAdTypes);
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             return expiredBids;
 | |
|         }
 | |
|     }
 | |
| }
 |