2016-08-20 17:13:29 +00:00
|
|
|
using Discord;
|
2016-03-25 11:48:22 +00:00
|
|
|
using Discord.Commands;
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
using NadekoBot.Core.Services;
|
2015-12-05 10:27:00 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2016-09-13 16:12:27 +00:00
|
|
|
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;
|
2016-12-31 12:21:18 +00:00
|
|
|
using Image = ImageSharp.Image;
|
2017-07-18 16:26:55 +00:00
|
|
|
using ImageSharp;
|
2015-12-05 10:27:00 +00:00
|
|
|
|
2016-03-25 11:48:22 +00:00
|
|
|
namespace NadekoBot.Modules.Gambling
|
|
|
|
{
|
2016-08-20 17:13:29 +00:00
|
|
|
public partial class Gambling
|
2016-03-25 11:48:22 +00:00
|
|
|
{
|
2016-09-14 01:38:44 +00:00
|
|
|
[Group]
|
2017-02-15 10:03:40 +00:00
|
|
|
public class DriceRollCommands : NadekoSubmodule
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
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);
|
2016-12-24 09:55:51 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-13 16:12:27 +00:00
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-01-01 12:26:17 +00:00
|
|
|
public async Task Roll()
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
2016-09-14 01:38:44 +00:00
|
|
|
var rng = new NadekoRandom();
|
|
|
|
var gen = rng.Next(1, 101);
|
2016-08-20 17:13:29 +00:00
|
|
|
|
2016-09-14 01:38:44 +00:00
|
|
|
var num1 = gen / 10;
|
|
|
|
var num2 = gen % 10;
|
|
|
|
var imageStream = await Task.Run(() =>
|
|
|
|
{
|
2017-02-04 08:34:51 +00:00
|
|
|
var ms = new MemoryStream();
|
2017-07-18 16:26:55 +00:00
|
|
|
new[] { GetDice(num1), GetDice(num2) }.Merge().SaveAsPng(ms);
|
2017-02-04 08:34:51 +00:00
|
|
|
ms.Position = 0;
|
|
|
|
return ms;
|
|
|
|
}).ConfigureAwait(false);
|
2016-09-13 16:12:27 +00:00
|
|
|
|
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);
|
2016-09-13 16:12:27 +00:00
|
|
|
}
|
2016-12-22 05:12:04 +00:00
|
|
|
|
|
|
|
public enum RollOrderType
|
|
|
|
{
|
|
|
|
Ordered,
|
|
|
|
Unordered
|
|
|
|
}
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-07-15 03:04:16 +00:00
|
|
|
[Priority(1)]
|
2017-01-01 12:26:17 +00:00
|
|
|
public async Task Roll(int num)
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
2017-01-01 12:26:17 +00:00
|
|
|
await InternalRoll(num, true).ConfigureAwait(false);
|
2016-12-22 05:12:04 +00:00
|
|
|
}
|
2016-09-14 01:38:44 +00:00
|
|
|
|
2016-10-19 03:45:35 +00:00
|
|
|
|
2016-12-22 05:12:04 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-07-15 03:04:16 +00:00
|
|
|
[Priority(1)]
|
2017-03-21 06:12:43 +00:00
|
|
|
public async Task Rolluo(int num = 1)
|
2016-12-22 05:12:04 +00:00
|
|
|
{
|
2017-01-01 12:26:17 +00:00
|
|
|
await InternalRoll(num, false).ConfigureAwait(false);
|
2016-09-14 01:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-07-15 03:04:16 +00:00
|
|
|
[Priority(0)]
|
2017-01-01 12:26:17 +00:00
|
|
|
public async Task Roll(string arg)
|
2016-12-22 05:12:04 +00:00
|
|
|
{
|
2017-01-01 12:26:17 +00:00
|
|
|
await InternallDndRoll(arg, true).ConfigureAwait(false);
|
2016-12-22 05:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-07-15 03:04:16 +00:00
|
|
|
[Priority(0)]
|
2017-01-01 12:26:17 +00:00
|
|
|
public async Task Rolluo(string arg)
|
2016-12-22 05:12:04 +00:00
|
|
|
{
|
2017-01-01 12:26:17 +00:00
|
|
|
await InternallDndRoll(arg, false).ConfigureAwait(false);
|
2016-12-22 05:12:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 08:34:51 +00:00
|
|
|
private async Task InternalRoll(int num, bool ordered)
|
2016-09-14 01:38:44 +00:00
|
|
|
{
|
|
|
|
if (num < 1 || num > 30)
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
2017-02-18 20:39:13 +00:00
|
|
|
await ReplyErrorLocalized("dice_invalid_number", 1, 30).ConfigureAwait(false);
|
2016-09-14 01:38:44 +00:00
|
|
|
return;
|
2016-09-13 16:12:27 +00:00
|
|
|
}
|
2016-08-20 17:13:29 +00:00
|
|
|
|
2016-09-14 01:38:44 +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);
|
2016-09-14 01:38:44 +00:00
|
|
|
var values = new List<int>(num);
|
|
|
|
for (var i = 0; i < num; i++)
|
2016-08-29 22:00:19 +00:00
|
|
|
{
|
2016-09-14 01:38:44 +00:00
|
|
|
var randomNumber = rng.Next(1, 7);
|
|
|
|
var toInsert = dice.Count;
|
|
|
|
if (ordered)
|
2016-08-29 22:00:19 +00:00
|
|
|
{
|
2016-09-14 01:38:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 22:00:19 +00:00
|
|
|
}
|
2016-09-14 01:38:44 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
toInsert = dice.Count;
|
|
|
|
}
|
|
|
|
dice.Insert(toInsert, GetDice(randomNumber));
|
|
|
|
values.Insert(toInsert, randomNumber);
|
2016-08-29 22:00:19 +00:00
|
|
|
}
|
2016-09-13 16:12:27 +00:00
|
|
|
|
2016-09-14 01:38:44 +00:00
|
|
|
var bitmap = dice.Merge();
|
|
|
|
var ms = new MemoryStream();
|
2017-07-18 16:26:55 +00:00
|
|
|
bitmap.SaveAsPng(ms);
|
2016-09-14 01:38:44 +00:00
|
|
|
ms.Position = 0;
|
2017-02-18 20:39:13 +00:00
|
|
|
await Context.Channel.SendFileAsync(ms, "dice.png",
|
2017-02-21 02:04:22 +00:00
|
|
|
Context.User.Mention + " " +
|
2017-02-18 20:39:13 +00:00
|
|
|
GetText("dice_rolled_num", Format.Bold(values.Count.ToString())) +
|
2017-02-21 02:04:22 +00:00
|
|
|
" " + 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);
|
2016-09-14 01:38:44 +00:00
|
|
|
}
|
2016-09-13 16:12:27 +00:00
|
|
|
|
2017-01-01 12:26:17 +00:00
|
|
|
private async Task InternallDndRoll(string arg, bool ordered)
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
2016-09-14 01:38:44 +00:00
|
|
|
Match match;
|
2016-12-24 09:55:51 +00:00
|
|
|
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
|
|
|
|
2016-12-24 09:55:51 +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)]);
|
2016-12-24 09:55:51 +00:00
|
|
|
}
|
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())))
|
2016-12-24 09:55:51 +00:00
|
|
|
.AddField(efb => efb.WithName(Format.Bold("Result"))
|
|
|
|
.WithValue(string.Join(" ", rolls.Select(c => Format.Code($"[{c}]")))));
|
2017-01-01 15:39:24 +00:00
|
|
|
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
|
2016-12-24 09:55:51 +00:00
|
|
|
}
|
|
|
|
else if ((match = dndRegex.Match(arg)).Length != 0)
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
2016-12-24 09:55:51 +00:00
|
|
|
var rng = new NadekoRandom();
|
2016-09-14 01:38:44 +00:00
|
|
|
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)
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
2016-10-19 03:45:35 +00:00
|
|
|
var add = 0;
|
|
|
|
var sub = 0;
|
|
|
|
int.TryParse(match.Groups["add"].Value, out add);
|
|
|
|
int.TryParse(match.Groups["sub"].Value, out sub);
|
|
|
|
|
2016-09-14 01:38:44 +00:00
|
|
|
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-09-14 01:38:44 +00:00
|
|
|
}
|
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)));
|
2017-01-01 15:39:24 +00:00
|
|
|
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
|
2016-09-13 16:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-29 22:00:19 +00:00
|
|
|
}
|
2016-08-20 17:13:29 +00:00
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-01-01 12:26:17 +00:00
|
|
|
public async Task NRoll([Remainder] string range)
|
2016-03-25 11:48:22 +00:00
|
|
|
{
|
2017-02-18 20:39:13 +00:00
|
|
|
int rolled;
|
|
|
|
if (range.Contains("-"))
|
2016-03-25 11:48:22 +00:00
|
|
|
{
|
2017-02-18 20:39:13 +00:00
|
|
|
var arr = range.Split('-')
|
|
|
|
.Take(2)
|
|
|
|
.Select(int.Parse)
|
|
|
|
.ToArray();
|
|
|
|
if (arr[0] > arr[1])
|
2016-09-14 01:38:44 +00:00
|
|
|
{
|
2017-02-18 20:39:13 +00:00
|
|
|
await ReplyErrorLocalized("second_larger_than_first").ConfigureAwait(false);
|
|
|
|
return;
|
2016-09-14 01:38:44 +00:00
|
|
|
}
|
2017-02-18 20:39:13 +00:00
|
|
|
rolled = new NadekoRandom().Next(arr[0], arr[1] + 1);
|
2016-03-25 11:48:22 +00:00
|
|
|
}
|
2017-02-18 20:39:13 +00:00
|
|
|
else
|
2016-04-01 17:05:15 +00:00
|
|
|
{
|
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)
|
2016-09-13 16:12:27 +00:00
|
|
|
{
|
2017-02-04 08:34:51 +00:00
|
|
|
if (num < 0 || num > 10)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(num));
|
2016-01-30 08:01:48 +00:00
|
|
|
|
2017-02-04 08:34:51 +00:00
|
|
|
if (num == 10)
|
2016-09-14 01:38:44 +00:00
|
|
|
{
|
2017-05-24 20:28:16 +00:00
|
|
|
var images = _images.Dice;
|
2017-05-29 04:13:22 +00:00
|
|
|
using (var imgOneStream = images[1].ToStream())
|
|
|
|
using (var imgZeroStream = images[0].ToStream())
|
2017-02-04 08:34:51 +00:00
|
|
|
{
|
2017-07-18 16:26:55 +00:00
|
|
|
var imgOne = Image.Load(imgOneStream);
|
|
|
|
var imgZero = Image.Load(imgZeroStream);
|
2016-09-13 16:12:27 +00:00
|
|
|
|
2017-02-04 08:34:51 +00:00
|
|
|
return new[] { imgOne, imgZero }.Merge();
|
|
|
|
}
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
using (var die = _images.Dice[num].ToStream())
|
2017-02-04 08:34:51 +00:00
|
|
|
{
|
2017-07-18 16:26:55 +00:00
|
|
|
return Image.Load(die);
|
2016-09-14 01:38:44 +00:00
|
|
|
}
|
2016-09-13 16:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-05 10:27:00 +00:00
|
|
|
}
|
|
|
|
}
|