|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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 Player localPlayer = null;
-
- public static void EnterGame()
- {
- if (IsInGame) return;
- Logging.MetaDebugLog("Entering game");
- IsInGame = true;
- IsGameSynced = false;
- IsGameComplete = false;
- GameId = Server.Tools.GameId;
- GameEnterTime = DateTime.UtcNow;
- }
-
- public static void ExitGame()
- {
- if (!IsInGame) return;
- Logging.MetaDebugLog("Exiting game");
- IsInGame = false;
- }
-
- public static void StartPlayingGame()
- {
- if (IsPlayingGame) return;
- Logging.MetaDebugLog("Starting to play game");
- 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.MetaDebugLog("Stopping game");
- 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);
- if (localPlayer == null) return;
- if (localPlayer.GameOver && !localPlayer.Dead)
- {
- State.StopPlayingGame();
- //State.GamePlayTime.TotalSeconds
- UploadJob scoreJob = new UploadJob(State.Score, State.GamePlayTime.TotalSeconds, localPlayer.Position, Tools.UserId, Tools.UserName, Tools.GameId);
- scoreJob.RunInNewThread();
- }
- }
- }
- }
|