geekbot/Geekbot.net/Commands/Randomness/Ship.cs

102 lines
3.7 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
2019-05-11 01:18:22 +02:00
using Geekbot.net.Lib.RandomNumberGenerator;
namespace Geekbot.net.Commands.Randomness
{
public class Ship : ModuleBase
{
2017-12-29 01:53:50 +01:00
private readonly IErrorHandler _errorHandler;
2019-05-11 01:18:22 +02:00
private readonly IRandomNumberGenerator _randomNumberGenerator;
private readonly DatabaseContext _database;
2017-09-15 22:56:03 +02:00
2019-05-11 01:18:22 +02:00
public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator)
{
_database = database;
_errorHandler = errorHandler;
2019-05-11 01:18:22 +02:00
_randomNumberGenerator = randomNumberGenerator;
}
2017-09-15 22:56:03 +02:00
[Command("Ship", RunMode = RunMode.Async)]
[Summary("Ask the Shipping meter")]
public async Task Command([Summary("@user1")] IUser user1, [Summary("@user2")] IUser user2)
{
try
{
var userKeys = user1.Id < user2.Id
? new Tuple<long, long>(user1.Id.AsLong(), user2.Id.AsLong())
: new Tuple<long, long>(user2.Id.AsLong(), user1.Id.AsLong());
var dbval = _database.Ships.FirstOrDefault(s =>
s.FirstUserId.Equals(userKeys.Item1) &&
s.SecondUserId.Equals(userKeys.Item2));
var shippingRate = 0;
if (dbval == null)
{
2019-05-11 01:18:22 +02:00
shippingRate = _randomNumberGenerator.Next(1, 100);
_database.Ships.Add(new ShipsModel()
{
FirstUserId = userKeys.Item1,
SecondUserId = userKeys.Item2,
Strength = shippingRate
});
2018-05-14 18:57:07 +02:00
await _database.SaveChangesAsync();
}
else
{
shippingRate = dbval.Strength;
}
var reply = ":heartpulse: **Matchmaking** :heartpulse:\r\n";
reply += $":two_hearts: {user1.Mention} :heart: {user2.Mention} :two_hearts:\r\n";
reply += $"0% [{BlockCounter(shippingRate)}] 100% - {DeterminateSuccess(shippingRate)}";
await ReplyAsync(reply);
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
}
}
private string DeterminateSuccess(int rate)
{
if (rate < 20)
return "Not gonna happen";
2017-09-15 22:56:03 +02:00
if (rate >= 20 && rate < 40)
return "Not such a good idea";
2017-09-15 22:56:03 +02:00
if (rate >= 40 && rate < 60)
return "There might be a chance";
2017-09-15 22:56:03 +02:00
if (rate >= 60 && rate < 80)
return "Almost a match, but could work";
2018-04-30 23:44:19 +02:00
return rate >= 80 ? "It's a match" : "a";
}
private string BlockCounter(int rate)
{
var amount = Math.Floor(decimal.Floor(rate / 10));
Console.WriteLine(amount);
var blocks = "";
2017-09-15 22:56:03 +02:00
for (var i = 1; i <= 10; i++)
if (i <= amount)
{
blocks += ":white_medium_small_square:";
2017-09-15 22:56:03 +02:00
if (i == amount)
blocks += $" {rate}% ";
2017-09-15 22:56:03 +02:00
}
else
{
blocks += ":black_medium_small_square:";
}
2017-12-29 01:53:50 +01:00
return blocks;
}
}
2017-05-15 21:14:38 +02:00
}