NadekoBot/src/NadekoBot/Modules/Gambling/Commands/FlipCoinCommand.cs

113 lines
4.6 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
2016-11-25 12:14:44 +00:00
using ImageSharp;
2016-09-14 12:23:09 +00:00
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services;
2016-10-20 03:35:00 +00:00
using System;
2016-09-14 12:23:09 +00:00
using System.IO;
using System.Threading.Tasks;
2016-12-17 04:09:04 +00:00
using Image = ImageSharp.Image;
namespace NadekoBot.Modules.Gambling
2016-09-14 12:23:09 +00:00
{
public partial class Gambling
{
2016-09-14 12:23:09 +00:00
[Group]
2016-12-17 04:09:04 +00:00
public class FlipCoinCommands : ModuleBase
2016-09-14 12:23:09 +00:00
{
2016-12-08 17:35:34 +00:00
private static NadekoRandom rng { get; } = new NadekoRandom();
2016-09-14 12:23:09 +00:00
private const string headsPath = "data/images/coins/heads.png";
private const string tailsPath = "data/images/coins/tails.png";
[NadekoCommand, Usage, Description, Aliases]
2016-09-14 12:23:09 +00:00
[RequireContext(ContextType.Guild)]
public async Task Flip(IUserMessage imsg, int count = 1)
{
2016-12-16 21:44:26 +00:00
//var channel = (ITextChannel)Context.Channel;
2016-09-14 12:23:09 +00:00
if (count == 1)
{
if (rng.Next(0, 2) == 1)
2016-12-16 21:44:26 +00:00
await Context.Channel.SendFileAsync(headsPath, $"{Context.User.Mention} flipped " + Format.Code("Heads") + ".").ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
else
2016-12-16 21:44:26 +00:00
await Context.Channel.SendFileAsync(tailsPath, $"{Context.User.Mention} flipped " + Format.Code("Tails") + ".").ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
return;
}
if (count > 10 || count < 1)
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync("`Invalid number specified. You can flip 1 to 10 coins.`");
2016-09-14 12:23:09 +00:00
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));
}
2016-12-16 21:44:26 +00:00
await Context.Channel.SendFileAsync(imgs.Merge().ToStream(), $"{count} coins.png").ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-14 12:23:09 +00:00
[RequireContext(ContextType.Guild)]
public async Task Betflip(IUserMessage umsg, int amount, string guess)
{
2016-12-16 21:44:26 +00:00
//var channel = (ITextChannel)Context.Channel;
2016-12-16 18:43:57 +00:00
var guildUser = (IGuildUser)Context.User;
2016-09-14 12:23:09 +00:00
var guessStr = guess.Trim().ToUpperInvariant();
if (guessStr != "H" && guessStr != "T" && guessStr != "HEADS" && guessStr != "TAILS")
return;
if (amount < 3)
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync($"You can't bet less than 3{Gambling.CurrencySign}.")
.ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
return;
}
2016-10-13 08:00:46 +00:00
// todo update this
2016-09-14 12:23:09 +00:00
long userFlowers;
using (var uow = DbHandler.UnitOfWork())
{
2016-12-16 18:43:57 +00:00
userFlowers = uow.Currency.GetOrCreate(Context.User.Id).Amount;
2016-09-14 12:23:09 +00:00
}
2016-09-14 12:23:09 +00:00
if (userFlowers < amount)
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync($"{Context.User.Mention} You don't have enough {Gambling.CurrencyPluralName}. You only have {userFlowers}{Gambling.CurrencySign}.").ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
return;
}
2016-09-14 12:23:09 +00:00
await CurrencyHandler.RemoveCurrencyAsync(guildUser, "Betflip Gamble", amount, false).ConfigureAwait(false);
//heads = true
//tails = false
2016-09-14 12:23:09 +00:00
var isHeads = guessStr == "HEADS" || guessStr == "H";
bool result = false;
string imgPathToSend;
if (rng.Next(0, 2) == 1)
{
imgPathToSend = headsPath;
result = true;
}
else
{
imgPathToSend = tailsPath;
}
2016-09-14 12:23:09 +00:00
string str;
if (isHeads == result)
2016-10-20 03:35:00 +00:00
{
var toWin = (int)Math.Round(amount * 1.8);
2016-12-16 18:43:57 +00:00
str = $"{Context.User.Mention}`You guessed it!` You won {toWin}{Gambling.CurrencySign}";
await CurrencyHandler.AddCurrencyAsync((IGuildUser)Context.User, "Betflip Gamble", toWin, false).ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
}
else
{
2016-12-16 18:43:57 +00:00
str = $"{Context.User.Mention}`Better luck next time.`";
2016-09-14 12:23:09 +00:00
}
2016-12-16 21:44:26 +00:00
await Context.Channel.SendFileAsync(imgPathToSend, str).ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
}
}
}
2016-09-14 12:23:09 +00:00
}