From 7e41fc93fccd2484d19eb6571f9756718b6ef7b3 Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 22 Dec 2023 21:43:01 +0200 Subject: [PATCH] feat(store): code cleanup, make some fund with "unlucky" members --- Core/Service/UserService.cs | 18 ++++++++------ modules/ContentStore/Handler/Store.cs | 2 +- .../Service/AbstractNsfwService.cs | 2 +- .../ContentStore/Service/AnimBoobsService.cs | 2 +- .../ContentStore/Service/AnimalAsAService.cs | 2 +- .../ContentStore/Service/CapyApiService.cs | 24 ++++++++++++++++--- .../Service/IRandomMediaService.cs | 2 +- .../Service/Kitties/AbstractKittiesService.cs | 2 +- modules/ContentStore/Service/OBoobsService.cs | 2 +- .../SomeRandomApiAbstractService.cs | 2 +- 10 files changed, 40 insertions(+), 18 deletions(-) diff --git a/Core/Service/UserService.cs b/Core/Service/UserService.cs index 06f1c10..116f92f 100644 --- a/Core/Service/UserService.cs +++ b/Core/Service/UserService.cs @@ -15,8 +15,9 @@ namespace Kruzya.TelegramBot.Core.Service; public class UserService { - private readonly List _superUsers; - private readonly Dictionary _statusMap; + private readonly IEnumerable _superUsers; + private readonly IEnumerable _badLuckUsers; + private readonly Dictionary _statusMap; private readonly ICacheStorage _userCache; private readonly ITelegramBotClient _bot; private readonly ILogger _logger; @@ -28,15 +29,13 @@ public class UserService ICacheStorage userCache, ILogger logger) { - _superUsers = configuration.GetSection("SuperUsers") - .GetChildren() - .Select(q => Convert.ToInt64(q.Value)) - .ToList(); + _superUsers = configuration.GetSection("SuperUsers").Get>() ?? new List(); + _badLuckUsers = configuration.GetSection("BadLuckUsers").Get>() ?? new List(); _statusMap = configuration.GetSection("CustomMemberStatus") .GetChildren() .ToDictionary(x => Convert.ToInt64(x.Key), x => x.Value); - + _userCache = userCache; _bot = bot.BotClient; _logger = logger; @@ -83,6 +82,11 @@ public class UserService return _superUsers.Contains(userId); } + public bool IsUserUnlucky(long userId) + { + return _badLuckUsers.Contains(userId); + } + public string? GetUserCustomStatus(long userId) { return _statusMap.ContainsKey(userId) ? _statusMap[userId] : null; diff --git a/modules/ContentStore/Handler/Store.cs b/modules/ContentStore/Handler/Store.cs index 0005f99..d360370 100644 --- a/modules/ContentStore/Handler/Store.cs +++ b/modules/ContentStore/Handler/Store.cs @@ -135,7 +135,7 @@ public class Store : CommonHandler private async Task PerformPurchase(IRandomMediaService mediaService, int messageId) { - var randomMedia = await mediaService!.GetRandomMediaAsync(); + var randomMedia = await mediaService!.GetRandomMediaAsync(Chat, From); var file = randomMedia.File; switch (randomMedia.Type) diff --git a/modules/ContentStore/Service/AbstractNsfwService.cs b/modules/ContentStore/Service/AbstractNsfwService.cs index 8030edb..495c3e5 100644 --- a/modules/ContentStore/Service/AbstractNsfwService.cs +++ b/modules/ContentStore/Service/AbstractNsfwService.cs @@ -8,7 +8,7 @@ public abstract class AbstractNsfwService : IRandomMediaService { private readonly AllowNsfw _allowNsfw; public abstract StoreItem StoreItem { get; } - public abstract Task GetRandomMediaAsync(); + public abstract Task GetRandomMediaAsync(Chat chat, User user); protected AbstractNsfwService(AllowNsfw allowNsfw) { diff --git a/modules/ContentStore/Service/AnimBoobsService.cs b/modules/ContentStore/Service/AnimBoobsService.cs index e755332..42fa277 100644 --- a/modules/ContentStore/Service/AnimBoobsService.cs +++ b/modules/ContentStore/Service/AnimBoobsService.cs @@ -16,7 +16,7 @@ public class AnimBoobsService : AbstractNsfwService _httpClient.BaseAddress = new Uri("https://westdev.me/_boobs/"); } - public override async Task GetRandomMediaAsync() + public override async Task GetRandomMediaAsync(Chat chat, User user) { var fileName = await _httpClient.GetStringAsync("index.php"); // https://bugs.telegram.org/c/23674 workaround diff --git a/modules/ContentStore/Service/AnimalAsAService.cs b/modules/ContentStore/Service/AnimalAsAService.cs index ef97ec1..7a6af3b 100644 --- a/modules/ContentStore/Service/AnimalAsAService.cs +++ b/modules/ContentStore/Service/AnimalAsAService.cs @@ -15,7 +15,7 @@ public abstract class AnimalAsAService : IRandomMediaService _httpClient.BaseAddress = baseAddress; } - public async Task GetRandomMediaAsync() + public async Task GetRandomMediaAsync(Chat chat, User user) { var resp = await _httpClient.GetAsync("images/search"); var catImage = JsonConvert.DeserializeObject>(await resp.Content.ReadAsStringAsync())![0]; diff --git a/modules/ContentStore/Service/CapyApiService.cs b/modules/ContentStore/Service/CapyApiService.cs index c23c8bf..095e399 100644 --- a/modules/ContentStore/Service/CapyApiService.cs +++ b/modules/ContentStore/Service/CapyApiService.cs @@ -1,13 +1,31 @@ -using Telegram.Bot.Types; +using System.Security.Cryptography; +using Kruzya.TelegramBot.Core.Service; +using Telegram.Bot.Types; using West.TelegramBot.ContentStore.Model; namespace West.TelegramBot.ContentStore.Service; public class CapyApiService : IRandomMediaService { + private readonly UserService _userService; + private const int BeaverCount = 161; + public StoreItem StoreItem { get; } = new("capybara", "Капибара", 60, 120); - public Task GetRandomMediaAsync() + public Task GetRandomMediaAsync(Chat chat, User user) { - return Task.FromResult(new RandomMedia(new InputFileUrl($"https://api.capy.lol/v1/capybara?{new Random().Next(0, 10000)}"))); + if (_userService.IsUserUnlucky(user.Id) && RandomNumberGenerator.GetInt32(2) == 1) + { + var beaverId = RandomNumberGenerator.GetInt32(BeaverCount); + return Task.FromResult(RandomMediaFromUrl($"https://westdev.me/_kurwa/{beaverId}.jpg")); + } + + return Task.FromResult(RandomMediaFromUrl($"https://api.capy.lol/v1/capybara?{new Random().Next(0, 10000)}")); + } + + public RandomMedia RandomMediaFromUrl(string url) => new(new InputFileUrl(url)); + + public CapyApiService(UserService userService) + { + _userService = userService; } } \ No newline at end of file diff --git a/modules/ContentStore/Service/IRandomMediaService.cs b/modules/ContentStore/Service/IRandomMediaService.cs index 9b29447..18ff633 100644 --- a/modules/ContentStore/Service/IRandomMediaService.cs +++ b/modules/ContentStore/Service/IRandomMediaService.cs @@ -7,6 +7,6 @@ public interface IRandomMediaService { public StoreItem StoreItem { get; } - public Task GetRandomMediaAsync(); + public Task GetRandomMediaAsync(Chat chat, User user); public Task CanView(Chat chat, User user) => Task.FromResult(true); } \ No newline at end of file diff --git a/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs index de63ca1..2528939 100644 --- a/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs @@ -20,7 +20,7 @@ public abstract class AbstractKittiesService : IRandomMediaService _allowNsfw = allowNsfw; } - public async Task GetRandomMediaAsync() + public async Task GetRandomMediaAsync(Chat chat, User user) { var response = await _api.GetKittiesImageAsync(VideoType); diff --git a/modules/ContentStore/Service/OBoobsService.cs b/modules/ContentStore/Service/OBoobsService.cs index b1863ed..8cbfeb6 100644 --- a/modules/ContentStore/Service/OBoobsService.cs +++ b/modules/ContentStore/Service/OBoobsService.cs @@ -16,7 +16,7 @@ public class OBoobsService : AbstractNsfwService _httpClient.BaseAddress = new Uri("http://api.oboobs.ru"); } - public override async Task GetRandomMediaAsync() + public override async Task GetRandomMediaAsync(Chat chat, User user) { // "/boobs/{start=0; sql offset}/{count=1; sql limit}/{order=-id;[id,rank,-rank,interest,-interest,random]}/ var httpResp = await _httpClient.GetAsync("boobs/1/1/random"); diff --git a/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs b/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs index 64291a2..9367351 100644 --- a/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs @@ -16,7 +16,7 @@ public abstract class SomeRandomApiAbstractService : IRandomMediaService _api = api; } - public async Task GetRandomMediaAsync() + public async Task GetRandomMediaAsync(Chat chat, User user) { var image = await _api.GetAnimalAsync(StoreItem.Id);