Follow the leader
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123 lines
3.3KB

  1. using System;
  2. using GamecraftModdingAPI;
  3. using GamecraftModdingAPI.Players;
  4. using GamecraftModdingAPI.Tasks;
  5. using GamecraftModdingAPI.Utility;
  6. using Leadercraft.Server;
  7. namespace Leadercraft.Scoring
  8. {
  9. internal static class State
  10. {
  11. public static bool IsInGame { get; private set; }= false;
  12. public static bool IsPlayingGame { get; private set; } = false;
  13. public static DateTime GameEnterTime { get; private set; }
  14. public static DateTime GameStartTime { get; private set; }
  15. public static TimeSpan GamePlayTime { get; private set; }
  16. public static float[] PlayerLocation { get; private set; }
  17. public static ulong GameId { get; private set; }
  18. public static int Score { get; private set; }
  19. public static bool IsGameComplete { get; private set; }
  20. public static bool IsGameSynced { get; private set; }
  21. private static bool isBadGame = false;
  22. private static Player localPlayer = null;
  23. public static void EnterGame()
  24. {
  25. if (IsInGame) return;
  26. IsInGame = true;
  27. IsGameSynced = false;
  28. IsGameComplete = false;
  29. GameId = Server.Tools.GameId;
  30. GameEnterTime = DateTime.UtcNow;
  31. Logging.MetaLog($"Entering game {GameId} at {GameEnterTime}");
  32. }
  33. public static void ExitGame()
  34. {
  35. if (!IsInGame) return;
  36. Logging.MetaLog($"Exiting game {GameId}");
  37. IsInGame = false;
  38. }
  39. public static void StartPlayingGame()
  40. {
  41. if (IsPlayingGame) return;
  42. Logging.MetaLog($"Starting to play game {GameId}");
  43. Score = 0;
  44. IsPlayingGame = true;
  45. GameStartTime = DateTime.UtcNow;
  46. // schedule game loop async task
  47. Action loop = () => { loopPass(); };
  48. ISchedulable looper = new Repeatable(loop, () => { return State.IsInGame; }, LeadercraftPlugin.LoopDelay);
  49. Scheduler.Schedule(looper);
  50. }
  51. public static void StopPlayingGame()
  52. {
  53. if (!IsPlayingGame) return;
  54. GamePlayTime = DateTime.UtcNow - GameStartTime;
  55. Logging.MetaLog($"Stopping game {GameId} after {GamePlayTime.TotalSeconds}s");
  56. IsPlayingGame = false;
  57. }
  58. public static void SetLocation(float x, float y, float z)
  59. {
  60. PlayerLocation = new float[] { x, y, z };
  61. }
  62. public static void AddScore(int points)
  63. {
  64. Score += points;
  65. }
  66. public static Server.CriteriaStruct Criteria()
  67. {
  68. IsGameSynced = true;
  69. return new Server.CriteriaStruct
  70. {
  71. Location = new float[][] { PlayerLocation, PlayerLocation },
  72. Time = Convert.ToInt32(Math.Round(GamePlayTime.TotalSeconds, MidpointRounding.AwayFromZero)),
  73. GameID = GameId,
  74. PlayerID = 0,
  75. Complete = IsGameComplete,
  76. Points = Score,
  77. };
  78. }
  79. private static void loopPass()
  80. {
  81. if (!State.IsPlayingGame) return;
  82. if (localPlayer == null && Player.Exists(PlayerType.Local))
  83. {
  84. localPlayer = new Player(PlayerType.Local);
  85. isBadGame = localPlayer.GameOver;
  86. if (isBadGame)
  87. {
  88. GamecraftModdingAPI.Utility.Logging.MetaLog($"Ignoring game {GameId} since it does not seem to have a GameOver state.");
  89. }
  90. }
  91. if (localPlayer == null) return;
  92. if (localPlayer.GameOver && !localPlayer.Dead && !isBadGame)
  93. {
  94. State.StopPlayingGame();
  95. //State.GamePlayTime.TotalSeconds
  96. UploadJob scoreJob = new UploadJob(State.Score, State.GamePlayTime.TotalSeconds, localPlayer.Position, Tools.UserId, Tools.UserName, Tools.GameId);
  97. scoreJob.RunInNewThread();
  98. }
  99. }
  100. }
  101. }