Exemplo n.º 1
0
        public async Task Relationships([Remainder] string speciesName)
        {
            // Get the species from the DB.

            speciesName = StringUtilities.StripOuterQuotes(speciesName);

            ISpecies sp = await GetSpeciesOrReplyAsync(speciesName);

            if (!sp.IsValid())
            {
                return;
            }

            // Get relationships and build the embed.

            SortedDictionary <string, List <string> > items = new SortedDictionary <string, List <string> >();

            // Get relationships where this species is the one acting upon another.

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM SpeciesRelationships LEFT JOIN Relationships ON SpeciesRelationships.relationship_id = Relationships.id WHERE species1_id=$species_id;")) {
                cmd.Parameters.AddWithValue("$species_id", sp.Id);

                foreach (DataRow row in await Db.GetRowsAsync(cmd))
                {
                    long     other_species_id = row.Field <long>("species2_id");
                    ISpecies other_species    = await Db.GetSpeciesAsync(other_species_id);

                    Relationship relationship = Relationship.FromDataRow(row);

                    if (other_species is null)
                    {
                        continue;
                    }

                    if (!items.ContainsKey(relationship.BeneficiaryName(plural: true)))
                    {
                        items[relationship.BeneficiaryName(plural: true)] = new List <string>();
                    }

                    items[relationship.BeneficiaryName(plural: true)].Add(TaxonFormatter.GetString(other_species));
                }
            }

            // Get relationships where this species is the one being acted upon.

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM SpeciesRelationships LEFT JOIN Relationships ON SpeciesRelationships.relationship_id = Relationships.id WHERE species2_id=$species_id;")) {
                cmd.Parameters.AddWithValue("$species_id", sp.Id);

                foreach (DataRow row in await Db.GetRowsAsync(cmd))
                {
                    long     other_species_id = row.Field <long>("species1_id");
                    ISpecies other_species    = await Db.GetSpeciesAsync(other_species_id);

                    Relationship relationship = Relationship.FromDataRow(row);

                    if (other_species is null)
                    {
                        continue;
                    }

                    if (!items.ContainsKey(relationship.BenefactorName(plural: true)))
                    {
                        items[relationship.BenefactorName(plural: true)] = new List <string>();
                    }

                    items[relationship.BenefactorName(plural: true)].Add(TaxonFormatter.GetString(other_species));
                }
            }

            // Get any prey/predator relationships.

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Predates WHERE species_id=$species_id;")) {
                cmd.Parameters.AddWithValue("$species_id", sp.Id);

                foreach (DataRow row in await Db.GetRowsAsync(cmd))
                {
                    long     other_species_id = row.Field <long>("eats_id");
                    ISpecies other_species    = await Db.GetSpeciesAsync(other_species_id);

                    if (other_species is null)
                    {
                        continue;
                    }

                    if (!items.ContainsKey("prey"))
                    {
                        items["prey"] = new List <string>();
                    }

                    items["prey"].Add(TaxonFormatter.GetString(other_species));
                }
            }

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Predates WHERE eats_id=$species_id;")) {
                cmd.Parameters.AddWithValue("$species_id", sp.Id);

                foreach (DataRow row in await Db.GetRowsAsync(cmd))
                {
                    long     other_species_id = row.Field <long>("species_id");
                    ISpecies other_species    = await Db.GetSpeciesAsync(other_species_id);

                    if (other_species is null)
                    {
                        continue;
                    }

                    if (!items.ContainsKey("predators"))
                    {
                        items["predators"] = new List <string>();
                    }

                    items["predators"].Add(TaxonFormatter.GetString(other_species));
                }
            }

            // If the species does not have any relationships with other species, state so.

            if (items.Count() <= 0)
            {
                await BotUtils.ReplyAsync_Info(Context, string.Format("**{0}** does not have any relationships with other species.", sp.GetShortName()));

                return;
            }

            // Build the embed.

            EmbedBuilder embed = new EmbedBuilder();
            int          relationship_count = 0;

            foreach (string key in items.Keys)
            {
                items[key].Sort((lhs, rhs) => lhs.CompareTo(rhs));

                embed.AddField(string.Format("{0} ({1})", StringUtilities.ToTitleCase(key), items[key].Count()), string.Join(Environment.NewLine, items[key]), inline: true);

                relationship_count += items[key].Count();
            }

            embed.WithTitle(string.Format("Relationships involving {0} ({1})", TaxonFormatter.GetString(sp, false), relationship_count));

            await ReplyAsync("", false, embed.Build());
        }