public async Task PlusRelationship(string genus1, string species1, string genus2, string species2, string relationship) { // Get the relationship from the DB. Relationship relation = await GetRelationshipFromDbAsync(relationship); if (!await ReplyValidateRelationshipAsync(Context, relation)) { return; } // Get the species from the DB. ISpecies sp1 = await GetSpeciesOrReplyAsync(genus1, species1); if (!sp1.IsValid()) { return; } ISpecies sp2 = await GetSpeciesOrReplyAsync(genus2, species2); if (!sp2.IsValid()) { return; } if (sp1.Id == sp2.Id) { await BotUtils.ReplyAsync_Warning(Context, "A species cannot be in a relationship with itself."); return; } // Create the new relationship. using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR REPLACE INTO SpeciesRelationships(species1_id, species2_id, relationship_id) VALUES($species1_id, $species2_id, $relationship_id)")) { cmd.Parameters.AddWithValue("$species1_id", sp1.Id); cmd.Parameters.AddWithValue("$species2_id", sp2.Id); cmd.Parameters.AddWithValue("$relationship_id", relation.id); await Db.ExecuteNonQueryAsync(cmd); } await BotUtils.ReplyAsync_Success(Context, string.Format("**{0}** has successfully been set as a {1} of **{2}**.", sp2.GetShortName(), relation.BeneficiaryName(), sp1.GetShortName())); }
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()); }