|
- using System;
- using GamecraftModdingAPI;
- using GamecraftModdingAPI.Players;
- using GamecraftModdingAPI.Tasks;
- using GamecraftModdingAPI.Utility;
-
- using Leadercraft.Server;
-
- namespace Leadercraft.Scoring
- {
- internal static class State
- {
- public static bool IsInGame { get; private set; }= false;
-
- public static bool IsPlayingGame { get; private set; } = false;
-
- public static DateTime GameEnterTime { get; private set; }
-
- public static DateTime GameStartTime { get; private set; }
-
- public static TimeSpan GamePlayTime { get; private set; }
-
- public static float[] PlayerLocation { get; private set; }
-
- public static ulong GameId { get; private set; }
-
- public static int Score { get; private set; }
-
- public static bool IsGameComplete { get; private set; }
-
- public static bool IsGameSynced { get; private set; }
-
- private static bool isBadGame = false;
-
- private static Player localPlayer = null;
-
- public static void EnterGame()
- {
- if (IsInGame) return;
- IsInGame = true;
- IsGameSynced = false;
- IsGameComplete = false;
- GameId = Server.Tools.GameId;
- GameEnterTime = DateTime.UtcNow;
- Logging.MetaLog($"Entering game {GameId} at {GameEnterTime}");
- }
-
- public static void ExitGame()
- {
- if (!IsInGame) return;
- Logging.MetaLog($"Exiting game {GameId}");
- IsInGame = false;
- }
-
- public static void StartPlayingGame()
- {
- if (IsPlayingGame) return;
- Logging.MetaLog($"Starting to play game {GameId}");
- Score = 0;
- IsPlayingGame = true;
- GameStartTime = DateTime.UtcNow;
- // schedule game loop async task
- Action loop = () => { loopPass(); };
- ISchedulable looper = new Repeatable(loop, () => { return State.IsInGame; }, LeadercraftPlugin.LoopDelay);
- Scheduler.Schedule(looper);
- }
-
- public static void StopPlayingGame()
- {
- if (!IsPlayingGame) return;
- GamePlayTime = DateTime.UtcNow - GameStartTime;
- Logging.MetaLog($"Stopping game {GameId} after {GamePlayTime.TotalSeconds}s");
- IsPlayingGame = false;
- }
-
- public static void SetLocation(float x, float y, float z)
- {
- PlayerLocation = new float[] { x, y, z };
- }
-
- public static void AddScore(int points)
- {
- Score += points;
- }
-
- public static Server.CriteriaStruct Criteria()
- {
- IsGameSynced = true;
- return new Server.CriteriaStruct
- {
- Location = new float[][] { PlayerLocation, PlayerLocation },
- Time = Convert.ToInt32(Math.Round(GamePlayTime.TotalSeconds, MidpointRounding.AwayFromZero)),
- GameID = GameId,
- PlayerID = 0,
- Complete = IsGameComplete,
- Points = Score,
- };
- }
-
- private static void loopPass()
- {
- if (!State.IsPlayingGame) return;
- if (localPlayer == null && Player.Exists(PlayerType.Local))
- {
- localPlayer = new Player(PlayerType.Local);
- isBadGame = localPlayer.GameOver;
- if (isBadGame)
- {
- GamecraftModdingAPI.Utility.Logging.MetaLog($"Ignoring game {GameId} since it does not seem to have a GameOver state.");
- }
- }
- if (localPlayer == null) return;
- if (localPlayer.GameOver && !localPlayer.Dead && !isBadGame)
- {
- State.StopPlayingGame();
- //State.GamePlayTime.TotalSeconds
- UploadJob scoreJob = new UploadJob(State.Score, State.GamePlayTime.TotalSeconds, localPlayer.Position, Tools.UserId, Tools.UserName, Tools.GameId);
- scoreJob.RunInNewThread();
- }
- }
- }
- }
|