NadekoBot/NadekoBot.Modules.Gambling/Gambling.cs

296 lines
12 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using System.Linq;
2016-08-20 14:10:37 +00:00
using System.Threading.Tasks;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
namespace NadekoBot.Modules.Gambling
{
public partial class Gambling : NadekoTopLevelModule
{
private readonly IBotConfigProvider _bc;
private readonly DbService _db;
private readonly CurrencyService _currency;
private string CurrencyName => _bc.BotConfig.CurrencyName;
private string CurrencyPluralName => _bc.BotConfig.CurrencyPluralName;
private string CurrencySign => _bc.BotConfig.CurrencySign;
2017-05-24 20:28:16 +00:00
public Gambling(IBotConfigProvider bc, DbService db, CurrencyService currency)
{
2017-05-24 20:28:16 +00:00
_bc = bc;
_db = db;
_currency = currency;
}
2017-05-24 20:28:16 +00:00
public long GetCurrency(ulong id)
2016-11-26 09:54:33 +00:00
{
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
2016-11-26 09:54:33 +00:00
{
return uow.Currency.GetUserCurrency(id);
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-20 14:10:37 +00:00
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Raffle([Remainder] IRole role = null)
2016-08-20 14:10:37 +00:00
{
2016-12-17 00:16:14 +00:00
role = role ?? Context.Guild.EveryoneRole;
2016-08-20 14:10:37 +00:00
var members = (await role.GetMembersAsync()).Where(u => u.Status != UserStatus.Offline);
2016-08-20 14:10:37 +00:00
var membersArray = members as IUser[] ?? members.ToArray();
2017-06-15 18:20:34 +00:00
if (membersArray.Length == 0)
{
}
var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)];
await Context.Channel.SendConfirmAsync("🎟 "+ GetText("raffled_user"), $"**{usr.Username}#{usr.Discriminator}**", footer: $"ID: {usr.Id}").ConfigureAwait(false);
2016-08-20 14:10:37 +00:00
}
2016-10-08 18:24:34 +00:00
[NadekoCommand, Usage, Description, Aliases]
[Priority(1)]
2016-12-17 00:16:14 +00:00
public async Task Cash([Remainder] IUser user = null)
{
if(user == null)
await ConfirmLocalized("has", Format.Bold(Context.User.ToString()), $"{GetCurrency(Context.User.Id)} {CurrencySign}").ConfigureAwait(false);
else
await ReplyConfirmLocalized("has", Format.Bold(user.ToString()), $"{GetCurrency(user.Id)} {CurrencySign}").ConfigureAwait(false);
}
2016-10-08 18:24:34 +00:00
[NadekoCommand, Usage, Description, Aliases]
[Priority(0)]
2016-12-17 00:16:14 +00:00
public async Task Cash(ulong userId)
2016-10-08 18:24:34 +00:00
{
await ReplyConfirmLocalized("has", Format.Code(userId.ToString()), $"{GetCurrency(userId)} {CurrencySign}").ConfigureAwait(false);
2016-10-08 18:24:34 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Give(long amount, [Remainder] IGuildUser receiver)
{
2016-12-16 18:43:57 +00:00
if (amount <= 0 || Context.User.Id == receiver.Id)
return;
var success = await _currency.RemoveAsync((IGuildUser)Context.User, $"Gift to {receiver.Username} ({receiver.Id}).", amount, false).ConfigureAwait(false);
if (!success)
{
await ReplyErrorLocalized("not_enough", CurrencyPluralName).ConfigureAwait(false);
return;
}
await _currency.AddAsync(receiver, $"Gift from {Context.User.Username} ({Context.User.Id}).", amount, true).ConfigureAwait(false);
await ReplyConfirmLocalized("gifted", amount + CurrencySign, Format.Bold(receiver.ToString()))
.ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-14 12:23:09 +00:00
[RequireContext(ContextType.Guild)]
[OwnerOnly]
[Priority(0)]
2016-12-17 00:16:14 +00:00
public Task Award(int amount, [Remainder] IGuildUser usr) =>
Award(amount, usr.Id);
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
[Priority(1)]
2016-12-17 00:16:14 +00:00
public async Task Award(int amount, ulong usrId)
2016-09-14 12:23:09 +00:00
{
if (amount <= 0)
return;
await _currency.AddAsync(usrId, $"Awarded by bot owner. ({Context.User.Username}/{Context.User.Id})", amount).ConfigureAwait(false);
await ReplyConfirmLocalized("awarded", amount + CurrencySign, $"<@{usrId}>").ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
[Priority(2)]
2016-12-17 00:16:14 +00:00
public async Task Award(int amount, [Remainder] IRole role)
{
2016-12-17 04:09:04 +00:00
var users = (await Context.Guild.GetUsersAsync())
.Where(u => u.GetRoles().Contains(role))
.ToList();
await Task.WhenAll(users.Select(u => _currency.AddAsync(u.Id,
2016-12-16 18:43:57 +00:00
$"Awarded by bot owner to **{role.Name}** role. ({Context.User.Username}/{Context.User.Id})",
amount)))
.ConfigureAwait(false);
await ReplyConfirmLocalized("mass_award",
amount + CurrencySign,
Format.Bold(users.Count.ToString()),
Format.Bold(role.Name)).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2016-12-17 00:16:14 +00:00
public async Task Take(long amount, [Remainder] IGuildUser user)
{
if (amount <= 0)
return;
if (await _currency.RemoveAsync(user, $"Taken by bot owner.({Context.User.Username}/{Context.User.Id})", amount, true).ConfigureAwait(false))
await ReplyConfirmLocalized("take", amount+CurrencySign, Format.Bold(user.ToString())).ConfigureAwait(false);
2016-10-08 18:24:34 +00:00
else
await ReplyErrorLocalized("take_fail", amount + CurrencySign, Format.Bold(user.ToString()), CurrencyPluralName).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
2016-12-17 00:16:14 +00:00
public async Task Take(long amount, [Remainder] ulong usrId)
{
if (amount <= 0)
return;
if (await _currency.RemoveAsync(usrId, $"Taken by bot owner.({Context.User.Username}/{Context.User.Id})", amount).ConfigureAwait(false))
await ReplyConfirmLocalized("take", amount + CurrencySign, $"<@{usrId}>").ConfigureAwait(false);
2016-10-08 18:24:34 +00:00
else
await ReplyErrorLocalized("take_fail", amount + CurrencySign, Format.Code(usrId.ToString()), CurrencyPluralName).ConfigureAwait(false);
}
2017-01-22 20:29:41 +00:00
//[NadekoCommand, Usage, Description, Aliases]
//[OwnerOnly]
//public Task BrTest(int tests = 1000)
//{
// var t = Task.Run(async () =>
// {
// if (tests <= 0)
// return;
// //multi vs how many times it occured
// var dict = new Dictionary<int, int>();
// var generator = new NadekoRandom();
// for (int i = 0; i < tests; i++)
// {
// var rng = generator.Next(0, 101);
// var mult = 0;
// if (rng < 67)
// {
// mult = 0;
// }
// else if (rng < 91)
// {
// mult = 2;
// }
// else if (rng < 100)
// {
// mult = 4;
// }
// else
// mult = 10;
// if (dict.ContainsKey(mult))
// dict[mult] += 1;
// else
// dict.Add(mult, 1);
// }
// var sb = new StringBuilder();
// const int bet = 1;
// int payout = 0;
// foreach (var key in dict.Keys.OrderByDescending(x => x))
// {
// sb.AppendLine($"x{key} occured {dict[key]} times. {dict[key] * 1.0f / tests * 100}%");
// payout += key * dict[key];
// }
// try
// {
// await Context.Channel.SendConfirmAsync("BetRoll Test Results", sb.ToString(),
// footer: $"Total Bet: {tests * bet} | Payout: {payout * bet} | {payout * 1.0f / tests * 100}%");
// }
// catch { }
// });
// return Task.CompletedTask;
//}
[NadekoCommand, Usage, Description, Aliases]
2016-12-17 00:16:14 +00:00
public async Task BetRoll(long amount)
{
if (amount < 1)
return;
if (!await _currency.RemoveAsync(Context.User, "Betroll Gamble", amount, false).ConfigureAwait(false))
{
await ReplyErrorLocalized("not_enough", CurrencyPluralName).ConfigureAwait(false);
return;
}
var rnd = new NadekoRandom().Next(0, 101);
var str = Context.User.Mention + Format.Code(GetText("roll", rnd));
if (rnd < 67)
{
str += GetText("better_luck");
}
else
{
if (rnd < 91)
{
str += GetText("br_win", (amount * _bc.BotConfig.Betroll67Multiplier) + CurrencySign, 66);
await _currency.AddAsync(Context.User, "Betroll Gamble",
(int) (amount * _bc.BotConfig.Betroll67Multiplier), false).ConfigureAwait(false);
}
else if (rnd < 100)
{
str += GetText("br_win", (amount * _bc.BotConfig.Betroll91Multiplier) + CurrencySign, 90);
await _currency.AddAsync(Context.User, "Betroll Gamble",
(int) (amount * _bc.BotConfig.Betroll91Multiplier), false).ConfigureAwait(false);
}
else
{
str += GetText("br_win", (amount * _bc.BotConfig.Betroll100Multiplier) + CurrencySign, 100) + " 👑";
await _currency.AddAsync(Context.User, "Betroll Gamble",
(int) (amount * _bc.BotConfig.Betroll100Multiplier), false).ConfigureAwait(false);
}
}
2016-12-16 21:44:26 +00:00
await Context.Channel.SendConfirmAsync(str).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
2017-03-09 01:17:18 +00:00
public async Task Leaderboard(int page = 1)
{
if (page < 1)
return;
List<Currency> richest;
2017-05-24 20:28:16 +00:00
using (var uow = _db.UnitOfWork)
{
2017-03-09 01:17:18 +00:00
richest = uow.Currency.GetTopRichest(9, 9 * (page - 1)).ToList();
}
2017-01-23 00:33:07 +00:00
var embed = new EmbedBuilder()
.WithOkColor()
2017-05-24 20:28:16 +00:00
.WithTitle(CurrencySign +
2017-03-09 01:17:18 +00:00
" " + GetText("leaderboard"))
.WithFooter(efb => efb.WithText(GetText("page", page)));
if (!richest.Any())
{
embed.WithDescription(GetText("no_users_found"));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
return;
}
2017-01-23 00:33:07 +00:00
for (var i = 0; i < richest.Count; i++)
{
var x = richest[i];
var usr = await Context.Guild.GetUserAsync(x.UserId).ConfigureAwait(false);
var usrStr = usr == null
? x.UserId.ToString()
: usr.Username?.TrimTo(20, true);
var j = i;
embed.AddField(efb => efb.WithName("#" + (9 * (page - 1) + j + 1) + " " + usrStr)
2017-05-24 20:28:16 +00:00
.WithValue(x.Amount.ToString() + " " + CurrencySign)
.WithIsInline(true));
2017-01-23 00:33:07 +00:00
}
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}
}
}