1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 21:12:55 +08:00

Save full api response in SessionStatics

This commit is contained in:
Max Hübner 2020-10-30 16:43:18 +01:00
parent 6f6a8e2a8f
commit f6eb5680ec
2 changed files with 12 additions and 20 deletions

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Configuration
@ -16,8 +14,7 @@ namespace osu.Game.Configuration
{
Set(Static.LoginOverlayDisplayed, false);
Set(Static.MutedAudioNotificationShownOnce, false);
Set(Static.SeasonEndDate, DateTimeOffset.MinValue);
Set(Static.SeasonalBackgrounds, new List<APISeasonalBackground>());
Set<APISeasonalBackgrounds>(Static.SeasonalBackgroundsResponse, null);
}
}
@ -25,7 +22,6 @@ namespace osu.Game.Configuration
{
LoginOverlayDisplayed,
MutedAudioNotificationShownOnce,
SeasonEndDate,
SeasonalBackgrounds,
SeasonalBackgroundsResponse,
}
}

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
@ -19,25 +18,21 @@ namespace osu.Game.Graphics.Backgrounds
[LongRunningLoad]
public class SeasonalBackgroundLoader : Component
{
private Bindable<DateTimeOffset> endDate;
private Bindable<List<APISeasonalBackground>> backgrounds;
private Bindable<APISeasonalBackgrounds> cachedResponse;
private int current;
[BackgroundDependencyLoader]
private void load(SessionStatics sessionStatics, IAPIProvider api)
{
endDate = sessionStatics.GetBindable<DateTimeOffset>(Static.SeasonEndDate);
backgrounds = sessionStatics.GetBindable<List<APISeasonalBackground>>(Static.SeasonalBackgrounds);
cachedResponse = sessionStatics.GetBindable<APISeasonalBackgrounds>(Static.SeasonalBackgroundsResponse);
if (backgrounds.Value.Any()) return;
if (cachedResponse.Value != null) return;
var request = new GetSeasonalBackgroundsRequest();
request.Success += response =>
{
endDate.Value = response.EndDate;
backgrounds.Value = response.Backgrounds ?? backgrounds.Value;
current = RNG.Next(0, backgrounds.Value.Count);
cachedResponse.Value = response;
current = RNG.Next(0, cachedResponse.Value.Backgrounds.Count);
};
api.PerformAsync(request);
@ -45,15 +40,16 @@ namespace osu.Game.Graphics.Backgrounds
public SeasonalBackground LoadBackground()
{
if (!backgrounds.Value.Any()) return null;
var backgrounds = cachedResponse.Value.Backgrounds;
if (!backgrounds.Any()) return null;
current = (current + 1) % backgrounds.Value.Count;
string url = backgrounds.Value[current].Url;
current = (current + 1) % backgrounds.Count;
string url = backgrounds[current].Url;
return new SeasonalBackground(url);
}
public bool IsInSeason => DateTimeOffset.Now < endDate.Value;
public bool IsInSeason => DateTimeOffset.Now < cachedResponse.Value.EndDate;
}
[LongRunningLoad]