152 lines
4.8 KiB
C#
152 lines
4.8 KiB
C#
using System.Collections.Generic;
|
|
using Logic.Multilingual;
|
|
using RuntimeData;
|
|
using TH1_Core.Events;
|
|
using TH1_Core.Managers;
|
|
using TH1_Logic.Core;
|
|
using TH1_Logic.Net;
|
|
using TH1_Logic.Steam;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
// 需要引入此命名空间以处理指针事件
|
|
|
|
namespace TH1_UI.View.Bottom
|
|
{
|
|
public class UIBottomNetRowMono : MonoBehaviour
|
|
{
|
|
|
|
|
|
public TextMeshProUGUI Name;
|
|
public TextMeshProUGUI Prop;
|
|
public TextMeshProUGUI Status;
|
|
public Image NetworkImage;
|
|
public Sprite Network_0;
|
|
public Sprite Network_1;
|
|
public Sprite Network_2;
|
|
public Sprite Network_3;
|
|
public Button InviteButton;
|
|
public TextMeshProUGUI InviteButtonText;
|
|
public Image InviteButtonImage;
|
|
public Sprite InviteButtonBG;
|
|
public Sprite AlreadyInvitedButtonBG;
|
|
private ulong _memberid;
|
|
private bool _isNetOk;
|
|
private bool _isRoomOwner;
|
|
private bool _invited;
|
|
public ulong MemberId => _memberid;
|
|
|
|
|
|
private void SetNoInvite()
|
|
{
|
|
InviteButton.gameObject.SetActive(false);
|
|
|
|
}
|
|
|
|
private void SetInvite()
|
|
{
|
|
_invited = false;
|
|
InviteButton.gameObject.SetActive(true);
|
|
InviteButtonText.text = MultilingualManager.Instance.GetMultilingualText(Table.Instance.TextDataAssets.MultiplayInviteButton);
|
|
InviteButtonImage.sprite = InviteButtonBG;
|
|
}
|
|
|
|
private void SetAlreadyInvited()
|
|
{
|
|
_invited = true;
|
|
InviteButton.gameObject.SetActive(true);
|
|
InviteButtonText.text = MultilingualManager.Instance.GetMultilingualText(Table.Instance.TextDataAssets.MultiplayAlreadyInvitedButton);
|
|
InviteButtonImage.sprite = AlreadyInvitedButtonBG;
|
|
}
|
|
|
|
public void InitContent(ulong memberid,MemberInfo memberInfo,bool isRoomOwner,bool isNetOk)
|
|
{
|
|
_memberid = memberid;
|
|
_isNetOk = isNetOk;
|
|
_isRoomOwner = isRoomOwner;
|
|
|
|
Name.text = memberInfo.Name;
|
|
MultilingualManager.Instance.SetUIText(Prop,isRoomOwner?Table.Instance.TextDataAssets.MultiplayRoomOwnerTitle:Table.Instance.TextDataAssets.MultiplayRoomGuestTitle);
|
|
UpdatePingStatus(isNetOk);
|
|
|
|
}
|
|
|
|
public void UpdateNetStatus(bool isRoomOwner,bool isNetOk)
|
|
{
|
|
_isNetOk = isNetOk;
|
|
_isRoomOwner = isRoomOwner;
|
|
MultilingualManager.Instance.SetUIText(Prop,isRoomOwner?Table.Instance.TextDataAssets.MultiplayRoomOwnerTitle:Table.Instance.TextDataAssets.MultiplayRoomGuestTitle);
|
|
UpdatePingStatus(isNetOk);
|
|
//处理是否显示Invite按钮
|
|
if (isNetOk) SetNoInvite();
|
|
else
|
|
{
|
|
//如果正在一个冷却周期,就不处理
|
|
if (_invited) return;
|
|
SetInvite();
|
|
InviteButton.onClick.RemoveAllListeners();
|
|
InviteButton.onClick.AddListener(()=>
|
|
{
|
|
var lobby = LobbyManager.Instance.Lobby as SteamLobbyManager;
|
|
lobby.InviteFriend(_memberid);
|
|
InviteButton.gameObject.SetActive(false);
|
|
|
|
if (_invited) return;
|
|
Timer.Instance.TimerRegister(this, () =>
|
|
{
|
|
SetInvite();
|
|
},3f,"UIBottomNetRowMono_InviteClick");
|
|
SetAlreadyInvited();
|
|
|
|
});
|
|
}
|
|
}
|
|
|
|
private void UpdatePingStatus(bool isNetOk)
|
|
{
|
|
if (!isNetOk)
|
|
{
|
|
Status.text = "Timeout";
|
|
SetNetworkSprite(0);
|
|
return;
|
|
}
|
|
|
|
var confirm = Main.Instance?.ConfirmMap?.GetPlayerConfirm(_memberid);
|
|
if (confirm == null)
|
|
{
|
|
Status.text = "-- ms";
|
|
SetNetworkSprite(0);
|
|
return;
|
|
}
|
|
|
|
var pingMs = Mathf.RoundToInt(confirm.GetPing() * 1000f);
|
|
Status.text = $"{pingMs} ms";
|
|
SetNetworkSprite(GetNetworkLevel(pingMs));
|
|
}
|
|
|
|
private int GetNetworkLevel(int pingMs)
|
|
{
|
|
if (pingMs < 0) return 0;
|
|
if (pingMs < 100) return 3;
|
|
if (pingMs < 200) return 2;
|
|
if (pingMs < 500) return 1;
|
|
return 0;
|
|
}
|
|
|
|
private void SetNetworkSprite(int level)
|
|
{
|
|
if (NetworkImage == null) return;
|
|
NetworkImage.sprite = level switch
|
|
{
|
|
3 => Network_3,
|
|
2 => Network_2,
|
|
1 => Network_1,
|
|
_ => Network_0
|
|
};
|
|
}
|
|
|
|
|
|
|
|
}
|
|
} |