Skip to content

Commit

Permalink
Enhanced offline handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelo420 committed Dec 20, 2024
1 parent a1ce4f8 commit 5505a72
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 36 additions & 17 deletions gamevault/Helper/GameTimeTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");

Expand Down Expand Up @@ -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);
Expand All @@ -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<int> 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())
{
Expand Down
32 changes: 32 additions & 0 deletions gamevault/Helper/LoginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +99,7 @@ public async Task StartupLogin()
});
m_User = user;
m_LoginState = state;
InitOnlineTimer();
}
public async Task<LoginState> ManualLogin(string username, string password)
{
Expand Down Expand Up @@ -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();
}
}
}
}
2 changes: 1 addition & 1 deletion gamevault/UserControls/CommunityUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion gamevault/UserControls/LibraryUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gamevault/UserControls/SettingsUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 5505a72

Please sign in to comment.