Magically import images and more into Gamecraft as blocks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
4.0KB

  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using Newtonsoft.Json;
  6. using GamecraftModdingAPI.Utility;
  7. namespace Pixi.Robots
  8. {
  9. public static class RoboAPIUtility
  10. {
  11. private const string ROBOT_API_LIST_URL = "https://factory.robocraftgame.com/api/roboShopItems/list";
  12. private const string ROBOT_API_GET_URL = "https://factory.robocraftgame.com/api/roboShopItems/get/";
  13. private const string ROBOT_API_TOKEN = "Web eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJQdWJsaWNJZCI6IjEyMyIsIkRpc3BsYXlOYW1lIjoiVGVzdCIsIlJvYm9jcmFmdE5hbWUiOiJGYWtlQ1JGVXNlciIsIkZsYWdzIjpbXSwiaXNzIjoiRnJlZWphbSIsInN1YiI6IldlYiIsImlhdCI6MTU0NTIyMzczMiwiZXhwIjoyNTQ1MjIzNzkyfQ.ralLmxdMK9rVKPZxGng8luRIdbTflJ4YMJcd25dKlqg";
  14. public static RobotBriefStruct[] ListRobots(string searchFilter, int pageSize = 10, bool playerFilter = false)
  15. {
  16. // pageSize <= 2 seems to retrieve items unreliably
  17. string bodyJson = $"{{\"page\": 1, \"pageSize\": {pageSize}, \"order\": 0, \"playerFilter\": {playerFilter.ToString().ToLower()}, \"movementFilter\": \"100000,200000,300000,400000,500000,600000,700000,800000,900000,1000000,1100000,1200000\", \"movementCategoryFilter\": \"100000,200000,300000,400000,500000,600000,700000,800000,900000,1000000,1100000,1200000\", \"weaponFilter\": \"10000000,20000000,25000000,30000000,40000000,50000000,60000000,65000000,70100000,75000000\", \"weaponCategoryFilter\": \"10000000,20000000,25000000,30000000,40000000,50000000,60000000,65000000,70100000,75000000\", \"minimumCpu\": -1, \"maximumCpu\": -1, \"minimumRobotRanking\": 0, \"maximumRobotRanking\": 1000000000, \"textFilter\": \"{searchFilter}\", \"textSearchField\": 0, \"buyable\": true, \"prependFeaturedRobot\": false, \"featuredOnly\": false, \"defaultPage\": false}}";
  18. byte[] reqBody = Encoding.UTF8.GetBytes(bodyJson);
  19. #if DEBUG
  20. Logging.MetaLog($"POST body\n{bodyJson}");
  21. #endif
  22. // download robot list
  23. // FIXME this blocks main thread
  24. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ROBOT_API_LIST_URL);
  25. // request
  26. request.Method = "POST";
  27. request.ContentLength = reqBody.Length;
  28. request.ContentType = "application/json";
  29. request.Headers.Add(HttpRequestHeader.Authorization, ROBOT_API_TOKEN);
  30. request.Accept = "application/json; charset=utf-8"; // HTTP Status 500 without
  31. Stream body;
  32. body = request.GetRequestStream();
  33. body.Write(reqBody, 0, reqBody.Length);
  34. body.Close();
  35. // response
  36. HttpWebResponse response;
  37. response = (HttpWebResponse)request.GetResponse();
  38. // regular Stream was unreliable
  39. // because they could read everything before everything was availabe
  40. StreamReader respReader = new StreamReader(response.GetResponseStream());
  41. string bodyStr = respReader.ReadToEnd();
  42. RobotListResponse rlr = JsonConvert.DeserializeObject<RobotListResponse>(bodyStr);
  43. return rlr.response.roboShopItems;
  44. }
  45. public static RobotStruct QueryRobotInfo(int robotId)
  46. {
  47. // download robot info
  48. // FIXME this blocks main thread
  49. string url = ROBOT_API_GET_URL + robotId.ToString();
  50. Logging.MetaLog(url);
  51. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  52. // request
  53. request.Method = "GET";
  54. request.ContentType = "application/json";
  55. request.Accept = "application/json; charset=utf-8"; // HTTP Status 500 without
  56. request.Headers.Add(HttpRequestHeader.Authorization, ROBOT_API_TOKEN);
  57. // response
  58. HttpWebResponse response;
  59. response = (HttpWebResponse)request.GetResponse();
  60. // regular Stream was unreliable
  61. // because they could read everything before everything was availabe
  62. StreamReader body = new StreamReader(response.GetResponseStream());
  63. string bodyStr = body.ReadToEnd();
  64. response.Close();
  65. RobotInfoResponse rir = JsonConvert.DeserializeObject<RobotInfoResponse>(bodyStr);
  66. return rir.response;
  67. }
  68. }
  69. }