From 5505a72b65c89342998d6f6a420bae7dba0c6703 Mon Sep 17 00:00:00 2001 From: Philip Date: Fri, 20 Dec 2024 16:35:44 +0100 Subject: [PATCH] Enhanced offline handling --- CHANGELOG.md | 1 + gamevault/Helper/GameTimeTracker.cs | 53 +++++++++++++------ gamevault/Helper/LoginManager.cs | 32 +++++++++++ .../UserControls/CommunityUserControl.xaml.cs | 2 +- .../UserControls/LibraryUserControl.xaml.cs | 2 +- .../UserControls/SettingsUserControl.xaml.cs | 2 +- 6 files changed, 72 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f27916..3609773 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Recommended Gamevault Server Version: `v13.1.2` - Sync the currently played game with the discord Presence API (+) - Sync installed Gamevault games with Steam shortcuts (+) - The popup window now also closes when you have uninstalled a setup game +- Enhanced offline handling - Bug fix: Added increasing tick rate to download retry timer to avoid continuous spam - Bug fix: Media slider video was sometimes rendered on top of a popup - Bug fix: Crash on Media Slider navigation diff --git a/gamevault/Helper/GameTimeTracker.cs b/gamevault/Helper/GameTimeTracker.cs index 4ab5c7a..42301a1 100644 --- a/gamevault/Helper/GameTimeTracker.cs +++ b/gamevault/Helper/GameTimeTracker.cs @@ -17,7 +17,7 @@ public class GameTimeTracker private Timer? m_Timer { get; set; } public async Task Start() { - await SendOfflineProcess(); + await SendOfflineProgess(); await SettingsViewModel.Instance.InitIgnoreList(); m_Timer = new Timer(); @@ -28,7 +28,7 @@ public async Task Start() } private void TimerCallback(object sender, ElapsedEventArgs e) { - Task.Run(() => + Task.Run(async () => { string installationPath = Path.Combine(SettingsViewModel.Instance.RootPath, "GameVault\\Installations"); @@ -85,10 +85,14 @@ private void TimerCallback(object sender, ElapsedEventArgs e) } } } - if (true == LoginManager.Instance.IsLoggedIn()) + if (LoginManager.Instance.IsLoggedIn()) { try { + if(AnyOfflineProgressToSend()) + { + await SendOfflineProgess(); + } foreach (int gameid in gamesToCountUp) { WebHelper.Put(@$"{SettingsViewModel.Instance.ServerUrl}/api/progresses/user/{LoginManager.Instance.GetCurrentUser().ID}/game/{gameid}/increment", string.Empty); @@ -97,30 +101,45 @@ private void TimerCallback(object sender, ElapsedEventArgs e) } catch (Exception ex) { - LoginManager.Instance.SwitchToOfflineMode(); + SaveToOfflineProgress(gamesToCountUp); } } else { - foreach (int gameid in gamesToCountUp) - { - try - { - string timeString = Preferences.Get(gameid.ToString(), AppFilePath.OfflineProgress, true); - int result = int.TryParse(timeString, out result) ? result : 0; - result++; - Preferences.Set(gameid.ToString(), result, AppFilePath.OfflineProgress, true); - } - catch { } - } + SaveToOfflineProgress(gamesToCountUp); } }); } - private async Task SendOfflineProcess() + private bool AnyOfflineProgressToSend() + { + try + { + return new FileInfo(AppFilePath.OfflineProgress).Length > 0; + } + catch + { + return false; + } + } + private void SaveToOfflineProgress(List progress) + { + foreach (int gameid in progress) + { + try + { + string timeString = Preferences.Get(gameid.ToString(), AppFilePath.OfflineProgress, true); + int result = int.TryParse(timeString, out result) ? result : 0; + result++; + Preferences.Set(gameid.ToString(), result, AppFilePath.OfflineProgress, true); + } + catch { } + } + } + private async Task SendOfflineProgess() { await Task.Run(() => { - if (true == LoginManager.Instance.IsLoggedIn()) + if (LoginManager.Instance.IsLoggedIn()) { foreach (string key in GetAllOfflineCacheKeys()) { diff --git a/gamevault/Helper/LoginManager.cs b/gamevault/Helper/LoginManager.cs index b8333c0..1cf3b6f 100644 --- a/gamevault/Helper/LoginManager.cs +++ b/gamevault/Helper/LoginManager.cs @@ -14,6 +14,7 @@ using System.Text; using System.Text.Json; using System.Threading.Tasks; +using System.Timers; using System.Windows.Documents; using System.Windows.Threading; using Windows.Media.Protection.PlayReady; @@ -52,6 +53,7 @@ public static LoginManager Instance private User? m_User { get; set; } private LoginState m_LoginState { get; set; } private string m_LoginMessage { get; set; } + private Timer onlineTimer { get; set; } public User? GetCurrentUser() { return m_User; @@ -97,6 +99,7 @@ public async Task StartupLogin() }); m_User = user; m_LoginState = state; + InitOnlineTimer(); } public async Task ManualLogin(string username, string password) { @@ -286,5 +289,34 @@ private LoginState DetermineLoginState(string code) } return LoginState.Error; } + private void InitOnlineTimer() + { + if (onlineTimer == null) + { + onlineTimer = new Timer(30000);//30 Seconds + onlineTimer.AutoReset = true; + onlineTimer.Elapsed += CheckOnlineStatus; + onlineTimer.Start(); + } + } + private async void CheckOnlineStatus(object sender, EventArgs e) + { + try + { + string serverResonse = await WebHelper.GetRequestAsync(@$"{SettingsViewModel.Instance.ServerUrl}/api/health"); + if (!IsLoggedIn()) + { + await StartupLogin(); + if (IsLoggedIn()) + { + MainWindowViewModel.Instance.OnlineState = System.Windows.Visibility.Collapsed; + } + } + } + catch (Exception ex) + { + SwitchToOfflineMode(); + } + } } } diff --git a/gamevault/UserControls/CommunityUserControl.xaml.cs b/gamevault/UserControls/CommunityUserControl.xaml.cs index 9c823ad..6625964 100644 --- a/gamevault/UserControls/CommunityUserControl.xaml.cs +++ b/gamevault/UserControls/CommunityUserControl.xaml.cs @@ -215,7 +215,7 @@ private async void ReloadUser_Clicked(object sender, EventArgs e) { if (!LoginManager.Instance.IsLoggedIn()) { - MainWindowViewModel.Instance.AppBarText = "You are not logged in"; + MainWindowViewModel.Instance.AppBarText = "You are not logged in or offline"; return; } if (uiBtnReloadUser.IsEnabled == false || (e.GetType() == typeof(KeyEventArgs) && ((KeyEventArgs)e).Key != Key.F5)) diff --git a/gamevault/UserControls/LibraryUserControl.xaml.cs b/gamevault/UserControls/LibraryUserControl.xaml.cs index cc40003..2a928c6 100644 --- a/gamevault/UserControls/LibraryUserControl.xaml.cs +++ b/gamevault/UserControls/LibraryUserControl.xaml.cs @@ -98,7 +98,7 @@ private async Task Search() { if (!LoginManager.Instance.IsLoggedIn()) { - MainWindowViewModel.Instance.AppBarText = "You are not logged in"; + MainWindowViewModel.Instance.AppBarText = "You are not logged in or offline"; return; } if (!uiExpanderGameCards.IsExpanded) diff --git a/gamevault/UserControls/SettingsUserControl.xaml.cs b/gamevault/UserControls/SettingsUserControl.xaml.cs index 88fb242..e3cb149 100644 --- a/gamevault/UserControls/SettingsUserControl.xaml.cs +++ b/gamevault/UserControls/SettingsUserControl.xaml.cs @@ -189,7 +189,7 @@ private void EditUser_Click(object sender, RoutedEventArgs e) { MainWindowViewModel.Instance.OpenPopup(new UserSettingsUserControl(LoginManager.Instance.GetCurrentUser()) { Width = 1200, Height = 800, Margin = new Thickness(50) }); } - else { MainWindowViewModel.Instance.AppBarText = "You are not logged in"; } + else { MainWindowViewModel.Instance.AppBarText = "You are not logged in or offline"; } } private async void PhalcodeLoginLogout_Click(object sender, RoutedEventArgs e) {