feat(store): code cleanup, make some fund with "unlucky" members

This commit is contained in:
Andriy
2023-12-22 21:43:01 +02:00
parent d32b976e1a
commit 7e41fc93fc
10 changed files with 40 additions and 18 deletions
+11 -7
View File
@@ -15,8 +15,9 @@ namespace Kruzya.TelegramBot.Core.Service;
public class UserService
{
private readonly List<long> _superUsers;
private readonly Dictionary<long, string> _statusMap;
private readonly IEnumerable<long> _superUsers;
private readonly IEnumerable<long> _badLuckUsers;
private readonly Dictionary<long, string?> _statusMap;
private readonly ICacheStorage<long, User> _userCache;
private readonly ITelegramBotClient _bot;
private readonly ILogger<UserService> _logger;
@@ -28,15 +29,13 @@ public class UserService
ICacheStorage<long, User> userCache,
ILogger<UserService> logger)
{
_superUsers = configuration.GetSection("SuperUsers")
.GetChildren()
.Select(q => Convert.ToInt64(q.Value))
.ToList();
_superUsers = configuration.GetSection("SuperUsers").Get<List<long>>() ?? new List<long>();
_badLuckUsers = configuration.GetSection("BadLuckUsers").Get<List<long>>() ?? new List<long>();
_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;
+1 -1
View File
@@ -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)
@@ -8,7 +8,7 @@ public abstract class AbstractNsfwService : IRandomMediaService
{
private readonly AllowNsfw _allowNsfw;
public abstract StoreItem StoreItem { get; }
public abstract Task<RandomMedia> GetRandomMediaAsync();
public abstract Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user);
protected AbstractNsfwService(AllowNsfw allowNsfw)
{
@@ -16,7 +16,7 @@ public class AnimBoobsService : AbstractNsfwService
_httpClient.BaseAddress = new Uri("https://westdev.me/_boobs/");
}
public override async Task<RandomMedia> GetRandomMediaAsync()
public override async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
{
var fileName = await _httpClient.GetStringAsync("index.php");
// https://bugs.telegram.org/c/23674 workaround
@@ -15,7 +15,7 @@ public abstract class AnimalAsAService : IRandomMediaService
_httpClient.BaseAddress = baseAddress;
}
public async Task<RandomMedia> GetRandomMediaAsync()
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
{
var resp = await _httpClient.GetAsync("images/search");
var catImage = JsonConvert.DeserializeObject<List<AnimalImage>>(await resp.Content.ReadAsStringAsync())![0];
+21 -3
View File
@@ -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<RandomMedia> GetRandomMediaAsync()
public Task<RandomMedia> 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;
}
}
@@ -7,6 +7,6 @@ public interface IRandomMediaService
{
public StoreItem StoreItem { get; }
public Task<RandomMedia> GetRandomMediaAsync();
public Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user);
public Task<bool> CanView(Chat chat, User user) => Task.FromResult(true);
}
@@ -20,7 +20,7 @@ public abstract class AbstractKittiesService : IRandomMediaService
_allowNsfw = allowNsfw;
}
public async Task<RandomMedia> GetRandomMediaAsync()
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
{
var response = await _api.GetKittiesImageAsync(VideoType);
@@ -16,7 +16,7 @@ public class OBoobsService : AbstractNsfwService
_httpClient.BaseAddress = new Uri("http://api.oboobs.ru");
}
public override async Task<RandomMedia> GetRandomMediaAsync()
public override async Task<RandomMedia> 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");
@@ -16,7 +16,7 @@ public abstract class SomeRandomApiAbstractService : IRandomMediaService
_api = api;
}
public async Task<RandomMedia> GetRandomMediaAsync()
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
{
var image = await _api.GetAnimalAsync(StoreItem.Id);