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.

UploadJob.cs 2.3KB

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