public PackContent GetCards(Pack pack, Set set) { var ret = new PackContent(); if (Quantity < 0) { ret.UnlimitedCards.AddRange( from card in set.Cards where card.Properties.SelectMany(x=>x.Value.Properties).Any( x => x.Key.Name.ToLower() ==Key.ToLower() && x.Value.ToString().ToLower() ==Value.ToLower()) select card); } else { var list = ( from card in set.Cards where card.Properties.SelectMany(x=>x.Value.Properties).Any( x => x.Key.Name.ToLower() ==Key.ToLower() && x.Value.ToString().ToLower() ==Value.ToLower()) select card).ToArray(); for (var i = 0; i <Quantity; i++) ret.LimitedCards.Add(list.RandomElement()); } return ret; }
public PackContent GenerateContent(Pack pack, Set set) { var result = new PackContent(); foreach (PackContent defContent in Items.Select(def => def.GetCards(pack,set))) { result.Merge(defContent); } return result; }
public PackContent GetCards(Pack pack, Set set) { var rnd = new Random(); double value = rnd.NextDouble(); double threshold = 0; foreach (Option option in Options) { threshold += option.Probability; if (value <= threshold) return option.Definition.GenerateContent(pack,set); } return new PackContent(); // Empty pack }
public Pack FromXml(Set set, string xml) { var ret = new Pack(); using (var stringReader = new StringReader(xml)) using (XmlReader reader = XmlReader.Create(stringReader)) { reader.Read(); string guid = reader.GetAttribute("id"); if (guid != null) ret.Id = new Guid(guid); ret.Name = reader.GetAttribute("name"); reader.ReadStartElement("pack"); ret.Definition = new PackDefinition(reader); reader.ReadEndElement(); // </pack> } return ret; }
public PackContent GetCards(Pack pack, Set set) { var ret = new PackContent(); if (Quantity < 0) { ret.UnlimitedCards.AddRange( from card in set.Cards where card.Properties.SelectMany(x=>x.Value.Properties).Any( x => x.Key.Name.ToLower() ==Key.ToLower() && x.Value.ToString().ToLower() ==Value.ToLower()) select card); } else { var list = ( from card in set.Cards where card.Properties.Where(x=> x.Key == "").SelectMany(x=>x.Value.Properties).Any( x => x.Key.Name.ToLower() ==Key.ToLower() && x.Value.ToString().ToLower() ==Value.ToLower()) select card).ToList(); for (var i = 0; i < Quantity; i++) { var pick = list.RandomElement(); if (pick != null) { ret.LimitedCards.Add(pick); list.Remove(pick); } else { Log.Warn(String.Format("Set {0} ({1}) does not contain enough cards to create this booster pack correctly.", set.Id, set.Name)); } } } return ret; }
public Set FromXmlReader(string packageName, XmlReader reader) { var ret = new Set(); ret.PackageName = packageName; ret.Name = reader.GetAttribute("name"); string gaid = reader.GetAttribute("id"); if (gaid != null) ret.Id = new Guid(gaid); string gi = reader.GetAttribute("gameId"); if (gi != null) { var gameId = new Guid(gi); ret.GameId = GameManager.Get().GetById(gameId).Id; } string gv = reader.GetAttribute("gameVersion"); if (gv != null) ret.GameVersion = new Version(gv); Version ver; Version.TryParse(reader.GetAttribute("version"), out ver); ret.Version = ver ?? new Version(0, 0); reader.ReadStartElement("set"); ret.Markers = new List<Marker>(); ret.Packs = new List<Pack>(); ret.Cards = new List<Card>(); return ret; }
public Card FromXmlReader(XmlReader reader, Game game, Set set, PackagePart definition, Package package) { var ret = new Card(); var Properties = new SortedList<PropertyDef, object>(game.CustomProperties.Count()); reader.MoveToAttribute("name"); ret.Name = reader.Value; reader.MoveToAttribute("id"); ret.Id = new Guid(reader.Value); // isMutable = false; if (reader.MoveToAttribute("alternate")) { try { ret.Alternate = new Guid(reader.Value); } catch (Exception e) { throw new ArgumentException(String.Format("The value {0} is not of expected type for property Alternate. Alternate must be a GUID.", reader.Value)); } } else { ret.Alternate = System.Guid.Empty; } if (reader.MoveToAttribute("dependent")) { try { ret.Dependent = new Guid(reader.Value).ToString(); } catch { try { ret.Dependent = Boolean.Parse(reader.Value).ToString(); } catch { throw new ArgumentException(String.Format("The value {0} is not of expected type for property Dependent. Dependent must be either true/false, or the Guid of the card to use instead.", reader.Value)); } } } else { ret.Dependent = String.Empty; } Uri cardImageUri = definition.GetRelationship("C" + ret.Id.ToString("N")).TargetUri; ret.ImageUri = cardImageUri.OriginalString; if (!package.PartExists(cardImageUri)) throw new Exception(string.Format("Image for card '{0}', with URI '{1}' was not found in the package.", ret.Name, ret.ImageUri)); reader.Read(); // <card> while (reader.IsStartElement("property")) { reader.MoveToAttribute("name"); PropertyDef prop = game.CustomProperties.FirstOrDefault(p => p.Name == reader.Value); if (prop == null) throw new ArgumentException(string.Format("The property '{0}' is unknown", reader.Value)); reader.MoveToAttribute("value"); try { switch (prop.Type) { case PropertyType.String: Properties.Add(prop, reader.Value); break; case PropertyType.Integer: Properties.Add(prop, Int32.Parse(reader.Value)); break; case PropertyType.Char: Properties.Add(prop, Char.Parse(reader.Value)); break; default: throw new NotImplementedException(); } } catch (FormatException) { throw new ArgumentException(String.Format("The value {0} is not of expected type for property {1}", reader.Value, prop.Name)); } reader.Read(); // <property/> } ret.Properties = Properties; reader.Read(); // </card> return ret; }
public object Deserialize(string fileName) { //var timer = new Stopwatch(); //timer.Start(); var ret = new Set(); var directory = new FileInfo(fileName).Directory.FullName; using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { var settings = new XmlReaderSettings(); settings.Schemas.Add(SetSchema); var doc = XDocument.Load(fs); doc.Validate(settings.Schemas, (a, b) => { Console.WriteLine(b.Exception); }); ret.Cards = new List<Card>(); var root = doc.Element("set"); ret.Id = new Guid(root.Attribute("id").Value); ret.Name = root.Attribute("name").Value; ret.Filename = fileName; ret.GameId = new Guid(root.Attribute("gameId").Value); ret.Cards = new List<Card>(); ret.GameVersion = new Version(root.Attribute("gameVersion").Value); ret.Markers = new List<Marker>(); ret.Packs = new List<Pack>(); ret.Version = new Version(root.Attribute("version").Value); ret.PackageName = ""; ret.InstallPath = directory; ret.DeckPath = Path.Combine(ret.InstallPath, "Decks"); ret.PackUri = Path.Combine(ret.InstallPath, "Cards"); var gameImageInstallPath = Path.Combine(Config.Instance.Paths.ImageDatabasePath, ret.GameId.ToString()); ret.ImageInstallPath = Path.Combine(gameImageInstallPath, "Sets", ret.Id.ToString()); ret.ImagePackUri = Path.Combine(ret.ImageInstallPath, "Cards"); ret.ProxyPackUri = Path.Combine(ret.ImagePackUri, "Proxies"); if (!Directory.Exists(ret.PackUri)) Directory.CreateDirectory(ret.PackUri); if (!Directory.Exists(ret.ImagePackUri)) Directory.CreateDirectory(ret.ImagePackUri); if (!Directory.Exists(ret.ProxyPackUri)) Directory.CreateDirectory(ret.ProxyPackUri); var game = DbContext.Get().Games.First(x => x.Id == ret.GameId); foreach (var c in doc.Document.Descendants("card")) { var card = new Card(new Guid(c.Attribute("id").Value), ret.Id, c.Attribute("name").Value,c.Attribute("id").Value, "",game.CardSizes["Default"],new Dictionary<string, CardPropertySet>()); //var card = new Card // { // Id = new Guid(c.Attribute("id").Value), // Name = c.Attribute("name").Value, // SetId = ret.Id, // Properties = new Dictionary<string, CardPropertySet>(), // ImageUri = c.Attribute("id").Value, // Alternate = "", // Size = game.CardSizes["Default"] // }; var cs = c.Attribute("size"); if (cs != null) { if(game.CardSizes.ContainsKey(cs.Value) == false) throw new UserMessageException(Octgn.Library.Localization.L.D.Exception__BrokenGameContactDev_Format, game.Name); card.Size = game.CardSizes[cs.Value]; } var defaultProperties = new CardPropertySet(); defaultProperties.Type = ""; defaultProperties.Properties = new Dictionary<PropertyDef, object>(); foreach (var p in c.Descendants("property").Where(x => x.Parent.Name == "card")) { var pd = game.CustomProperties.FirstOrDefault(x => x.Name == p.Attribute("name").Value); if (pd == null) { throw new UserMessageException(Octgn.Library.Localization.L.D.Exception__BrokenGameContactDev_Format, game.Name); } var newpd = pd.Clone() as PropertyDef; defaultProperties.Properties.Add(newpd, p.Attribute("value").Value); } foreach (var cp in game.CustomProperties) { if (!defaultProperties.Properties.ContainsKey(cp)) { var cpnew = cp.Clone() as PropertyDef; cpnew.IsUndefined = true; defaultProperties.Properties.Add(cpnew, ""); } } var np = new PropertyDef() { Hidden = false, Name = "Name", Type = PropertyType.String, TextKind = PropertyTextKind.FreeText, IgnoreText = false, IsUndefined = false }; if (defaultProperties.Properties.ContainsKey(np)) defaultProperties.Properties.Remove(np); defaultProperties.Properties.Add(np, card.Name); card.Properties.Add("", defaultProperties); // Add all of the other property sets foreach (var a in c.Descendants("alternate")) { var propset = new CardPropertySet(); propset.Properties = new Dictionary<PropertyDef, object>(); propset.Type = a.Attribute("type").Value; var thisName = a.Attribute("name").Value; foreach (var p in a.Descendants("property")) { var pd = game.CustomProperties.First(x => x.Name.Equals(p.Attribute("name").Value, StringComparison.InvariantCultureIgnoreCase)); var newprop = pd.Clone() as PropertyDef; var val = p.Attribute("value").Value; propset.Properties.Add(newprop, val); } foreach (var cp in game.CustomProperties) { if (!propset.Properties.ContainsKey(cp)) { var cpnew = cp.Clone() as PropertyDef; cpnew.IsUndefined = true; propset.Properties.Add(cpnew, ""); } } var np2 = new PropertyDef() { Hidden = false, Name = "Name", Type = PropertyType.String, TextKind = PropertyTextKind.FreeText, IgnoreText = false, IsUndefined = false }; if (propset.Properties.ContainsKey(np2)) propset.Properties.Remove(np2); propset.Properties.Add(np2, thisName); card.Properties.Add(propset.Type, propset); } (ret.Cards as List<Card>).Add(card); } foreach (var p in doc.Document.Descendants("pack")) { var pack = new Pack(); pack.Id = new Guid(p.Attribute("id").Value); pack.Name = p.Attribute("name").Value; pack.Definition = DeserializePack(p.Elements()); pack.SetId = ret.Id; (ret.Packs as List<Pack>).Add(pack); } foreach (var m in doc.Document.Descendants("marker")) { var marker = new Marker(); marker.Id = new Guid(m.Attribute("id").Value); marker.Name = m.Attribute("name").Value; var mpathd = new DirectoryInfo(Path.Combine(directory, "Markers")); var mpath = mpathd.Exists == false ? null : mpathd.GetFiles(marker.Id.ToString() + ".*", SearchOption.TopDirectoryOnly).First(); marker.IconUri = mpath == null ? null : Path.Combine(directory, "Markers", mpath.FullName); (ret.Markers as List<Marker>).Add(marker); } } if (ret.Cards == null) ret.Cards = new Card[0]; if (ret.Markers == null) ret.Markers = new Marker[0]; if (ret.Packs == null) ret.Packs = new Pack[0]; //Console.WriteLine(timer.ElapsedMilliseconds); return ret; }
public void Invalidate(Set set) { var s = Sets.FirstOrDefault(x => x.Id == set.Id); if (s == null) return; Db.Config.Cache.InvalidateObject(s); }
public void Save(Set set) { var game = Games.FirstOrDefault(x => x.Id == set.GameId); if (game == null) throw new Exception("Game doesn't exist!"); throw new NotImplementedException(); }
public SetListSetItem(Set set) { this.Id = set.Id; this.Markers = set.Markers; this.Name = set.Name; this.PackageName = set.PackageName; this.Packs = set.Packs; this.Version = set.Version; this.GameId = set.GameId; this.GameVersion = set.GameVersion; this.Filename = set.Filename; }
public PackContent GetCards(Pack pack, Set set) { var ret = new PackContent(); var cardList = set.Cards.ToList(); // add the include cards to the set for this booster var cards = ( from qset in pack.Includes.Select(x=>x.SetId).Distinct().SelectMany(x=>DbContext.Get().SetsById(x)) from card in qset.Cards join inc in pack.Includes on qset.Id equals inc.SetId where card.Id == inc.Id select new { Card = card, Include = inc } ) .Select(dong => { var card = new Card(dong.Card); foreach (var p in dong.Include.Properties) { var key = dong.Card.Properties[""].Properties.Where(x => x.Key.Name.ToLower() == p.Item1.ToLower()).FirstOrDefault().Key; if (key != null) // if the include property name isn't a defined custom property, ignore it card.Properties[""].Properties[key] = p.Item2; } return card; }) ; cardList.AddRange(cards); foreach (var prop in Properties) { var Key = prop.Item1; var Value = prop.Item2; var list = ( from card in cardList where card.Properties.Where(x => x.Key == "").SelectMany(x => x.Value.Properties).Any( x => x.Key.Name.ToLower() == Key.ToLower() && x.Value.ToString().ToLower() == Value.ToLower()) select card).ToList(); cardList = list; } if (Quantity < 0) { ret.UnlimitedCards.AddRange(cardList); } else { for (var i = 0; i < Quantity; i++) { var pick = cardList.RandomElement(); if (pick != null) { ret.LimitedCards.Add(pick); cardList.Remove(pick); } else { Log.Warn(String.Format("Set {0} ({1}) does not contain enough cards to create this booster pack correctly.", set.Id, set.Name)); } } } return ret; }
public void Save(Set set) { var game = Games.FirstOrDefault(x => x.Id == set.GameId); if(game == null) throw new Exception("Game doesn't exist!"); var curSet = Sets.FirstOrDefault(x => x.Id == set.Id); if (curSet != null) { var id = this.Client.Ext().GetID(curSet); this.Client.Ext().Bind(set,id); } this.Client.Store(set); this.Client.Commit(); }
public void Remove(Set set) { var s = Sets.FirstOrDefault(x => x.Id == set.Id); if (s == null) return; this.Client.Delete(s); this.Client.Commit(); }
public void UninstallSet(Set set) { foreach(var c in set.Cards()) DbContext.Get().Remove(c); DbContext.Get().Remove(set); }
public static void DeleteSet(this Game game, Set set) { SetManager.Get().UninstallSet(set); }
private static void GenerateProxyImage(this ICard card, Set set, string uri) { string cropPath = Path.Combine(Path.Combine(set.ImagePackUri, "Crops")); if (Directory.Exists(cropPath)) { var files = Directory.GetFiles(cropPath, card.GetImageUri() + ".*").OrderBy(x => x.Length).ToArray(); if (files.Length == 0) { set.GetGame().GetCardProxyDef().SaveProxyImage(card.GetProxyMappings(), uri); } else { set.GetGame().GetCardProxyDef().SaveProxyImage(card.GetProxyMappings(), uri, files.First()); } } else { set.GetGame().GetCardProxyDef().SaveProxyImage(card.GetProxyMappings(), uri); } }