예제 #1
0
파일: Spawn.cs 프로젝트: zsuatem/sanakan
        private Executable GetSafariExe(EmbedBuilder embed, IUserMessage msg, Card newCard,
                                        SafariImage pokeImage, ICharacterInfo character, ITextChannel trashChannel, IUser winner)
        {
            return(new Executable("safari", new Task(() =>
            {
                using (var db = new Database.UserContext(_config))
                {
                    var botUser = db.GetUserOrCreateAsync(winner.Id).Result;

                    newCard.FirstIdOwner = winner.Id;
                    newCard.Affection += botUser.GameDeck.AffectionFromKarma();
                    botUser.GameDeck.RemoveCharacterFromWishList(newCard.Character);

                    botUser.GameDeck.Cards.Add(newCard);
                    db.SaveChanges();

                    QueryCacheManager.ExpireTag(new string[] { $"user-{botUser.Id}", "users" });

                    using (var dba = new Database.AnalyticsContext(_config))
                    {
                        dba.UsersData.Add(new Database.Models.Analytics.UserAnalytics
                        {
                            Value = 1,
                            UserId = winner.Id,
                            MeasureDate = DateTime.Now,
                            GuildId = trashChannel?.Guild?.Id ?? 0,
                            Type = Database.Models.Analytics.UserAnalyticsEventType.Card
                        });
                        dba.SaveChanges();
                    }
                }

                _ = Task.Run(async() =>
                {
                    try
                    {
                        embed.ImageUrl = await _waifu.GetSafariViewAsync(pokeImage, newCard, trashChannel);
                        embed.Description = $"{winner.Mention} zdobył na polowaniu i wsadził do klatki:\n"
                                            + $"{newCard.GetString(false, false, true)}\n({newCard.Title})";
                        await msg.ModifyAsync(x => x.Embed = embed.Build());

                        var privEmb = new EmbedBuilder()
                        {
                            Color = EMType.Info.Color(),
                            Description = $"Na [polowaniu]({msg.GetJumpUrl()}) zdobyłeś: {newCard.GetString(false, false, true)}"
                        };

                        var priv = await winner.GetOrCreateDMChannelAsync();
                        if (priv != null)
                        {
                            await priv.SendMessageAsync("", false, privEmb.Build());
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Log($"In Safari: {ex}");
                    }
                });
            })));
        }
예제 #2
0
파일: Spawn.cs 프로젝트: fossabot/sanakan
        private void RunSafari(EmbedBuilder embed, IUserMessage msg, Card newCard,
                               SafariImage pokeImage, ICharacterInfo character, ITextChannel trashChannel)
        {
            _ = Task.Run(async() =>
            {
                try
                {
                    await Task.Delay(TimeSpan.FromMinutes(5));

                    var usersReacted = await msg.GetReactionUsersAsync(ClaimEmote, 300).FlattenAsync();
                    var users        = usersReacted.ToList();

                    IUser winner = null;
                    using (var db = new Database.UserContext(_config))
                    {
                        var watch = Stopwatch.StartNew();
                        while (winner == null)
                        {
                            if (watch.ElapsedMilliseconds > 60000)
                            {
                                throw new Exception("Timeout");
                            }

                            if (users.Count < 1)
                            {
                                embed.Description = $"Na polowanie nie stawił się żaden łowca!";
                                await msg.ModifyAsync(x => x.Embed = embed.Build());
                                return;
                            }

                            var selected = Fun.GetOneRandomFrom(users);
                            var dUser    = await db.GetCachedFullUserAsync(selected.Id);

                            if (dUser != null)
                            {
                                if (!dUser.IsBlacklisted)
                                {
                                    winner = selected;
                                }
                            }
                            else
                            {
                                users.Remove(selected);
                            }
                        }
                    }

                    var exe = GetSafariExe(embed, msg, newCard, pokeImage, character, trashChannel, winner);
                    await _executor.TryAdd(exe, TimeSpan.FromSeconds(1));
                    await msg.RemoveAllReactionsAsync();
                }
                catch (Exception ex)
                {
                    _logger.Log($"In Safari: {ex}");
                    await msg.ModifyAsync(x => x.Embed = "Karta uciekła!".ToEmbedMessage(EMType.Error).Build());
                    await msg.RemoveAllReactionsAsync();
                }
            });
        }
예제 #3
0
파일: Waifu.cs 프로젝트: zsuatem/sanakan
        public async Task <string> GetSafariViewAsync(SafariImage info, ITextChannel trashChannel)
        {
            string uri = info != null?info.Uri(SafariImage.Type.Mystery) : SafariImage.DefaultUri(SafariImage.Type.Mystery);

            var msg = await trashChannel.SendFileAsync(uri);

            return(msg.Attachments.First().Url);
        }
예제 #4
0
파일: Waifu.cs 프로젝트: zsuatem/sanakan
        public SafariImage GetRandomSarafiImage()
        {
            SafariImage dImg   = null;
            var         reader = new Config.JsonFileReader($"./Pictures/Poke/List.json");

            try
            {
                var images = reader.Load <List <SafariImage> >();
                dImg = Fun.GetOneRandomFrom(images);
            }
            catch (Exception) { }

            return(dImg);
        }
예제 #5
0
파일: Spawn.cs 프로젝트: fossabot/sanakan
        private Executable GetSafariExe(EmbedBuilder embed, IUserMessage msg, Card newCard,
                                        SafariImage pokeImage, ICharacterInfo character, ITextChannel trashChannel, IUser winner)
        {
            return(new Executable("safari", new Task(() =>
            {
                using (var db = new Database.UserContext(_config))
                {
                    var botUser = db.GetUserOrCreateAsync(winner.Id).Result;
                    botUser.GameDeck.Cards.Add(newCard);
                    db.SaveChanges();

                    QueryCacheManager.ExpireTag(new string[] { $"user-{botUser.Id}", "users" });
                }

                _ = Task.Run(async() =>
                {
                    try
                    {
                        embed.ImageUrl = await _waifu.GetSafariViewAsync(pokeImage, character, newCard, trashChannel);
                        embed.Description = $"{winner.Mention} zdobył na polowaniu i wsadził do klatki:\n"
                                            + $"{newCard.GetString(false, false, true)}\n({newCard.Title})";
                        await msg.ModifyAsync(x => x.Embed = embed.Build());

                        var privEmb = new EmbedBuilder()
                        {
                            Color = EMType.Info.Color(),
                            Description = $"Na [polowaniu]({msg.GetJumpUrl()}) zdobyłeś: {newCard.GetString(false, false, true)}"
                        };

                        var priv = await winner.GetOrCreateDMChannelAsync();
                        if (priv != null)
                        {
                            await priv.SendMessageAsync("", false, privEmb.Build());
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Log($"In Safari: {ex}");
                    }
                });
            })));
        }
예제 #6
0
파일: Waifu.cs 프로젝트: zsuatem/sanakan
        public async Task <string> GetSafariViewAsync(SafariImage info, Card card, ITextChannel trashChannel)
        {
            string uri = info != null?info.Uri(SafariImage.Type.Truth) : SafariImage.DefaultUri(SafariImage.Type.Truth);

            var cardUri = await GetCardUrlIfExistAsync(card);

            using (var cardImage = await _img.GetWaifuCardAsync(cardUri, card))
            {
                int posX = info != null?info.GetX() : SafariImage.DefaultX();

                int posY = info != null?info.GetY() : SafariImage.DefaultY();

                using (var pokeImage = _img.GetCatchThatWaifuImage(cardImage, uri, posX, posY))
                {
                    using (var stream = pokeImage.ToJpgStream())
                    {
                        var msg = await trashChannel.SendFileAsync(stream, $"poke.jpg");

                        return(msg.Attachments.First().Url);
                    }
                }
            }
        }