From 522ce336e5fd4f2c14e76bae85fb633431a61248 Mon Sep 17 00:00:00 2001 From: Piotrekol Date: Thu, 26 Apr 2018 21:33:27 +0200 Subject: [PATCH] Add gamemode selection to score generator and adjust score/beatmap caching to account for gamemode Closes #2 --- .../Controls/UserTopGeneratorPresenter.cs | 1 + .../CollectionGeneratorConfiguration.cs | 5 + .../Modules/API/osu/OsuApi.cs | 12 +- .../CollectionGenerator/UserTopGenerator.cs | 64 +++++++--- .../Interfaces/Controls/IUserTopGenerator.cs | 1 + .../Controls/UserTopGeneratorView.Designer.cs | 111 +++++++++++------- .../Controls/UserTopGeneratorView.cs | 3 +- 7 files changed, 135 insertions(+), 62 deletions(-) diff --git a/App/Presenters/Controls/UserTopGeneratorPresenter.cs b/App/Presenters/Controls/UserTopGeneratorPresenter.cs index 2caf75d..1fd71d9 100644 --- a/App/Presenters/Controls/UserTopGeneratorPresenter.cs +++ b/App/Presenters/Controls/UserTopGeneratorPresenter.cs @@ -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, diff --git a/CollectionManagerExtensionsDll/DataTypes/CollectionGeneratorConfiguration.cs b/CollectionManagerExtensionsDll/DataTypes/CollectionGeneratorConfiguration.cs index 7aa0d78..c2bb81d 100644 --- a/CollectionManagerExtensionsDll/DataTypes/CollectionGeneratorConfiguration.cs +++ b/CollectionManagerExtensionsDll/DataTypes/CollectionGeneratorConfiguration.cs @@ -20,6 +20,11 @@ public class CollectionGeneratorConfiguration /// api key that should be used to get data /// public string ApiKey { get; set; } = ""; + + /// + /// Gamemode to get top scores from + /// + public int Gamemode { get; set; } public ScoreSaveConditions ScoreSaveConditions; diff --git a/CollectionManagerExtensionsDll/Modules/API/osu/OsuApi.cs b/CollectionManagerExtensionsDll/Modules/API/osu/OsuApi.cs index 4cef4e9..bf5345e 100644 --- a/CollectionManagerExtensionsDll/Modules/API/osu/OsuApi.cs +++ b/CollectionManagerExtensionsDll/Modules/API/osu/OsuApi.cs @@ -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 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 GetUserScoresResult(string url) { diff --git a/CollectionManagerExtensionsDll/Modules/CollectionGenerator/UserTopGenerator.cs b/CollectionManagerExtensionsDll/Modules/CollectionGenerator/UserTopGenerator.cs index d92a20c..815b109 100644 --- a/CollectionManagerExtensionsDll/Modules/CollectionGenerator/UserTopGenerator.cs +++ b/CollectionManagerExtensionsDll/Modules/CollectionGenerator/UserTopGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using CollectionManager.DataTypes; using CollectionManager.Enums; @@ -27,16 +28,37 @@ public class UserTopGenerator private readonly OsuApi _osuApi; private readonly CollectionsManager _collectionManager; private LogCollectionGeneration _logger; - readonly Dictionary> _scoreCache = new Dictionary>(); - readonly Dictionary _beatmapCache = new Dictionary(); - + readonly Dictionary> _scoreCache = new Dictionary>(); + readonly Dictionary _beatmapCache = new Dictionary(); + + 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); @@ -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)); @@ -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 collectionsDict = new Dictionary(); var collections = new Collections(); foreach (var s in validScores) @@ -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) @@ -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 @@ -133,7 +157,7 @@ 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) { @@ -141,14 +165,16 @@ private Beatmap GetBeatmapFromId(int beatmapId) Thread.Sleep(Cooldown * 1000); } } while (result == null); - _beatmapCache.Add(beatmapId,result); + _beatmapCache.Add(new BeatmapModePair(beatmapId, gamemode), result); return result; } - private IList GetPlayerScores(string username, ScoreSaveConditions configuration) + private IList 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 egibleScores = new List(); IList scores; @@ -160,7 +186,7 @@ private IList 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) { @@ -169,7 +195,7 @@ private IList 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)) diff --git a/Common/Interfaces/Controls/IUserTopGenerator.cs b/Common/Interfaces/Controls/IUserTopGenerator.cs index e5022dd..9d49e05 100644 --- a/Common/Interfaces/Controls/IUserTopGenerator.cs +++ b/Common/Interfaces/Controls/IUserTopGenerator.cs @@ -21,6 +21,7 @@ public interface IUserTopGenerator double AccMax { get; } bool GroupByMods { get; } bool MergeCollectionsWithSameName { get; } + int Gamemode { get; } ICollectionListingView CollectionListing { get; } diff --git a/GuiComponents/Controls/UserTopGeneratorView.Designer.cs b/GuiComponents/Controls/UserTopGeneratorView.Designer.cs index 77ff089..80c7e6a 100644 --- a/GuiComponents/Controls/UserTopGeneratorView.Designer.cs +++ b/GuiComponents/Controls/UserTopGeneratorView.Designer.cs @@ -29,12 +29,8 @@ protected override void Dispose(bool disposing) private void InitializeComponent() { this.groupBox_configuration = new System.Windows.Forms.GroupBox(); - this.label_processingStatus = new System.Windows.Forms.Label(); this.button_GenerateUsernames = new System.Windows.Forms.Button(); - this.progressBar_usernames = new System.Windows.Forms.ProgressBar(); this.label6 = new System.Windows.Forms.Label(); - this.button_Abort = new System.Windows.Forms.Button(); - this.button_Start = new System.Windows.Forms.Button(); this.textBox_apiKey = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.label10 = new System.Windows.Forms.Label(); @@ -58,8 +54,14 @@ private void InitializeComponent() this.textBox_collectionNameFormat = new System.Windows.Forms.TextBox(); this.textBox_usernames = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); + this.label_processingStatus = new System.Windows.Forms.Label(); + this.progressBar_usernames = new System.Windows.Forms.ProgressBar(); + this.button_Abort = new System.Windows.Forms.Button(); + this.button_Start = new System.Windows.Forms.Button(); this.collectionListingView1 = new GuiComponents.Controls.CollectionListingView(); this.panel1 = new System.Windows.Forms.Panel(); + this.comboBox_gamemode = new System.Windows.Forms.ComboBox(); + this.label11 = new System.Windows.Forms.Label(); this.groupBox_configuration.SuspendLayout(); this.groupBox3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown_maximumPP)).BeginInit(); @@ -73,6 +75,8 @@ private void InitializeComponent() // this.groupBox_configuration.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); + this.groupBox_configuration.Controls.Add(this.label11); + this.groupBox_configuration.Controls.Add(this.comboBox_gamemode); this.groupBox_configuration.Controls.Add(this.button_GenerateUsernames); this.groupBox_configuration.Controls.Add(this.label6); this.groupBox_configuration.Controls.Add(this.textBox_apiKey); @@ -90,15 +94,6 @@ private void InitializeComponent() this.groupBox_configuration.TabStop = false; this.groupBox_configuration.Text = "Configuration"; // - // label_processingStatus - // - this.label_processingStatus.AutoSize = true; - this.label_processingStatus.Location = new System.Drawing.Point(19, 58); - this.label_processingStatus.Name = "label_processingStatus"; - this.label_processingStatus.Size = new System.Drawing.Size(52, 13); - this.label_processingStatus.TabIndex = 29; - this.label_processingStatus.Text = "Waiting..."; - // // button_GenerateUsernames // this.button_GenerateUsernames.Location = new System.Drawing.Point(395, 95); @@ -109,13 +104,6 @@ private void InitializeComponent() this.button_GenerateUsernames.UseVisualStyleBackColor = true; this.button_GenerateUsernames.Click += new System.EventHandler(this.button_GenerateUsernames_Click); // - // progressBar_usernames - // - this.progressBar_usernames.Location = new System.Drawing.Point(22, 32); - this.progressBar_usernames.Name = "progressBar_usernames"; - this.progressBar_usernames.Size = new System.Drawing.Size(496, 23); - this.progressBar_usernames.TabIndex = 26; - // // label6 // this.label6.AutoSize = true; @@ -125,27 +113,6 @@ private void InitializeComponent() this.label6.TabIndex = 25; this.label6.Text = "Your osu!api key:"; // - // button_Abort - // - this.button_Abort.Enabled = false; - this.button_Abort.Location = new System.Drawing.Point(278, 3); - this.button_Abort.Name = "button_Abort"; - this.button_Abort.Size = new System.Drawing.Size(240, 23); - this.button_Abort.TabIndex = 24; - this.button_Abort.Text = "Abort"; - this.button_Abort.UseVisualStyleBackColor = true; - this.button_Abort.Click += new System.EventHandler(this.button_Abort_Click); - // - // button_Start - // - this.button_Start.Location = new System.Drawing.Point(22, 3); - this.button_Start.Name = "button_Start"; - this.button_Start.Size = new System.Drawing.Size(240, 23); - this.button_Start.TabIndex = 23; - this.button_Start.Text = "Start"; - this.button_Start.UseVisualStyleBackColor = true; - this.button_Start.Click += new System.EventHandler(this.button_Start_Click); - // // textBox_apiKey // this.textBox_apiKey.Location = new System.Drawing.Point(19, 279); @@ -415,6 +382,43 @@ private void InitializeComponent() this.label1.TabIndex = 9; this.label1.Text = "Enter comma separated list of usernames to get data from"; // + // label_processingStatus + // + this.label_processingStatus.AutoSize = true; + this.label_processingStatus.Location = new System.Drawing.Point(19, 58); + this.label_processingStatus.Name = "label_processingStatus"; + this.label_processingStatus.Size = new System.Drawing.Size(52, 13); + this.label_processingStatus.TabIndex = 29; + this.label_processingStatus.Text = "Waiting..."; + // + // progressBar_usernames + // + this.progressBar_usernames.Location = new System.Drawing.Point(22, 32); + this.progressBar_usernames.Name = "progressBar_usernames"; + this.progressBar_usernames.Size = new System.Drawing.Size(496, 23); + this.progressBar_usernames.TabIndex = 26; + // + // button_Abort + // + this.button_Abort.Enabled = false; + this.button_Abort.Location = new System.Drawing.Point(278, 3); + this.button_Abort.Name = "button_Abort"; + this.button_Abort.Size = new System.Drawing.Size(240, 23); + this.button_Abort.TabIndex = 24; + this.button_Abort.Text = "Abort"; + this.button_Abort.UseVisualStyleBackColor = true; + this.button_Abort.Click += new System.EventHandler(this.button_Abort_Click); + // + // button_Start + // + this.button_Start.Location = new System.Drawing.Point(22, 3); + this.button_Start.Name = "button_Start"; + this.button_Start.Size = new System.Drawing.Size(240, 23); + this.button_Start.TabIndex = 23; + this.button_Start.Text = "Start"; + this.button_Start.UseVisualStyleBackColor = true; + this.button_Start.Click += new System.EventHandler(this.button_Start_Click); + // // collectionListingView1 // this.collectionListingView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -437,6 +441,29 @@ private void InitializeComponent() this.panel1.Size = new System.Drawing.Size(538, 88); this.panel1.TabIndex = 2; // + // comboBox_gamemode + // + this.comboBox_gamemode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBox_gamemode.FormattingEnabled = true; + this.comboBox_gamemode.Items.AddRange(new object[] { + "osu", + "taiko", + "ctb", + "mania"}); + this.comboBox_gamemode.Location = new System.Drawing.Point(302, 279); + this.comboBox_gamemode.Name = "comboBox_gamemode"; + this.comboBox_gamemode.Size = new System.Drawing.Size(121, 21); + this.comboBox_gamemode.TabIndex = 29; + // + // label11 + // + this.label11.AutoSize = true; + this.label11.Location = new System.Drawing.Point(299, 262); + this.label11.Name = "label11"; + this.label11.Size = new System.Drawing.Size(64, 13); + this.label11.TabIndex = 30; + this.label11.Text = "Gamemode:"; + // // UserTopGeneratorView // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -494,5 +521,7 @@ private void InitializeComponent() public System.Windows.Forms.Button button_GenerateUsernames; private System.Windows.Forms.Label label_processingStatus; private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.Label label11; + private System.Windows.Forms.ComboBox comboBox_gamemode; } } diff --git a/GuiComponents/Controls/UserTopGeneratorView.cs b/GuiComponents/Controls/UserTopGeneratorView.cs index 7959dd8..7e4ca74 100644 --- a/GuiComponents/Controls/UserTopGeneratorView.cs +++ b/GuiComponents/Controls/UserTopGeneratorView.cs @@ -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 @@ -61,7 +62,7 @@ public string ProcessingStatus public UserTopGeneratorView() { InitializeComponent(); - + comboBox_gamemode.SelectedIndex = 0; } private void RadioButton_AllowScores_CheckedChanged(object sender, EventArgs e)