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

266 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using EnhancedUI.EnhancedScroller;
using UnityEngine;
using ScrollViewItemInfo = EventPartnerScrollViewItem.ScrollViewItemInfo;
using EScrollViewType = EventPartnerScrollViewItem.EScrollViewType;
using asap.core;
using Castle.Core.Internal;
using game;
using GameCore;
using UniRx;
using UnityEngine.UI;
public class EventPartnerInvitedPanel : MonoBehaviour, IEnhancedScrollerDelegate
{
private readonly ICustomServerMgr _customServerMgr = GContext.container.Resolve<ICustomServerMgr>();
private readonly EventPartnerData _eventPartnerData = GContext.container.Resolve<EventPartnerData>();
private readonly IUserService _userService = GContext.container.Resolve<IUserService>();
[SerializeField] private Button btnClose;
private readonly string PlayerFullKey = "UI_ToastPanel_80", PartnerFullKey = "UI_ToastPanel_99";
private async void Start()
{
btnClose.onClick.AddListener(OnClickClose);
await InitScrollView();
EventPartnerAct.Ctx.EventAggregator.GetEvent<EventPartnerClickAccept>()
.Subscribe(e => _ = OnAccept(e.Info)).AddTo(this);
EventPartnerAct.Ctx.EventAggregator.GetEvent<EventPartnerClickIgnore>()
.Subscribe(e => _ = OnIgnore(e.Info.PlayfabId)).AddTo(this);
}
private void HideBtn()
{
EventPartnerAct.Ctx.EventAggregator.Publish(new EventPartnerHideApplicationBtn());
}
private void OnClickClose()
{
UIManager.Instance.DestroyUI(UITypes.EventPartnerInvitedPanel);
}
#region ScrollView
[SerializeField] private EnhancedScroller scrollView;
[SerializeField] private float jiandaItemHeight = 150f;
[SerializeField] private EventPartnerScrollViewItem scrollViewItemPrefab;
private List<ScrollViewItemInfo> _itemInfos = new List<ScrollViewItemInfo>();
private async System.Threading.Tasks.Task InitScrollView()
{
await RequestItemInfo();
var invitationList = _eventPartnerData.RequestPartners.ToList();
// foreach (Transform child in scrollView.transform)
// {
// Destroy(child.gameObject);
// }
scrollView.Delegate = this;
scrollView.ReloadData();
}
public int GetNumberOfCells(EnhancedScroller scroller)
{
return _itemInfos.Count;
}
public float GetCellViewSize(EnhancedScroller scroller, int dataIndex)
{
return jiandaItemHeight;
}
public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
{
var info = _itemInfos[dataIndex];
if (scroller.GetCellView(scrollViewItemPrefab) is not EventPartnerScrollViewItem cellView)
{
Debug.Log("<color=red>[EventPartner] Cell view is null.</color>");
return null;
}
cellView.Init(info);
return cellView;
}
private async System.Threading.Tasks.Task OnAccept(ScrollViewItemInfo info)
{
if (_eventPartnerData.IsFullyMatched)
{
Debug.Log($"<color=#c191ff>[EventPartner]Cannot accept. Fully matched.</color>");
ToastPanel.Show(LocalizationMgr.GetText(PlayerFullKey));
return;
}
var request = new EventPartnerData.EventBuildPartnerRequest { partnerId = info.PlayfabId, eventId = _eventPartnerData.EventId};
try
{
var response = await _customServerMgr.EventPartnerRequest<EventPartnerData.EventBuildOperationResp>(EventPartnerData.AcceptUrl, request);
if (response.State != EventPartnerData.EEventBuildState.Success)
{
Debug.Log($"<color=#c191ff>[EventPartner]{response.State}: {response.ErrorMessage}</color>");
ToastPanel.Show(LocalizationMgr.GetText(PartnerFullKey));
}
else
{
var idx = response.SlotId ?? throw new Exception($"Null slot id for {info.DisplayName}");
_eventPartnerData.Components[idx].SetComponent(info);
_ = EventPartnerAct.Ctx.ShowMainPanel(true, info.DisplayName, info.AvatarUrl, _eventPartnerData.Components[idx].GetComponentName());
}
for (int i = 0; i < _itemInfos.Count; i++)
{
if (_itemInfos[i].PlayfabId != info.PlayfabId)
continue;
_itemInfos.RemoveAt(i);
break;
}
_eventPartnerData.Cache.ListInvitedBy = _eventPartnerData.Cache.ListInvitedBy.Where(i => i.PlayfabId != info.PlayfabId).ToList();
_eventPartnerData.RequestPartners.Remove(info.PlayfabId);
_eventPartnerData.SavePlayerPreferenceData();
scrollView.ReloadData();
}
catch (Exception e)
{
Debug.Log($"<color=#c191ff>[EventPartner]{e.Message}</color>");
}
if (_itemInfos.Count <= 0 || _eventPartnerData.IsFullyMatched)
{
OnClickClose();
HideBtn();
}
}
private async System.Threading.Tasks.Task OnIgnore(string partnerId)
{
var request = new EventPartnerData.EventBuildPartnerRequest { partnerId = partnerId , eventId = _eventPartnerData.EventId};
try
{
var response = await _customServerMgr
.EventPartnerRequest<EventPartnerData.EventBuildOperationResp>(
EventPartnerData.IgnoreUrl, request);
if (response.State != EventPartnerData.EEventBuildState.Success)
Debug.Log(
$"<color=#c191ff>[EventPartner]{response.State}: {response.ErrorMessage}</color>");
else
{
for (int i = 0; i < _itemInfos.Count; i++)
{
if (_itemInfos[i].PlayfabId != partnerId)
continue;
_itemInfos.RemoveAt(i);
break;
}
_eventPartnerData.Cache.ListInvitedBy = _eventPartnerData.Cache.ListInvitedBy.Where(info => info.PlayfabId != partnerId).ToList();
_eventPartnerData.RequestPartners.Remove(partnerId);
_eventPartnerData.SavePlayerPreferenceData();
scrollView.ReloadData();
}
}
catch (Exception e)
{
Debug.Log($"<color=#c191ff>[EventPartner]{e.Message}</color>");
}
if (_itemInfos.Count <= 0)
{
OnClickClose();
HideBtn();
}
}
private async System.Threading.Tasks.Task RequestItemInfo()
{
var originSet = new HashSet<string>();
foreach (var partner in _eventPartnerData.RequestPartners)
{
originSet.Add(partner);
}
// Find in cache
if (_eventPartnerData.Cache?.ListInvitedBy is { Count: > 0 })
{
foreach (var info in _eventPartnerData.Cache.ListInvitedBy.Where(info =>
originSet.Contains(info.PlayfabId)))
{
originSet.Remove(info.PlayfabId);
_itemInfos.Add(info);
}
}
if (originSet.IsNullOrEmpty())
return;
// Find in friendList
var friendDictionary = GContext.container.Resolve<FriendService>().FriendList
.ToDictionary(f => f.playFabId);
if (!friendDictionary.IsNullOrEmpty())
{
var i = originSet.Intersect(friendDictionary.Keys);
_itemInfos.AddRange(i.Select(s => new ScrollViewItemInfo
{
Type = EScrollViewType.Partner2Accept,
PlayfabId = friendDictionary[s].playFabId,
AvatarUrl = friendDictionary[s].avatarUrl,
DisplayName = friendDictionary[s].displayName,
Level = friendDictionary[s].value / LeadboardData.LV_MODELING
}));
originSet.RemoveWhere(friendDictionary.ContainsKey);
}
if (originSet.IsNullOrEmpty())
return;
// Find in server cache
var request = new EventPartnerData.EventBuildRecentPlayerInfoRequest();
request.playerPrefabs = originSet.ToArray();
var resp = await _customServerMgr
.EventPartnerRequest<EventPartnerData.EventBuildRecentPlayerInfoResponse>(
EventPartnerData.MatchRecentPlayerInfoUrl, request);
if (resp.State == EventPartnerData.EEventBuildState.Success && !resp.playerInfos.IsNullOrEmpty())
{
originSet = resp.leftOvers.ToHashSet();
_itemInfos.AddRange(resp.playerInfos.Select(p => new ScrollViewItemInfo
{
Type = EScrollViewType.Partner2Accept, PlayfabId = p.PlayFabId,
AvatarUrl = p.AvatarUrl,
DisplayName = p.DisplayName, Level = p.Level
}));
if (originSet.IsNullOrEmpty())
return;
}
// Find in server database.
var displayInfoRequest = new EventPartnerData.EventBuildPlayerDisplayInfoRequest();
displayInfoRequest.PlayfabIds = originSet.ToArray();
var res = await _customServerMgr
.EventPartnerRequest<EventPartnerData.EventBuildPlayerDisplayInfoResp>(
EventPartnerData.PlayerDisplayInfoUrl, displayInfoRequest);
if (res.State != EventPartnerData.EEventBuildState.Success || res.PlayerInfos.IsNullOrEmpty())
Debug.Log($"<color=#c191ff>[EventPartner]Server response in invite panel info: {res.Message}</color>");
foreach (var info in res.PlayerInfos)
{
originSet.Remove(info.PlayFabId);
_itemInfos.Add(new ScrollViewItemInfo
{
Type = EScrollViewType.Partner2Accept, PlayfabId = info.PlayFabId,
AvatarUrl = info.AvatarUrl, DisplayName = info.DisplayName, Level = info.Level
});
}
if (originSet.IsNullOrEmpty())
return;
// Show default
_itemInfos.AddRange(originSet.Select(p =>
new ScrollViewItemInfo
{
Type = EScrollViewType.Partner2Accept,
PlayfabId = p,
AvatarUrl = "",
DisplayName = _userService.GetDefaultName(p),
Level = 60
}));
}
#endregion
}