From 8c7a68cc6b742e7e1967cb1e80df303385351715 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Fri, 22 Jul 2022 14:01:41 +0300 Subject: [PATCH] feat: add save system --- Assets/_Scripts/Managers/SaveManager.cs | 17 ++++++++++ Assets/_Scripts/Managers/SaveManager.cs.meta | 11 +++++++ Assets/_Scripts/Managers/ScoreManager.cs | 19 ++++++++++- .../Systems/SaveSystem/SessionStore.cs | 33 +++++++++++++++++++ .../Systems/SaveSystem/SessionStore.cs.meta | 3 ++ .../_Scripts/Systems/SaveSystem/UserData.cs | 5 +++ .../Systems/SaveSystem/UserData.cs.meta | 3 ++ 7 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 Assets/_Scripts/Managers/SaveManager.cs create mode 100644 Assets/_Scripts/Managers/SaveManager.cs.meta create mode 100644 Assets/_Scripts/Systems/SaveSystem/SessionStore.cs create mode 100644 Assets/_Scripts/Systems/SaveSystem/SessionStore.cs.meta create mode 100644 Assets/_Scripts/Systems/SaveSystem/UserData.cs create mode 100644 Assets/_Scripts/Systems/SaveSystem/UserData.cs.meta diff --git a/Assets/_Scripts/Managers/SaveManager.cs b/Assets/_Scripts/Managers/SaveManager.cs new file mode 100644 index 0000000..a6abfa3 --- /dev/null +++ b/Assets/_Scripts/Managers/SaveManager.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +public class SaveManager : MonoBehaviour +{ + [SerializeField] private string ApiUrl; + + private void Awake() + { + SessionStore.ApiUrl = ApiUrl; + SessionStore.Load(); + } + + private void OnApplicationQuit() + { + SessionStore.Save(); + } +} diff --git a/Assets/_Scripts/Managers/SaveManager.cs.meta b/Assets/_Scripts/Managers/SaveManager.cs.meta new file mode 100644 index 0000000..a404aa8 --- /dev/null +++ b/Assets/_Scripts/Managers/SaveManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0d152cc19f7d546e382626f05f73467d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Scripts/Managers/ScoreManager.cs b/Assets/_Scripts/Managers/ScoreManager.cs index 51734a1..4b80a2d 100644 --- a/Assets/_Scripts/Managers/ScoreManager.cs +++ b/Assets/_Scripts/Managers/ScoreManager.cs @@ -23,12 +23,13 @@ public class ScoreManager : MonoBehaviour PlayerEvents.OnBallTouched += AddScore; PlayerEvents.OnBallTouched += AddExperience; PlayerEvents.OnWallTouched += ResetExperienceAndRewardMultiplier; + PlayerEvents.OnDeath += SaveHighScore; GameStateManager.Instance.OnGameStateChange += OnGameStateChange; } private void Start() { - _highScore = SessionStore.HighScore; + LoadHighScore(); } private void Update() @@ -94,6 +95,22 @@ public class ScoreManager : MonoBehaviour ResetExperienceAndRewardMultiplier(); } + private void SaveHighScore() + { + if (_currentScore < _highScore) + { + return; + } + + _highScore = _currentScore; + SessionStore.HighScore = _highScore; + } + + private void LoadHighScore() + { + _highScore = SessionStore.HighScore; + } + private void OnGameStateChange(GameState newGameState) { switch (newGameState) diff --git a/Assets/_Scripts/Systems/SaveSystem/SessionStore.cs b/Assets/_Scripts/Systems/SaveSystem/SessionStore.cs new file mode 100644 index 0000000..4a81580 --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/SessionStore.cs @@ -0,0 +1,33 @@ +using System.Linq; +using DatabaseModels.DataTransferObjets; +using JetBrains.Annotations; + +public static class SessionStore +{ + public static string ApiUrl { get; set; } + + public static UserData UserData { get; private set; } + public static int HighScore { get; set; } + + public static ScoreboardRecordDto[] ScoreboardRecords; + + public static async void Save() + { + await SaveSystem.SaveToJsonAsync("userData.json", UserData); + await SaveSystem.SaveToBinaryAsync("HighScore.bin", HighScore); + } + + public static async void Load() + { + UserData = await SaveSystem.LoadFromJsonAsync("userData.json"); + HighScore = await SaveSystem.LoadFromBinaryAsync("HighScore.bin"); + + ScoreboardRecords = await HttpClient.Get($"{ApiUrl}/scoreboard"); + + int? dbHighScore = ScoreboardRecords?.FirstOrDefault(sbr => sbr.User.Username == UserData.Username)?.Score; + if (dbHighScore != null && dbHighScore > HighScore) + { + HighScore = (int) dbHighScore; + } + } +} \ No newline at end of file diff --git a/Assets/_Scripts/Systems/SaveSystem/SessionStore.cs.meta b/Assets/_Scripts/Systems/SaveSystem/SessionStore.cs.meta new file mode 100644 index 0000000..07957f5 --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/SessionStore.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 64ad7cad6b2d4b6c8e5bf9684216b484 +timeCreated: 1658237964 \ No newline at end of file diff --git a/Assets/_Scripts/Systems/SaveSystem/UserData.cs b/Assets/_Scripts/Systems/SaveSystem/UserData.cs new file mode 100644 index 0000000..75b04bb --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/UserData.cs @@ -0,0 +1,5 @@ +public class UserData +{ + public string Username { get; set; } + public string Password { get; set; } +} \ No newline at end of file diff --git a/Assets/_Scripts/Systems/SaveSystem/UserData.cs.meta b/Assets/_Scripts/Systems/SaveSystem/UserData.cs.meta new file mode 100644 index 0000000..7c312dc --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/UserData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8fe7c657c4e04ed5998db05dc8771aae +timeCreated: 1658238022 \ No newline at end of file