Advertisement
Guest User

Untitled

a guest
Sep 12th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.53 KB | None | 0 0
  1. public class ServerSaving
  2. {
  3.     public static ServerSaving Instance;
  4.  
  5.     const string savedPlayersKey = "savedPlayers";
  6.     const string savedZombiesSpawn = "savedZombiesSpawn";
  7.  
  8.     Dictionary<string, DictValue> savedPlayers = new Dictionary<string, DictValue>();
  9.     public static bool zombiesSpawningEnabled = true;
  10.  
  11.     // day night manager
  12.     const string savedCurTime = "savedCurTime";
  13.     const string savedToNight = "savedToNight";
  14.     const string savedCurPresetId = "savedCurPresetId";
  15.     const string savedNextPresetId = "savedNextPresetId";
  16.  
  17.     static float curTime;
  18.     static bool toNight;
  19.     static int curPresetId, nextPresetId;
  20.  
  21.     public static bool savesLoaded = false;
  22.     private static bool isSaving = false;
  23.  
  24.     ES3File playersSave;
  25.     ES3File serverSave;
  26.  
  27.     public void GetSaves()
  28.     {
  29.         //  loading players
  30.         try
  31.         {
  32.             if (Instance == null)
  33.                 Instance = this;
  34.  
  35.             playersSave = new ES3File("players.txt");
  36.  
  37.             savedPlayers = playersSave.Load<Dictionary<string, DictValue>>(savedPlayersKey, new Dictionary<string, DictValue>());
  38.  
  39.             //  на всякий случай, если сервер крашнится, когда какой-нибудь игрок
  40.             //  находился в процессе выхода с сервера
  41.             foreach (KeyValuePair<string, DictValue> player in savedPlayers)
  42.                 player.Value.savedPlayer.status = SavedPlayer.PlayerStatus.saved;
  43.  
  44.             Debug.Log("Players Loaded");
  45.         }
  46.         catch (Exception e)
  47.         {
  48.             savedPlayers = new Dictionary<string, DictValue>();
  49.             Debug.LogError("Loading Players Error: " + e);
  50.         }
  51.  
  52.         //  loading server data
  53.  
  54.         try
  55.         {
  56.             serverSave = new ES3File("server.txt");
  57.  
  58.             zombiesSpawningEnabled = serverSave.Load<bool>(savedZombiesSpawn, true);
  59.  
  60.             curTime = serverSave.Load(savedCurTime, -1f);
  61.             toNight = serverSave.Load(savedToNight, true);
  62.             curPresetId = serverSave.Load(savedCurPresetId, 0);
  63.             nextPresetId = serverSave.Load(savedNextPresetId, 0);
  64.  
  65.             Debug.Log("Server Info Loaded");
  66.         }
  67.         catch (Exception e)
  68.         {
  69.             curTime = -1f;
  70.             toNight = true;
  71.             curPresetId = 0;
  72.             nextPresetId = 0;
  73.             Debug.LogError("Loading Server Info Error: " + e);
  74.         }
  75.  
  76.         savesLoaded = true;
  77.     }
  78.  
  79.     public void GetDNM(out float _curTime, out bool _toNight, out int _curPresetId, out int _nextPresetId)
  80.     {
  81.         _curTime = curTime;
  82.         _toNight = toNight;
  83.         _curPresetId = curPresetId;
  84.         _nextPresetId = nextPresetId;
  85.     }
  86.  
  87.     public void SaveDNM(float _curTime, bool _toNight, int _curPresetId, int _nextPresetId)
  88.     {
  89.         curTime = _curTime;
  90.         toNight = _toNight;
  91.         curPresetId = _curPresetId;
  92.         nextPresetId = _nextPresetId;
  93.  
  94.         Task saving = new Task(SaveDNMTask);
  95.         saving.Start();
  96.     }
  97.  
  98.     async void SaveDNMTask()
  99.     {
  100.         if (isSaving)
  101.             await Task.Yield();
  102.         isSaving = true;
  103.  
  104.         try
  105.         {
  106.             serverSave.Save<float>(savedCurTime, curTime);
  107.             serverSave.Save<bool>(savedToNight, toNight);
  108.             serverSave.Save<int>(savedCurPresetId, curPresetId);
  109.             serverSave.Save<int>(savedNextPresetId, nextPresetId);
  110.             serverSave.Sync();
  111.         }
  112.         catch (Exception e)
  113.         {
  114.             Debug.LogError("Saving Server Info Error: " + e);
  115.         }
  116.  
  117.  
  118.         isSaving = false;
  119.     }
  120.  
  121.     public bool HasPlayer(string uniqueId)
  122.     {
  123.         return savedPlayers.ContainsKey(uniqueId);
  124.     }
  125.  
  126.     public void IsAlive(string uniqueId, out bool isDead, out bool needWatchAd)
  127.     {
  128.         isDead = savedPlayers[uniqueId].savedPlayer.isDead;
  129.         needWatchAd = savedPlayers[uniqueId].savedPlayer.needWatchAd;
  130.     }
  131.  
  132.     public void GetPlayerData(string uniqueId, out Vector3 pos, out Quaternion rot, out PlayerInventory.PlayerItem[] playerItems, out PlayerInventory.PlayerItem[] backpackItems, out bool inBunker,
  133.                               out int health, out int food, out int thirst)
  134.     {
  135.         pos = savedPlayers[uniqueId].savedPlayer.savedPos;
  136.         rot = savedPlayers[uniqueId].savedPlayer.savedRot;
  137.         playerItems = savedPlayers[uniqueId].savedPlayer.playerItems;
  138.         backpackItems = savedPlayers[uniqueId].savedPlayer.backpackItems;
  139.         inBunker = savedPlayers[uniqueId].savedPlayer.inBunker;
  140.         health = savedPlayers[uniqueId].savedPlayer.health;
  141.         food = savedPlayers[uniqueId].savedPlayer.food;
  142.         thirst = savedPlayers[uniqueId].savedPlayer.thirst;
  143.     }
  144.  
  145.     public void RemovePlayerData(string uniqueId)
  146.     {
  147.         if (!savedPlayers.ContainsKey(uniqueId))
  148.             return;
  149.  
  150.         savedPlayers.Remove(uniqueId);
  151.         Save();
  152.     }
  153.  
  154.     public void SavePlayerData(string uniqueId, Vector3 pos, Quaternion rot, PlayerInventory.PlayerItem[] playerItems, PlayerInventory.PlayerItem[] backpackItems, bool inBunker, int health, int food, int thirst)
  155.     {
  156.         Debuger.Log("Save Full");
  157.         SavedPlayer newPlayer = new SavedPlayer();
  158.         newPlayer.uniqueId = uniqueId;
  159.         newPlayer.savedPos = pos;
  160.         newPlayer.savedRot = rot;
  161.         newPlayer.playerItems = playerItems;
  162.         newPlayer.backpackItems = backpackItems;
  163.         newPlayer.inBunker = inBunker;
  164.         newPlayer.isDead = false;
  165.         newPlayer.needWatchAd = false;
  166.         newPlayer.status = SavedPlayer.PlayerStatus.disconnecting;
  167.  
  168.         newPlayer.health = health;
  169.         newPlayer.food = food;
  170.         newPlayer.thirst = thirst;
  171.  
  172.         if (HasPlayer(uniqueId))
  173.         {
  174.             //if (!NetworkManagerCustom.isHost)
  175.             //{
  176.  
  177.             //    //Debug.LogError("Tring to save already saved player");
  178.             //    //return;
  179.             //}
  180.             //else
  181.             savedPlayers[uniqueId].savedPlayer = newPlayer;
  182.         }
  183.         else
  184.         {
  185.             DictValue test = new DictValue();
  186.             test.savedPlayer = newPlayer;
  187.             savedPlayers.Add(uniqueId, test);
  188.         }
  189.         Save();
  190.     }
  191.  
  192.     public void SavePlayerData(string uniqueId, bool isDead, bool needWatchAd)
  193.     {
  194.         Debuger.Log("Save Dead: " + needWatchAd);
  195.         SavedPlayer newPlayer = new SavedPlayer();
  196.         newPlayer.uniqueId = uniqueId;
  197.         newPlayer.status = SavedPlayer.PlayerStatus.saved;
  198.         newPlayer.isDead = isDead;
  199.         newPlayer.needWatchAd = needWatchAd;
  200.  
  201.         if (HasPlayer(uniqueId))
  202.         {
  203.             //if (!NetworkManagerCustom.isHost)
  204.             //    Debug.LogError("Tring to save already saved player");
  205.             //return;
  206.             savedPlayers[uniqueId].savedPlayer = newPlayer;
  207.         }
  208.         else
  209.         {
  210.             DictValue test = new DictValue();
  211.             test.savedPlayer = newPlayer;
  212.             savedPlayers.Add(uniqueId, test);
  213.         }
  214.         Save();
  215.     }
  216.  
  217.     public SavedPlayer.PlayerStatus GetPlayerStatus(string uniqueId)
  218.     {
  219.         return savedPlayers[uniqueId].savedPlayer.status;
  220.     }
  221.  
  222.     public void ChangePlayerStatus(string uniqueId, SavedPlayer.PlayerStatus _status)
  223.     {
  224.         savedPlayers[uniqueId].savedPlayer.status = _status;
  225.     }
  226.  
  227.     public void Save()
  228.     {
  229.         Task saving = new Task(SaveTask);
  230.         saving.Start();
  231.     }
  232.  
  233.     private async void SaveTask()
  234.     {
  235.         if (isSaving)
  236.             await Task.Yield();
  237.         isSaving = true;
  238.  
  239.         try
  240.         {
  241.             playersSave.Save<Dictionary<string, DictValue>>(savedPlayersKey, savedPlayers);
  242.             playersSave.Sync();
  243.         }
  244.         catch (Exception e)
  245.         {
  246.             Debug.LogError("Saving Players Error: " + e);
  247.         }
  248.  
  249.         isSaving = false;
  250.     }
  251.  
  252.     public static bool IsHaveSaves()
  253.     {
  254.         if (ES3.KeyExists(savedCurTime) ||
  255.             ES3.KeyExists(savedToNight) ||
  256.             ES3.KeyExists(savedCurPresetId) ||
  257.             ES3.KeyExists(savedNextPresetId) ||
  258.             ES3.KeyExists(savedPlayersKey) ||
  259.             ES3.KeyExists(savedZombiesSpawn))
  260.             return true;
  261.         return false;
  262.     }
  263.  
  264.     public static void ClearSinglePlayerSaves()
  265.     {
  266.         //  DNM
  267.         ES3.DeleteKey(savedCurTime);
  268.         ES3.DeleteKey(savedToNight);
  269.         ES3.DeleteKey(savedCurPresetId);
  270.         ES3.DeleteKey(savedNextPresetId);
  271.  
  272.         //  saved players
  273.         ES3.DeleteKey(savedPlayersKey);
  274.  
  275.         //  zombies spawn toggle
  276.         ES3.DeleteKey(savedZombiesSpawn);
  277.     }
  278.  
  279.     public void SetZombiesSpawning(bool what)
  280.     {
  281.         zombiesSpawningEnabled = what;
  282.         try
  283.         {
  284.             serverSave.Save<bool>(savedZombiesSpawn, what);
  285.             serverSave.Sync();
  286.         }
  287.         catch (Exception e)
  288.         {
  289.             Debug.LogError("Saving SetZombies Error: " + e);
  290.         }
  291.  
  292.     }
  293. }
  294.  
  295. public class SavedPlayer
  296. {
  297.     public string uniqueId;
  298.     public Vector3 savedPos;
  299.     public Quaternion savedRot;
  300.     public PlayerInventory.PlayerItem[] playerItems;
  301.     public PlayerInventory.PlayerItem[] backpackItems;
  302.  
  303.     public int health;
  304.     public int food;
  305.     public int thirst;
  306.  
  307.     public bool inBunker;
  308.     public bool isDead;
  309.     public bool needWatchAd;
  310.  
  311.     public enum PlayerStatus { disconnecting, saved };
  312.     public PlayerStatus status;
  313. }
  314. public class DictValue
  315. {
  316.     public SavedPlayer savedPlayer;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement