Skip to content

Commit

Permalink
Add gamemode selection to score generator and adjust score/beatmap ca…
Browse files Browse the repository at this point in the history
…ching to account for gamemode

Closes #2
  • Loading branch information
Piotrekol committed Apr 26, 2018
1 parent d7acd75 commit 522ce33
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 62 deletions.
1 change: 1 addition & 0 deletions App/Presenters/Controls/UserTopGeneratorPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private void ViewOnStart(object sender, EventArgs eventArgs)
CollectionNameSavePattern = _view.CollectionNamingFormat,
Usernames = _view.Usernames.Split(',').ToList(),
ApiKey = _view.ApiKey,
Gamemode = _view.Gamemode,
ScoreSaveConditions = new ScoreSaveConditions()
{
MinimumPp = _view.PpMin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public class CollectionGeneratorConfiguration
/// api key that should be used to get data
/// </summary>
public string ApiKey { get; set; } = "";

/// <summary>
/// Gamemode to get top scores from
/// </summary>
public int Gamemode { get; set; }

public ScoreSaveConditions ScoreSaveConditions;

Expand Down
12 changes: 11 additions & 1 deletion CollectionManagerExtensionsDll/Modules/API/osu/OsuApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,23 @@ public Beatmap GetBeatmap(int beatmapId)
_downloadedBeatmaps.Add(beatmapId, map);
return map;
}

public Beatmap GetBeatmap(int beatmapId, PlayMode gamemode)
{
if (_downloadedBeatmaps.ContainsKey(beatmapId))
return _downloadedBeatmaps[beatmapId];
var map = GetBeatmapResult(GetBeatmapsURL + "?k=" + ApiKey + "&b=" + beatmapId + "&m=" + (int)gamemode);
if (map != null)
_downloadedBeatmaps.Add(beatmapId, map);
return map;
}
public Beatmap GetBeatmap(string hash)
{
return GetBeatmapResult(GetBeatmapsURL + "?k=" + ApiKey + "&h=" + hash);
}
public IList<ApiScore> GetUserBest(string username, PlayMode mode, int limit = 100)
{
return GetUserScoresResult(GetUserBestURL + "?k=" + ApiKey + "&u=" + username + "&m=" + mode + "&type=string" + "&limit=" + limit);
return GetUserScoresResult(GetUserBestURL + "?k=" + ApiKey + "&u=" + username + "&m=" + (int)mode + "&type=string" + "&limit=" + limit);
}
private IList<ApiScore> GetUserScoresResult(string url)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CollectionManager.DataTypes;
using CollectionManager.Enums;
Expand Down Expand Up @@ -27,16 +28,37 @@ public class UserTopGenerator
private readonly OsuApi _osuApi;
private readonly CollectionsManager _collectionManager;
private LogCollectionGeneration _logger;
readonly Dictionary<string, IList<ApiScore>> _scoreCache = new Dictionary<string, IList<ApiScore>>();
readonly Dictionary<int, Beatmap> _beatmapCache = new Dictionary<int, Beatmap>();

readonly Dictionary<UserModePair, IList<ApiScore>> _scoreCache = new Dictionary<UserModePair, IList<ApiScore>>();
readonly Dictionary<BeatmapModePair, Beatmap> _beatmapCache = new Dictionary<BeatmapModePair, Beatmap>();

private class UserModePair
{
public UserModePair(string username, PlayMode playMode)
{
Username = username;
PlayMode = playMode;
}
public string Username { get; }
public PlayMode PlayMode { get; }
}
private class BeatmapModePair
{
public BeatmapModePair(int beatmapId, PlayMode playMode)
{
BeatmapId = beatmapId;
PlayMode = playMode;
}
public int BeatmapId { get; }
public PlayMode PlayMode { get; }
}

public UserTopGenerator(string osuApiKey, MapCacher mapCacher)
{
if (mapCacher == null)
throw new ArgumentNullException(nameof(mapCacher));
if (string.IsNullOrEmpty(osuApiKey))
throw new ArgumentException("osuApiKey is required.");

_osuApi = new OsuApi(osuApiKey);
_mapCacher = mapCacher;
_collectionManager = new CollectionsManager(_mapCacher.Beatmaps);
Expand All @@ -57,7 +79,7 @@ public Collections GetPlayersCollections(CollectionGeneratorConfiguration cfg, L
foreach (var username in cfg.Usernames)
{
var collections = GetPlayerCollections(username,
cfg.CollectionNameSavePattern, cfg.ScoreSaveConditions);
cfg.CollectionNameSavePattern, cfg.Gamemode, cfg.ScoreSaveConditions);
Log(username, ParsingFinished,
++processedCounter / (double)totalUsernames * 100);
_collectionManager.EditCollection(CollectionEditArgs.AddOrMergeCollections(collections));
Expand Down Expand Up @@ -85,11 +107,11 @@ private void Log(string username, string message, double precentage = -1d)
_lastUsername = username;
_logger?.Invoke(string.Format(ParsingUser, username, message), precentage);
}
private Collections GetPlayerCollections(string username, string collectionNameSavePattern,
private Collections GetPlayerCollections(string username, string collectionNameSavePattern, int gamemode,
ScoreSaveConditions configuration)
{
_currentUserMissingMapCount = 0;
var validScores = GetPlayerScores(username, configuration);
var validScores = GetPlayerScores(username, (PlayMode)gamemode, configuration);
Dictionary<string, Beatmaps> collectionsDict = new Dictionary<string, Beatmaps>();
var collections = new Collections();
foreach (var s in validScores)
Expand All @@ -98,9 +120,9 @@ private Collections GetPlayerCollections(string username, string collectionNameS
{
string collectionName = CreateCollectionName(s, username, collectionNameSavePattern);
if (collectionsDict.ContainsKey(collectionName))
collectionsDict[collectionName].Add(GetBeatmapFromId(s.BeatmapId));
collectionsDict[collectionName].Add(GetBeatmapFromId(s.BeatmapId, (PlayMode)gamemode));
else
collectionsDict.Add(collectionName, new Beatmaps() { GetBeatmapFromId(s.BeatmapId) });
collectionsDict.Add(collectionName, new Beatmaps() { GetBeatmapFromId(s.BeatmapId, (PlayMode)gamemode) });
}
}
foreach (var c in collectionsDict)
Expand All @@ -115,15 +137,17 @@ private Collections GetPlayerCollections(string username, string collectionNameS
return collections;
}

private Beatmap GetBeatmapFromId(int beatmapId)
private Beatmap GetBeatmapFromId(int beatmapId, PlayMode gamemode)
{
foreach (var loadedBeatmap in _mapCacher.Beatmaps)
{
if (loadedBeatmap.MapId == beatmapId)
return loadedBeatmap;
}
if (_beatmapCache.ContainsKey(beatmapId))
return _beatmapCache[beatmapId];
var beatmapFromCache = _beatmapCache.FirstOrDefault(s => s.Key.BeatmapId == beatmapId & s.Key.PlayMode == gamemode).Value;
if (beatmapFromCache != null)
return beatmapFromCache;

Beatmap result;
_currentUserMissingMapCount++;
do
Expand All @@ -133,22 +157,24 @@ private Beatmap GetBeatmapFromId(int beatmapId)
do
{
Log(null, string.Format(GettingBeatmaps, _currentUserMissingMapCount));
result = _osuApi.GetBeatmap(beatmapId);
result = _osuApi.GetBeatmap(beatmapId, gamemode);
} while (result == null && i++ < 5);
if (result == null)
{
Log(null, string.Format(GettingBeatmapFailed, i, Cooldown));
Thread.Sleep(Cooldown * 1000);
}
} while (result == null);
_beatmapCache.Add(beatmapId,result);
_beatmapCache.Add(new BeatmapModePair(beatmapId, gamemode), result);
return result;
}
private IList<ApiScore> GetPlayerScores(string username, ScoreSaveConditions configuration)
private IList<ApiScore> GetPlayerScores(string username, PlayMode gamemode, ScoreSaveConditions configuration)
{
Log(username, string.Format(GettingScores, 1));
if (_scoreCache.ContainsKey(username))
return _scoreCache[username];
var scoresFromCache =
_scoreCache.FirstOrDefault(s => s.Key.Username == username & s.Key.PlayMode == gamemode).Value;
if (scoresFromCache != null)
return scoresFromCache;

List<ApiScore> egibleScores = new List<ApiScore>();
IList<ApiScore> scores;
Expand All @@ -160,7 +186,7 @@ private IList<ApiScore> GetPlayerScores(string username, ScoreSaveConditions con
do
{
Log(username, string.Format(GettingScores, i));
scores = _osuApi.GetUserBest(username, PlayMode.Osu);
scores = _osuApi.GetUserBest(username, (PlayMode)gamemode);
} while (scores == null && i++ < 5);
if (scores == null)
{
Expand All @@ -169,7 +195,7 @@ private IList<ApiScore> GetPlayerScores(string username, ScoreSaveConditions con
}
} while (scores == null);

_scoreCache.Add(username, scores);
_scoreCache.Add(new UserModePair(username, gamemode), scores);
foreach (var s in scores)
{
if (configuration.IsEgibleForSaving(s))
Expand Down
1 change: 1 addition & 0 deletions Common/Interfaces/Controls/IUserTopGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface IUserTopGenerator
double AccMax { get; }
bool GroupByMods { get; }
bool MergeCollectionsWithSameName { get; }
int Gamemode { get; }

ICollectionListingView CollectionListing { get; }

Expand Down
111 changes: 70 additions & 41 deletions GuiComponents/Controls/UserTopGeneratorView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion GuiComponents/Controls/UserTopGeneratorView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public string CollectionNamingExample
public double AccMax => Convert.ToDouble(numericUpDown_accMax.Value);
public bool GroupByMods => checkBox_GroupByMods.Checked;
public bool MergeCollectionsWithSameName => checkBox_mergeCollections.Checked;
public int Gamemode => comboBox_gamemode.SelectedIndex;
public ICollectionListingView CollectionListing => collectionListingView1;

public string ProcessingStatus
Expand All @@ -61,7 +62,7 @@ public string ProcessingStatus
public UserTopGeneratorView()
{
InitializeComponent();

comboBox_gamemode.SelectedIndex = 0;
}

private void RadioButton_AllowScores_CheckedChanged(object sender, EventArgs e)
Expand Down

0 comments on commit 522ce33

Please sign in to comment.