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.

State.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }