kanagara

Untitled

Nov 17th, 2020
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.70 KB | None | 0 0
  1. public class MyAzureRequests : Requests
  2. {
  3.     [SerializeField] private string URL;
  4.     [SerializeField] private string Key;
  5.     protected void Awake()
  6.     {
  7.         InitSingleton(this);
  8.         SavingLoading.Init(Key);
  9.     }
  10.  
  11.     public override IEnumerator Login(string email, string password,
  12.         Action<JSONNode>  successCallback, Action<string>  failCallback)
  13.     {
  14.         var dto = new UserDto(){ Email = email, Password = password};
  15.        
  16.         var request = Post($"{URL}/users/authenticate?platform={GetPlatform()}&id={GetPlatformId()}", JsonUtility.ToJson(dto), true);
  17.         request.downloadHandler = new DownloadHandlerBuffer();
  18.         var x = request.SendWebRequest();
  19.         yield return x;        
  20.         HandleRequest(request, successCallback, failCallback);
  21.     }
  22.  
  23.     private static string GetPlatform()
  24.     {
  25.         #if UNITY_EDITOR
  26.         return "";
  27.         #endif
  28.         switch (Application.platform)
  29.         {
  30.             case RuntimePlatform.Android: return "android";
  31.             case RuntimePlatform.IPhonePlayer: return "ios";
  32.             case RuntimePlatform.WindowsPlayer: return "steam";
  33.             default: return "None";
  34.         }
  35.     }
  36.  
  37.     public override IEnumerator LoginWithPlatform(Action<JSONNode> successCallback, Action<string> failCallback)
  38.     {
  39.         var request = UnityWebRequest.Get($"{URL}/{GetPlatform()}/{GetPlatformId()}");
  40.         var x = request.SendWebRequest();
  41.         yield return x;
  42.        
  43.         HandleRequest(x.webRequest, successCallback, failCallback);
  44.     }
  45.  
  46.     public override IEnumerator LoadItems(bool mine, Action<JSONNode> successCallback, Action<string> failCallback)
  47.     {
  48.         var request = Get(mine ? $"{URL}/machines/my_machines" : $"{URL}/machines/other");
  49.         yield return request.SendWebRequest();
  50.         HandleRequest(request, successCallback, failCallback);
  51.     }
  52.  
  53.     public override IEnumerator BuyPart(string name, Action<JSONNode> successCallback, Action<string> failCallback)
  54.     {
  55.         var request = Get($"{URL}/parts/{name}");
  56.        
  57.         yield return request.SendWebRequest();
  58.        
  59.         HandleRequest(request, successCallback, failCallback);
  60.     }
  61.  
  62.     public override IEnumerator ItemDetails(int itemId, bool isMine, Action<JSONNode> successCallback, Action<string> failCallback)
  63.     {
  64.         var request = Get($"{URL}/machines/{(isMine ? "mine/" : "")}{itemId}");
  65.         yield return request.SendWebRequest();
  66.         HandleRequest(request, successCallback, failCallback);
  67.     }
  68.  
  69.     private static string GetPlatformId()
  70.     {
  71.         #if UNITY_EDITOR
  72.         return "";
  73.         #endif
  74.         return !Social.localUser.authenticated ? "" : Social.localUser.id;
  75.     }
  76.  
  77.     private void SetPlatformId(UserDto user)
  78.     {
  79.         if (!Social.localUser.authenticated) return;
  80.         switch (Application.platform)
  81.         {
  82.             case RuntimePlatform.Android: user.GooglePlayGamesId = Social.localUser.id; break;
  83.             case RuntimePlatform.IPhonePlayer: user.GameCenterId = Social.localUser.id; break;
  84.             case RuntimePlatform.WindowsPlayer:  user.SteamId = Social.localUser.id; break;
  85.             default: return;
  86.         }
  87.     }
  88.  
  89.     public override IEnumerator Register(string email, string password, string displayName, Action<JSONNode> action, Action<string> failcallback)
  90.     {
  91.         var user = new UserDto{ Email = email, Password = password, DisplayName = displayName};
  92.         SetPlatformId(user);
  93.         var request = Post($"{URL}/users/register", JsonUtility.ToJson(user) ,true);
  94.        
  95.         yield return request.SendWebRequest();
  96.        
  97.         HandleRequest(request, action, failcallback);
  98.        
  99.     }
  100.  
  101.     public override IEnumerator GetParts(Action<JSONNode> successCallback, Action<string> failCallback)
  102.     {
  103.         var request = UnityWebRequest.Get($"{URL}/user_parts");
  104.         request.SetHeader(false);
  105.         yield return request.SendWebRequest();
  106.        
  107.         HandleRequest(request, successCallback, failCallback);
  108.     }
  109.  
  110.  
  111.     public override IEnumerator Authenticate(Action<JSONNode> successCallback, Action<string> failCallback)
  112.     {
  113.         if (SavingLoading.GetToken() == null)
  114.         {
  115.             failCallback("Invalid token!");
  116.             yield break;
  117.         }
  118.         var request = UnityWebRequest.Get($"{URL}/users/test");
  119.         request.SetHeader();
  120.         yield return request.SendWebRequest();
  121.         HandleRequest(request, successCallback, failCallback);
  122.        
  123.     }
  124.  
  125.     public override IEnumerator SaveItem(GameObject item, string name, int price,
  126.         Action<JSONNode> successCallback, Action<string> failCallback)
  127.     {
  128.         var databaseItem = new DatabaseItem
  129.         {
  130.             Name = name, Price = price, Image = Convert.ToBase64String(Statics.TakeScreenshot()),
  131.             OwnersIds = new List<string>(), ItemElements = new List<ItemElement>()
  132.         };
  133.            
  134.        
  135.         foreach (Transform child in item.transform)
  136.         {
  137.             var childName = child.transform.name;
  138.             databaseItem.ItemElements.Add(new ItemElement
  139.             {
  140.                 Name = childName,
  141.                 ItemPath = $"Prefabs/{childName}/{childName}.prefab",
  142.                 Scale = child.localScale.ToString(),
  143.                 Position = child.position.ToString(),
  144.                 Rotation = transform.rotation.eulerAngles.ToString()
  145.             });
  146.         }
  147.         var json = JsonUtility.ToJson(databaseItem);
  148.  
  149.         var request = Post($"{URL}/machines/create", json, true);
  150.         yield return request.SendWebRequest();
  151.         HandleRequest(request, successCallback, failCallback);
  152.  
  153.     }
  154.    
  155.    
  156.     public override IEnumerator BuyItem(int itemId, Action<JSONNode> successCallback, Action<string> failCallback)
  157.     {
  158.         yield return new WaitForSeconds(0.2f);
  159.         var req = UnityWebRequest.Get($"{URL}/machines/buy/{itemId}");
  160.         req.SetHeader(false);
  161.         yield return req.SendWebRequest();
  162.         HandleRequest(req, successCallback, failCallback);
  163.     }
  164.  
  165.     /// <summary>
  166.     /// Method which handles the unity web request.
  167.     /// </summary>
  168.     /// <param name="request">Request sent and received back</param>
  169.     /// <param name="success">Callback if everything went OK</param>
  170.     /// <param name="fail">Callback if something failed</param>
  171.     private void HandleRequest(UnityWebRequest request, Action<JSONNode>  success, Action<string> fail)
  172.     {
  173.         if (string.IsNullOrEmpty(request.error))
  174.         {
  175.             var result = JSON.Parse(request.downloadHandler.text);
  176.             success?.Invoke(result);
  177.         }
  178.         else
  179.         {
  180.             fail?.Invoke(request.error);
  181.         }
  182.     }
  183. }
Add Comment
Please, Sign In to add comment