Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerSaving
- {
- public static ServerSaving Instance;
- const string savedPlayersKey = "savedPlayers";
- const string savedZombiesSpawn = "savedZombiesSpawn";
- Dictionary<string, DictValue> savedPlayers = new Dictionary<string, DictValue>();
- public static bool zombiesSpawningEnabled = true;
- // day night manager
- const string savedCurTime = "savedCurTime";
- const string savedToNight = "savedToNight";
- const string savedCurPresetId = "savedCurPresetId";
- const string savedNextPresetId = "savedNextPresetId";
- static float curTime;
- static bool toNight;
- static int curPresetId, nextPresetId;
- public static bool savesLoaded = false;
- private static bool isSaving = false;
- ES3File playersSave;
- ES3File serverSave;
- public void GetSaves()
- {
- // loading players
- try
- {
- if (Instance == null)
- Instance = this;
- playersSave = new ES3File("players.txt");
- savedPlayers = playersSave.Load<Dictionary<string, DictValue>>(savedPlayersKey, new Dictionary<string, DictValue>());
- // на всякий случай, если сервер крашнится, когда какой-нибудь игрок
- // находился в процессе выхода с сервера
- foreach (KeyValuePair<string, DictValue> player in savedPlayers)
- player.Value.savedPlayer.status = SavedPlayer.PlayerStatus.saved;
- Debug.Log("Players Loaded");
- }
- catch (Exception e)
- {
- savedPlayers = new Dictionary<string, DictValue>();
- Debug.LogError("Loading Players Error: " + e);
- }
- // loading server data
- try
- {
- serverSave = new ES3File("server.txt");
- zombiesSpawningEnabled = serverSave.Load<bool>(savedZombiesSpawn, true);
- curTime = serverSave.Load(savedCurTime, -1f);
- toNight = serverSave.Load(savedToNight, true);
- curPresetId = serverSave.Load(savedCurPresetId, 0);
- nextPresetId = serverSave.Load(savedNextPresetId, 0);
- Debug.Log("Server Info Loaded");
- }
- catch (Exception e)
- {
- curTime = -1f;
- toNight = true;
- curPresetId = 0;
- nextPresetId = 0;
- Debug.LogError("Loading Server Info Error: " + e);
- }
- savesLoaded = true;
- }
- public void GetDNM(out float _curTime, out bool _toNight, out int _curPresetId, out int _nextPresetId)
- {
- _curTime = curTime;
- _toNight = toNight;
- _curPresetId = curPresetId;
- _nextPresetId = nextPresetId;
- }
- public void SaveDNM(float _curTime, bool _toNight, int _curPresetId, int _nextPresetId)
- {
- curTime = _curTime;
- toNight = _toNight;
- curPresetId = _curPresetId;
- nextPresetId = _nextPresetId;
- Task saving = new Task(SaveDNMTask);
- saving.Start();
- }
- async void SaveDNMTask()
- {
- if (isSaving)
- await Task.Yield();
- isSaving = true;
- try
- {
- serverSave.Save<float>(savedCurTime, curTime);
- serverSave.Save<bool>(savedToNight, toNight);
- serverSave.Save<int>(savedCurPresetId, curPresetId);
- serverSave.Save<int>(savedNextPresetId, nextPresetId);
- serverSave.Sync();
- }
- catch (Exception e)
- {
- Debug.LogError("Saving Server Info Error: " + e);
- }
- isSaving = false;
- }
- public bool HasPlayer(string uniqueId)
- {
- return savedPlayers.ContainsKey(uniqueId);
- }
- public void IsAlive(string uniqueId, out bool isDead, out bool needWatchAd)
- {
- isDead = savedPlayers[uniqueId].savedPlayer.isDead;
- needWatchAd = savedPlayers[uniqueId].savedPlayer.needWatchAd;
- }
- public void GetPlayerData(string uniqueId, out Vector3 pos, out Quaternion rot, out PlayerInventory.PlayerItem[] playerItems, out PlayerInventory.PlayerItem[] backpackItems, out bool inBunker,
- out int health, out int food, out int thirst)
- {
- pos = savedPlayers[uniqueId].savedPlayer.savedPos;
- rot = savedPlayers[uniqueId].savedPlayer.savedRot;
- playerItems = savedPlayers[uniqueId].savedPlayer.playerItems;
- backpackItems = savedPlayers[uniqueId].savedPlayer.backpackItems;
- inBunker = savedPlayers[uniqueId].savedPlayer.inBunker;
- health = savedPlayers[uniqueId].savedPlayer.health;
- food = savedPlayers[uniqueId].savedPlayer.food;
- thirst = savedPlayers[uniqueId].savedPlayer.thirst;
- }
- public void RemovePlayerData(string uniqueId)
- {
- if (!savedPlayers.ContainsKey(uniqueId))
- return;
- savedPlayers.Remove(uniqueId);
- Save();
- }
- public void SavePlayerData(string uniqueId, Vector3 pos, Quaternion rot, PlayerInventory.PlayerItem[] playerItems, PlayerInventory.PlayerItem[] backpackItems, bool inBunker, int health, int food, int thirst)
- {
- Debuger.Log("Save Full");
- SavedPlayer newPlayer = new SavedPlayer();
- newPlayer.uniqueId = uniqueId;
- newPlayer.savedPos = pos;
- newPlayer.savedRot = rot;
- newPlayer.playerItems = playerItems;
- newPlayer.backpackItems = backpackItems;
- newPlayer.inBunker = inBunker;
- newPlayer.isDead = false;
- newPlayer.needWatchAd = false;
- newPlayer.status = SavedPlayer.PlayerStatus.disconnecting;
- newPlayer.health = health;
- newPlayer.food = food;
- newPlayer.thirst = thirst;
- if (HasPlayer(uniqueId))
- {
- //if (!NetworkManagerCustom.isHost)
- //{
- // //Debug.LogError("Tring to save already saved player");
- // //return;
- //}
- //else
- savedPlayers[uniqueId].savedPlayer = newPlayer;
- }
- else
- {
- DictValue test = new DictValue();
- test.savedPlayer = newPlayer;
- savedPlayers.Add(uniqueId, test);
- }
- Save();
- }
- public void SavePlayerData(string uniqueId, bool isDead, bool needWatchAd)
- {
- Debuger.Log("Save Dead: " + needWatchAd);
- SavedPlayer newPlayer = new SavedPlayer();
- newPlayer.uniqueId = uniqueId;
- newPlayer.status = SavedPlayer.PlayerStatus.saved;
- newPlayer.isDead = isDead;
- newPlayer.needWatchAd = needWatchAd;
- if (HasPlayer(uniqueId))
- {
- //if (!NetworkManagerCustom.isHost)
- // Debug.LogError("Tring to save already saved player");
- //return;
- savedPlayers[uniqueId].savedPlayer = newPlayer;
- }
- else
- {
- DictValue test = new DictValue();
- test.savedPlayer = newPlayer;
- savedPlayers.Add(uniqueId, test);
- }
- Save();
- }
- public SavedPlayer.PlayerStatus GetPlayerStatus(string uniqueId)
- {
- return savedPlayers[uniqueId].savedPlayer.status;
- }
- public void ChangePlayerStatus(string uniqueId, SavedPlayer.PlayerStatus _status)
- {
- savedPlayers[uniqueId].savedPlayer.status = _status;
- }
- public void Save()
- {
- Task saving = new Task(SaveTask);
- saving.Start();
- }
- private async void SaveTask()
- {
- if (isSaving)
- await Task.Yield();
- isSaving = true;
- try
- {
- playersSave.Save<Dictionary<string, DictValue>>(savedPlayersKey, savedPlayers);
- playersSave.Sync();
- }
- catch (Exception e)
- {
- Debug.LogError("Saving Players Error: " + e);
- }
- isSaving = false;
- }
- public static bool IsHaveSaves()
- {
- if (ES3.KeyExists(savedCurTime) ||
- ES3.KeyExists(savedToNight) ||
- ES3.KeyExists(savedCurPresetId) ||
- ES3.KeyExists(savedNextPresetId) ||
- ES3.KeyExists(savedPlayersKey) ||
- ES3.KeyExists(savedZombiesSpawn))
- return true;
- return false;
- }
- public static void ClearSinglePlayerSaves()
- {
- // DNM
- ES3.DeleteKey(savedCurTime);
- ES3.DeleteKey(savedToNight);
- ES3.DeleteKey(savedCurPresetId);
- ES3.DeleteKey(savedNextPresetId);
- // saved players
- ES3.DeleteKey(savedPlayersKey);
- // zombies spawn toggle
- ES3.DeleteKey(savedZombiesSpawn);
- }
- public void SetZombiesSpawning(bool what)
- {
- zombiesSpawningEnabled = what;
- try
- {
- serverSave.Save<bool>(savedZombiesSpawn, what);
- serverSave.Sync();
- }
- catch (Exception e)
- {
- Debug.LogError("Saving SetZombies Error: " + e);
- }
- }
- }
- public class SavedPlayer
- {
- public string uniqueId;
- public Vector3 savedPos;
- public Quaternion savedRot;
- public PlayerInventory.PlayerItem[] playerItems;
- public PlayerInventory.PlayerItem[] backpackItems;
- public int health;
- public int food;
- public int thirst;
- public bool inBunker;
- public bool isDead;
- public bool needWatchAd;
- public enum PlayerStatus { disconnecting, saved };
- public PlayerStatus status;
- }
- public class DictValue
- {
- public SavedPlayer savedPlayer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement