コード例 #1
0
ファイル: ImportCommand.cs プロジェクト: DIVIY/TCGUABot
        public override async void Execute(Message message, TelegramBotClient client, ApplicationDbContext context)
        {
            var text = message.Text.Replace("/import ", "");
            var deck = ImportDeck.StringToDeck(text, null);

            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddJsonFile("appsettings.json");
            IConfiguration configuration = configurationBuilder.Build();
            var            baseAddress   = configuration.GetSection("TelegramSettings").GetSection("TelegramWebHookHostBase").Value;
            //HttpClient httpClient = new HttpClient();
            //httpClient.BaseAddress = new Uri(baseAddress);
            //HttpResponseMessage response = await httpClient.PostAsJsonAsync("/Decklist/Import", deck);
            var msg          = string.Empty;
            var telegramUser = context.UserLogins.FirstOrDefault(l => l.LoginProvider == "Telegram" && l.ProviderKey == message.From.Id.ToString());

            if (telegramUser == null)
            {
                msg       += "Вы не зарегистрированы на сайте. Деклист будет импортирован временно и может быть удалён в любое время\r\n";
                deck.Owner = context.Users.FirstOrDefault(u => u.Id == "d34f08f5-9daa-46d6-a87c-cc3a6fda538a");
            }
            else
            {
                deck.Owner = context.Users.FirstOrDefault(u => u.Id == telegramUser.UserId);
            }

            var deckListController = new DecklistController(context);

            if (deck.MainDeck.Count > 0)
            {
                try
                {
                    var id   = deckListController.Import(deck);
                    var link = string.Format(baseAddress, "Decks/Details?id=" + id);
                    msg += "Ссылка на деку: https://t.me/iv?url=" + HttpUtility.UrlEncode(link) + "&rhash=32913b8ff178b0";
                }
                catch
                {
                    msg = "❌Ошибка импорта деки.";
                }
            }
            else
            {
                msg = "❌Ошибка импорта деки.";
            }
            //response.EnsureSuccessStatusCode();

            //var id = await response.Content.ReadAsStringAsync();

            //var controller = new TCGUABot.Controllers.DecklistController();
            //var id = controller.Import(deck);
            var chatId = message.Chat.Id;

            //https://ace.od.ua:8443/decks/"+id
            await client.SendTextMessageAsync(chatId, msg, Telegram.Bot.Types.Enums.ParseMode.Html, replyToMessageId : message.MessageId);
        }
コード例 #2
0
        public override async void Execute(Message message, TelegramBotClient client, ApplicationDbContext context)
        {
            var text = message.Text.Replace("/timport ", "");
            var deck = new ImportDeck();

            deck.MainDeck  = new List <ImportCard>();
            deck.SideBoard = new List <ImportCard>();
            bool side = false;

            foreach (var myString in text.Split(new string[] { "\r\n" }, StringSplitOptions.None))
            {
                ImportCard card = new ImportCard();

                if (myString.Trim().Length > 1)
                {
                    var   cardData = myString.Split(" ");
                    Regex exp      = new Regex(@"(\d+)x?\s+(.*)\s*(\(.+\))?\s*(\d+)?");
                    var   matches  = exp.Matches(myString);

                    int.TryParse(matches[0].Groups[1].Value, out card.count);
                    card.name = matches[0].Groups[2].Value;
                    card.set  = matches[0].Groups[3].Value.Replace("DAR", "DOM");
                    int.TryParse(matches[0].Groups[4].Value, out card.collectorNumber);

                    if (side)
                    {
                        deck.SideBoard.Add(card);
                    }
                    else
                    {
                        deck.MainDeck.Add(card);
                    }
                }

                else
                {
                    side = true;
                }
            }

            var chatId = message.Chat.Id;

            string msg = string.Empty;

            msg += deck.MainDeck.Count + ":" + deck.SideBoard.Count;
            await client.SendTextMessageAsync(chatId, msg);
        }
コード例 #3
0
 public static string ToShortDeck(string cards)
 {
     if (cards != null)
     {
         var decklist = ImportDeck.StringToDeck(cards, null);
         var result   = string.Empty;
         result += "<div class=\"deck-short\"";
         result += "<p><b>Maindeck:</b><br/>";
         foreach (var card in decklist.MainDeck)
         {
             result += card.count + "x " +
                       "<a " +
                       "class=\"gathererTooltip\" " +
                       "data-image=\"https://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=" + card.multiverseId + "&type=card\" " +
                       "data-width=\"223px\"" +
                       "data-height=\"311px\"" +
                       "href=\"https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=" + card.multiverseId + "\"" +
                       ">" + card.name + "</a><br/>";
         }
         if (decklist.SideBoard.Count > 0)
         {
             result += "</p><p><b>Sideboard:</b><br/>";
             foreach (var card in decklist.SideBoard)
             {
                 result += card.count + "x " +
                           "<a " +
                           "class=\"gathererTooltip\" " +
                           "data-image=\"https://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=" + card.multiverseId + "&type=card\" " +
                           "data-width=\"223px\"" +
                           "data-height=\"311px\"" +
                           "href=\"https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=" + card.multiverseId + "\"" +
                           ">" + card.name + "</a><br/>";
             }
             result += "</p>";
         }
         result += "</div>";
         return(result);
     }
     else
     {
         return("No cards");
     }
 }
コード例 #4
0
ファイル: DecklistController.cs プロジェクト: DIVIY/TCGUABot
        public string Import([FromBody] ImportDeck deck)
        {

            if (deck.MainDeck.Count == 0)
            {
                throw new InvalidOperationException("Deck is empty or in wrong format.");
            }

            var deckId = Guid.NewGuid().ToString();

            var dbdeck = new Deck();

            dbdeck.ApplicationUser = deck.Owner;
            dbdeck.UserId = deck.Owner.Id;
            dbdeck.Name = deck.MainDeck[0].name;
            dbdeck.Id = deckId;
            dbdeck.Cards = deck.ToString();
            dbdeck.CreationDate = DateTime.UtcNow;
            context.Decks.Add(dbdeck);
            context.SaveChanges();
            
            return deckId;
        }
コード例 #5
0
 public static string Htmlize(string deck)
 {
     return(ImportDeck.HtmlizeString(deck));
 }
コード例 #6
0
ファイル: DecklistController.cs プロジェクト: DIVIY/TCGUABot
 public string HtmlizeStringDeck([FromBody] string deck)
 {
     var result = ImportDeck.HtmlizeString(deck);
     return result;
 }