经验数据同步,更名逻辑上传
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Threading.Tasks;
|
||||
using TcgEngine.UI;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.Events;
|
||||
|
||||
@@ -243,7 +244,11 @@ namespace TcgEngine
|
||||
udata = ApiTool.JsonToObject<UserData>(res.data);
|
||||
// Debug.Log($"获取玩家数据:{res.data}");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Debug.LogError("失败!!!!!!!!!!!!");
|
||||
}
|
||||
Debug.Log($"{udata.id}");
|
||||
return udata;
|
||||
}
|
||||
|
||||
@@ -375,6 +380,32 @@ namespace TcgEngine
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateUsername(string newUsername)
|
||||
{
|
||||
if(!IsConnected())
|
||||
return false;
|
||||
|
||||
string url = ServerURL + "/users/username/edit";
|
||||
EditUsernameRequest req = new EditUsernameRequest();
|
||||
req.username = newUsername;
|
||||
string json = ApiTool.ToJson(req);
|
||||
|
||||
WebResponse res = await SendPostRequest(url, json);
|
||||
if (res.success)
|
||||
{
|
||||
Debug.Log($"更名前:{username}");
|
||||
username = newUsername;
|
||||
MainMenu.Get().RefreshUserData();
|
||||
PlayerPrefs.SetString("tcg_last_user", newUsername);
|
||||
Debug.Log($"更名后:{username}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("改名失败" + res.error);
|
||||
}
|
||||
return res.success;
|
||||
}
|
||||
|
||||
private string GetError(WebResponse res)
|
||||
{
|
||||
if (res.success)
|
||||
|
||||
@@ -52,6 +52,13 @@ namespace TcgEngine
|
||||
public string password_new;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct EditUsernameRequest
|
||||
{
|
||||
public string username;
|
||||
}
|
||||
|
||||
|
||||
[Serializable]
|
||||
public struct FriendAddRequest
|
||||
{
|
||||
|
||||
@@ -61,14 +61,7 @@ namespace TcgEngine
|
||||
return Mathf.FloorToInt(xp / 1000) + 1;
|
||||
}
|
||||
|
||||
public float GetLevelProgress()
|
||||
{
|
||||
int currentLeve = GetLevel();
|
||||
int baseXp = (currentLeve - 1) * 1000;
|
||||
int xpIntoCurrentLevel = xp - baseXp;
|
||||
return (float)xpIntoCurrentLevel / 1000f;
|
||||
|
||||
}
|
||||
|
||||
public string GetAvatar()
|
||||
{
|
||||
|
||||
@@ -53,6 +53,14 @@ namespace TcgEngine
|
||||
{
|
||||
return Mathf.FloorToInt(xp / 1000f) + 1;
|
||||
}
|
||||
|
||||
public float GetLevelProgress(int xp)
|
||||
{
|
||||
int currentLeve = GetPlayerLevel(xp);
|
||||
int baseXp = (currentLeve - 1) * 1000;
|
||||
int xpIntoCurrentLevel = xp - baseXp;
|
||||
return (float)xpIntoCurrentLevel / 1000f;
|
||||
}
|
||||
|
||||
public string GetRandomArena()
|
||||
{
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace TcgEngine.UI
|
||||
public UnityAction<string> onClick;
|
||||
|
||||
public string username;
|
||||
public string id;
|
||||
|
||||
public PlayerRank pd;
|
||||
|
||||
@@ -45,6 +46,7 @@ namespace TcgEngine.UI
|
||||
int rankId, int rankScore, int stars, bool highlight)
|
||||
{
|
||||
this.username = udata.username;
|
||||
id = udata.playerId;
|
||||
RankMedalShow(ranking);
|
||||
this.playerName.text = username;
|
||||
this.rankScore.text = rankScore.ToString();
|
||||
|
||||
@@ -47,6 +47,8 @@ namespace TcgEngine.UI
|
||||
private bool isHideObject = false;
|
||||
|
||||
public List<string> Usersid = new List<string>();
|
||||
|
||||
UserData udata = ApiClient.Get().UserData;
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
@@ -55,6 +57,7 @@ namespace TcgEngine.UI
|
||||
|
||||
my_line.onClick += OnClickLine;
|
||||
my_ladderLine.onClick += OnClickRankLine;
|
||||
|
||||
InitLines();
|
||||
}
|
||||
|
||||
@@ -123,7 +126,7 @@ namespace TcgEngine.UI
|
||||
return lrline;
|
||||
}
|
||||
|
||||
private async void RefreshPanel()
|
||||
public async void RefreshPanel()
|
||||
{
|
||||
my_line.Hide();
|
||||
foreach (RankLine line in lines)
|
||||
@@ -134,8 +137,6 @@ namespace TcgEngine.UI
|
||||
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);
|
||||
@@ -152,7 +153,7 @@ namespace TcgEngine.UI
|
||||
if (user.permission_level != 1 || user.matches == 0)
|
||||
continue; //Dont show admins and user with no matches
|
||||
|
||||
if (user.username == udata.username)
|
||||
if (user.id == udata.id)
|
||||
{
|
||||
my_line.SetLine(user, index + 1, true);
|
||||
}
|
||||
@@ -168,11 +169,9 @@ namespace TcgEngine.UI
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private async void RefreshLadderPanel()
|
||||
public async void RefreshLadderPanel()
|
||||
{
|
||||
my_ladderLine.Hide();
|
||||
foreach (LadderRankLine line in ladderLines)
|
||||
@@ -183,8 +182,6 @@ namespace TcgEngine.UI
|
||||
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);
|
||||
@@ -210,7 +207,7 @@ namespace TcgEngine.UI
|
||||
|
||||
foreach (LadderLeaderboardEntry rankData in sorted_users)
|
||||
{
|
||||
if (rankData.username == udata.username)
|
||||
if (rankData.playerId == udata.id)
|
||||
{
|
||||
my_ladderLine.SetLine(rankData, index + 1, GetAvatar(udata.avatar), rankData.rankId,
|
||||
rankData.rankScore, rankData.stars, true);
|
||||
@@ -287,6 +284,7 @@ namespace TcgEngine.UI
|
||||
public override void Show(bool instant = false)
|
||||
{
|
||||
base.Show(instant);
|
||||
udata = ApiClient.Get().UserData;
|
||||
RefreshPanel();
|
||||
RefreshLadderPanel();
|
||||
ladderRank.Show();
|
||||
|
||||
@@ -191,7 +191,7 @@ namespace TcgEngine.UI
|
||||
error_msg.text = "";
|
||||
test_area.SetActive(Authenticator.Get().IsTest());
|
||||
|
||||
string user = PlayerPrefs.GetString("tcg_last_user", "");
|
||||
string user = PlayerPrefs.GetString("tcg_last_user");
|
||||
login_user.text = user;
|
||||
|
||||
if (Authenticator.Get().IsTest())
|
||||
|
||||
@@ -119,8 +119,8 @@ namespace TcgEngine.UI
|
||||
credits_txt.text = GameUI.FormatNumber(user.coins);
|
||||
|
||||
// xp
|
||||
level_text.text = user.GetLevel().ToString();
|
||||
level_slider.value = user.GetLevelProgress();
|
||||
level_text.text = GameplayData.Get().GetPlayerLevel(user.xp).ToString();
|
||||
level_slider.value = GameplayData.Get().GetLevelProgress(user.xp);
|
||||
|
||||
AvatarData avatar = AvatarData.Get(user.avatar);
|
||||
this.avatar.SetAvatar(avatar);
|
||||
|
||||
@@ -15,7 +15,10 @@ namespace TcgEngine.UI
|
||||
{
|
||||
[Header("Player")]
|
||||
public Text player_name;
|
||||
public InputField renameInput;
|
||||
public Button renameButton;
|
||||
public Text player_level;
|
||||
public Slider player_level_slider;
|
||||
public AvatarUI avatar;
|
||||
public CardbackUI cardback;
|
||||
public Text elo;
|
||||
@@ -24,6 +27,7 @@ namespace TcgEngine.UI
|
||||
public Text victories;
|
||||
public Text defeats;
|
||||
public Text userId;
|
||||
public Text player_error;
|
||||
|
||||
[Header("Bottom bar")]
|
||||
public GameObject buttons_area;
|
||||
@@ -70,12 +74,13 @@ namespace TcgEngine.UI
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
base.Start();
|
||||
renameButton.onClick.AddListener(OnPlayerRename);
|
||||
}
|
||||
|
||||
private async void LoadData()
|
||||
@@ -95,6 +100,7 @@ namespace TcgEngine.UI
|
||||
winrate.text = "";
|
||||
player_level.text = "";
|
||||
userId.text = "";
|
||||
player_error.text = "";
|
||||
avatar.Hide();
|
||||
cardback.Hide();
|
||||
}
|
||||
@@ -109,6 +115,7 @@ namespace TcgEngine.UI
|
||||
UserData user = user_data;
|
||||
player_name.text = user.username;
|
||||
player_level.text = GameplayData.Get().GetPlayerLevel(user.xp).ToString();
|
||||
player_level_slider.value = GameplayData.Get().GetLevelProgress(user.xp);
|
||||
userId.text = user.id;
|
||||
|
||||
AvatarData avatar = AvatarData.Get(user.avatar);
|
||||
@@ -379,6 +386,35 @@ namespace TcgEngine.UI
|
||||
username = user;
|
||||
LoadData();
|
||||
}
|
||||
|
||||
private async void OnPlayerRename()
|
||||
{
|
||||
if (string.IsNullOrEmpty(renameInput.text))
|
||||
{
|
||||
player_error.text = "请输入更改的用户名!";
|
||||
}
|
||||
|
||||
else if (renameInput.text == player_name.text)
|
||||
{
|
||||
player_error.text = "新用户名不能和旧用户名一样!";
|
||||
}
|
||||
else
|
||||
{
|
||||
bool success = await ApiClient.Get().UpdateUsername(renameInput.text);
|
||||
if (success)
|
||||
{
|
||||
player_name.text = renameInput.text;
|
||||
renameInput.text = "";
|
||||
player_error.text = "修改成功!";
|
||||
LeaderboardPanel.Get()?.RefreshPanel();
|
||||
LeaderboardPanel.Get()?.RefreshLadderPanel();
|
||||
}
|
||||
else
|
||||
{
|
||||
player_error.text = ApiClient.Get().GetLastError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Show(bool instant = false)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user