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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Player localPlayer = null;
  22. public static void EnterGame()
  23. {
  24. if (IsInGame) return;
  25. Logging.MetaDebugLog("Entering game");
  26. IsInGame = true;
  27. IsGameSynced = false;
  28. IsGameComplete = false;
  29. GameId = Server.Tools.GameId;
  30. GameEnterTime = DateTime.UtcNow;
  31. }
  32. public static void ExitGame()
  33. {
  34. if (!IsInGame) return;
  35. Logging.MetaDebugLog("Exiting game");
  36. IsInGame = false;
  37. }
  38. public static void StartPlayingGame()
  39. {
  40. if (IsPlayingGame) return;
  41. Logging.MetaDebugLog("Starting to play game");
  42. Score = 0;
  43. IsPlayingGame = true;
  44. GameStartTime = DateTime.UtcNow;
  45. // schedule game loop async task
  46. Action loop = () => { loopPass(); };
  47. ISchedulable looper = new Repeatable(loop, () => { return State.IsInGame; }, LeadercraftPlugin.LoopDelay);
  48. Scheduler.Schedule(looper);
  49. }
  50. public static void StopPlayingGame()
  51. {
  52. if (!IsPlayingGame) return;
  53. GamePlayTime = DateTime.UtcNow - GameStartTime;
  54. Logging.MetaDebugLog("Stopping game");
  55. IsPlayingGame = false;
  56. }
  57. public static void SetLocation(float x, float y, float z)
  58. {
  59. PlayerLocation = new float[] { x, y, z };
  60. }
  61. public static void AddScore(int points)
  62. {
  63. Score += points;
  64. }
  65. public static Server.CriteriaStruct Criteria()
  66. {
  67. IsGameSynced = true;
  68. return new Server.CriteriaStruct
  69. {
  70. Location = new float[][] { PlayerLocation, PlayerLocation },
  71. Time = Convert.ToInt32(Math.Round(GamePlayTime.TotalSeconds, MidpointRounding.AwayFromZero)),
  72. GameID = GameId,
  73. PlayerID = 0,
  74. Complete = IsGameComplete,
  75. Points = Score,
  76. };
  77. }
  78. private static void loopPass()
  79. {
  80. if (!State.IsPlayingGame) return;
  81. if (localPlayer == null && Player.Exists(PlayerType.Local)) localPlayer = new Player(PlayerType.Local);
  82. if (localPlayer == null) return;
  83. if (localPlayer.GameOver && !localPlayer.Dead)
  84. {
  85. State.StopPlayingGame();
  86. //State.GamePlayTime.TotalSeconds
  87. UploadJob scoreJob = new UploadJob(State.Score, State.GamePlayTime.TotalSeconds, localPlayer.Position, Tools.UserId, Tools.UserName, Tools.GameId);
  88. scoreJob.RunInNewThread();
  89. }
  90. }
  91. }
  92. }