Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyAzureRequests : Requests
- {
- [SerializeField] private string URL;
- [SerializeField] private string Key;
- protected void Awake()
- {
- InitSingleton(this);
- SavingLoading.Init(Key);
- }
- public override IEnumerator Login(string email, string password,
- Action<JSONNode> successCallback, Action<string> failCallback)
- {
- var dto = new UserDto(){ Email = email, Password = password};
- var request = Post($"{URL}/users/authenticate?platform={GetPlatform()}&id={GetPlatformId()}", JsonUtility.ToJson(dto), true);
- request.downloadHandler = new DownloadHandlerBuffer();
- var x = request.SendWebRequest();
- yield return x;
- HandleRequest(request, successCallback, failCallback);
- }
- private static string GetPlatform()
- {
- #if UNITY_EDITOR
- return "";
- #endif
- switch (Application.platform)
- {
- case RuntimePlatform.Android: return "android";
- case RuntimePlatform.IPhonePlayer: return "ios";
- case RuntimePlatform.WindowsPlayer: return "steam";
- default: return "None";
- }
- }
- public override IEnumerator LoginWithPlatform(Action<JSONNode> successCallback, Action<string> failCallback)
- {
- var request = UnityWebRequest.Get($"{URL}/{GetPlatform()}/{GetPlatformId()}");
- var x = request.SendWebRequest();
- yield return x;
- HandleRequest(x.webRequest, successCallback, failCallback);
- }
- public override IEnumerator LoadItems(bool mine, Action<JSONNode> successCallback, Action<string> failCallback)
- {
- var request = Get(mine ? $"{URL}/machines/my_machines" : $"{URL}/machines/other");
- yield return request.SendWebRequest();
- HandleRequest(request, successCallback, failCallback);
- }
- public override IEnumerator BuyPart(string name, Action<JSONNode> successCallback, Action<string> failCallback)
- {
- var request = Get($"{URL}/parts/{name}");
- yield return request.SendWebRequest();
- HandleRequest(request, successCallback, failCallback);
- }
- public override IEnumerator ItemDetails(int itemId, bool isMine, Action<JSONNode> successCallback, Action<string> failCallback)
- {
- var request = Get($"{URL}/machines/{(isMine ? "mine/" : "")}{itemId}");
- yield return request.SendWebRequest();
- HandleRequest(request, successCallback, failCallback);
- }
- private static string GetPlatformId()
- {
- #if UNITY_EDITOR
- return "";
- #endif
- return !Social.localUser.authenticated ? "" : Social.localUser.id;
- }
- private void SetPlatformId(UserDto user)
- {
- if (!Social.localUser.authenticated) return;
- switch (Application.platform)
- {
- case RuntimePlatform.Android: user.GooglePlayGamesId = Social.localUser.id; break;
- case RuntimePlatform.IPhonePlayer: user.GameCenterId = Social.localUser.id; break;
- case RuntimePlatform.WindowsPlayer: user.SteamId = Social.localUser.id; break;
- default: return;
- }
- }
- public override IEnumerator Register(string email, string password, string displayName, Action<JSONNode> action, Action<string> failcallback)
- {
- var user = new UserDto{ Email = email, Password = password, DisplayName = displayName};
- SetPlatformId(user);
- var request = Post($"{URL}/users/register", JsonUtility.ToJson(user) ,true);
- yield return request.SendWebRequest();
- HandleRequest(request, action, failcallback);
- }
- public override IEnumerator GetParts(Action<JSONNode> successCallback, Action<string> failCallback)
- {
- var request = UnityWebRequest.Get($"{URL}/user_parts");
- request.SetHeader(false);
- yield return request.SendWebRequest();
- HandleRequest(request, successCallback, failCallback);
- }
- public override IEnumerator Authenticate(Action<JSONNode> successCallback, Action<string> failCallback)
- {
- if (SavingLoading.GetToken() == null)
- {
- failCallback("Invalid token!");
- yield break;
- }
- var request = UnityWebRequest.Get($"{URL}/users/test");
- request.SetHeader();
- yield return request.SendWebRequest();
- HandleRequest(request, successCallback, failCallback);
- }
- public override IEnumerator SaveItem(GameObject item, string name, int price,
- Action<JSONNode> successCallback, Action<string> failCallback)
- {
- var databaseItem = new DatabaseItem
- {
- Name = name, Price = price, Image = Convert.ToBase64String(Statics.TakeScreenshot()),
- OwnersIds = new List<string>(), ItemElements = new List<ItemElement>()
- };
- foreach (Transform child in item.transform)
- {
- var childName = child.transform.name;
- databaseItem.ItemElements.Add(new ItemElement
- {
- Name = childName,
- ItemPath = $"Prefabs/{childName}/{childName}.prefab",
- Scale = child.localScale.ToString(),
- Position = child.position.ToString(),
- Rotation = transform.rotation.eulerAngles.ToString()
- });
- }
- var json = JsonUtility.ToJson(databaseItem);
- var request = Post($"{URL}/machines/create", json, true);
- yield return request.SendWebRequest();
- HandleRequest(request, successCallback, failCallback);
- }
- public override IEnumerator BuyItem(int itemId, Action<JSONNode> successCallback, Action<string> failCallback)
- {
- yield return new WaitForSeconds(0.2f);
- var req = UnityWebRequest.Get($"{URL}/machines/buy/{itemId}");
- req.SetHeader(false);
- yield return req.SendWebRequest();
- HandleRequest(req, successCallback, failCallback);
- }
- /// <summary>
- /// Method which handles the unity web request.
- /// </summary>
- /// <param name="request">Request sent and received back</param>
- /// <param name="success">Callback if everything went OK</param>
- /// <param name="fail">Callback if something failed</param>
- private void HandleRequest(UnityWebRequest request, Action<JSONNode> success, Action<string> fail)
- {
- if (string.IsNullOrEmpty(request.error))
- {
- var result = JSON.Parse(request.downloadHandler.text);
- success?.Invoke(result);
- }
- else
- {
- fail?.Invoke(request.error);
- }
- }
- }
Add Comment
Please, Sign In to add comment