Added coin flipping and betflipping

This commit is contained in:
Kwoth 2016-09-14 14:23:09 +02:00
parent a4cc1ab563
commit 438fb65c04
4 changed files with 125 additions and 98 deletions

View File

@ -1,93 +1,107 @@
using Discord;
using Discord.Commands;
using ImageProcessorCore;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services;
using System.IO;
using System.Threading.Tasks;
//todo drawing
namespace NadekoBot.Modules.Gambling
{
public partial class Gambling
{
[Group]
public class FlipCoinCommands
{
NadekoRandom rng { get; } = new NadekoRandom();
private const string headsPath = "data/images/coins/heads.png";
private const string tailsPath = "data/images/coins/tails.png";
public FlipCoinCommands() { }
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task Flip(IUserMessage imsg, int count = 1)
{
var channel = (ITextChannel)imsg.Channel;
if (count == 1)
{
if (rng.Next(0, 2) == 1)
await channel.SendFileAsync(headsPath, $"{imsg.Author.Mention} rolled " + Format.Code("Heads") + ".").ConfigureAwait(false);
else
await channel.SendFileAsync(tailsPath, $"{imsg.Author.Mention} rolled " + Format.Code("Tails") + ".").ConfigureAwait(false);
return;
}
if (count > 10 || count < 1)
{
await channel.SendMessageAsync("`Invalid number specified. You can flip 1 to 10 coins.`");
return;
}
var imgs = new Image[count];
for (var i = 0; i < count; i++)
{
imgs[i] = rng.Next(0, 10) < 5 ?
new Image(File.OpenRead(headsPath)) :
new Image(File.OpenRead(tailsPath));
}
await channel.SendFileAsync(imgs.Merge().ToStream(), $"{count} coins.png").ConfigureAwait(false);
}
////todo drawing
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)]
//public async Task Flip(IUserMessage imsg, int count = 0)
//{
// var channel = (ITextChannel)imsg.Channel;
// if (count == 0)
// {
// if (rng.Next(0, 2) == 1)
// await channel.SendFileAsync("heads.png", ).ConfigureAwait(false);
// else
// await channel.SendFileAsync("tails.png", ).ConfigureAwait(false);
// return;
// }
// if (result > 10)
// result = 10;
// var imgs = new Image[result];
// for (var i = 0; i < result; i++)
// {
// imgs[i] = rng.Next(0, 2) == 0 ?
// Properties.Resources.tails :
// Properties.Resources.heads;
// }
// await channel.SendFile($"{result} coins.png", imgs.Merge().ToStream(System.Drawing.Imaging.ImageFormat.Png)).ConfigureAwait(false);
// return;
// await channel.SendMessageAsync("Invalid number").ConfigureAwait(false);
//}
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task Betflip(IUserMessage umsg, int amount, string guess)
{
var channel = (ITextChannel)umsg.Channel;
var guildUser = (IGuildUser)umsg.Author;
var guessStr = guess.Trim().ToUpperInvariant();
if (guessStr != "H" && guessStr != "T" && guessStr != "HEADS" && guessStr != "TAILS")
return;
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)]
//public async Task Betflip(IUserMessage umsg, int amount, string guess)
//{
// var channel = (ITextChannel)umsg.Channel;
// var guildUser = (IGuildUser)umsg.Author;
// var guessStr = guess.Trim().ToUpperInvariant();
// if (guessStr != "H" && guessStr != "T" && guessStr != "HEADS" && guessStr != "TAILS")
// return;
if (amount < 1)
return;
// if (amount < 1)
// return;
long userFlowers;
using (var uow = DbHandler.UnitOfWork())
{
userFlowers = uow.Currency.GetOrCreate(umsg.Author.Id).Amount;
}
// var userFlowers = Gambling.GetUserFlowers(umsg.Author.Id);
if (userFlowers < amount)
{
await channel.SendMessageAsync($"{umsg.Author.Mention} You don't have enough {Gambling.CurrencyPluralName}. You only have {userFlowers}{Gambling.CurrencySign}.").ConfigureAwait(false);
return;
}
// if (userFlowers < amount)
// {
// await channel.SendMessageAsync($"{umsg.Author.Mention} You don't have enough {Gambling.CurrencyName}s. You only have {userFlowers}{Gambling.CurrencySign}.").ConfigureAwait(false);
// return;
// }
await CurrencyHandler.RemoveCurrencyAsync(guildUser, "Betflip Gamble", amount, false).ConfigureAwait(false);
//heads = true
//tails = false
// await CurrencyHandler.RemoveCurrencyAsync(guildUser, "Betflip Gamble", amount, false).ConfigureAwait(false);
// //heads = true
// //tails = false
var isHeads = guessStr == "HEADS" || guessStr == "H";
bool result = false;
string imgPathToSend;
if (rng.Next(0, 2) == 1)
{
imgPathToSend = headsPath;
result = true;
}
else
{
imgPathToSend = tailsPath;
}
// var isHeads = guessStr == "HEADS" || guessStr == "H";
// bool result = false;
// var rng = new NadekoRandom();
// if (rng.Next(0, 2) == 1)
// {
// await channel.SendFileAsync("heads.png", Properties.Resources.heads.ToStream(System.Drawing.Imaging.ImageFormat.Png)).ConfigureAwait(false);
// result = true;
// }
// else
// {
// await channel.SendFileAsync("tails.png", Properties.Resources.tails.ToStream(System.Drawing.Imaging.ImageFormat.Png)).ConfigureAwait(false);
// }
string str;
if (isHeads == result)
{
str = $"{umsg.Author.Mention}`You guessed it!` You won {amount * 2}{Gambling.CurrencySign}";
await CurrencyHandler.AddCurrencyAsync((IGuildUser)umsg.Author, "Betflip Gamble", amount * 2, false).ConfigureAwait(false);
}
else
{
str = $"{umsg.Author.Mention}`Better luck next time.`";
}
// string str;
// if (isHeads == result)
// {
// str = $"{umsg.Author.Mention}`You guessed it!` You won {amount * 2}{Gambling.CurrencySign}";
// await CurrencyHandler.AddCurrencyAsync((IGuildUser)umsg.Author, "Betflip Gamble", amount * 2, false).ConfigureAwait(false);
// }
// else
// str = $"{umsg.Author.Mention}`More luck next time.`";
// await channel.SendMessageAsync(str).ConfigureAwait(false);
//}
await channel.SendFileAsync(imgPathToSend, str).ConfigureAwait(false);
}
}
}
}

View File

@ -82,25 +82,25 @@ namespace NadekoBot.Modules.Gambling
await channel.SendMessageAsync($"{umsg.Author.Mention} successfully sent {amount} {Gambling.CurrencyPluralName}s to {receiver.Mention}!").ConfigureAwait(false);
}
////todo owner only
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)]
//public Task Award(IUserMessage umsg, long amount, [Remainder] IGuildUser usr) =>
// Award(umsg, amount, usr.Id);
//todo owner only
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public Task Award(IUserMessage umsg, long amount, [Remainder] IGuildUser usr) =>
Award(umsg, amount, usr.Id);
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)]
//public async Task Award(IUserMessage umsg, long amount, [Remainder] ulong usrId)
//{
// var channel = (ITextChannel)umsg.Channel;
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task Award(IUserMessage umsg, long amount, [Remainder] ulong usrId)
{
var channel = (ITextChannel)umsg.Channel;
// if (amount <= 0)
// return;
if (amount <= 0)
return;
// await CurrencyHandler.AddFlowersAsync(usrId, $"Awarded by bot owner. ({umsg.Author.Username}/{umsg.Author.Id})", (int)amount).ConfigureAwait(false);
await CurrencyHandler.AddCurrencyAsync(usrId, $"Awarded by bot owner. ({umsg.Author.Username}/{umsg.Author.Id})", (int)amount).ConfigureAwait(false);
// await channel.SendMessageAsync($"{umsg.Author.Mention} successfully awarded {amount} {Gambling.CurrencyName}s to <@{usrId}>!").ConfigureAwait(false);
//}
await channel.SendMessageAsync($"{umsg.Author.Mention} successfully awarded {amount} {Gambling.CurrencyName}s to <@{usrId}>!").ConfigureAwait(false);
}
////todo owner only
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]

View File

@ -33,6 +33,14 @@ namespace NadekoBot.Services
}
public static async Task AddCurrencyAsync(IGuildUser author, string reason, long amount, bool sendMessage)
{
await AddCurrencyAsync(author.Id, reason, amount);
if (sendMessage)
await author.SendMessageAsync($"`You received:` {amount} {Gambling.CurrencySign}\n`Reason:` {reason}").ConfigureAwait(false);
}
public static async Task AddCurrencyAsync(ulong receiverId, string reason, long amount)
{
if (amount < 0)
throw new ArgumentNullException(nameof(amount));
@ -40,12 +48,9 @@ namespace NadekoBot.Services
using (var uow = DbHandler.UnitOfWork())
{
uow.Currency.TryUpdateState(author.Id, amount);
uow.Currency.TryUpdateState(receiverId, amount);
await uow.CompleteAsync();
}
if (sendMessage)
await author.SendMessageAsync($"`You received:` {amount} {Gambling.CurrencySign}\n`Reason:` {reason}").ConfigureAwait(false);
}
}
}

View File

@ -290,5 +290,13 @@ namespace NadekoBot.Extensions
return canvas;
}
public static Stream ToStream(this Image img)
{
var imageStream = new MemoryStream();
img.SaveAsPng(imageStream);
imageStream.Position = 0;
return imageStream;
}
}
}