Added coin flipping and betflipping
This commit is contained in:
		| @@ -1,93 +1,107 @@ | |||||||
| using Discord; | using Discord; | ||||||
| using Discord.Commands; | 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 | namespace NadekoBot.Modules.Gambling | ||||||
| { | { | ||||||
|     [Group] |     public partial class Gambling | ||||||
|     public class FlipCoinCommands |  | ||||||
|     { |     { | ||||||
|  |         [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() { } | ||||||
|              |              | ||||||
|         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); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [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; | ||||||
|  |  | ||||||
|         ////todo drawing |                 if (amount < 1) | ||||||
|         //[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] |                     return; | ||||||
|         //[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] |                 long userFlowers; | ||||||
|         //[RequireContext(ContextType.Guild)] |                 using (var uow = DbHandler.UnitOfWork()) | ||||||
|         //public async Task Betflip(IUserMessage umsg, int amount, string guess) |                 { | ||||||
|         //{ |                     userFlowers = uow.Currency.GetOrCreate(umsg.Author.Id).Amount; | ||||||
|         //    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) |                 if (userFlowers < amount) | ||||||
|         //        return; |                 { | ||||||
|  |                     await channel.SendMessageAsync($"{umsg.Author.Mention} You don't have enough {Gambling.CurrencyPluralName}. You only have {userFlowers}{Gambling.CurrencySign}.").ConfigureAwait(false); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|         //    var userFlowers = Gambling.GetUserFlowers(umsg.Author.Id); |                 await CurrencyHandler.RemoveCurrencyAsync(guildUser, "Betflip Gamble", amount, false).ConfigureAwait(false); | ||||||
|  |                 //heads = true | ||||||
|  |                 //tails = false | ||||||
|  |  | ||||||
|         //    if (userFlowers < amount) |                 var isHeads = guessStr == "HEADS" || guessStr == "H"; | ||||||
|         //    { |                 bool result = false; | ||||||
|         //        await channel.SendMessageAsync($"{umsg.Author.Mention} You don't have enough {Gambling.CurrencyName}s. You only have {userFlowers}{Gambling.CurrencySign}.").ConfigureAwait(false); |                 string imgPathToSend; | ||||||
|         //        return; |                 if (rng.Next(0, 2) == 1) | ||||||
|         //    } |                 { | ||||||
|  |                     imgPathToSend = headsPath; | ||||||
|  |                     result = true; | ||||||
|  |                 } | ||||||
|  |                 else | ||||||
|  |                 { | ||||||
|  |                     imgPathToSend = tailsPath; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|         //    await CurrencyHandler.RemoveCurrencyAsync(guildUser, "Betflip Gamble", amount, false).ConfigureAwait(false); |                 string str; | ||||||
|         //    //heads = true |                 if (isHeads == result) | ||||||
|         //    //tails = false |                 { | ||||||
|  |                     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.`"; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|         //    var isHeads = guessStr == "HEADS" || guessStr == "H"; |                 await channel.SendFileAsync(imgPathToSend, str).ConfigureAwait(false); | ||||||
|         //    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}`More luck next time.`"; |  | ||||||
|  |  | ||||||
|         //    await channel.SendMessageAsync(str).ConfigureAwait(false); |  | ||||||
|         //} |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -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); |             await channel.SendMessageAsync($"{umsg.Author.Mention} successfully sent {amount} {Gambling.CurrencyPluralName}s to {receiver.Mention}!").ConfigureAwait(false); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         ////todo owner only |         //todo owner only | ||||||
|         //[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] |         [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] | ||||||
|         //[RequireContext(ContextType.Guild)] |         [RequireContext(ContextType.Guild)] | ||||||
|         //public Task Award(IUserMessage umsg, long amount, [Remainder] IGuildUser usr) => |         public Task Award(IUserMessage umsg, long amount, [Remainder] IGuildUser usr) => | ||||||
|         //    Award(umsg, amount, usr.Id); |             Award(umsg, amount, usr.Id); | ||||||
|  |  | ||||||
|         //[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] |         [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] | ||||||
|         //[RequireContext(ContextType.Guild)] |         [RequireContext(ContextType.Guild)] | ||||||
|         //public async Task Award(IUserMessage umsg, long amount, [Remainder] ulong usrId) |         public async Task Award(IUserMessage umsg, long amount, [Remainder] ulong usrId) | ||||||
|         //{ |         { | ||||||
|         //    var channel = (ITextChannel)umsg.Channel; |             var channel = (ITextChannel)umsg.Channel; | ||||||
|  |  | ||||||
|         //    if (amount <= 0) |             if (amount <= 0) | ||||||
|         //        return; |                 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 |         ////todo owner only | ||||||
|         //[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] |         //[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] | ||||||
|   | |||||||
| @@ -33,6 +33,14 @@ namespace NadekoBot.Services | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         public static async Task AddCurrencyAsync(IGuildUser author, string reason, long amount, bool sendMessage) |         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) |             if (amount < 0) | ||||||
|                 throw new ArgumentNullException(nameof(amount)); |                 throw new ArgumentNullException(nameof(amount)); | ||||||
| @@ -40,12 +48,9 @@ namespace NadekoBot.Services | |||||||
|  |  | ||||||
|             using (var uow = DbHandler.UnitOfWork()) |             using (var uow = DbHandler.UnitOfWork()) | ||||||
|             { |             { | ||||||
|                 uow.Currency.TryUpdateState(author.Id, amount); |                 uow.Currency.TryUpdateState(receiverId, amount); | ||||||
|                 await uow.CompleteAsync(); |                 await uow.CompleteAsync(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             if (sendMessage) |  | ||||||
|                 await author.SendMessageAsync($"`You received:` {amount} {Gambling.CurrencySign}\n`Reason:` {reason}").ConfigureAwait(false); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -290,5 +290,13 @@ namespace NadekoBot.Extensions | |||||||
|  |  | ||||||
|             return canvas; |             return canvas; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         public static Stream ToStream(this Image img) | ||||||
|  |         { | ||||||
|  |             var imageStream = new MemoryStream(); | ||||||
|  |             img.SaveAsPng(imageStream); | ||||||
|  |             imageStream.Position = 0; | ||||||
|  |             return imageStream; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user