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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user