311 lines
10 KiB
C#
311 lines
10 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace TcgEngine.UI
|
|
{
|
|
/// <summary>
|
|
/// Leaderboard panel contains the ranking of all top players
|
|
/// </summary>
|
|
|
|
public class LeaderboardPanel : UIPanel
|
|
{
|
|
#region 默认排行榜
|
|
[Header("-----默认排行榜参数------")]
|
|
public RectTransform content;
|
|
public RankLine line_template;
|
|
public RankLine my_line;
|
|
public float line_spacing = 80f;
|
|
[Header("----------------------")]
|
|
#endregion
|
|
|
|
#region 天梯排行榜
|
|
[Header("-----天梯排行榜参数------")]
|
|
public RectTransform ladderContent;
|
|
public LadderRankLine ladderLine_template;
|
|
public LadderRankLine my_ladderLine;
|
|
public float ladderLine_spacing = 120f;
|
|
[Header("----------------------")]
|
|
#endregion
|
|
|
|
public Text test_text;
|
|
|
|
public bool isLadderRank = true;
|
|
public UIPanel defaultRank;
|
|
public UIPanel ladderRank;
|
|
|
|
public List<RankLine> lines = new List<RankLine>();
|
|
public List<LadderRankLine> ladderLines = new List<LadderRankLine>();
|
|
|
|
private const string DefaultAvatarId = "bear";
|
|
|
|
private static LeaderboardPanel instance;
|
|
|
|
[Header("管理指定组件")]
|
|
public GameObject[] hideGameObject;
|
|
private bool isHideObject = false;
|
|
|
|
public List<string> Usersid = new List<string>();
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
instance = this;
|
|
//lines = scroll_content.GetComponentsInChildren<RankLine>();
|
|
|
|
my_line.onClick += OnClickLine;
|
|
my_ladderLine.onClick += OnClickRankLine;
|
|
InitLines();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
}
|
|
|
|
private void InitLines()
|
|
{
|
|
#region 加载默认排行榜
|
|
|
|
for (int i = 0; i < content.transform.childCount; i++)
|
|
Destroy(content.transform.GetChild(i).gameObject);
|
|
|
|
int nlines = 100;
|
|
for (int i = 0; i < nlines; i++)
|
|
{
|
|
RankLine line = AddLine(line_template, i);
|
|
lines.Add(line);
|
|
}
|
|
|
|
content.sizeDelta = new Vector2(content.sizeDelta.x, nlines * line_spacing + 20f);
|
|
|
|
#endregion
|
|
|
|
#region 加载天梯排行榜
|
|
|
|
for (int i = 0; i < ladderContent.transform.childCount; i++)
|
|
Destroy(ladderContent.transform.GetChild(i).gameObject);
|
|
|
|
int ladderNlines = 100;
|
|
for (int i = 0; i < ladderNlines; i++)
|
|
{
|
|
LadderRankLine line = LadderAddLine(ladderLine_template, i);
|
|
ladderLines.Add(line);
|
|
}
|
|
|
|
ladderContent.sizeDelta = new Vector2(content.sizeDelta.x, ladderNlines * ladderLine_spacing + 20f);
|
|
|
|
#endregion
|
|
}
|
|
|
|
private RankLine AddLine(RankLine template, int index)
|
|
{
|
|
Vector2 pos = Vector2.down * line_spacing;
|
|
GameObject line = Instantiate(template.gameObject, content);
|
|
RectTransform rtrans = line.GetComponent<RectTransform>();
|
|
RankLine rline = line.GetComponent<RankLine>();
|
|
rtrans.anchorMin = new Vector2(0.5f, 1f);
|
|
rtrans.anchorMax = new Vector2(0.5f, 1f);
|
|
rtrans.anchoredPosition = pos + Vector2.down * index * line_spacing;
|
|
rline.onClick += OnClickLine;
|
|
return rline;
|
|
}
|
|
|
|
private LadderRankLine LadderAddLine(LadderRankLine template, int index)
|
|
{
|
|
Vector2 pos = Vector2.down * (ladderLine_spacing-75);
|
|
GameObject line = Instantiate(template.gameObject, ladderContent);
|
|
RectTransform rtrans = line.GetComponent<RectTransform>();
|
|
LadderRankLine lrline = line.GetComponent<LadderRankLine>();
|
|
rtrans.anchorMin = new Vector2(0.5f, 1);
|
|
rtrans.anchorMax = new Vector2(0.5f, 1f);
|
|
rtrans.anchoredPosition = pos + Vector2.down * index * (ladderLine_spacing + 15);
|
|
lrline.onClick += OnClickRankLine;
|
|
return lrline;
|
|
}
|
|
|
|
private async void RefreshPanel()
|
|
{
|
|
my_line.Hide();
|
|
foreach (RankLine line in lines)
|
|
line.Hide();
|
|
|
|
test_text.enabled = !Authenticator.Get().IsApi();
|
|
|
|
if (!Authenticator.Get().IsApi())
|
|
return;
|
|
|
|
UserData udata = ApiClient.Get().UserData;
|
|
|
|
int index = 0;
|
|
string url = ApiClient.ServerURL + "/users";
|
|
WebResponse res = await ApiClient.Get().SendGetRequest(url);
|
|
|
|
UserData[] users = ApiTool.JsonToArray<UserData>(res.data);
|
|
List<UserData> sorted_users = new List<UserData>(users);
|
|
sorted_users.Sort((UserData a, UserData b) => { return b.elo.CompareTo(a.elo); });
|
|
|
|
int previous_rank = 0;
|
|
int previous_index = 0;
|
|
|
|
foreach (UserData user in sorted_users)
|
|
{
|
|
if (user.permission_level != 1 || user.matches == 0)
|
|
continue; //Dont show admins and user with no matches
|
|
|
|
if (user.username == udata.username)
|
|
{
|
|
my_line.SetLine(user, index + 1, true);
|
|
}
|
|
|
|
if (index < lines.Count)
|
|
{
|
|
RankLine line = lines[index];
|
|
int rank_order = (previous_rank == user.elo) ? previous_index : index;
|
|
line.SetLine(user, rank_order + 1, user.username == udata.username);
|
|
previous_rank = user.elo;
|
|
previous_index = rank_order;
|
|
}
|
|
|
|
index++;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private async void RefreshLadderPanel()
|
|
{
|
|
my_ladderLine.Hide();
|
|
foreach (LadderRankLine line in ladderLines)
|
|
line.Hide();
|
|
|
|
test_text.enabled = !Authenticator.Get().IsApi();
|
|
|
|
if (!Authenticator.Get().IsApi())
|
|
return;
|
|
|
|
UserData udata = ApiClient.Get().UserData;
|
|
|
|
int index = 0;
|
|
string url = ApiClient.ServerURL + "/ladder/leaderboard";
|
|
WebResponse res = await ApiClient.Get().SendGetRequest(url);
|
|
|
|
LadderLeaderboardEntry[] users = ApiTool.JsonToArray<LadderLeaderboardEntry>(res.data);
|
|
List<LadderLeaderboardEntry> sorted_users = new List<LadderLeaderboardEntry>(users);
|
|
sorted_users.Sort((a, b) =>
|
|
{
|
|
return a.position.CompareTo(b.position);
|
|
});
|
|
|
|
int previous_rank = 0;
|
|
int previous_index = 0;
|
|
|
|
if (users == null || users.Length == 0)
|
|
{
|
|
Debug.LogError("玩家数据为空");
|
|
return;
|
|
}
|
|
|
|
Debug.Log($"收到排行榜数据,共 {users.Length} 条");
|
|
ladderContent.sizeDelta = new Vector2(content.sizeDelta.x, users.Length * (ladderLine_spacing + 20f));
|
|
|
|
foreach (LadderLeaderboardEntry rankData in sorted_users)
|
|
{
|
|
if (rankData.username == udata.username)
|
|
{
|
|
my_ladderLine.SetLine(rankData, index + 1, GetAvatar(udata.avatar), rankData.rankId,
|
|
rankData.rankScore, rankData.stars, true);
|
|
}
|
|
|
|
if (index < ladderLines.Count)
|
|
{
|
|
LadderRankLine line = ladderLines[index];
|
|
int rank_order = (previous_rank == rankData.position) ? previous_index : index;
|
|
line.SetLine(rankData, rank_order + 1,GetAvatar(rankData.avatar), rankData.rankId,
|
|
rankData.rankScore, rankData.stars, false);
|
|
previous_rank = rankData.position;
|
|
previous_index = rank_order;
|
|
Usersid.Add(rankData.playerId);
|
|
//68a6ca87c1f7ac52b66ef8dc
|
|
}
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// private void RankPanelColl()
|
|
// {
|
|
// if (isLadderRank)
|
|
// {
|
|
// ladderRank.Show();
|
|
// var lColor = ladderRankButton.image.color;
|
|
// lColor.a = 0.5f;
|
|
// ladderRankButton.image.color = lColor;
|
|
//
|
|
//
|
|
// defaultRank.Hide();
|
|
// var dColor = defaultRankButton.image.color;
|
|
// dColor.a = 1f;
|
|
// defaultRankButton.image.color = dColor;
|
|
// }
|
|
// else
|
|
// {
|
|
// ladderRank.Hide();
|
|
// var lColor = ladderRankButton.image.color;
|
|
// lColor.a = 1f;
|
|
// ladderRankButton.image.color = lColor;
|
|
//
|
|
// defaultRank.Show();
|
|
// var dColor = defaultRankButton.image.color;
|
|
// dColor.a = 0.5f;
|
|
// defaultRankButton.image.color = dColor;
|
|
//
|
|
// }
|
|
// ladderRankButton.enabled = !isLadderRank;
|
|
// defaultRankButton.enabled = isLadderRank;
|
|
// }
|
|
|
|
private void OnClickLine(string username)
|
|
{
|
|
|
|
}
|
|
|
|
private void OnClickRankLine(string username)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
private Sprite GetAvatar(string id)
|
|
{
|
|
string targetId = string.IsNullOrEmpty(id) ? DefaultAvatarId : id;
|
|
AvatarData avaData = AvatarData.Get(targetId);
|
|
return avaData != null ? avaData.GetAvatar() : null;
|
|
}
|
|
|
|
|
|
public override void Show(bool instant = false)
|
|
{
|
|
base.Show(instant);
|
|
RefreshPanel();
|
|
RefreshLadderPanel();
|
|
ladderRank.Show();
|
|
}
|
|
|
|
public override void Hide(bool instant = false)
|
|
{
|
|
base.Hide(instant);
|
|
ladderRank.Hide();
|
|
}
|
|
|
|
public void OnClickBack()
|
|
{
|
|
Hide();
|
|
}
|
|
|
|
public static LeaderboardPanel Get()
|
|
{
|
|
return instance;
|
|
}
|
|
}
|
|
} |