Пример #1
0
        private static string GeneratePictureFilenameFromSpecies(ISpecies species)
        {
            if (string.IsNullOrEmpty(species.GetPictureUrl()))
            {
                return(string.Empty);
            }

            string pictureFilename = GetPictureFilenameFromPictureUrl(species.GetPictureUrl());

            return(string.Format("{0}{1}", species.GetFullName().ToLower().Replace(' ', '_'), System.IO.Path.GetExtension(pictureFilename).ToLower()));
        }
Пример #2
0
        private async Task <string[]> UploadSpeciesGalleryAsync(MediaWikiClient client, EditHistory history, SQLiteDatabase database, ISpecies species)
        {
            // Upload all images in the given species' gallery.

            IEnumerable <IPicture> pictures = await database.GetPicturesAsync(species);

            List <string> uploadedFilenames = new List <string>();

            if (pictures != null)
            {
                foreach (IPicture picture in pictures)
                {
                    // Skip the image if it's the same as the species' default image, because we would've already uploaded it

                    if (picture.Url == species.GetPictureUrl())
                    {
                        continue;
                    }

                    string uploadedFilename = GeneratePictureFilenameFromSpecies(species, picture.Url);

                    if (!string.IsNullOrEmpty(uploadedFilename))
                    {
                        UploadParameters uploadParameters = new UploadParameters {
                            UploadFileName = uploadedFilename,
                            FilePath       = picture.Url,
                            Text           = picture.Description
                        };

                        uploadedFilename = await UploadPictureAsync(client, history, uploadParameters, true);

                        if (!string.IsNullOrEmpty(uploadedFilename))
                        {
                            uploadedFilenames.Add(uploadedFilename);
                        }
                    }
                    else
                    {
                        Log(string.Format("Failed to generate filename for picture: {0}", picture.Url));
                    }
                }
            }

            return(uploadedFilenames.ToArray());
        }
Пример #3
0
        public async Task Stats()
        {
            // Get this user's gotchi.

            Gotchi gotchi = await Db.GetGotchiAsync(Context.User.ToCreator());

            if (await this.ReplyValidateGotchiAsync(gotchi))
            {
                ISpecies sp = await Db.GetSpeciesAsync(gotchi.SpeciesId);

                if (sp.IsValid())
                {
                    // Calculate stats for this gotchi.
                    // If the user is currently in battle, show their battle stats instead.

                    GotchiStats stats;

                    GotchiBattleState battle_state = GotchiBattleState.GetBattleStateByUserId(Context.User.Id);

                    if (!(battle_state is null))
                    {
                        stats = battle_state.GetGotchiStats(gotchi);
                    }
                    else
                    {
                        stats = await new GotchiStatsCalculator(Db, Global.GotchiContext).GetStatsAsync(gotchi);
                    }

                    // Create the embed.

                    EmbedBuilder stats_page = new EmbedBuilder();

                    stats_page.WithTitle(string.Format("{0}'s {2}, **Level {1}** (Age {3})", Context.User.Username, stats.Level, TaxonFormatter.GetString(sp, false), gotchi.Age));
                    stats_page.WithThumbnailUrl(sp.GetPictureUrl());
                    stats_page.WithFooter(string.Format("{0} experience points until next level", stats.ExperienceToNextLevel));

                    stats_page.AddField("❤ Hit points", stats.Hp, inline: true);
                    stats_page.AddField("💥 Attack", stats.Atk, inline: true);
                    stats_page.AddField("🛡 Defense", stats.Def, inline: true);
                    stats_page.AddField("💨 Speed", stats.Spd, inline: true);

                    await ReplyAsync(embed : stats_page.Build());
                }
            }
Пример #4
0
        private static async Task <string> UploadSpeciesPictureAsync(MediaWikiClient client, EditHistory history, ISpecies species)
        {
            // Generate a filename for the image, which will be the filename when it's uploaded to the wiki.

            string uploadedFilename = GeneratePictureFilenameFromSpecies(species);

            if (!string.IsNullOrEmpty(uploadedFilename))
            {
                // Attempt to upload the image.

                UploadParameters uploadParameters = new UploadParameters {
                    UploadFileName = uploadedFilename,
                    FilePath       = species.GetPictureUrl()
                };

                return(await UploadPictureAsync(client, history, uploadParameters, true));
            }

            return(string.Empty);
        }
Пример #5
0
        public async Task <IPaginatedMessage> BuildSpeciesMessageAsync(ISpecies species)
        {
            if (!species.IsValid())
            {
                return(null);
            }

            Discord.Messaging.Embed embed = new Discord.Messaging.Embed {
                Title = species.GetFullName()
            };

            if (species.CommonNames.Count() > 0)
            {
                embed.Title += string.Format(" ({0})", string.Join(", ", species.CommonNames.Select(name => name.ToTitle())));
            }

            if (Config.GenerationsEnabled)
            {
                // Add a field for the generation.

                IGeneration gen = await Db.GetGenerationByDateAsync(species.CreationDate);

                embed.AddField("Gen", gen is null ? "???" : gen.Number.ToString(), inline: true);
            }

            // Add a field for the species owner.

            embed.AddField("Owner", await GetCreatorAsync(species.Creator), inline: true);

            // Add a field for the species' zones.

            IEnumerable <ISpeciesZoneInfo> speciesZoneList = (await Db.GetZonesAsync(species))
                                                             .Where(info => !info.Zone.Flags.HasFlag(ZoneFlags.Retired));

            string zonesFieldValue = speciesZoneList.ToString(ZoneListToStringOptions.Default, DiscordUtilities.MaxFieldLength);

            embed.AddField("Zone(s)", string.IsNullOrEmpty(zonesFieldValue) ? "None" : zonesFieldValue, inline: true);

            // Add the species' description.

            StringBuilder descriptionBuilder = new StringBuilder();

            if (species.IsExtinct())
            {
                embed.Title = "[EXTINCT] " + embed.Title;

                if (!string.IsNullOrEmpty(species.Status.Reason))
                {
                    descriptionBuilder.AppendLine(string.Format("**Extinct ({0}):** _{1}_\n", await BotUtils.TimestampToDateStringAsync(DateUtilities.GetTimestampFromDate((DateTimeOffset)species.Status.Date), BotContext), species.Status.Reason));
                }
            }

            descriptionBuilder.Append(species.GetDescriptionOrDefault());

            embed.Description = descriptionBuilder.ToString();

            // Add the species' picture.

            embed.ThumbnailUrl = species.GetPictureUrl();

            if (!string.IsNullOrEmpty(Config.WikiUrlFormat))
            {
                // Discord automatically encodes certain characters in URIs, which doesn't allow us to update the config via Discord when we have "{0}" in the URL.
                // Replace this with the proper string before attempting to call string.Format.

                string format = Config.WikiUrlFormat.Replace("%7B0%7D", "{0}");

                embed.Url = string.Format(format, Uri.EscapeUriString(WikiUtilities.GetWikiPageTitle(species)));
            }

            // Create embed pages.

            IEnumerable <Discord.Messaging.IEmbed> embedPages = EmbedUtilities.CreateEmbedPages(embed, EmbedPaginationOptions.AddPageNumbers | EmbedPaginationOptions.CopyFields);
            IPaginatedMessage paginatedMessage = new PaginatedMessage(embedPages);

            if (speciesZoneList.Count() > 0)
            {
                paginatedMessage.SetColor((await Db.GetZoneTypeAsync(speciesZoneList.GroupBy(x => x.Zone.TypeId).OrderBy(x => x.Count()).Last().Key)).Color);
            }

            if (species.IsExtinct())
            {
                paginatedMessage.SetColor(Color.Red);
            }

            return(paginatedMessage);
        }