NadekoBot/NadekoBot.Modules.Gambling/DrawCommands.cs

106 lines
3.9 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
2017-06-11 14:07:27 +00:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Gambling.Common;
2016-12-17 04:09:04 +00:00
using Image = ImageSharp.Image;
2017-07-18 16:26:55 +00:00
using ImageSharp;
2015-12-05 10:27:00 +00:00
namespace NadekoBot.Modules.Gambling
{
2016-10-05 05:01:19 +00:00
public partial class Gambling
{
2016-10-05 05:01:19 +00:00
[Group]
public class DrawCommands : NadekoSubmodule
{
2017-02-15 10:17:55 +00:00
private static readonly ConcurrentDictionary<IGuild, Cards> _allDecks = new ConcurrentDictionary<IGuild, Cards>();
private const string _cardsPath = "data/images/cards";
2016-10-05 05:01:19 +00:00
2017-06-11 14:07:27 +00:00
private async Task<(Stream ImageStream, string ToSend)> InternalDraw(int num, ulong? guildId = null)
{
2017-06-11 14:07:27 +00:00
if (num < 1 || num > 10)
throw new ArgumentOutOfRangeException(nameof(num));
Cards cards = guildId == null ? new Cards() : _allDecks.GetOrAdd(Context.Guild, (s) => new Cards());
2017-07-18 16:26:55 +00:00
var images = new List<Image<Rgba32>>();
2016-10-05 05:01:19 +00:00
var cardObjects = new List<Cards.Card>();
for (var i = 0; i < num; i++)
{
if (cards.CardPool.Count == 0 && i != 0)
{
2017-02-15 10:17:55 +00:00
try
{
await ReplyErrorLocalized("no_more_cards").ConfigureAwait(false);
}
catch
{
// ignored
}
2016-10-05 05:01:19 +00:00
break;
}
var currentCard = cards.DrawACard();
cardObjects.Add(currentCard);
2017-06-11 14:07:27 +00:00
using (var stream = File.OpenRead(Path.Combine(_cardsPath, currentCard.ToString().ToLowerInvariant() + ".jpg").Replace(' ', '_')))
2017-07-18 16:26:55 +00:00
images.Add(Image.Load(stream));
2016-10-05 05:01:19 +00:00
}
MemoryStream bitmapStream = new MemoryStream();
2017-07-18 16:26:55 +00:00
images.Merge().SaveAsPng(bitmapStream);
2016-10-05 05:01:19 +00:00
bitmapStream.Position = 0;
2017-06-11 14:07:27 +00:00
2016-12-16 18:43:57 +00:00
var toSend = $"{Context.User.Mention}";
2016-10-05 05:01:19 +00:00
if (cardObjects.Count == 5)
2016-10-12 17:43:13 +00:00
toSend += $" drew `{Cards.GetHandValue(cardObjects)}`";
2017-06-11 14:07:27 +00:00
return (bitmapStream, toSend);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Draw(int num = 1)
{
if (num < 1)
num = 1;
if (num > 10)
num = 10;
var data = await InternalDraw(num, Context.Guild.Id).ConfigureAwait(false);
await Context.Channel.SendFileAsync(data.ImageStream, num + " cards.jpg", data.ToSend).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
public async Task DrawNew(int num = 1)
{
if (num < 1)
num = 1;
if (num > 10)
num = 10;
var data = await InternalDraw(num).ConfigureAwait(false);
await Context.Channel.SendFileAsync(data.ImageStream, num + " cards.jpg", data.ToSend).ConfigureAwait(false);
}
2016-10-05 05:01:19 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task DeckShuffle()
2016-10-05 05:01:19 +00:00
{
2016-12-16 21:44:26 +00:00
//var channel = (ITextChannel)Context.Channel;
2017-02-15 10:17:55 +00:00
_allDecks.AddOrUpdate(Context.Guild,
2016-10-05 05:01:19 +00:00
(g) => new Cards(),
(g, c) =>
{
c.Restart();
return c;
});
2015-12-05 10:27:00 +00:00
2017-02-15 10:17:55 +00:00
await ReplyConfirmLocalized("deck_reshuffled").ConfigureAwait(false);
2016-10-05 05:01:19 +00:00
}
}
}
2016-10-05 05:01:19 +00:00
}