NadekoBot/NadekoBot.Modules.Gambling/FlowerShopCommands.cs

366 lines
16 KiB
C#
Raw Normal View History

2017-04-06 19:00:46 +00:00
using Discord;
using Discord.Commands;
2017-05-24 20:28:16 +00:00
using Discord.WebSocket;
2017-04-06 19:00:46 +00:00
using Microsoft.EntityFrameworkCore;
using NadekoBot.Extensions;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using NadekoBot.Common.Collections;
2017-04-06 19:00:46 +00:00
namespace NadekoBot.Modules.Gambling
{
public partial class Gambling
{
[Group]
2017-07-17 19:42:36 +00:00
public class FlowerShopCommands : NadekoSubmodule
2017-04-06 19:00:46 +00:00
{
private readonly IBotConfigProvider _bc;
private readonly DbService _db;
private readonly CurrencyService _cs;
private readonly DiscordSocketClient _client;
2017-05-24 20:28:16 +00:00
public enum Role
{
2017-04-06 19:00:46 +00:00
Role
}
public enum List
{
List
}
public FlowerShopCommands(IBotConfigProvider bc, DbService db, CurrencyService cs, DiscordSocketClient client)
2017-05-24 20:28:16 +00:00
{
_db = db;
_bc = bc;
_cs = cs;
2017-05-24 20:28:16 +00:00
_client = client;
}
2017-04-06 19:00:46 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Shop(int page = 1)
{
2017-06-14 15:19:27 +00:00
if (--page < 0)
2017-04-06 19:00:46 +00:00
return;
List<ShopEntry> entries;
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
2017-04-06 19:00:46 +00:00
{
entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries);
2017-04-06 19:00:46 +00:00
}
2017-06-14 15:19:27 +00:00
await Context.Channel.SendPaginatedConfirmAsync(_client, page, (curPage) =>
2017-04-06 19:00:46 +00:00
{
2017-07-19 09:02:14 +00:00
var theseEntries = entries.Skip(curPage * 9).Take(9).ToArray();
2017-04-06 19:00:46 +00:00
if (!theseEntries.Any())
return new EmbedBuilder().WithErrorColor()
.WithDescription(GetText("shop_none"));
var embed = new EmbedBuilder().WithOkColor()
.WithTitle(GetText("shop", _bc.BotConfig.CurrencySign));
2017-04-06 19:00:46 +00:00
2017-07-19 09:02:14 +00:00
for (int i = 0; i < theseEntries.Length; i++)
2017-04-06 19:00:46 +00:00
{
2017-08-05 07:52:32 +00:00
var entry = theseEntries[i];
embed.AddField(efb => efb.WithName($"#{curPage * 9 + i + 1} - {entry.Price}{_bc.BotConfig.CurrencySign}").WithValue(EntryToString(entry)).WithIsInline(true));
2017-04-06 19:00:46 +00:00
}
return embed;
}, entries.Count / 9, true);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Buy(int index, [Remainder]string message = null)
2017-04-06 19:00:46 +00:00
{
index -= 1;
if (index < 0)
return;
ShopEntry entry;
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
{
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set
.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items));
var entries = new IndexedCollection<ShopEntry>(config.ShopEntries);
entry = entries.ElementAtOrDefault(index);
uow.Complete();
}
if (entry == null)
{
await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);
return;
}
if (entry.Type == ShopEntryType.Role)
{
var guser = (IGuildUser)Context.User;
var role = Context.Guild.GetRole(entry.RoleId);
if (role == null)
{
await ReplyErrorLocalized("shop_role_not_found").ConfigureAwait(false);
return;
}
if (await _cs.RemoveAsync(Context.User.Id, $"Shop purchase - {entry.Type}", entry.Price))
{
try
{
2017-04-15 00:54:19 +00:00
await guser.AddRoleAsync(role).ConfigureAwait(false);
}
catch (Exception ex)
{
_log.Warn(ex);
await _cs.AddAsync(Context.User.Id, $"Shop error refund", entry.Price);
await ReplyErrorLocalized("shop_role_purchase_error").ConfigureAwait(false);
return;
}
await _cs.AddAsync(entry.AuthorId, $"Shop sell item - {entry.Type}", GetProfitAmount(entry.Price));
await ReplyConfirmLocalized("shop_role_purchase", Format.Bold(role.Name)).ConfigureAwait(false);
return;
}
else
{
await ReplyErrorLocalized("not_enough", _bc.BotConfig.CurrencySign).ConfigureAwait(false);
return;
}
}
else if (entry.Type == ShopEntryType.List)
{
if (entry.Items.Count == 0)
{
await ReplyErrorLocalized("out_of_stock").ConfigureAwait(false);
return;
}
var item = entry.Items.ToArray()[new NadekoRandom().Next(0, entry.Items.Count)];
if (await _cs.RemoveAsync(Context.User.Id, $"Shop purchase - {entry.Type}", entry.Price))
{
int removed;
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
{
var x = uow._context.Set<ShopEntryItem>().Remove(item);
removed = uow.Complete();
}
try
{
2017-07-05 15:38:38 +00:00
await (await Context.User.GetOrCreateDMChannelAsync())
.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle(GetText("shop_purchase", Context.Guild.Name))
.AddField(efb => efb.WithName(GetText("item")).WithValue(item.Text).WithIsInline(false))
.AddField(efb => efb.WithName(GetText("price")).WithValue(entry.Price.ToString()).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("name")).WithValue(entry.Name).WithIsInline(true)))
.ConfigureAwait(false);
await _cs.AddAsync(entry.AuthorId,
2017-04-08 15:48:54 +00:00
$"Shop sell item - {entry.Name}",
GetProfitAmount(entry.Price)).ConfigureAwait(false);
}
catch
{
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
{
uow._context.Set<ShopEntryItem>().Add(item);
uow.Complete();
await _cs.AddAsync(Context.User.Id,
$"Shop error refund - {entry.Name}",
entry.Price,
uow).ConfigureAwait(false);
}
await ReplyErrorLocalized("shop_buy_error").ConfigureAwait(false);
return;
}
await ReplyConfirmLocalized("shop_item_purchase").ConfigureAwait(false);
}
else
{
await ReplyErrorLocalized("not_enough", _bc.BotConfig.CurrencySign).ConfigureAwait(false);
return;
}
}
2017-04-06 19:00:46 +00:00
}
private long GetProfitAmount(int price) =>
(int)(Math.Ceiling(0.90 * price));
2017-04-06 19:00:46 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
[RequireBotPermission(GuildPermission.ManageRoles)]
public async Task ShopAdd(Role _, int price, [Remainder] IRole role)
2017-04-06 19:00:46 +00:00
{
var entry = new ShopEntry()
{
Name = "-",
2017-04-06 19:00:46 +00:00
Price = price,
Type = ShopEntryType.Role,
2017-04-06 19:00:46 +00:00
AuthorId = Context.User.Id,
RoleId = role.Id,
RoleName = role.Name
2017-04-06 19:00:46 +00:00
};
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
2017-04-06 19:00:46 +00:00
{
2017-04-15 00:54:19 +00:00
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id,
set => set.Include(x => x.ShopEntries)
2017-04-15 00:54:19 +00:00
.ThenInclude(x => x.Items)).ShopEntries)
{
entry
};
2017-04-06 19:00:46 +00:00
uow.GuildConfigs.For(Context.Guild.Id, set => set).ShopEntries = entries;
uow.Complete();
}
await Context.Channel.EmbedAsync(EntryToEmbed(entry)
.WithTitle(GetText("shop_item_add")));
2017-04-06 19:00:46 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
public async Task ShopAdd(List _, int price, [Remainder]string name)
2017-04-06 19:00:46 +00:00
{
var entry = new ShopEntry()
{
Name = name.TrimTo(100),
2017-04-06 19:00:46 +00:00
Price = price,
Type = ShopEntryType.List,
2017-04-06 19:00:46 +00:00
AuthorId = Context.User.Id,
Items = new HashSet<ShopEntryItem>(),
2017-04-06 19:00:46 +00:00
};
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
2017-04-06 19:00:46 +00:00
{
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id,
set => set.Include(x => x.ShopEntries)
2017-04-15 00:54:19 +00:00
.ThenInclude(x => x.Items)).ShopEntries)
{
entry
};
2017-04-06 19:00:46 +00:00
uow.GuildConfigs.For(Context.Guild.Id, set => set).ShopEntries = entries;
uow.Complete();
}
await Context.Channel.EmbedAsync(EntryToEmbed(entry)
.WithTitle(GetText("shop_item_add")));
2017-04-06 19:00:46 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
public async Task ShopListAdd(int index, [Remainder] string itemText)
{
index -= 1;
if (index < 0)
return;
var item = new ShopEntryItem()
{
Text = itemText
};
ShopEntry entry;
bool rightType = false;
bool added = false;
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
{
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries);
entry = entries.ElementAtOrDefault(index);
if (entry != null && (rightType = (entry.Type == ShopEntryType.List)))
{
if (added = entry.Items.Add(item))
{
uow.Complete();
}
}
}
if (entry == null)
await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);
else if (!rightType)
await ReplyErrorLocalized("shop_item_wrong_type").ConfigureAwait(false);
else if (added == false)
await ReplyErrorLocalized("shop_list_item_not_unique").ConfigureAwait(false);
else
await ReplyConfirmLocalized("shop_list_item_added").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
2017-04-06 19:00:46 +00:00
public async Task ShopRemove(int index)
{
index -= 1;
2017-04-06 19:00:46 +00:00
if (index < 0)
return;
ShopEntry removed;
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
2017-04-06 19:00:46 +00:00
{
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set
.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items));
2017-04-06 19:00:46 +00:00
var entries = new IndexedCollection<ShopEntry>(config.ShopEntries);
removed = entries.ElementAtOrDefault(index);
if (removed != null)
{
entries.Remove(removed);
config.ShopEntries = entries;
uow.Complete();
}
}
if (removed == null)
await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);
2017-04-06 19:00:46 +00:00
else
await Context.Channel.EmbedAsync(EntryToEmbed(removed)
.WithTitle(GetText("shop_item_rm")));
}
public EmbedBuilder EntryToEmbed(ShopEntry entry)
{
var embed = new EmbedBuilder().WithOkColor();
if (entry.Type == ShopEntryType.Role)
return embed.AddField(efb => efb.WithName(GetText("name")).WithValue(GetText("shop_role", Format.Bold(Context.Guild.GetRole(entry.RoleId)?.Name ?? "MISSING_ROLE"))).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("price")).WithValue(entry.Price.ToString()).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("type")).WithValue(entry.Type.ToString()).WithIsInline(true));
else if (entry.Type == ShopEntryType.List)
return embed.AddField(efb => efb.WithName(GetText("name")).WithValue(entry.Name).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("price")).WithValue(entry.Price.ToString()).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("type")).WithValue(GetText("random_unique_item")).WithIsInline(true));
//else if (entry.Type == ShopEntryType.Infinite_List)
// return embed.AddField(efb => efb.WithName(GetText("name")).WithValue(GetText("shop_role", Format.Bold(entry.RoleName))).WithIsInline(true))
// .AddField(efb => efb.WithName(GetText("price")).WithValue(entry.Price.ToString()).WithIsInline(true))
// .AddField(efb => efb.WithName(GetText("type")).WithValue(entry.Type.ToString()).WithIsInline(true));
else return null;
2017-04-06 19:00:46 +00:00
}
public string EntryToString(ShopEntry entry)
{
if (entry.Type == ShopEntryType.Role)
{
return GetText("shop_role", Format.Bold(Context.Guild.GetRole(entry.RoleId)?.Name ?? "MISSING_ROLE"));
2017-04-06 19:00:46 +00:00
}
else if (entry.Type == ShopEntryType.List)
{
return GetText("unique_items_left", entry.Items.Count) + "\n" + entry.Name;
2017-04-06 19:00:46 +00:00
}
//else if (entry.Type == ShopEntryType.Infinite_List)
//{
2017-04-06 19:00:46 +00:00
//}
2017-04-06 19:00:46 +00:00
return "";
}
}
}
}