geekbot/Geekbot.net/Commands/Integrations/MagicTheGathering.cs

95 lines
3.7 KiB
C#
Raw Normal View History

2017-12-24 14:54:35 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib.Converters;
using Geekbot.net.Lib.ErrorHandling;
2018-05-17 22:06:58 +02:00
using Geekbot.net.Lib.Extensions;
2017-12-24 14:54:35 +01:00
using MtgApiManager.Lib.Service;
namespace Geekbot.net.Commands.Integrations
2017-12-24 14:54:35 +01:00
{
2018-05-17 22:06:58 +02:00
public class MagicTheGathering : ModuleBase
2017-12-24 14:54:35 +01:00
{
2017-12-29 01:53:50 +01:00
private readonly IErrorHandler _errorHandler;
2018-02-19 21:35:45 +01:00
private readonly IMtgManaConverter _manaConverter;
2017-12-24 14:54:35 +01:00
2018-05-17 22:06:58 +02:00
public MagicTheGathering(IErrorHandler errorHandler, IMtgManaConverter manaConverter)
2017-12-24 14:54:35 +01:00
{
_errorHandler = errorHandler;
2018-02-19 21:35:45 +01:00
_manaConverter = manaConverter;
2017-12-24 14:54:35 +01:00
}
[Command("mtg", RunMode = RunMode.Async)]
[Summary("Find a Magic The Gathering Card.")]
public async Task GetCard([Remainder] [Summary("card-name")] string cardName)
2017-12-24 14:54:35 +01:00
{
try
{
var service = new CardService();
2019-03-02 07:21:42 +01:00
var result = service
.Where(x => x.Name, cardName)
// fewer cards less risk of deserialization problems, don't need more than one anyways...
// ToDo: fix the deserialization issue in card[n].foreignNames[]
.Where(x => x.PageSize, 1);
2017-12-24 14:54:35 +01:00
var card = result.All().Value.FirstOrDefault();
if (card == null)
{
await ReplyAsync("I couldn't find that card...");
return;
}
2017-12-29 01:53:50 +01:00
2017-12-24 14:54:35 +01:00
var eb = new EmbedBuilder();
eb.Title = card.Name;
eb.Description = card.Type;
2017-12-29 01:53:50 +01:00
2017-12-24 14:54:35 +01:00
if (card.Colors != null) eb.WithColor(GetColor(card.Colors));
if (card.ImageUrl != null) eb.ImageUrl = card.ImageUrl.ToString();
2018-02-19 21:35:45 +01:00
if (!string.IsNullOrEmpty(card.Text)) eb.AddField("Text", _manaConverter.ConvertMana(card.Text));
2017-12-29 01:53:50 +01:00
2017-12-24 14:54:35 +01:00
if (!string.IsNullOrEmpty(card.Flavor)) eb.AddField("Flavor", card.Flavor);
if (!string.IsNullOrEmpty(card.SetName)) eb.AddInlineField("Set", card.SetName);
if (!string.IsNullOrEmpty(card.Power)) eb.AddInlineField("Power", card.Power);
if (!string.IsNullOrEmpty(card.Loyalty)) eb.AddInlineField("Loyality", card.Loyalty);
if (!string.IsNullOrEmpty(card.Toughness)) eb.AddInlineField("Thoughness", card.Toughness);
2017-12-29 01:53:50 +01:00
2018-02-19 21:35:45 +01:00
if (!string.IsNullOrEmpty(card.ManaCost)) eb.AddInlineField("Cost", _manaConverter.ConvertMana(card.ManaCost));
2017-12-24 14:54:35 +01:00
if (!string.IsNullOrEmpty(card.Rarity)) eb.AddInlineField("Rarity", card.Rarity);
2017-12-29 01:53:50 +01:00
if (card.Legalities != null && card.Legalities.Count > 0)
2017-12-29 01:53:50 +01:00
eb.AddField("Legality", string.Join(", ", card.Legalities.Select(e => e.Format)));
2017-12-24 14:54:35 +01:00
await ReplyAsync("", false, eb.Build());
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-12-24 14:54:35 +01:00
}
}
private Color GetColor(IEnumerable<string> colors)
{
var color = colors.FirstOrDefault();
switch (color)
{
case "Black":
2018-04-28 01:28:48 +02:00
return new Color(203, 194, 191);
2017-12-24 14:54:35 +01:00
case "White":
2018-04-28 01:28:48 +02:00
return new Color(255, 251, 213);
2017-12-24 14:54:35 +01:00
case "Blue":
2018-04-28 01:28:48 +02:00
return new Color(170, 224, 250);
2017-12-24 14:54:35 +01:00
case "Red":
2018-04-28 01:28:48 +02:00
return new Color(250, 170, 143);
2017-12-24 14:54:35 +01:00
case "Green":
2018-04-28 01:28:48 +02:00
return new Color(155, 211, 174);
2017-12-24 14:54:35 +01:00
default:
2018-04-28 01:28:48 +02:00
return new Color(204, 194, 212);
2017-12-24 14:54:35 +01:00
}
}
}
}