public Card ReadCard(Guid id) { try { using NpgsqlConnection conn = OpenConnection(); using var cmd = new NpgsqlCommand("select \"Name\", \"Damage\", \"isLocked\", \"Element\", \"Cardtype\" from \"Card\" where \"id\"=@id", conn); cmd.Parameters.AddWithValue("@id", id); cmd.Prepare(); NpgsqlDataReader reader = cmd.ExecuteReader(); if (!reader.Read()) { return(null); } string name = reader.GetString(0); int damage = reader.GetInt32(1); bool isLocked = reader.GetBoolean(2); Element element = reader.GetFieldValue <Element>(3); Cardtype type = reader.GetFieldValue <Cardtype>(4); return(new Card(id, element, name, damage, isLocked)); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }
public override Cardtype CreateOrGetCardtype(Game game, string title, bool persist = true) { //See if we already have the cardtype Cardtype _cardtype = GetCardtype(game, title); if (_cardtype == null) { //Create and store a new cardtype _cardtype = new Cardtype(nextID, game); _cardtype.Title = title; //Put cardtype in dictionaries cardtypesByComposite.Add(BuildComposite(game, _cardtype.Title), _cardtype); cardtypesByID.Add(_cardtype.ID, _cardtype); //Put the cardtype into the XML document XElement element = new XElement("Cardtype", new XAttribute("Title", _cardtype.Title), new XAttribute("ID", _cardtype.ID)); (factory.GameRepository as XMLGameRepository).FindElementByID(game.ID).Add(element); //Update the NextCardtypeID, both here and in configuration. //This will also persist the element above if called for. nextID++; factory.ConfigurationRepository.SetValue("NextCardtypeID", Convert.ToString(nextID), persist); } return(_cardtype); }
public Trade(User user, Guid id, Card card, Cardtype cardtype, int minDamage) { this.id = id; this.user = user; this.card = card; this.cardtype = cardtype; this.minDamage = minDamage; }
public int minDamage; //minDamage of other Card public Trade(User user, Guid id, Card card, Cardtype cardtype, Element element) //two constructors for different requirements { this.id = id; this.user = user; this.card = card; this.cardtype = cardtype; this.element = element; }
public Card(Guid id, Element element, string name, int damage) { this.id = id; this.element = element; this.name = name; this.damage = damage; this.isLocked = false; this.type = name.ToLower().Contains("spell")?Cardtype.Spell:Cardtype.Monster; }
public override void DeleteCardtype(Cardtype deleted, bool cascade = false) { //Updated references and cache cardtypesByComposite.Remove(BuildComposite(deleted.Game, deleted.Title)); cardtypesByID.Remove(deleted.ID); //Remove from tree & persist FindElementByID(deleted.ID).Remove(); factory.Save(); }
internal Cardtype ParseCardtype(XElement element) { if (element == null) { return(null); } //Parent - Game Game _game = (factory.GameRepository as XMLGameRepository).ParseGame(element.Parent); Cardtype _cardtype; //ID attribute XAttribute _id = element.Attribute("ID"); if (_id == null) { //This really shouldn't be possible, but we can always fix it by setting a new ID. _cardtype = new Cardtype(nextID, _game); element.Add(new XAttribute("ID", _cardtype.ID)); nextID++; factory.ConfigurationRepository.SetValue("NextCardtypeID", Convert.ToString(nextID)); } else { //Check if the ID is stored first if (cardtypesByID.TryGetValue(Convert.ToInt32(_id.Value), out _cardtype)) { return(_cardtype); } //It's not stored, so create a new and parse it _cardtype = new Cardtype(Convert.ToInt32(_id.Value), _game); } //Attribute - title XAttribute _title = element.Attribute("Title"); if (_title != null) { _cardtype.Title = _title.Value; } //Attribute - iconPath XAttribute _iconPath = element.Attribute("IconPath"); if (_iconPath != null) { _cardtype.IconPath = _iconPath.Value; } //Add to dictionaries and parent cardtypesByID.Add(_cardtype.ID, _cardtype); cardtypesByComposite.Add(BuildComposite(_cardtype.Game, _cardtype.Title), _cardtype); _game.Cardtypes.Add(_cardtype); return(_cardtype); }
public override void UpdateCardtype(Cardtype updated, bool persist = true) { //Find the corresponding element in the document XElement element = FindElementByID(updated.ID); //Attribute - title XAttribute _title = element.Attribute("Title"); if (_title != null) { _title.Value = updated.Title; } else { element.Add(new XAttribute("Title", updated.Title)); } //TODO: Change title dictionary //Attribute - basePath XAttribute _iconPath = element.Attribute("IconPath"); if (updated.IconPath != null && !updated.IconPath.Equals("")) { if (_iconPath != null) { _iconPath.Value = updated.IconPath; } else { element.Add(new XAttribute("IconPath", updated.IconPath)); } } else { if (_iconPath != null) { _iconPath.Remove(); } } //Ignored: ID, Game //Flush the file if (persist) { factory.Save(); } }
public List <Trade> ReadTrades() { try { using NpgsqlConnection conn = OpenConnection(); using var cmd = new NpgsqlCommand("SELECT \"Id\", card, cardtype, element, \"minDamage\", player FROM \"Trade\"", conn); cmd.Prepare(); NpgsqlDataReader reader = cmd.ExecuteReader(); if (!reader.Read()) { return(null); } List <Trade> trades = new List <Trade>(); do { Guid guid = reader.GetFieldValue <Guid>(0); Guid carduuid = reader.GetFieldValue <Guid>(1); Cardtype type = reader.GetFieldValue <Cardtype>(2); Element element = reader.GetFieldValue <Element>(3); int minDamage = reader.GetInt32(4); Guid userid = reader.GetGuid(5); User user = ReadPlayer(userid); Card card = ReadCard(carduuid); if (element == default) { trades.Add(new Trade(user, guid, card, type, minDamage)); } else { trades.Add(new Trade(user, guid, card, type, element)); } } while (reader.Read()); return(trades); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }
public abstract void DeleteCardtype(Cardtype deleted, bool cascade = false);
public abstract void UpdateCardtype(Cardtype updated, bool persist = true);
public Card(int val, Cardtype type) { this.val = val; this.type = type; }