-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarRatings.cs
311 lines (266 loc) · 11.4 KB
/
StarRatings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using Playnite.SDK;
using Playnite.SDK.Events;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace StarRatings
{
public class StarRatings : GenericPlugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private StarRatingsSettingsViewModel SettingsViewModel { get; set; }
private StarRatingsSettings CurrentSettings => SettingsViewModel.Settings;
private readonly List<GameMenuItem> ratingMenuItems = new List<GameMenuItem>();
private int lastIndexOfMenuItemWithCheck = -1;
private readonly Dictionary<int, RatingData> scoreToRatingData = new Dictionary<int, RatingData>();
public override Guid Id { get; } = Guid.Parse("1d6c5e6a-2198-4b40-b3a1-28fe46f5704a");
public StarRatings(IPlayniteAPI api) : base(api)
{
SettingsViewModel = new StarRatingsSettingsViewModel(this);
Properties = new GenericPluginProperties
{
HasSettings = true
};
}
private class RatingData
{
public string ratingLabel { get; set; }
public int ratingIndex { get; set; }
public Guid ratingTagId { get; set; }
}
public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
InitializeRatings();
}
public override IEnumerable<MainMenuItem> GetMainMenuItems(GetMainMenuItemsArgs args)
{
if (CurrentSettings.ShouldApplyRatingTag)
{
yield return new MainMenuItem
{
MenuSection = "StarRatings",
Description = "Rebuild Rating Tags",
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),
};
}
}
public override IEnumerable<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs args)
{
// clear old check if previously applied
if (lastIndexOfMenuItemWithCheck != -1)
{
ratingMenuItems[lastIndexOfMenuItemWithCheck].Icon = null;
}
// show check only if all selected games have the same score
// (TODO: show multiple dots if multiple are applicable)
bool hasUniformScore = false;
int targetScore = -1;
// initialize variables w/ first score if available, otherwise
// don't show a score at all
if (args.Games[0].UserScore.HasValue)
{
targetScore = args.Games[0].UserScore.Value;
hasUniformScore = true;
}
// are the scores all the same?
if (hasUniformScore)
{
for (int index = 1; index < args.Games.Count; index++)
{
var game = args.Games[index];
if (game.UserScore.HasValue == false ||
game.UserScore.Value != targetScore)
{
hasUniformScore = false;
break;
}
}
}
// identify score to place checkmark on
if (hasUniformScore)
{
var curScore = args.Games[0].UserScore;
int indexToModify = GetRatingIndexFromUserScore(CurrentSettings.RatingSteps, curScore.Value, CurrentSettings.EnableHalfStars, CurrentSettings.ShowZeroRating);
if (indexToModify != -1)
{
ratingMenuItems[indexToModify].Icon = PluginUtils.ResolveIconPath("check.png");
lastIndexOfMenuItemWithCheck = indexToModify;
}
}
// return final menu list
return ratingMenuItems;
}
public override ISettings GetSettings(bool firstRunSettings)
{
return SettingsViewModel;
}
public override UserControl GetSettingsView(bool firstRunSettings)
{
return new StarRatingsSettingsView();
}
public void InitializeRatings()
{
ratingMenuItems.Clear();
scoreToRatingData.Clear();
// reload settings
var curSettings = ((StarRatingsSettingsViewModel)GetSettings(false)).Settings;
// add all steps
int ratingIndex = 0;
for (int i = curSettings.ShowZeroRating ? 0 : 1; i <= curSettings.RatingSteps; ++i)
{
int starLevel = i;
int actualScore = GetUserScoreFromRating(curSettings.RatingSteps, starLevel);
ratingMenuItems.Add(new GameMenuItem
{
MenuSection = "Set Rating",
Description = $"{starLevel} Stars",
Action = (selectedGames) => ApplyUserScore(selectedGames.Games, actualScore)
});
scoreToRatingData[actualScore] = new RatingData() { ratingIndex = ratingIndex++, ratingLabel = $"{CurrentSettings.RatingTagPrefix}{starLevel} Stars" };
// add one for the half-star?
if (curSettings.EnableHalfStars && i < curSettings.RatingSteps)
{
int actualHalfScore = GetUserScoreFromRating(curSettings.RatingSteps, starLevel, true);
ratingMenuItems.Add(new GameMenuItem
{
MenuSection = "Set Rating",
Description = $"{starLevel}.5 Stars",
Action = (selectedGames) => ApplyUserScore(selectedGames.Games, actualHalfScore)
});
scoreToRatingData[actualHalfScore] = new RatingData() { ratingIndex = ratingIndex++, ratingLabel = $"{CurrentSettings.RatingTagPrefix}{starLevel}.5 Stars" };
}
}
// add reset if enabled
if (curSettings.ShowReset)
{
// divider
ratingMenuItems.Add(new GameMenuItem
{
MenuSection = "Set Rating",
Description = $"-" // a "-" denotes a divider
});
// actual item
ratingMenuItems.Add(new GameMenuItem
{
MenuSection = "Set Rating",
Description = $"Clear Rating",
Icon = PluginUtils.ResolveIconPath("return.png"),
Action = (selectedGames) => ApplyUserScore(selectedGames.Games, null)
});
}
lastIndexOfMenuItemWithCheck = -1;
// make tags available if enabled
if (curSettings.ShouldApplyRatingTag)
{
foreach (var ratingPair in scoreToRatingData)
{
var rating = ratingPair.Value;
var ratingTag = PlayniteApi.Database.Tags.SingleOrDefault(x => x.Name == rating.ratingLabel);
if (ratingTag == null)
{
rating.ratingTagId = PlayniteApi.Database.Tags.Add(rating.ratingLabel).Id;
}
else
{
rating.ratingTagId = ratingTag.Id;
}
}
}
}
public int GetRatingIndexFromUserScore(int ratingSteps, int userScore, bool allowHalfRatings, bool allowZeroRating)
{
if (scoreToRatingData.ContainsKey(userScore))
{
return scoreToRatingData[userScore].ratingIndex;
}
// doesn't map
return -1;
}
public static int GetUserScoreFromRating(int ratingSteps, int starLevel, bool isHalfRating = false)
{
int ratingStepSize = 100 / ratingSteps;
return ratingStepSize * starLevel + (isHalfRating ? ratingStepSize/2 : 0);
}
private void ApplyUserScore(IEnumerable<Game> 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
if (game.TagIds == null) { game.TagIds = new List<Guid>(1); }
game.TagIds.Add(scoreToRatingData[userScore.Value].ratingTagId);
}
}
PlayniteApi.Database.Games.Update(games);
}
public void ReapplyRatingTags(IEnumerable<Game> games)
{
foreach (var game in games)
{
// skip if no score
if(!game.UserScore.HasValue) { continue; }
int userScore = game.UserScore.Value;
// round down to previous level
if (!scoreToRatingData.ContainsKey(userScore))
{
int stepSize = 100 / CurrentSettings.RatingSteps;
userScore = (userScore / stepSize) * stepSize;
}
// retrieve and stage tag
game.TagIds.Add(scoreToRatingData[userScore].ratingTagId);
}
PlayniteApi.Database.Games.Update(games);
}
public void ClearRatingTags(IEnumerable<Game> games)
{
// cache list of tags to remove
List<Guid> tagsToRemove = new List<Guid>();
foreach (var rating in scoreToRatingData)
{
tagsToRemove.Add(rating.Value.ratingTagId);
}
foreach (var game in games)
{
// skip if it doesn't have any tags
if (game.TagIds == null) { continue; }
// remove all known rating tags
game.TagIds.RemoveAll(x => tagsToRemove.Contains(x));
}
PlayniteApi.Database.Games.Update(games);
}
}
}