private UpsertResult <Set> UpsertSet(JsonSet set) { var result = new UpsertResult <Set>(); var dbSet = db.Sets.Local .Where(s => s.Name == set.Name) .FirstOrDefault(); if (dbSet == null) { dbSet = new Set() { Name = set.Name }; result.ObjectsToAdd.Add(dbSet); } dbSet.Code = set.Code; dbSet.KeyruneCode = set.KeyruneCode; dbSet.Date = set.ReleaseDate; dbSet.SetType = UpsertSimpleLookup(db.SetTypes, set.Type); dbSet.Block = UpsertSimpleLookup(db.Blocks, set.Block); foreach (var card in set.Cards) { var printingUpsert = this.UpsertPrinting(card); result.Merge(printingUpsert); if (printingUpsert.MainObject != null) { dbSet.Printings.Add(printingUpsert.MainObject); } } result.MainObject = dbSet; return(result); }
private UpsertResult <Card> UpsertCard(JsonCard printing) { var result = new UpsertResult <Card>(); var dbCard = db.Cards.Local .Where(c => c.Name == printing.CockatriceName) .SingleOrDefault(); if (dbCard == null) { dbCard = new Card() { Name = printing.CockatriceName }; result.ObjectsToAdd.Add(dbCard); } dbCard.CockatriceName = printing.Name; dbCard.ManaCost = printing.ManaCost; dbCard.CMC = printing.ConvertedManaCost; dbCard.TypeLine = printing.Type; dbCard.OracleText = printing.Text ?? string.Empty; dbCard.Power = printing.Power; dbCard.Toughness = printing.Toughness; dbCard.Loyalty = printing.Loyalty; dbCard.EDHRECRank = printing.EDHRECRank; printing.Types = printing.Types.ConvertAll(t => t.ToLower()); if (dbCard.Colors.Select(t => t.Color.Symbol).Except(printing.Colors).Any() || printing.Colors.Except(dbCard.Colors.Select(t => t.Color.Symbol)).Any()) { result.ObjectsToRemove.AddRange(dbCard.Colors); var colors = db.Colors.Where(c => printing.Colors.Contains(c.Symbol)); var cardColors = new List <CardColor>(); foreach (var color in colors) { cardColors.Add(new CardColor() { Card = dbCard, Color = color }); } result.ObjectsToAdd.AddRange(cardColors); } if (printing.Types.Contains("land") && !printing.Supertypes.Contains("Basic")) { if (printing.Text.Contains("plains", StringComparison.CurrentCultureIgnoreCase) && !printing.ColorIdentity.Contains("W")) { printing.ColorIdentity.Add("W"); } if (printing.Text.Contains("island", StringComparison.CurrentCultureIgnoreCase) && !printing.ColorIdentity.Contains("U")) { printing.ColorIdentity.Add("U"); } if (printing.Text.Contains("swamp", StringComparison.CurrentCultureIgnoreCase) && !printing.ColorIdentity.Contains("B")) { printing.ColorIdentity.Add("B"); } if (printing.Text.Contains("mountain", StringComparison.CurrentCultureIgnoreCase) && !printing.ColorIdentity.Contains("R")) { printing.ColorIdentity.Add("R"); } if (printing.Text.Contains("forest", StringComparison.CurrentCultureIgnoreCase) && !printing.ColorIdentity.Contains("G")) { printing.ColorIdentity.Add("G"); } } if (dbCard.ColorIdentity.Select(t => t.Color.Symbol).Except(printing.ColorIdentity).Any() || printing.ColorIdentity.Except(dbCard.ColorIdentity.Select(t => t.Color.Symbol)).Any()) { result.ObjectsToRemove.AddRange(dbCard.ColorIdentity); var colors = db.Colors.Where(c => printing.ColorIdentity.Contains(c.Symbol)); var cardColorIdentity = new List <CardColorIdentity>(); foreach (var color in colors) { cardColorIdentity.Add(new CardColorIdentity() { Card = dbCard, Color = color }); } result.ObjectsToAdd.AddRange(cardColorIdentity); } if (dbCard.Supertypes.Select(t => t.Supertype.Name).Except(printing.Supertypes).Any() || printing.Supertypes.Except(dbCard.Supertypes.Select(t => t.Supertype.Name)).Any()) { result.ObjectsToRemove.AddRange(dbCard.Supertypes); var supertypes = db.Supertypes.Where(t => printing.Supertypes.Contains(t.Name)); var cardSupertypes = new List <CardSupertype>(); foreach (var supertype in supertypes) { cardSupertypes.Add(new CardSupertype() { Card = dbCard, Supertype = supertype }); } result.ObjectsToAdd.AddRange(cardSupertypes); } if (dbCard.Types.Select(t => t.CardType.Name).Except(printing.Types).Any() || printing.Types.Except(dbCard.Types.Select(t => t.CardType.Name)).Any()) { result.ObjectsToRemove.AddRange(dbCard.Types); var cardTypes = db.CardTypes.Where(t => printing.Types.Contains(t.Name)); var cardCardTypes = new List <CardCardType>(); foreach (var cardType in cardTypes) { cardCardTypes.Add(new CardCardType() { Card = dbCard, CardType = cardType }); } result.ObjectsToAdd.AddRange(cardCardTypes); } if (dbCard.Subtypes.Select(t => t.Subtype.Name).Except(printing.Subtypes).Any() || printing.Subtypes.Except(dbCard.Subtypes.Select(t => t.Subtype.Name)).Any()) { result.ObjectsToRemove.AddRange(dbCard.Subtypes); var subtypes = db.Subtypes.Where(t => printing.Subtypes.Contains(t.Name)); var cardSubtypes = new List <CardSubtype>(); foreach (var subtype in subtypes) { cardSubtypes.Add(new CardSubtype() { Card = dbCard, Subtype = subtype }); } result.ObjectsToAdd.AddRange(cardSubtypes); } var keywords = printing.Keywords.Select(k => k.ToLower()); if (dbCard.Keywords.Select(k => k.Keyword.Name.ToLower()).Except(keywords).Any() || keywords.Except(dbCard.Keywords.Select(k => k.Keyword.Name.ToLower())).Any()) { result.ObjectsToRemove.AddRange(dbCard.Keywords); var dbKeywords = db.Keywords.Where(k => printing.Keywords.Contains(k.Name)); var cardKeywords = new List <CardKeyword>(); foreach (var keyword in dbKeywords) { cardKeywords.Add(new CardKeyword() { Card = dbCard, Keyword = keyword }); } result.ObjectsToAdd.AddRange(cardKeywords); } dbCard.Layout = UpsertSimpleLookup(db.Layouts, printing.Layout); var legalityUpsert = legalityHelper.UpsertLegalities(dbCard, printing.Legalities, printing.LeadershipSkills); dbCard.Legalities = legalityUpsert.MainObject; result.Merge(legalityUpsert); dbCard.Side = printing.Side; if (printing.Side == null || printing.Side == "a") { dbCard.MainSide = null; } result.MainObject = dbCard; return(result); }
private UpsertResult <Printing> UpsertPrinting(JsonCard printing) { var card = this.UpsertCard(printing); if (card.ObjectsToRemove.Count > 0) { db.RemoveRange(card.ObjectsToRemove); } if (card.ObjectsToAdd.Count > 0) { db.AddRange(card.ObjectsToAdd); } var result = new UpsertResult <Printing>(); if (!printing.Identifiers.MultiverseId.HasValue) { return(result); } var dbPrinting = db.Printings.Local .Where(p => p.MultiverseId == printing.Identifiers.MultiverseId.Value && p.Side == printing.Side) .FirstOrDefault(); if (dbPrinting == null) { dbPrinting = new Printing() { MultiverseId = printing.Identifiers.MultiverseId.Value, Side = printing.Side }; result.ObjectsToAdd.Add(dbPrinting); } dbPrinting.UUID = printing.UUID; dbPrinting.FlavorText = printing.FlavorText; dbPrinting.CollectorNumber = printing.Number; dbPrinting.Artist = UpsertSimpleLookup(db.Artists, printing.Artist); dbPrinting.Watermark = UpsertSimpleLookup(db.Watermarks, printing.Watermark); dbPrinting.Frame = UpsertSimpleLookup(db.Frames, printing.FrameVersion); dbPrinting.Rarity = UpsertSimpleLookup(db.Rarities, printing.Rarity); dbPrinting.Border = UpsertSimpleLookup(db.Borders, printing.BorderColor); if (printing.Prices?.Paper != null) { var pricingUpsert = UpsertPricing(dbPrinting, printing.Prices.Paper, false); result.Merge(pricingUpsert); } if (printing.Prices?.PaperFoil != null) { var pricingUpsert = UpsertPricing(dbPrinting, printing.Prices.PaperFoil, true); result.Merge(pricingUpsert); } dbPrinting.Card = card.MainObject; result.MainObject = dbPrinting; return(result); }
public UpsertResult <List <Legality> > UpsertLegalities(Card card, JsonLegality legality, JsonLeadership leadership) { var result = new UpsertResult <List <Legality> >() { MainObject = new List <Legality>() }; var brawl = this.UpsertLegality(card, "Brawl", IsLegal(legality.Brawl), IsLegal(legality.Brawl) && card.Types.Any(c => c.CardType.Name == "creature" || c.CardType.Name == "planeswalker") && card.Supertypes.Any(c => c.Supertype.Name == "Legendary")); result.MainObject.Add(brawl.MainObject); result.Merge(brawl); var commander = this.UpsertLegality(card, "Commander", IsLegal(legality.Commander), legality.Commander != "Banned" && leadership.Commander); result.MainObject.Add(commander.MainObject); result.Merge(commander); var duel = this.UpsertLegality(card, "Duel", IsLegal(legality.Duel), legality.Duel != "Banned" && leadership.Commander); result.MainObject.Add(duel.MainObject); result.Merge(duel); var frontier = this.UpsertLegality(card, "Frontier", IsLegal(legality.Frontier), false); result.MainObject.Add(frontier.MainObject); result.Merge(frontier); var historic = this.UpsertLegality(card, "Historic", IsLegal(legality.Historic), false); result.MainObject.Add(historic.MainObject); result.Merge(historic); var future = this.UpsertLegality(card, "Future", IsLegal(legality.Future), false); result.MainObject.Add(future.MainObject); result.Merge(future); var legacy = this.UpsertLegality(card, "Legacy", IsLegal(legality.Legacy), false); result.MainObject.Add(legacy.MainObject); result.Merge(legacy); var modern = this.UpsertLegality(card, "Modern", IsLegal(legality.Modern), false); result.MainObject.Add(modern.MainObject); result.Merge(modern); var pioneer = this.UpsertLegality(card, "Pioneer", IsLegal(legality.Pioneer), false); result.MainObject.Add(pioneer.MainObject); result.Merge(pioneer); var pauper = this.UpsertLegality(card, "Pauper", IsLegal(legality.Pauper), legality.Commander != "Banned" && card.Types.Any(c => c.CardType.Name == "creature") && card.Printings.Any(p => p.Rarity.Name == "uncommon")); result.MainObject.Add(pauper.MainObject); result.Merge(pauper); var penny = this.UpsertLegality(card, "Penny", IsLegal(legality.Penny), false); result.MainObject.Add(penny.MainObject); result.Merge(penny); var standard = this.UpsertLegality(card, "Standard", IsLegal(legality.Standard), false); result.MainObject.Add(standard.MainObject); result.Merge(standard); var vintage = this.UpsertLegality(card, "Vintage", IsLegal(legality.Vintage), false); result.MainObject.Add(vintage.MainObject); result.Merge(vintage); string obLegality = legality.Vintage; var OathbreakerBans = configuration.GetSection("BanLists:Oathbreaker").Get <List <string> >(); if (OathbreakerBans.Contains(card.Name)) { obLegality = "Banned"; } var oathbreaker = this.UpsertLegality(card, "Oathbreaker", IsLegal(obLegality), leadership.Oathbreaker); result.MainObject.Add(oathbreaker.MainObject); result.Merge(oathbreaker); string tlLegality = legality.Commander; var TinyLeadersBans = configuration.GetSection("BanLists:TinyLeaders").Get <List <string> >(); if (card.CMC > 3 || TinyLeadersBans.Contains(card.Name)) { tlLegality = "Banned"; } bool tlCmdrLegality = leadership.Commander; var TinyLeadersCmdrBans = configuration.GetSection("BanLists:TinyLeadersCmdr").Get <List <string> >(); if (card.CMC > 3 || TinyLeadersCmdrBans.Contains(card.Name)) { tlCmdrLegality = false; } var tinyLeaders = this.UpsertLegality(card, "TinyLeaders", IsLegal(tlLegality), tlCmdrLegality); result.MainObject.Add(tinyLeaders.MainObject); result.Merge(tinyLeaders); return(result); }