diff --git a/StarRatings.cs b/StarRatings.cs index 590a546..2d48104 100644 --- a/StarRatings.cs +++ b/StarRatings.cs @@ -56,14 +56,18 @@ public override IEnumerable GetMainMenuItems(GetMainMenuItemsArgs { MenuSection = "StarRatings", Description = "Rebuild Rating Tags", - Action = (menuArgs) => ApplyRatingTags(PlayniteApi.Database.Games.ToList()), + Action = (menuArgs) => + { + ClearRatingTags(PlayniteApi.Database.Games); + ReapplyRatingTags(PlayniteApi.Database.Games); + }, }; yield return new MainMenuItem { MenuSection = "StarRatings", Description = "Clear All Rating Tags", - Action = (menuArgs) => ClearRatingTags(PlayniteApi.Database.Games.ToList()), + Action = (menuArgs) => ClearRatingTags(PlayniteApi.Database.Games), }; } } @@ -230,8 +234,25 @@ private void ApplyUserScore(IEnumerable games, int? userScore) { foreach (Game game in games) { + // remove existing tag if needed + if (CurrentSettings.ShouldApplyRatingTag && game.UserScore.HasValue && game.TagIds != null) + { + int originalScore = game.UserScore.Value; + + // round down to previous level + if (!scoreToRatingData.ContainsKey(originalScore)) + { + int stepSize = 100 / CurrentSettings.RatingSteps; + originalScore = (originalScore / stepSize) * stepSize; + } + + game.TagIds.Remove(scoreToRatingData[originalScore].ratingTagId); + } + + // set new score game.UserScore = userScore; + // update tag if needed if (CurrentSettings.ShouldApplyRatingTag && userScore.HasValue) { // create tags if game has none @@ -243,7 +264,7 @@ private void ApplyUserScore(IEnumerable games, int? userScore) PlayniteApi.Database.Games.Update(games); } - private void ApplyRatingTags(IEnumerable games) + public void ReapplyRatingTags(IEnumerable games) { foreach (var game in games) { @@ -251,17 +272,14 @@ private void ApplyRatingTags(IEnumerable games) if(!game.UserScore.HasValue) { continue; } int userScore = game.UserScore.Value; - - // skip if score does not correspond to anything - // TODO: consider rounding down to next applicable score - if (!scoreToRatingData.ContainsKey(userScore)) { continue; } - // create TagIds for game if none exists - if (game.TagIds == null) + // round down to previous level + if (!scoreToRatingData.ContainsKey(userScore)) { - game.TagIds = new List(); + int stepSize = 100 / CurrentSettings.RatingSteps; + userScore = (userScore / stepSize) * stepSize; } - + // retrieve and stage tag game.TagIds.Add(scoreToRatingData[userScore].ratingTagId); } @@ -269,7 +287,7 @@ private void ApplyRatingTags(IEnumerable games) PlayniteApi.Database.Games.Update(games); } - private void ClearRatingTags(IEnumerable games) + public void ClearRatingTags(IEnumerable games) { // cache list of tags to remove List tagsToRemove = new List(); diff --git a/StarRatingsSettings.cs b/StarRatingsSettings.cs index 8bc40da..849f240 100644 --- a/StarRatingsSettings.cs +++ b/StarRatingsSettings.cs @@ -32,7 +32,7 @@ public class StarRatingsSettings : ObservableObject public class StarRatingsSettingsViewModel : ObservableObject, ISettings { private readonly StarRatings plugin; - private StarRatingsSettings editingClone { get; set; } + private StarRatingsSettings prevSavedSettings { get; set; } private StarRatingsSettings settings; public StarRatingsSettings Settings @@ -54,27 +54,20 @@ public StarRatingsSettingsViewModel(StarRatings plugin) var savedSettings = plugin.LoadPluginSettings(); // LoadPluginSettings returns null if not saved data is available. - if (savedSettings != null) - { - Settings = savedSettings; - } - else - { - Settings = new StarRatingsSettings(); - } + Settings = savedSettings ?? new StarRatingsSettings(); } public void BeginEdit() { // Code executed when settings view is opened and user starts editing values. - editingClone = Serialization.GetClone(Settings); + prevSavedSettings = Serialization.GetClone(Settings); } public void CancelEdit() { // Code executed when user decides to cancel any changes made since BeginEdit was called. // This method should revert any changes made to Option1 and Option2. - Settings = editingClone; + Settings = prevSavedSettings; } public void EndEdit() @@ -82,10 +75,29 @@ public void EndEdit() // Code executed when user decides to confirm changes made since BeginEdit was called. // This method should save settings made to Option1 and Option2. plugin.SavePluginSettings(Settings); + + bool hasChangedRatingScheme = prevSavedSettings.EnableHalfStars != Settings.EnableHalfStars || + prevSavedSettings.RatingSteps != Settings.RatingSteps || + prevSavedSettings.ShowZeroRating != Settings.ShowZeroRating || + prevSavedSettings.ShouldApplyRatingTag != Settings.ShouldApplyRatingTag || + prevSavedSettings.RatingTagPrefix != Settings.RatingTagPrefix; + + // did we have tags enabled before? + if (hasChangedRatingScheme && prevSavedSettings.ShouldApplyRatingTag) + { + plugin.ClearRatingTags(plugin.PlayniteApi.Database.Games); + } // Reinitialize ratings on edit // TODO - We could listen for actual changes but this should be fairly cheap + // and uncomplicated to maintain until we need more complexity plugin.InitializeRatings(); + + // do we need to apply tags again? + if (hasChangedRatingScheme && Settings.ShouldApplyRatingTag) + { + plugin.ReapplyRatingTags(plugin.PlayniteApi.Database.Games); + } } public bool VerifySettings(out List errors)