Skip to content

Commit

Permalink
Hide clan leaderboard on unranked
Browse files Browse the repository at this point in the history
  • Loading branch information
Reezonate committed Jan 15, 2024
1 parent cc6bc0c commit 0f702b0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 70 deletions.
1 change: 1 addition & 0 deletions BeatLeader.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
BeatLeader.csproj = Source/BeatLeader.csproj
BeatLeader.csproj.user = Source/BeatLeader.csproj.user
README.md = README.md
EndProjectSection
EndProject
Expand Down
130 changes: 60 additions & 70 deletions Source/2_Core/Managers/DataManager/LeaderboardManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public void Start() {
UploadReplayRequest.AddStateListener(OnUploadRequestStateChanged);
UserRequest.AddStateListener(OnUserRequestStateChanged);

PluginConfig.ScoresContextChangedEvent += ChangeScoreContext;
LeaderboardState.ScoresScopeChangedEvent += ChangeScoreProvider;
PluginConfig.ScoresContextChangedEvent += OnScoresContextWasChanged;
LeaderboardState.ScoresScopeChangedEvent += OnScoresScopeWasSelected;
LeaderboardState.IsVisibleChangedEvent += OnIsVisibleChanged;
LeaderboardEvents.UpButtonWasPressedAction += FetchPreviousPage;
LeaderboardEvents.AroundButtonWasPressedAction += SeekAroundMePage;
LeaderboardEvents.DownButtonWasPressedAction += FetchNextPage;
LeaderboardEvents.CaptorClanWasClickedEvent += HandleCaptorClanClick;
LeaderboardEvents.UpButtonWasPressedAction += OnPreviousPageClick;
LeaderboardEvents.AroundButtonWasPressedAction += OnAroundMeClick;
LeaderboardEvents.DownButtonWasPressedAction += OnNextPageClick;
LeaderboardEvents.CaptorClanWasClickedEvent += OnCaptorClanClick;
ProfileManager.FriendsUpdatedEvent += OnFriendsUpdated;
LeaderboardsCache.CacheWasChangedEvent += OnCacheUpdated;

_selectedScoreContext = PluginConfig.ScoresContext;
_selectedScoreScope = LeaderboardState.ScoresScope;
Expand All @@ -49,14 +50,15 @@ private void OnDestroy() {
UploadReplayRequest.RemoveStateListener(OnUploadRequestStateChanged);
UserRequest.RemoveStateListener(OnUserRequestStateChanged);

PluginConfig.ScoresContextChangedEvent -= ChangeScoreContext;
LeaderboardState.ScoresScopeChangedEvent -= ChangeScoreProvider;
PluginConfig.ScoresContextChangedEvent -= OnScoresContextWasChanged;
LeaderboardState.ScoresScopeChangedEvent -= OnScoresScopeWasSelected;
LeaderboardState.IsVisibleChangedEvent -= OnIsVisibleChanged;
LeaderboardEvents.UpButtonWasPressedAction -= FetchPreviousPage;
LeaderboardEvents.AroundButtonWasPressedAction -= SeekAroundMePage;
LeaderboardEvents.DownButtonWasPressedAction -= FetchNextPage;
LeaderboardEvents.CaptorClanWasClickedEvent -= HandleCaptorClanClick;
LeaderboardEvents.UpButtonWasPressedAction -= OnPreviousPageClick;
LeaderboardEvents.AroundButtonWasPressedAction -= OnAroundMeClick;
LeaderboardEvents.DownButtonWasPressedAction -= OnNextPageClick;
LeaderboardEvents.CaptorClanWasClickedEvent -= OnCaptorClanClick;
ProfileManager.FriendsUpdatedEvent -= OnFriendsUpdated;
LeaderboardsCache.CacheWasChangedEvent -= OnCacheUpdated;
}

#endregion
Expand All @@ -80,7 +82,7 @@ private static void SetFakeBloomProperty() {

#endregion

#region TryUpdateScores / OnIsVisibleChanged
#region Scores Update

private LeaderboardType _leaderboardType = LeaderboardType.SongDiffPlayerScores;
private bool _updateRequired;
Expand All @@ -91,11 +93,6 @@ private void TryUpdateScores() {
UpdateScores();
}

private void OnIsVisibleChanged(bool isVisible) {
if (!isVisible || !_updateRequired) return;
UpdateScores();
}

private void UpdateScores() {
_updateRequired = false;

Expand All @@ -112,32 +109,23 @@ private void UpdateScores() {
}
}

#endregion

#region OnLeaderboardSet

public void OnLeaderboardSet(IDifficultyBeatmap difficultyBeatmap) {
Plugin.Log.Debug($"Selected beatmap: {difficultyBeatmap.level.songName}, diff: {difficultyBeatmap.difficulty}");
_lastSelectedBeatmap = difficultyBeatmap;
_lastSelectedPage = 1;

TryUpdateScores();

LeaderboardState.SelectedBeatmap = difficultyBeatmap;
private void LoadPlayerScores() {
if (!ProfileManager.TryGetUserId(out var userId)) return;
ScoresRequest.SendPlayerScoresPageRequest(userId, Hash, Diff, Mode, Context, Scope, _lastSelectedPage);
}

#endregion

#region OnFriendsUpdated
private void SeekPlayerScores() {
if (!ProfileManager.TryGetUserId(out var userId)) return;
ScoresRequest.SendPlayerScoresSeekRequest(userId, Hash, Diff, Mode, Context, Scope);
}

private void OnFriendsUpdated() {
if (_selectedScoreScope is not ScoresScope.Friends) return;
TryUpdateScores();
private void LoadClanScores() {
ScoresRequest.SendClanScoresPageRequest(Hash, Diff, Mode, _lastSelectedPage);
}

#endregion

#region OnUploadRequestStateChanged
#region Request Events

private LeaderboardKey _uploadLeaderboardKey;

Expand All @@ -155,24 +143,6 @@ private void OnUploadRequestStateChanged(API.RequestState state, Score result, s
}
}

#endregion

#region Score fetching

private void LoadPlayerScores() {
if (!ProfileManager.TryGetUserId(out var userId)) return;
ScoresRequest.SendPlayerScoresPageRequest(userId, Hash, Diff, Mode, Context, Scope, _lastSelectedPage);
}

private void SeekPlayerScores() {
if (!ProfileManager.TryGetUserId(out var userId)) return;
ScoresRequest.SendPlayerScoresSeekRequest(userId, Hash, Diff, Mode, Context, Scope);
}

private void LoadClanScores() {
ScoresRequest.SendClanScoresPageRequest(Hash, Diff, Mode, _lastSelectedPage);
}

private void OnScoresRequestStateChanged(API.RequestState state, ScoresTableContent result, string failReason) {
if (state is not API.RequestState.Finished || _leaderboardType is not LeaderboardType.SongDiffPlayerScores) return;
_lastSelectedPage = result.CurrentPage;
Expand All @@ -183,11 +153,39 @@ private void OnUserRequestStateChanged(API.RequestState state, User result, stri
TryUpdateScores();
}

private void OnFriendsUpdated() {
if (_selectedScoreScope is not ScoresScope.Friends) return;
TryUpdateScores();
}

private void OnCacheUpdated() {
if (_leaderboardType is not LeaderboardType.SongDiffClanScores) return;
if (!LeaderboardsCache.TryGetLeaderboardInfo(LeaderboardState.SelectedBeatmapKey, out var cacheEntry)) return;
if (FormatUtils.GetRankedStatus(cacheEntry.DifficultyInfo) is RankedStatus.Ranked) return;
_leaderboardType = LeaderboardType.SongDiffPlayerScores;
TryUpdateScores();
}

#endregion

#region Select score scope
#region UI Events

private void OnIsVisibleChanged(bool isVisible) {
if (!isVisible || !_updateRequired) return;
UpdateScores();
}

public void OnLeaderboardSet(IDifficultyBeatmap difficultyBeatmap) {
Plugin.Log.Debug($"Selected beatmap: {difficultyBeatmap.level.songName}, diff: {difficultyBeatmap.difficulty}");
_lastSelectedBeatmap = difficultyBeatmap;
_lastSelectedPage = 1;

TryUpdateScores();

private void ChangeScoreProvider(ScoresScope scope) {
LeaderboardState.SelectedBeatmap = difficultyBeatmap;
}

private void OnScoresScopeWasSelected(ScoresScope scope) {
Plugin.Log.Debug($"Attempt to switch score scope from [{_selectedScoreScope}] to [{scope}]");

if (_selectedScoreScope != scope) {
Expand All @@ -199,11 +197,7 @@ private void ChangeScoreProvider(ScoresScope scope) {
}
}

#endregion

#region Select score context

private void ChangeScoreContext(ScoresContext context) {
private void OnScoresContextWasChanged(ScoresContext context) {
Plugin.Log.Debug($"Attempt to switch score context from [{_selectedScoreContext}] to [{context}]");

if (_selectedScoreContext != context) {
Expand All @@ -215,11 +209,7 @@ private void ChangeScoreContext(ScoresContext context) {
}
}

#endregion

#region Pagination

private void FetchPreviousPage() {
private void OnPreviousPageClick() {
if (_lastSelectedPage <= 1) {
_lastSelectedPage = 1;
return;
Expand All @@ -229,16 +219,16 @@ private void FetchPreviousPage() {
TryUpdateScores();
}

private void FetchNextPage() {
private void OnNextPageClick() {
_lastSelectedPage++;
TryUpdateScores();
}

private void SeekAroundMePage() {
private void OnAroundMeClick() {
SeekPlayerScores();
}

private void HandleCaptorClanClick() {
private void OnCaptorClanClick() {
_leaderboardType = _leaderboardType switch {
LeaderboardType.SongDiffPlayerScores => LeaderboardType.SongDiffClanScores,
_ => LeaderboardType.SongDiffPlayerScores
Expand Down

0 comments on commit 0f702b0

Please sign in to comment.