Initial commit
This commit is contained in:
487 files changed
+181148
No files matched your search
@@ -0,0 +1,202 @@
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using Newtonsoft.Json;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class IconMarketplaceDownloadIcon : MonoBehaviour
|
||||
{
|
||||
private static WaitForSeconds _waitForSeconds2 = new(2f);
|
||||
public IconMarketplaceManager marketplaceManager;
|
||||
public TMP_Text statusText;
|
||||
public TMP_Text balanceText;
|
||||
public Button backButton;
|
||||
public GameObject content;
|
||||
public GameObject sample;
|
||||
private string statusMessage;
|
||||
private Coroutine statusRoutine;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
backButton.onClick.AddListener(() => marketplaceManager.SwitchPanel(0));
|
||||
}
|
||||
|
||||
internal void Load()
|
||||
{
|
||||
GetIcons();
|
||||
balanceText.text = "You have " + Tools.FormatWithCommas(BazookaManager.Instance.GetCustomBirdIconData().Balance) + " coins to spend";
|
||||
}
|
||||
|
||||
async void GetIcons()
|
||||
{
|
||||
backButton.interactable = false;
|
||||
foreach (Transform item in content.transform)
|
||||
{
|
||||
if (item.gameObject.activeSelf)
|
||||
{
|
||||
Destroy(item.gameObject);
|
||||
}
|
||||
}
|
||||
ShowStatus("Loading...");
|
||||
using UnityWebRequest request = UnityWebRequest.Get(SensitiveInfo.SERVER_DATABASE_PREFIX + "getMarketplaceIcons.php");
|
||||
request.SetRequestHeader("Requester", "BerryDashClient");
|
||||
request.SetRequestHeader("ClientVersion", Application.version);
|
||||
request.SetRequestHeader("ClientPlatform", Application.platform.ToString());
|
||||
await request.SendWebRequest();
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
backButton.interactable = true;
|
||||
ShowStatus("Failed to make HTTP request");
|
||||
return;
|
||||
}
|
||||
string response = SensitiveInfo.Decrypt(request.downloadHandler.text, SensitiveInfo.SERVER_RECEIVE_TRANSFER_KEY);
|
||||
if (response == "-999")
|
||||
{
|
||||
backButton.interactable = true;
|
||||
ShowStatus("Server error while fetching data");
|
||||
return;
|
||||
}
|
||||
else if (response == "-998")
|
||||
{
|
||||
backButton.interactable = true;
|
||||
ShowStatus("Client version too outdated to access servers");
|
||||
return;
|
||||
}
|
||||
else if (response == "-997")
|
||||
{
|
||||
backButton.interactable = true;
|
||||
ShowStatus("Encryption/decryption issues");
|
||||
return;
|
||||
}
|
||||
else if (response == "-996")
|
||||
{
|
||||
backButton.interactable = true;
|
||||
ShowStatus("Can't send requests on self-built instance");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowStatus(null);
|
||||
var icons = JsonConvert.DeserializeObject<MarketplaceIconType[]>(response);
|
||||
var localUserID = BazookaManager.Instance.GetAccountID();
|
||||
foreach (var entry in icons)
|
||||
{
|
||||
GameObject newIcon = Instantiate(sample, content.transform);
|
||||
newIcon.name = "IconEntry";
|
||||
|
||||
Tools.RenderFromBase64(entry.Data, newIcon.transform.GetChild(0).GetChild(0).GetComponent<Image>());
|
||||
newIcon.transform.GetChild(1).GetComponent<TMP_Text>().text = "Bird Name: " + entry.Name;
|
||||
newIcon.transform.GetChild(2).GetComponent<TMP_Text>().text = "Price " + Tools.FormatWithCommas(entry.Price) + " coin";
|
||||
newIcon.transform.GetChild(3).GetComponent<TMP_Text>().text = "Designer Name: " + entry.CreatorUsername;
|
||||
|
||||
var btn = newIcon.transform.GetChild(4).GetComponent<Button>();
|
||||
var btnText = btn.transform.GetChild(0).GetComponent<TMP_Text>();
|
||||
|
||||
bool alreadyBought = BazookaManager.Instance.GetCustomBirdIconData().Data.Any(d => d.UUID == entry.UUID);
|
||||
if (alreadyBought)
|
||||
{
|
||||
btn.interactable = false;
|
||||
if (localUserID != entry.CreatorUserID)
|
||||
{
|
||||
btnText.text = "Purchased";
|
||||
}
|
||||
else
|
||||
{
|
||||
btnText.text = "Claimed";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
btn.onClick.AddListener(() => HandlePurchase(entry, btn, localUserID));
|
||||
if (localUserID != entry.CreatorUserID)
|
||||
{
|
||||
btnText.text = "Purchase";
|
||||
}
|
||||
else
|
||||
{
|
||||
btnText.text = "Claim";
|
||||
}
|
||||
}
|
||||
|
||||
newIcon.SetActive(true);
|
||||
}
|
||||
}
|
||||
backButton.interactable = true;
|
||||
}
|
||||
|
||||
void HandlePurchase(MarketplaceIconType data, Button button, BigInteger? localUserID)
|
||||
{
|
||||
button.interactable = false;
|
||||
MarketplaceIconStorageType marketplaceIconStorage = BazookaManager.Instance.GetCustomBirdIconData();
|
||||
if (localUserID != data.CreatorUserID)
|
||||
{
|
||||
if (data.Price > marketplaceIconStorage.Balance)
|
||||
{
|
||||
button.interactable = true;
|
||||
ShowStatus("You can't afford this icon! You need " + Tools.FormatWithCommas(data.Price - marketplaceIconStorage.Balance) + " more coins");
|
||||
return;
|
||||
}
|
||||
marketplaceIconStorage.Balance -= data.Price;
|
||||
}
|
||||
var list = marketplaceIconStorage.Data.ToList();
|
||||
list.Add(data);
|
||||
marketplaceIconStorage.Data = list.ToArray();
|
||||
if (localUserID != data.CreatorUserID)
|
||||
{
|
||||
button.transform.GetChild(0).GetComponent<TMP_Text>().text = "Purchased";
|
||||
}
|
||||
else
|
||||
{
|
||||
button.transform.GetChild(0).GetComponent<TMP_Text>().text = "Claimed";
|
||||
}
|
||||
balanceText.text = "You have " + Tools.FormatWithCommas(marketplaceIconStorage.Balance) + " coins to spend";
|
||||
BazookaManager.Instance.SetCustomBirdIconData(marketplaceIconStorage);
|
||||
}
|
||||
|
||||
void ShowStatus(string content)
|
||||
{
|
||||
if (content == null)
|
||||
{
|
||||
if (statusRoutine != null) StopCoroutine(statusRoutine);
|
||||
statusText.gameObject.SetActive(false);
|
||||
statusText.text = "";
|
||||
}
|
||||
statusMessage = content;
|
||||
if (statusRoutine != null) StopCoroutine(statusRoutine);
|
||||
statusRoutine = StartCoroutine(StatusRoutine());
|
||||
}
|
||||
|
||||
IEnumerator StatusRoutine()
|
||||
{
|
||||
statusText.gameObject.SetActive(true);
|
||||
statusText.text = statusMessage;
|
||||
statusText.color = new Color(statusText.color.r, statusText.color.g, statusText.color.b, 0f);
|
||||
|
||||
float t = 0f;
|
||||
while (t < 0.5f)
|
||||
{
|
||||
t += Time.deltaTime;
|
||||
float a = t / 0.5f;
|
||||
statusText.color = new Color(statusText.color.r, statusText.color.g, statusText.color.b, a);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
yield return _waitForSeconds2;
|
||||
|
||||
t = 0f;
|
||||
while (t < 0.5f)
|
||||
{
|
||||
t += Time.deltaTime;
|
||||
float a = 1f - (t / 0.5f);
|
||||
statusText.color = new Color(statusText.color.r, statusText.color.g, statusText.color.b, a);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
statusText.gameObject.SetActive(false);
|
||||
statusText.text = "";
|
||||
statusRoutine = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8133693b9ee6eec098e9cef8b719edf
|
||||
@@ -0,0 +1,65 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class IconMarketplaceManager : MonoBehaviour
|
||||
{
|
||||
public GameObject normalPanel;
|
||||
public GameObject downloadPanel;
|
||||
public GameObject uploadPanel;
|
||||
|
||||
public IconMarketplaceUploadIcon uploadPanelScript;
|
||||
public IconMarketplaceDownloadIcon downloadPanelScript;
|
||||
|
||||
public Button downloadButton;
|
||||
public Button uploadButton;
|
||||
public TMP_Text coinText;
|
||||
|
||||
void Start()
|
||||
{
|
||||
downloadButton.onClick.AddListener(() => SwitchPanel(1));
|
||||
if (BazookaManager.Instance.GetAccountID() != null && BazookaManager.Instance.GetAccountName() != null && BazookaManager.Instance.GetAccountSession() != null)
|
||||
{
|
||||
uploadButton.onClick.AddListener(() => SwitchPanel(2));
|
||||
}
|
||||
else
|
||||
{
|
||||
uploadButton.interactable = false;
|
||||
uploadButton.transform.GetChild(0).transform.localPosition = new Vector3(0, 10, 0);
|
||||
uploadButton.transform.GetChild(1).GetComponent<TMP_Text>().text = "Not logged in to an account";
|
||||
}
|
||||
SwitchPanel(0);
|
||||
}
|
||||
|
||||
internal void SwitchPanel(int panelIndex)
|
||||
{
|
||||
switch (panelIndex)
|
||||
{
|
||||
case 0:
|
||||
coinText.text = "You have " + Tools.FormatWithCommas(BazookaManager.Instance.GetCustomBirdIconData().Balance) + " coins";
|
||||
normalPanel.SetActive(true);
|
||||
downloadPanel.SetActive(false);
|
||||
uploadPanel.SetActive(false);
|
||||
break;
|
||||
case 1:
|
||||
foreach (Transform item in downloadPanelScript.content.transform)
|
||||
{
|
||||
if (item.gameObject.activeSelf)
|
||||
{
|
||||
Destroy(item.gameObject);
|
||||
}
|
||||
}
|
||||
normalPanel.SetActive(false);
|
||||
downloadPanel.SetActive(true);
|
||||
uploadPanel.SetActive(false);
|
||||
downloadPanelScript.Load();
|
||||
break;
|
||||
case 2:
|
||||
uploadPanelScript.Reset();
|
||||
normalPanel.SetActive(false);
|
||||
downloadPanel.SetActive(false);
|
||||
uploadPanel.SetActive(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd46bad161f2c81e2a36dd6a25154ad4
|
||||
@@ -0,0 +1,173 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SimpleFileBrowser;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class IconMarketplaceUploadIcon : MonoBehaviour
|
||||
{
|
||||
public IconMarketplaceManager marketplaceManager;
|
||||
public TMP_Text statusText;
|
||||
public Button backButton;
|
||||
public Button uploadButton;
|
||||
public Button selectButton;
|
||||
public TMP_InputField birdNameInput;
|
||||
public TMP_InputField birdPriceInput;
|
||||
public Image birdImage;
|
||||
public GameObject birdImageNone;
|
||||
private string birdData = null;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
backButton.onClick.AddListener(() => marketplaceManager.SwitchPanel(0));
|
||||
uploadButton.onClick.AddListener(() =>
|
||||
{
|
||||
if (birdNameInput.text.Trim() == string.Empty)
|
||||
{
|
||||
Tools.UpdateStatusText(statusText, "Bird name can't be empty", Color.red);
|
||||
}
|
||||
else if (birdPriceInput.text.Trim() == string.Empty)
|
||||
{
|
||||
Tools.UpdateStatusText(statusText, "Bird price can't be empty", Color.red);
|
||||
}
|
||||
else if (birdData == null)
|
||||
{
|
||||
Tools.UpdateStatusText(statusText, "You must upload an image", Color.red);
|
||||
}
|
||||
else
|
||||
{
|
||||
UploadIcon();
|
||||
}
|
||||
});
|
||||
selectButton.onClick.AddListener(async () =>
|
||||
{
|
||||
string path = await OpenFilePicker();
|
||||
if (path == null) return;
|
||||
byte[] fileContent;
|
||||
try
|
||||
{
|
||||
fileContent = File.ReadAllBytes(path);
|
||||
if (fileContent.Length > 1024 * 1024)
|
||||
{
|
||||
Tools.UpdateStatusText(statusText, "File size exceeds 1 MB limit", Color.red);
|
||||
return;
|
||||
}
|
||||
birdData = Convert.ToBase64String(fileContent);
|
||||
Tools.RenderFromBase64(birdData, birdImage);
|
||||
birdImageNone.SetActive(false);
|
||||
birdImage.gameObject.SetActive(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Tools.UpdateStatusText(statusText, "Failed to read file: " + e.Message, Color.red);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async Task<string> OpenFilePicker()
|
||||
{
|
||||
FileBrowser.SetFilters(true, new FileBrowser.Filter("Images", ".png"));
|
||||
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
|
||||
FileBrowser.ShowLoadDialog(
|
||||
(paths) => tcs.SetResult(paths.Length > 0 ? paths[0] : null),
|
||||
() => tcs.SetResult(null),
|
||||
FileBrowser.PickMode.Files,
|
||||
false
|
||||
);
|
||||
|
||||
return await tcs.Task;
|
||||
}
|
||||
|
||||
async void UploadIcon()
|
||||
{
|
||||
uploadButton.interactable = false;
|
||||
backButton.interactable = false;
|
||||
selectButton.interactable = false;
|
||||
EncryptedWWWForm dataForm = new();
|
||||
dataForm.AddField("token", BazookaManager.Instance.GetAccountSession());
|
||||
dataForm.AddField("username", BazookaManager.Instance.GetAccountName());
|
||||
dataForm.AddField("name", birdNameInput.text);
|
||||
dataForm.AddField("price", birdPriceInput.text);
|
||||
dataForm.AddField("filecontent", birdData);
|
||||
using UnityWebRequest request = UnityWebRequest.Post(SensitiveInfo.SERVER_DATABASE_PREFIX + "uploadMarketplaceIcon.php", dataForm.form);
|
||||
request.SetRequestHeader("Requester", "BerryDashClient");
|
||||
request.SetRequestHeader("ClientVersion", Application.version);
|
||||
request.SetRequestHeader("ClientPlatform", Application.platform.ToString());
|
||||
await request.SendWebRequest();
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
uploadButton.interactable = true;
|
||||
backButton.interactable = true;
|
||||
selectButton.interactable = true;
|
||||
Tools.UpdateStatusText(statusText, "Failed to make HTTP request", Color.red);
|
||||
return;
|
||||
}
|
||||
string response = SensitiveInfo.Decrypt(request.downloadHandler.text, SensitiveInfo.SERVER_RECEIVE_TRANSFER_KEY);
|
||||
if (response == "-999")
|
||||
{
|
||||
uploadButton.interactable = true;
|
||||
backButton.interactable = true;
|
||||
selectButton.interactable = true;
|
||||
Tools.UpdateStatusText(statusText, "Server error while fetching data", Color.red);
|
||||
return;
|
||||
}
|
||||
else if (response == "-998")
|
||||
{
|
||||
uploadButton.interactable = true;
|
||||
backButton.interactable = true;
|
||||
selectButton.interactable = true;
|
||||
Tools.UpdateStatusText(statusText, "Client version too outdated to access servers", Color.red);
|
||||
return;
|
||||
}
|
||||
else if (response == "-997")
|
||||
{
|
||||
uploadButton.interactable = true;
|
||||
backButton.interactable = true;
|
||||
selectButton.interactable = true;
|
||||
Tools.UpdateStatusText(statusText, "Encryption/decryption issues", Color.red);
|
||||
return;
|
||||
}
|
||||
else if (response == "-996")
|
||||
{
|
||||
uploadButton.interactable = true;
|
||||
backButton.interactable = true;
|
||||
selectButton.interactable = true;
|
||||
Tools.UpdateStatusText(statusText, "Can't send requests on self-built instance", Color.red);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
var jsonResponse = JObject.Parse(response);
|
||||
if ((bool)jsonResponse["success"])
|
||||
{
|
||||
Reset();
|
||||
Tools.UpdateStatusText(statusText, (string)jsonResponse["message"], Color.green);
|
||||
}
|
||||
else
|
||||
{
|
||||
Tools.UpdateStatusText(statusText, (string)jsonResponse["message"], Color.red);
|
||||
}
|
||||
}
|
||||
uploadButton.interactable = true;
|
||||
backButton.interactable = true;
|
||||
selectButton.interactable = true;
|
||||
}
|
||||
|
||||
internal void Reset()
|
||||
{
|
||||
birdImage.sprite = null;
|
||||
birdImageNone.SetActive(true);
|
||||
birdImage.gameObject.SetActive(false);
|
||||
birdData = null;
|
||||
birdNameInput.text = string.Empty;
|
||||
birdPriceInput.text = string.Empty;
|
||||
statusText.text = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac25b048bc0f6aded8697b98caef8382
|
||||
Reference in new issue
Block a user