Add a user search menu
This commit is contained in:
39
Assets/Scripts/Types/Account.cs
Normal file
39
Assets/Scripts/Types/Account.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Numerics;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine.Scripting;
|
||||
|
||||
[Preserve]
|
||||
public class Account
|
||||
{
|
||||
[Preserve]
|
||||
[JsonProperty("id")]
|
||||
public BigInteger UserID { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("username")]
|
||||
public string Username { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("icon")]
|
||||
public int Icon { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("overlay")]
|
||||
public int Overlay { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("birdColor")]
|
||||
public int[] BirdColor { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("overlayColor")]
|
||||
public int[] OverlayColor { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("customIcon")]
|
||||
public string CustomIcon { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("stats")]
|
||||
public Stats Stats { get; set; }
|
||||
}
|
||||
2
Assets/Scripts/Types/Account.cs.meta
Normal file
2
Assets/Scripts/Types/Account.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98be53ab03e46424096c025c9dafd0eb
|
||||
47
Assets/Scripts/Types/Stats.cs
Normal file
47
Assets/Scripts/Types/Stats.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Numerics;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine.Scripting;
|
||||
|
||||
[Preserve]
|
||||
public class Stats
|
||||
{
|
||||
[Preserve]
|
||||
[JsonProperty("highScore")]
|
||||
public BigInteger HighScore { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalNormalBerries")]
|
||||
public BigInteger TotalNormalBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalPoisonBerries")]
|
||||
public BigInteger TotalPoisonBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalSlowBerries")]
|
||||
public BigInteger TotalSlowBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalUltraBerries")]
|
||||
public BigInteger TotalUltraBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalSpeedyBerries")]
|
||||
public BigInteger TotalSpeedyBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalCoinBerries")]
|
||||
public BigInteger TotalCoinBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalRandomBerries")]
|
||||
public BigInteger TotalRandomBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("totalAntiBerries")]
|
||||
public BigInteger TotalAntiBerries { get; set; }
|
||||
|
||||
[Preserve]
|
||||
[JsonProperty("coins")]
|
||||
public BigInteger Coins { get; set; }
|
||||
}
|
||||
2
Assets/Scripts/Types/Stats.cs.meta
Normal file
2
Assets/Scripts/Types/Stats.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdfa01999e81b4974b97832cf44a3784
|
||||
107
Assets/Scripts/UserSearchMenu.cs
Normal file
107
Assets/Scripts/UserSearchMenu.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UserSearchMenu : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject content;
|
||||
[SerializeField] private GameObject sampleObject;
|
||||
[SerializeField] private Button searchButton;
|
||||
[SerializeField] private TMP_InputField usernameInput;
|
||||
[SerializeField] private TMP_Text statusText;
|
||||
[SerializeField] private ProfileMenu profilePrefab;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
usernameInput.onSelect.AddListener((_) => usernameInput.caretPosition = usernameInput.text.Length);
|
||||
searchButton.onClick.AddListener(Search);
|
||||
Search();
|
||||
}
|
||||
|
||||
async void Search()
|
||||
{
|
||||
searchButton.interactable = false;
|
||||
foreach (Transform item in content.transform)
|
||||
if (item.gameObject.activeSelf)
|
||||
Destroy(item.gameObject);
|
||||
using UnityWebRequest request = UnityWebRequest.Get(Endpoints.BD_ACCOUNT_ENDPOINT + "?username=" + usernameInput.text);
|
||||
request.SetRequestHeader("Requester", "BerryDashClient");
|
||||
request.SetRequestHeader("ClientVersion", Application.version);
|
||||
request.SetRequestHeader("ClientPlatform", Application.platform.ToString());
|
||||
await request.SendWebRequest();
|
||||
if (request.downloadHandler.text == null)
|
||||
{
|
||||
searchButton.interactable = true;
|
||||
UpdateStatus(true, "Failed to get a list of users");
|
||||
return;
|
||||
}
|
||||
UpdateStatus(false);
|
||||
var jsonResponse = JObject.Parse(request.downloadHandler.text);
|
||||
if ((bool)jsonResponse["success"])
|
||||
{
|
||||
foreach (var entry in jsonResponse["data"].ToObject<Account[]>())
|
||||
{
|
||||
if (entry.UserID == BazookaManager.Instance.GetAccountID()) continue;
|
||||
var entryInfo = Instantiate(sampleObject, content.transform);
|
||||
var playerIcon = entryInfo.transform.GetChild(0).GetComponent<Image>();
|
||||
var playerOverlay = playerIcon.transform.GetChild(0).GetComponent<Image>();
|
||||
var usernameText = entryInfo.transform.GetChild(1).GetComponent<TMP_Text>();
|
||||
var playerIconButton = playerIcon.GetComponent<Button>();
|
||||
var highScoreText = entryInfo.transform.GetChild(2).GetComponent<TMP_Text>();
|
||||
|
||||
usernameText.text = entry.Username;
|
||||
highScoreText.text += Tools.FormatWithCommas(entry.Stats.HighScore);
|
||||
|
||||
if (entry.CustomIcon == null)
|
||||
{
|
||||
playerIcon.sprite = Resources.Load<Sprite>("Icons/Icons/bird_" + entry.Icon);
|
||||
if (entry.Icon == 1) playerIcon.sprite = Tools.GetIconForUser(entry.UserID);
|
||||
playerOverlay.sprite = Resources.Load<Sprite>("Icons/Overlays/overlay_" + entry.Overlay);
|
||||
if (entry.Overlay != 0) playerOverlay.gameObject.SetActive(true);
|
||||
if (entry.Overlay == 8) playerOverlay.transform.localPosition = new Vector2(-16.56f, 14.81f);
|
||||
else if (entry.Overlay == 11) playerOverlay.transform.localPosition = new Vector2(-14.74451f, 20.39122f);
|
||||
else if (entry.Overlay == 13) playerOverlay.transform.localPosition = new Vector2(-16.54019f, 14.70365f);
|
||||
try
|
||||
{
|
||||
playerIcon.color = new Color(entry.BirdColor[0] / 255f, entry.BirdColor[1] / 255f, entry.BirdColor[2] / 255f);
|
||||
playerOverlay.color = new Color(entry.BirdColor[0] / 255f, entry.BirdColor[1] / 255f, entry.BirdColor[2] / 255f);
|
||||
}
|
||||
catch
|
||||
{
|
||||
playerIcon.color = Color.white;
|
||||
playerOverlay.color = Color.white;
|
||||
}
|
||||
}
|
||||
else playerIcon.gameObject.AddComponent<WaitingForCustomIcon>().ID = entry.CustomIcon;
|
||||
playerIconButton.onClick.AddListener(async () =>
|
||||
{
|
||||
var clone = Instantiate(profilePrefab.gameObject, profilePrefab.gameObject.transform.parent);
|
||||
clone.SetActive(true);
|
||||
await clone.GetComponent<ProfileMenu>().Init(entry.UserID);
|
||||
});
|
||||
entryInfo.SetActive(true);
|
||||
}
|
||||
CustomIconLoader.Init(FindObjectsByType<WaitingForCustomIcon>(FindObjectsSortMode.None));
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateStatus(true, (string)jsonResponse["message"]);
|
||||
}
|
||||
searchButton.interactable = true;
|
||||
}
|
||||
|
||||
async void Update()
|
||||
{
|
||||
if (Keyboard.current.escapeKey.wasPressedThisFrame) await SceneManager.LoadSceneAsync("MainMenu");
|
||||
}
|
||||
|
||||
private void UpdateStatus(bool enabled, string message = "")
|
||||
{
|
||||
statusText.gameObject.SetActive(enabled);
|
||||
statusText.text = message;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UserSearchMenu.cs.meta
Normal file
2
Assets/Scripts/UserSearchMenu.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c6d4c7b82c4d401996b55734bbe5b65
|
||||
Reference in New Issue
Block a user