NadekoBot/NadekoBot.Modules.Gambling/DiceRollCommands.cs

241 lines
9.2 KiB
C#
Raw Normal View History

2016-08-20 17:13:29 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
2015-12-05 10:27:00 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2015-12-05 10:27:00 +00:00
using System.Linq;
2016-04-01 17:59:46 +00:00
using System.Text.RegularExpressions;
2015-12-05 10:27:00 +00:00
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
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-08-20 17:13:29 +00:00
public partial class Gambling
{
[Group]
public class DriceRollCommands : NadekoSubmodule
{
2017-07-18 16:26:55 +00:00
private readonly Regex dndRegex = new Regex(@"^(?<n1>\d+)d(?<n2>\d+)(?:\+(?<add>\d+))?(?:\-(?<sub>\d+))?$", RegexOptions.Compiled);
private readonly Regex fudgeRegex = new Regex(@"^(?<n1>\d+)d(?:F|f)$", RegexOptions.Compiled);
2017-02-18 20:39:13 +00:00
private readonly char[] _fateRolls = { '-', ' ', '+' };
2017-05-24 20:28:16 +00:00
private readonly IImagesService _images;
public DriceRollCommands(IImagesService images)
{
_images = images;
}
[NadekoCommand, Usage, Description, Aliases]
public async Task Roll()
{
var rng = new NadekoRandom();
var gen = rng.Next(1, 101);
2016-08-20 17:13:29 +00:00
var num1 = gen / 10;
var num2 = gen % 10;
var imageStream = await Task.Run(() =>
{
var ms = new MemoryStream();
2017-07-18 16:26:55 +00:00
new[] { GetDice(num1), GetDice(num2) }.Merge().SaveAsPng(ms);
ms.Position = 0;
return ms;
}).ConfigureAwait(false);
2017-02-18 20:39:13 +00:00
await Context.Channel.SendFileAsync(imageStream,
"dice.png",
Context.User.Mention + " " + GetText("dice_rolled", Format.Code(gen.ToString()))).ConfigureAwait(false);
}
public enum RollOrderType
{
Ordered,
Unordered
}
[NadekoCommand, Usage, Description, Aliases]
[Priority(1)]
public async Task Roll(int num)
{
await InternalRoll(num, true).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[Priority(1)]
2017-03-21 06:12:43 +00:00
public async Task Rolluo(int num = 1)
{
await InternalRoll(num, false).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[Priority(0)]
public async Task Roll(string arg)
{
await InternallDndRoll(arg, true).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[Priority(0)]
public async Task Rolluo(string arg)
{
await InternallDndRoll(arg, false).ConfigureAwait(false);
}
private async Task InternalRoll(int num, bool ordered)
{
if (num < 1 || num > 30)
{
2017-02-18 20:39:13 +00:00
await ReplyErrorLocalized("dice_invalid_number", 1, 30).ConfigureAwait(false);
return;
}
2016-08-20 17:13:29 +00:00
var rng = new NadekoRandom();
2016-08-20 17:13:29 +00:00
2017-07-18 16:26:55 +00:00
var dice = new List<Image<Rgba32>>(num);
var values = new List<int>(num);
for (var i = 0; i < num; i++)
{
var randomNumber = rng.Next(1, 7);
var toInsert = dice.Count;
if (ordered)
{
if (randomNumber == 6 || dice.Count == 0)
toInsert = 0;
else if (randomNumber != 1)
for (var j = 0; j < dice.Count; j++)
{
if (values[j] < randomNumber)
{
toInsert = j;
break;
}
}
}
else
{
toInsert = dice.Count;
}
dice.Insert(toInsert, GetDice(randomNumber));
values.Insert(toInsert, randomNumber);
}
var bitmap = dice.Merge();
var ms = new MemoryStream();
2017-07-18 16:26:55 +00:00
bitmap.SaveAsPng(ms);
ms.Position = 0;
2017-02-18 20:39:13 +00:00
await Context.Channel.SendFileAsync(ms, "dice.png",
Context.User.Mention + " " +
2017-02-18 20:39:13 +00:00
GetText("dice_rolled_num", Format.Bold(values.Count.ToString())) +
" " + GetText("total_average",
2017-02-18 20:39:13 +00:00
Format.Bold(values.Sum().ToString()),
Format.Bold((values.Sum() / (1.0f * values.Count)).ToString("N2")))).ConfigureAwait(false);
}
private async Task InternallDndRoll(string arg, bool ordered)
{
Match match;
int n1;
int n2;
if ((match = fudgeRegex.Match(arg)).Length != 0 &&
int.TryParse(match.Groups["n1"].ToString(), out n1) &&
n1 > 0 && n1 < 500)
{
var rng = new NadekoRandom();
var rolls = new List<char>();
2016-12-24 10:01:06 +00:00
for (int i = 0; i < n1; i++)
{
2017-02-18 20:39:13 +00:00
rolls.Add(_fateRolls[rng.Next(0, _fateRolls.Length)]);
}
2017-02-18 20:39:13 +00:00
var embed = new EmbedBuilder().WithOkColor().WithDescription(Context.User.Mention + " " + GetText("dice_rolled_num", Format.Bold(n1.ToString())))
.AddField(efb => efb.WithName(Format.Bold("Result"))
.WithValue(string.Join(" ", rolls.Select(c => Format.Code($"[{c}]")))));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}
else if ((match = dndRegex.Match(arg)).Length != 0)
{
var rng = new NadekoRandom();
if (int.TryParse(match.Groups["n1"].ToString(), out n1) &&
int.TryParse(match.Groups["n2"].ToString(), out n2) &&
n1 <= 50 && n2 <= 100000 && n1 > 0 && n2 > 0)
{
var add = 0;
var sub = 0;
int.TryParse(match.Groups["add"].Value, out add);
int.TryParse(match.Groups["sub"].Value, out sub);
var arr = new int[n1];
for (int i = 0; i < n1; i++)
{
2017-01-03 18:45:01 +00:00
arr[i] = rng.Next(1, n2 + 1);
}
2016-12-24 10:01:06 +00:00
2017-01-03 18:45:01 +00:00
var sum = arr.Sum();
2017-02-18 20:39:13 +00:00
var embed = new EmbedBuilder().WithOkColor().WithDescription(Context.User.Mention + " " +GetText("dice_rolled_num", n1) + $"`1 - {n2}`")
2017-01-03 18:45:01 +00:00
.AddField(efb => efb.WithName(Format.Bold("Rolls"))
.WithValue(string.Join(" ", (ordered ? arr.OrderBy(x => x).AsEnumerable() : arr).Select(x => Format.Code(x.ToString())))))
.AddField(efb => efb.WithName(Format.Bold("Sum"))
.WithValue(sum + " + " + add + " - " + sub + " = " + (sum + add - sub)));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}
}
}
2016-08-20 17:13:29 +00:00
[NadekoCommand, Usage, Description, Aliases]
public async Task NRoll([Remainder] string range)
{
2017-02-18 20:39:13 +00:00
int rolled;
if (range.Contains("-"))
{
2017-02-18 20:39:13 +00:00
var arr = range.Split('-')
.Take(2)
.Select(int.Parse)
.ToArray();
if (arr[0] > arr[1])
{
2017-02-18 20:39:13 +00:00
await ReplyErrorLocalized("second_larger_than_first").ConfigureAwait(false);
return;
}
2017-02-18 20:39:13 +00:00
rolled = new NadekoRandom().Next(arr[0], arr[1] + 1);
}
2017-02-18 20:39:13 +00:00
else
{
2017-02-18 20:39:13 +00:00
rolled = new NadekoRandom().Next(0, int.Parse(range) + 1);
2016-04-01 17:59:46 +00:00
}
2017-02-18 20:39:13 +00:00
await ReplyConfirmLocalized("dice_rolled", Format.Bold(rolled.ToString())).ConfigureAwait(false);
2016-08-20 17:13:29 +00:00
}
2015-12-05 10:27:00 +00:00
2017-07-18 16:26:55 +00:00
private Image<Rgba32> GetDice(int num)
{
if (num < 0 || num > 10)
throw new ArgumentOutOfRangeException(nameof(num));
if (num == 10)
{
2017-05-24 20:28:16 +00:00
var images = _images.Dice;
using (var imgOneStream = images[1].ToStream())
using (var imgZeroStream = images[0].ToStream())
{
2017-07-18 16:26:55 +00:00
var imgOne = Image.Load(imgOneStream);
var imgZero = Image.Load(imgZeroStream);
return new[] { imgOne, imgZero }.Merge();
}
}
using (var die = _images.Dice[num].ToStream())
{
2017-07-18 16:26:55 +00:00
return Image.Load(die);
}
}
}
2015-12-05 10:27:00 +00:00
}
}