Move the roll command logic into the shared commands project
This commit is contained in:
parent
89ea6df6e2
commit
78c139293f
3 changed files with 113 additions and 94 deletions
|
@ -1,14 +1,9 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Geekbot.Bot.Utils;
|
||||
using Geekbot.Commands.Roll;
|
||||
using Geekbot.Core;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.Database.Models;
|
||||
using Geekbot.Core.ErrorHandling;
|
||||
using Geekbot.Core.Extensions;
|
||||
using Geekbot.Core.GuildSettingsManager;
|
||||
using Geekbot.Core.KvInMemoryStore;
|
||||
using Geekbot.Core.RandomNumberGenerator;
|
||||
|
@ -36,47 +31,14 @@ namespace Geekbot.Bot.Commands.Games.Roll
|
|||
{
|
||||
try
|
||||
{
|
||||
var inputSpan = Transaction.StartChild("CommandInput");
|
||||
var number = _randomNumberGenerator.Next(1, 100);
|
||||
int.TryParse(stuff, out var guess);
|
||||
inputSpan.Finish();
|
||||
|
||||
if (guess <= 100 && guess > 0)
|
||||
{
|
||||
var prevRollCheckSpan = Transaction.StartChild("PrevRollCheck");
|
||||
var kvKey = $"{Context?.Guild?.Id ?? 0}:{Context.User.Id}:RollsPrevious";
|
||||
|
||||
var prevRoll = _kvInMemoryStore.Get<RollTimeout>(kvKey);
|
||||
|
||||
if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now)
|
||||
{
|
||||
await ReplyAsync(string.Format(
|
||||
Localization.Roll.NoPrevGuess,
|
||||
Context.Message.Author.Mention,
|
||||
DateLocalization.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1))));
|
||||
Transaction.Status = SpanStatus.InvalidArgument;
|
||||
return;
|
||||
}
|
||||
|
||||
_kvInMemoryStore.Set(kvKey, new RollTimeout {LastGuess = guess, GuessedOn = DateTime.Now});
|
||||
prevRollCheckSpan.Finish();
|
||||
|
||||
await ReplyAsync(string.Format(Localization.Roll.Rolled, Context.Message.Author.Mention, number, guess));
|
||||
if (guess == number)
|
||||
{
|
||||
var correctGuessSpan = Transaction.StartChild("CorrectGuess");
|
||||
await ReplyAsync(string.Format(Localization.Roll.Gratz, Context.Message.Author));
|
||||
var user = await GetUser(Context.User.Id);
|
||||
user.Rolls += 1;
|
||||
_database.Rolls.Update(user);
|
||||
await _database.SaveChangesAsync();
|
||||
correctGuessSpan.Finish();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplyAsync(string.Format(Localization.Roll.RolledNoGuess, Context.Message.Author.Mention, number));
|
||||
}
|
||||
var res = await new Geekbot.Commands.Roll.Roll(_kvInMemoryStore, _database, _randomNumberGenerator)
|
||||
.RunFromGateway(
|
||||
Context.Guild.Id,
|
||||
Context.User.Id,
|
||||
Context.User.Username,
|
||||
stuff ?? "0"
|
||||
);
|
||||
await ReplyAsync(res);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -84,24 +46,5 @@ namespace Geekbot.Bot.Commands.Games.Roll
|
|||
Transaction.Status = SpanStatus.InternalError;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<RollsModel> GetUser(ulong userId)
|
||||
{
|
||||
var user = _database.Rolls.FirstOrDefault(u => u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId);
|
||||
return user;
|
||||
}
|
||||
|
||||
private async Task<RollsModel> CreateNewRow(ulong userId)
|
||||
{
|
||||
var user = new RollsModel()
|
||||
{
|
||||
GuildId = Context.Guild.Id.AsLong(),
|
||||
UserId = userId.AsLong(),
|
||||
Rolls = 0
|
||||
};
|
||||
var newUser = _database.Rolls.Add(user).Entity;
|
||||
await _database.SaveChangesAsync();
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
}
|
95
src/Commands/Roll/Roll.cs
Normal file
95
src/Commands/Roll/Roll.cs
Normal file
|
@ -0,0 +1,95 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Geekbot.Core;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.Database.Models;
|
||||
using Geekbot.Core.Extensions;
|
||||
using Geekbot.Core.KvInMemoryStore;
|
||||
using Geekbot.Core.RandomNumberGenerator;
|
||||
|
||||
namespace Geekbot.Commands.Roll
|
||||
{
|
||||
public class Roll
|
||||
{
|
||||
private readonly IKvInMemoryStore _kvInMemoryStore;
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IRandomNumberGenerator _randomNumberGenerator;
|
||||
|
||||
public Roll(IKvInMemoryStore kvInMemoryStore, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator)
|
||||
{
|
||||
_kvInMemoryStore = kvInMemoryStore;
|
||||
_database = database;
|
||||
_randomNumberGenerator = randomNumberGenerator;
|
||||
}
|
||||
|
||||
public async Task<string> RunFromGateway(ulong guildId, ulong userId, string userName, string unparsedGuess)
|
||||
{
|
||||
int.TryParse(unparsedGuess, out var guess);
|
||||
return await this.Run(guildId, userId, userName, guess);
|
||||
}
|
||||
|
||||
public async Task<string> RunFromInteraction(string guildId, string userId, string userName, int guess)
|
||||
{
|
||||
|
||||
return await this.Run(ulong.Parse(guildId), ulong.Parse(userId), userName, guess);
|
||||
}
|
||||
|
||||
private async Task<string> Run(ulong guildId, ulong userId, string userName, int guess)
|
||||
{
|
||||
var number = _randomNumberGenerator.Next(1, 100);
|
||||
|
||||
if (guess <= 100 && guess > 0)
|
||||
{
|
||||
var kvKey = $"{guildId}:{userId}:RollsPrevious";
|
||||
var prevRoll = _kvInMemoryStore.Get<RollTimeout>(kvKey);
|
||||
|
||||
if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now)
|
||||
{
|
||||
return string.Format(
|
||||
Core.Localization.Roll.NoPrevGuess,
|
||||
$"<@{userId}>",
|
||||
DateLocalization.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1)));
|
||||
}
|
||||
|
||||
_kvInMemoryStore.Set(kvKey, new RollTimeout { LastGuess = guess, GuessedOn = DateTime.Now });
|
||||
|
||||
var answer = string.Format(Core.Localization.Roll.Rolled, $"<@{userId}>", number, guess);
|
||||
|
||||
if (guess == number)
|
||||
{
|
||||
var user = await GetUser(guildId, userId);
|
||||
user.Rolls += 1;
|
||||
_database.Rolls.Update(user);
|
||||
await _database.SaveChangesAsync();
|
||||
answer += string.Format(($"\n{Core.Localization.Roll.Gratz}"), userName);
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format(Core.Localization.Roll.RolledNoGuess, $"<@{userId}>", number);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<RollsModel> GetUser(ulong guildId, ulong userId)
|
||||
{
|
||||
var user = _database.Rolls.FirstOrDefault(u => u.GuildId.Equals(guildId) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(guildId, userId);
|
||||
return user;
|
||||
}
|
||||
|
||||
private async Task<RollsModel> CreateNewRow(ulong guildId, ulong userId)
|
||||
{
|
||||
var user = new RollsModel()
|
||||
{
|
||||
GuildId = guildId.AsLong(),
|
||||
UserId = userId.AsLong(),
|
||||
Rolls = 0
|
||||
};
|
||||
var newUser = _database.Rolls.Add(user).Entity;
|
||||
await _database.SaveChangesAsync();
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,4 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Geekbot.Commands.Roll;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.Interactions;
|
||||
using Geekbot.Core.Interactions.ApplicationCommand;
|
||||
|
@ -14,11 +12,13 @@ namespace Geekbot.Web.Commands
|
|||
public class Roll : InteractionBase
|
||||
{
|
||||
private readonly IKvInMemoryStore _kvInMemoryStore;
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IRandomNumberGenerator _randomNumberGenerator;
|
||||
|
||||
public Roll(IKvInMemoryStore kvInMemoryStore, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator)
|
||||
{
|
||||
_kvInMemoryStore = kvInMemoryStore;
|
||||
_database = database;
|
||||
_randomNumberGenerator = randomNumberGenerator;
|
||||
}
|
||||
|
||||
|
@ -47,39 +47,20 @@ namespace Geekbot.Web.Commands
|
|||
var guessOption = interaction.Data.Options.Find(o => o.Name == "guess");
|
||||
var guess = guessOption.Value.GetInt32();
|
||||
|
||||
var number = _randomNumberGenerator.Next(1, 100);
|
||||
|
||||
var replyContent = "";
|
||||
|
||||
if (guess <= 100 && guess > 0)
|
||||
{
|
||||
var kvKey = $"{interaction.GuildId}:{interaction.Member.User.Id}:RollsPrevious";
|
||||
var prevRoll = _kvInMemoryStore.Get<RollTimeout>(kvKey);
|
||||
|
||||
if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now)
|
||||
{
|
||||
replyContent = string.Format(
|
||||
":red_circle: {0}, you can't guess the same number again, guess another number or wait {1}",
|
||||
var res = await new Geekbot.Commands.Roll.Roll(_kvInMemoryStore, _database, _randomNumberGenerator)
|
||||
.RunFromInteraction(
|
||||
interaction.GuildId,
|
||||
interaction.Member.User.Id,
|
||||
interaction.Member.Nick ?? interaction.Member.User.Username,
|
||||
prevRoll.GuessedOn.AddDays(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
_kvInMemoryStore.Set(kvKey, new RollTimeout {LastGuess = guess, GuessedOn = DateTime.Now});
|
||||
replyContent = $"{interaction.Member?.User?.Mention}, you rolled {number}, your guess was {guess}";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
replyContent = $"{interaction?.Member?.User?.Mention}, you rolled {number}";
|
||||
}
|
||||
guess
|
||||
);
|
||||
|
||||
return new InteractionResponse()
|
||||
{
|
||||
Type = InteractionResponseType.ChannelMessageWithSource,
|
||||
Data = new InteractionResponseData()
|
||||
{
|
||||
Content = replyContent
|
||||
Content = res
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue