-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizationService.cs
32 lines (27 loc) · 1.08 KB
/
RandomizationService.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
using DiscordImagePoster.Common.IndexService;
using Microsoft.Extensions.Logging;
namespace DiscordImagePoster.Common.RandomizationService;
public class RandomizationService : IRandomizationService
{
private readonly ILogger<RandomizationService> _logger;
public RandomizationService(ILogger<RandomizationService> logger)
{
_logger = logger;
}
public ImageIndexMetadata? GetRandomImage(ImageIndex imageIndex)
{
_logger.LogTrace("Getting random image from image index.");
var allowedImages = imageIndex.Images.Where(image => !image.Ignore);
if (!allowedImages.Any())
{
_logger.LogTrace("No allowed images found in index.");
return null;
}
var minimunPosts = allowedImages.Min(image => image.TimesPosted);
_logger.LogTrace("Minimum posts for allowed images: {MinimunPosts}", minimunPosts);
return allowedImages
.Where(image => image.TimesPosted == minimunPosts)
.OrderBy(x => Guid.NewGuid())
.FirstOrDefault();
}
}