Files
back_cantanBuilding/Assets/Scripts/UI/FaceUIService.cs
2026-05-26 16:15:54 +08:00

304 lines
9.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using asap.core;
using cfg;
using game;
using GameCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
public struct FaceUIData
{
public FaceUIData(FaceUI faceUI, int redirectID)
{
this.faceUI = faceUI;
this.redirectID = redirectID;
}
public FaceUI faceUI;
public int redirectID;
}
public struct FaceGiftUIData
{
public FaceGiftUIData(UIType faceUI, int redirectID, int PackType)
{
this.faceUI = faceUI;
this.redirectID = redirectID;
this.PackType = PackType;
}
public int PackType;
public UIType faceUI;
public int redirectID;
}
public struct FaceCustomUIData
{
public FaceCustomUIData(UIType faceUI)
{
this.faceUI = faceUI;
}
public UIType faceUI;
}
public class FaceUICloseEvent
{
}
public interface IFaceUIService
{
void AddFaceCustomUIData(UIType faceUI, bool forceAdd = false);
void AddFaceUIData(int id, int redirectID, int NoticeType);
void ShowFaceUI(string uiName = "");
void AddGiftFaceUI(int id, UIType faceUI, int PackType, bool forceAdd = false);
int GetFaceUICount();
void StopFaceUI();
public int curRedirectID { set; get; }
}
public class FaceUIService : IFaceUIService
{
List<FaceUIData> faceUIDatas = new List<FaceUIData>();
List<FaceGiftUIData> FaceGiftUIDatas = new List<FaceGiftUIData>();
Queue<FaceCustomUIData> faceCustomUIDatas = new Queue<FaceCustomUIData>();
public int curRedirectID { set; get; }
bool isFace;
public int GetFaceUICount()
{
return faceUIDatas.Count + FaceGiftUIDatas.Count + faceCustomUIDatas.Count;
}
void RemoveFace(string uiName)
{
if (faceCustomUIDatas.Count > 0)
{
var data = faceCustomUIDatas.Peek();
if (data.faceUI.Name == uiName)
{
faceCustomUIDatas.Dequeue();
return;
}
}
if (FaceGiftUIDatas.Count > 0)
{
int result = 0;// FaceGiftUIDatas.RemoveAll(x => x.faceUI.Name == uiName);
for (int i = 0; i < FaceGiftUIDatas.Count; i++)
{
if (FaceGiftUIDatas[i].faceUI.Name == uiName)
{
FaceGiftUIDatas.RemoveAt(i);
result = 1;
break;
}
}
if (result > 0)
{
return;
}
}
if (faceUIDatas.Count > 0)
{
int result = faceUIDatas.RemoveAll(x => x.faceUI.Prefab == uiName);
if (result > 0)
{
return;
}
}
}
public async void ShowFaceUI(string uiName = "")
{
CurRewardQCount curRewardQCount = new CurRewardQCount();
if (!string.IsNullOrEmpty(uiName))
{
GContext.Publish(curRewardQCount);
RemoveFace(uiName);
}
//触发礼包拍脸
if (isFace)
{
return;
}
if (FaceGiftUIDatas.Count > 0 && curRewardQCount.count == 0)
{
var data = FaceGiftUIDatas[0];
UIType faceUI = data.faceUI;
curRedirectID = data.redirectID;
ShowUI(data.faceUI, faceUI.Name);
await Awaiters.NextFrame;
curRedirectID = 0;
return;
}
//FaceUI配置中的拍脸
if (faceUIDatas.Count > 0 && curRewardQCount.count == 0)
{
var data = faceUIDatas[0];
FaceUI faceUI = data.faceUI;
curRedirectID = data.redirectID;
UIType uIType = UITypes.GetUIType(faceUI.Prefab);
if (uIType != null)
{
ShowUI(uIType, faceUI.Prefab);
await Awaiters.NextFrame;
curRedirectID = 0;
return;
}
}
//不在配置中的拍脸
if (faceCustomUIDatas.Count > 0 && curRewardQCount.count == 0)
{
var data = faceCustomUIDatas.Peek();
ShowUI(data.faceUI, data.faceUI.Name);
return;
}
GContext.Publish(new FaceUICloseEvent());
}
async void ShowUI(UIType ui_type, string uiname)
{
isFace = true;
GameObject go = await UIManager.Instance.ShowUIPack(ui_type);
if (go == null)
{
isFace = false;
ShowFaceUI(uiname);
}
else
{
NextFaceUI(go, uiname);
}
}
TaskCompletionSource<bool> ConfirmTask;
async void NextFaceUI(GameObject go, string uiname)
{
try
{
ConfirmTask = new TaskCompletionSource<bool>();
AwaitConfirmTask(go);
await ConfirmTask.Task;
isFace = false;
ShowFaceUI(uiname);
}
catch (OperationCanceledException)
{
Debug.Log($"<color=yellow> Manual Stop Face => {uiname}</color>");
//打断拍脸并删除当前拍脸
RemoveFace(uiname);
}
finally
{
isFace = false;
if (ConfirmTask != null)
{
ConfirmTask.Task?.Dispose();
ConfirmTask = null;
}
}
}
async void AwaitConfirmTask(GameObject go)
{
//当gameObject.activeSelf==false时说明UI已经被关闭
await Awaiters.Until(() => go == null || !go.activeSelf);
isFace = false;
if (ConfirmTask != null && !ConfirmTask.Task.IsCompleted)
{
ConfirmTask.SetResult(true);
}
}
//打断拍脸
public void StopFaceUI()
{
isFace = false;
if (ConfirmTask != null)
{
ConfirmTask.SetCanceled();
}
}
public void AddFaceCustomUIData(UIType faceUI, bool forceAdd = false)
{
bool idAdd = false;
int day = ZZTimeHelper.UtcNow().UtcNowOffset().DayOfYear;
string userID = GContext.container.Resolve<IUserService>().UserId;
if (PlayerPrefs.GetInt($"{faceUI.Name}_{userID}", 0) != day)
{
PlayerPrefs.SetInt($"{faceUI.Name}_{userID}", day);
idAdd = true;
}
if (idAdd || forceAdd)
{
FaceCustomUIData faceUIData = new FaceCustomUIData(faceUI);
faceCustomUIDatas.Enqueue(faceUIData);
}
}
public void AddGiftFaceUI(int id, UIType faceUI, int PackType, bool forceAdd = false)
{
bool idAdd = false;
if (!forceAdd)
{
TbGlobalConfig tbGlobalConfig = GContext.container.Resolve<Tables>().TbGlobalConfig;
if (FaceGiftUIDatas.Count >= tbGlobalConfig.ShowPopupPack || FaceGiftUIDatas.Where(_ => _.faceUI == faceUI).Count() > 0)
{
return;
}
int day = ZZTimeHelper.UtcNow().UtcNowOffset().DayOfYear;
//本地存储
string userID = GContext.container.Resolve<IUserService>().UserId;
if (PlayerPrefs.GetInt($"{id}_{userID}", 0) != day)
{
PlayerPrefs.SetInt($"{id}_{userID}", day);
idAdd = true;
}
}
if (idAdd || forceAdd)
{
//可能多个活动用同一个界面,所以使用有时不能用界面名判断 用 id 判断
if (FaceGiftUIDatas.Where(_ => _.redirectID == id).Count() > 0)
{
return;
}
FaceGiftUIData faceUIData = new FaceGiftUIData(faceUI, id, PackType);
FaceGiftUIDatas.Add(faceUIData);
FaceGiftUIDatas.Sort((a, b) => a.PackType.CompareTo(b.PackType));
}
}
public void AddFaceUIData(int id, int redirectID, int NoticeType)
{
TbGlobalConfig tbGlobalConfig = GContext.container.Resolve<Tables>().TbGlobalConfig;
FaceUI faceUI = GContext.container.Resolve<Tables>().TbFaceUI.GetOrDefault(id);
if (faceUIDatas.Count >= tbGlobalConfig.ShowPopupEvent && faceUI.ServerType == 0)
{
int allCount = faceUIDatas.Where(x => x.faceUI.ServerType == 0).Count();
if (allCount >= tbGlobalConfig.ShowPopupEvent)
{
return;
}
}
if (faceUI == null)
{
return;
}
bool idAdd = false;
if (faceUI.NoticeType == NoticeType)
{
int day = ZZTimeHelper.UtcNow().UtcNowOffset().DayOfYear;
string userID = GContext.container.Resolve<IUserService>().UserId;
if (PlayerPrefs.GetInt($"{id}_{userID}", 0) != day)
{
PlayerPrefs.SetInt($"{id}_{userID}", day);
PlayerPrefs.SetInt($"{faceUI.Prefab}_{userID}", 0);
}
int count = PlayerPrefs.GetInt($"{faceUI.Prefab}_{userID}", 0);
if (count < faceUI.NoticeParam)
{
PlayerPrefs.SetInt($"{faceUI.Prefab}_{userID}", count + 1);
idAdd = true;
}
}
if (idAdd || (faceUI.ServerType == 1 && NoticeType == 2))
{
FaceUIData faceUIData = new FaceUIData(faceUI, redirectID);
faceUIDatas.Add(faceUIData);
faceUIDatas.Sort((a, b) => a.faceUI.FaceOrder.CompareTo(b.faceUI.FaceOrder));
}
}
}