mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[store] Reduce amount of hardcoded values.
This commit is contained in:
@@ -8,25 +8,25 @@ namespace West.TelegramBot.ContentStore;
|
||||
|
||||
public class ContentStore : Module
|
||||
{
|
||||
private readonly List<Type> _mediaServiceTypes = new();
|
||||
public readonly List<Type> MediaServiceTypes = new();
|
||||
|
||||
public ContentStore(Core core) : base(core)
|
||||
{
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
foreach (var exportedType in assembly.GetExportedTypes()
|
||||
.Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(IRandomMediaService))))
|
||||
.Where(t => !t.IsAbstract && t.GetInterface(nameof(IRandomMediaService)) != null))
|
||||
{
|
||||
_mediaServiceTypes.Add(exportedType);
|
||||
MediaServiceTypes.Add(exportedType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
foreach (var serviceType in _mediaServiceTypes)
|
||||
foreach (var serviceType in MediaServiceTypes)
|
||||
{
|
||||
services.AddSingleton(serviceType);
|
||||
services.AddSingleton(typeof(IRandomMediaService), serviceType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,19 +19,13 @@ public class Store : BotEventHandler
|
||||
{
|
||||
private readonly IReputation? _reputation;
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
private readonly Dictionary<string, StoreItem> _storeItemMap = new()
|
||||
{
|
||||
{ "cat", new StoreItem("cat", "Котэ", 40) },
|
||||
{ "dog", new StoreItem("dog", "Пёсель", 40) },
|
||||
{ "boobs", new StoreItem("boobs", "Сиськи", 60) },
|
||||
{ "anim_boobs", new StoreItem("anim_boobs", "Сиськи движущиеся", 120) }
|
||||
};
|
||||
|
||||
public Store(IServiceProvider provider)
|
||||
private readonly ContentStore _store;
|
||||
|
||||
public Store(IServiceProvider provider, ContentStore store)
|
||||
{
|
||||
_reputation = provider.GetService<IReputation>();
|
||||
_provider = provider;
|
||||
_store = store;
|
||||
}
|
||||
|
||||
[Command(InChat.Public, "store", CommandParseMode.Both)]
|
||||
@@ -47,9 +41,9 @@ public class Store : BotEventHandler
|
||||
|
||||
var counter = 0;
|
||||
var list = new List<InlineKeyboardButton>();
|
||||
foreach (var item in _storeItemMap)
|
||||
foreach (var item in _provider.GetServices<IRandomMediaService>())
|
||||
{
|
||||
var btn = item.Value.ToButton(From.Id);
|
||||
var btn = item.StoreItem.ToButton(From.Id);
|
||||
if ((counter & 1) == 0) // 0 - even, 1 - odd
|
||||
{
|
||||
list = new List<InlineKeyboardButton>
|
||||
@@ -97,13 +91,13 @@ public class Store : BotEventHandler
|
||||
var purchaseType = paramList[3];
|
||||
answer = "Спасибо за покупку";
|
||||
|
||||
if (!_storeItemMap.TryGetValue(purchaseType, out var storeItem)
|
||||
|| !TryGetMediaService(purchaseType, out var mediaService))
|
||||
if (!TryGetMediaService(purchaseType, out var mediaService))
|
||||
{
|
||||
await AnswerQuery(query.Id, "Unknown purchasable.");
|
||||
return;
|
||||
}
|
||||
|
||||
var storeItem = mediaService.StoreItem;
|
||||
var messageId = query.Message!.MessageId;
|
||||
if (await _reputation.GetUserReputationAsync(Chat, From) < storeItem.Price)
|
||||
{
|
||||
@@ -140,22 +134,9 @@ public class Store : BotEventHandler
|
||||
|
||||
private bool TryGetMediaService(string name, out IRandomMediaService? mediaService)
|
||||
{
|
||||
var serviceType = name switch
|
||||
{
|
||||
"boobs" => typeof(OBoobsService),
|
||||
"cat" => typeof(CatApiService),
|
||||
"dog" => typeof(DogApiService),
|
||||
"anim_boobs" => typeof(AnimBoobsService),
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (serviceType == null)
|
||||
{
|
||||
mediaService = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
mediaService = _provider.GetService(serviceType) as IRandomMediaService;
|
||||
mediaService = _provider.GetServices<IRandomMediaService>()
|
||||
.FirstOrDefault(s => s.StoreItem.Id == name);
|
||||
|
||||
return mediaService != null;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace West.TelegramBot.ContentStore.Model;
|
||||
|
||||
class StoreItem
|
||||
public class StoreItem
|
||||
{
|
||||
public string Id;
|
||||
public readonly string Name;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using West.TelegramBot.ContentStore.Model;
|
||||
|
||||
namespace West.TelegramBot.ContentStore.Service;
|
||||
@@ -7,6 +6,7 @@ namespace West.TelegramBot.ContentStore.Service;
|
||||
public class AnimBoobsService : IRandomMediaService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
public StoreItem StoreItem { get; } = new("boobs", "Сиськи движущиеся", 120);
|
||||
|
||||
public AnimBoobsService()
|
||||
{
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace West.TelegramBot.ContentStore.Service;
|
||||
public abstract class AnimalAsAService : IRandomMediaService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
public abstract StoreItem StoreItem { get; }
|
||||
|
||||
protected AnimalAsAService(Uri baseAddress)
|
||||
{
|
||||
@@ -15,6 +16,7 @@ public abstract class AnimalAsAService : IRandomMediaService
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public async Task<RandomMedia> GetRandomMediaAsync()
|
||||
{
|
||||
var resp = await _httpClient.GetAsync("images/search");
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
using Newtonsoft.Json;
|
||||
using Telegram.Bot.Types;
|
||||
using West.TelegramBot.ContentStore.Model;
|
||||
using West.TelegramBot.ContentStore.Model;
|
||||
|
||||
namespace West.TelegramBot.ContentStore.Service;
|
||||
|
||||
public class CatApiService : AnimalAsAService
|
||||
{
|
||||
public override StoreItem StoreItem { get; } = new("cat", "Котэ", 40);
|
||||
public CatApiService() : base(new Uri("https://api.thecatapi.com/v1/")) {}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
namespace West.TelegramBot.ContentStore.Service;
|
||||
using West.TelegramBot.ContentStore.Model;
|
||||
|
||||
namespace West.TelegramBot.ContentStore.Service;
|
||||
|
||||
public class DogApiService : AnimalAsAService
|
||||
{
|
||||
public override StoreItem StoreItem { get; } = new("dog", "Пёсель", 40);
|
||||
public DogApiService() : base(new Uri("https://api.thedogapi.com/v1/")) {}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
using Telegram.Bot.Types;
|
||||
using West.TelegramBot.ContentStore.Model;
|
||||
using West.TelegramBot.ContentStore.Model;
|
||||
|
||||
namespace West.TelegramBot.ContentStore.Service;
|
||||
|
||||
public interface IRandomMediaService
|
||||
{
|
||||
public StoreItem StoreItem { get; }
|
||||
|
||||
public Task<RandomMedia> GetRandomMediaAsync();
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Newtonsoft.Json;
|
||||
using Telegram.Bot.Types;
|
||||
using West.TelegramBot.ContentStore.Model;
|
||||
|
||||
namespace West.TelegramBot.ContentStore.Service;
|
||||
@@ -7,7 +6,8 @@ namespace West.TelegramBot.ContentStore.Service;
|
||||
public class OBoobsService : IRandomMediaService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public StoreItem StoreItem { get; } = new("boobs", "Сиськи", 60);
|
||||
|
||||
public OBoobsService()
|
||||
{
|
||||
_httpClient = new HttpClient
|
||||
|
||||
Reference in New Issue
Block a user