Follow the leader
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

84 lines
2.2KB

  1. using System;
  2. using System.Threading;
  3. using Unity.Collections;
  4. using Unity.Jobs;
  5. using Unity.Mathematics;
  6. using GamecraftModdingAPI.Utility;
  7. using Leadercraft.Server;
  8. namespace Leadercraft.Scoring
  9. {
  10. public struct UploadJob
  11. {
  12. private int points;
  13. private int time;
  14. private float[] location;
  15. private ulong player;
  16. private string playerName;
  17. private ulong game;
  18. public UploadJob(int score, double timespan, float3 position, ulong playerId, string playerName, ulong gameId)
  19. {
  20. this.points = score;
  21. this.time = (int)Math.Ceiling(timespan);
  22. this.location = new float[3] { position.x, position.y, position.z};
  23. this.player = playerId;
  24. this.playerName = playerName;
  25. this.game = 2;//gameId;
  26. LeadercraftPlugin.BuildApi();
  27. }
  28. public void RunInNewThread()
  29. {
  30. Thread job = new Thread(new ThreadStart(Execute));
  31. job.Start();
  32. }
  33. private void Execute()
  34. {
  35. if (player < 1000 || game == 0) return; // offline game
  36. LeadercraftResult<KeyStruct> tokenResult = LeadercraftPlugin.Api.RequestPOSTToken(playerName);
  37. if (tokenResult.IsError)
  38. {
  39. Logging.LogWarning($"Leadercraft API token web request failed with status {tokenResult.StatusCode}");
  40. try
  41. {
  42. Logging.LogWarning($"API Error Response: {tokenResult.ParseError().Items[0]}");
  43. }
  44. catch (Exception) { }
  45. return;
  46. }
  47. string token = tokenResult.ParseResult().Items[0].Token;
  48. CriteriaStruct criteria = new CriteriaStruct
  49. {
  50. Location = new float[][] { location, location },
  51. Time = time,
  52. GameID = game,
  53. PlayerID = player,
  54. Complete = true,
  55. Points = points
  56. };
  57. LeadercraftResult<string> criteriaResult = LeadercraftPlugin.Api.RequestPOSTCriteria(criteria, token);
  58. if (criteriaResult.IsError)
  59. {
  60. Logging.LogWarning($"Leadercraft API criteria web request failed with status {criteriaResult.StatusCode}");
  61. try
  62. {
  63. Logging.LogWarning($"API Error Response: {criteriaResult.ParseError().Items[0]}");
  64. }
  65. catch (Exception) { }
  66. return;
  67. }
  68. Logging.MetaLog($"Criteria request succeeded with status {criteriaResult.StatusCode}");
  69. }
  70. }
  71. }