NadekoBot/NadekoBot.Modules.Gambling/FlipCoinCommands.cs

133 lines
4.9 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
2016-09-14 12:23:09 +00:00
using NadekoBot.Extensions;
using NadekoBot.Services;
2016-10-20 03:35:00 +00:00
using System;
using System.Collections.Generic;
2016-09-14 12:23:09 +00:00
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
2016-12-17 04:09:04 +00:00
using Image = ImageSharp.Image;
2017-07-18 16:26:55 +00:00
using ImageSharp;
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]
public class FlipCoinCommands : NadekoSubmodule
2016-09-14 12:23:09 +00:00
{
private readonly IImagesService _images;
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
2017-05-24 20:28:16 +00:00
private readonly NadekoRandom rng = new NadekoRandom();
public FlipCoinCommands(IImagesService images, CurrencyService cs, IBotConfigProvider bc)
{
2017-05-24 20:28:16 +00:00
_images = images;
_bc = bc;
_cs = cs;
}
[NadekoCommand, Usage, Description, Aliases]
public async Task Flip(int count = 1)
2016-09-14 12:23:09 +00:00
{
if (count == 1)
{
if (rng.Next(0, 2) == 1)
{
using (var heads = _images.Heads.ToStream())
{
2017-05-25 02:24:43 +00:00
await Context.Channel.SendFileAsync(heads, "heads.jpg", Context.User.Mention + " " + GetText("flipped", Format.Bold(GetText("heads")))).ConfigureAwait(false);
}
}
2016-09-14 12:23:09 +00:00
else
{
using (var tails = _images.Tails.ToStream())
{
2017-05-25 02:24:43 +00:00
await Context.Channel.SendFileAsync(tails, "tails.jpg", Context.User.Mention + " " + GetText("flipped", Format.Bold(GetText("tails")))).ConfigureAwait(false);
}
}
2016-09-14 12:23:09 +00:00
return;
}
if (count > 10 || count < 1)
{
await ReplyErrorLocalized("flip_invalid", 10).ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
return;
}
2017-07-18 16:26:55 +00:00
var imgs = new Image<Rgba32>[count];
for (var i = 0; i < count; i++)
2016-09-14 12:23:09 +00:00
{
using (var heads = _images.Heads.ToStream())
using (var tails = _images.Tails.ToStream())
{
if (rng.Next(0, 10) < 5)
{
2017-07-18 16:26:55 +00:00
imgs[i] = Image.Load(heads);
}
else
{
2017-07-18 16:26:55 +00:00
imgs[i] = Image.Load(tails);
}
}
2016-09-14 12:23:09 +00:00
}
await Context.Channel.SendFileAsync(imgs.Merge().ToStream(), $"{count} coins.png").ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
}
public enum BetFlipGuess
2016-09-14 12:23:09 +00:00
{
H = 1,
Head = 1,
Heads = 1,
T = 2,
Tail = 2,
Tails = 2
}
[NadekoCommand, Usage, Description, Aliases]
public async Task Betflip(int amount, BetFlipGuess guess)
{
if (amount < _bc.BotConfig.MinimumBetAmount)
{
await ReplyErrorLocalized("min_bet_limit", _bc.BotConfig.MinimumBetAmount + _bc.BotConfig.CurrencySign).ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
return;
}
var removed = await _cs.RemoveAsync(Context.User, "Betflip Gamble", amount, false).ConfigureAwait(false);
2016-12-21 11:52:01 +00:00
if (!removed)
2016-09-14 12:23:09 +00:00
{
await ReplyErrorLocalized("not_enough", _bc.BotConfig.CurrencyPluralName).ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
return;
}
BetFlipGuess result;
IEnumerable<byte> imageToSend;
2016-09-14 12:23:09 +00:00
if (rng.Next(0, 2) == 1)
{
imageToSend = _images.Heads;
result = BetFlipGuess.Heads;
2016-09-14 12:23:09 +00:00
}
else
{
imageToSend = _images.Tails;
result = BetFlipGuess.Tails;
2016-09-14 12:23:09 +00:00
}
2016-09-14 12:23:09 +00:00
string str;
if (guess == result)
2016-10-20 03:35:00 +00:00
{
var toWin = (int)Math.Round(amount * _bc.BotConfig.BetflipMultiplier);
str = Context.User.Mention + " " + GetText("flip_guess", toWin + _bc.BotConfig.CurrencySign);
await _cs.AddAsync(Context.User, "Betflip Gamble", toWin, false).ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
}
else
{
str = Context.User.Mention + " " + GetText("better_luck");
2016-09-14 12:23:09 +00:00
}
using (var toSend = imageToSend.ToStream())
{
await Context.Channel.SendFileAsync(toSend, "result.png", str).ConfigureAwait(false);
}
2016-09-14 12:23:09 +00:00
}
}
}
2016-09-14 12:23:09 +00:00
}