/// <summary> /// sell a cargo /// </summary> /// <param name="cargo">cargoDesc structure</param> /// <remarks>if we find the node, set the sold to true and update price and location sold</remarks> public void sellCargo(cargoDesc cargo) { this.errMsg = ""; try { XDocument xmlDoc = XDocument.Load(this.filename); XElement cargoNode = xmlDoc.Root.Elements("Cargo").Elements("lot").Where(r => (int)r.Attribute("cargoID") == cargo.cargoID & (string)r.Attribute("sold") == "false").FirstOrDefault(); if (cargoNode != null) { cargoNode.SetAttributeValue("sold", true); XElement sellNode = cargoNode.Element("selling"); sellNode.SetAttributeValue("system", cargo.destSEC); sellNode.SetElementValue("baseprice", cargo.basecostbuy); sellNode.SetElementValue("avMod", cargo.avSell); sellNode.SetAttributeValue("date", cargo.sellingDate); sellNode.SetElementValue("finalprice", cargo.sellingPrice); // and update the ship's coffers this.cargoHeld -= cargo.dtons; // free up the space if (this.cargoHeld < 0) this.cargoHeld = 0; // just in case this.credits += cargo.sellingPrice; // more money! XElement shipNode = xmlDoc.Element("ShipData").Element("Ship"); shipNode.SetElementValue("CargoHeld", this.cargoHeld.ToString()); shipNode.SetElementValue("credits", this.credits.ToString()); // and update the datafile xmlDoc.Save(this.filename); } else { this.errMsg = "cannot find cargo! " + cargoid.ToString(); } } catch (Exception ex) { this.errMsg = ex.Message; } }
/// <summary> /// load up the actual ship data /// </summary> /// <param name="filename">string - filename</param> private void loadData(string filename) { if (!filename.EndsWith(".xml")) { filename = filename + ".xml"; } XDocument shipdoc = XDocument.Load(filename); this.filename = filename; try { XElement shipNode = shipdoc.Element("ShipData").Element("Ship"); XElement nameNode = shipNode.Element("Name"); this.name = nameNode.Value; XElement powNode = shipNode.Element("Power"); this.power = Convert.ToInt32(powNode.Value); XElement manNode = shipNode.Element("Manuever"); this.man = Convert.ToInt32(manNode.Value); XElement jumpNode = shipNode.Element("Jump"); this.jump = Convert.ToInt32(jumpNode.Value); XElement cargoNode = shipNode.Element("Cargo"); this.cargo = Convert.ToInt32(cargoNode.Value); XElement creditNode = shipNode.Element("credits"); this.credits = Convert.ToInt32(creditNode.Value); XElement chNode = shipNode.Element("CargoHeld"); this.cargoHeld = Convert.ToInt32(chNode.Value); XElement costsNode = shipNode.Element("costs"); XElement monthlyNode = costsNode.Element("Monthly"); this.monthly = Convert.ToInt32(monthlyNode.Value); XElement jNode = costsNode.Element("perJump"); this.jumpCost = Convert.ToInt32(jNode.Value); XElement mNode = costsNode.Element("lastPaid"); this.lastPaid = mNode.Value; XElement manifestNode = shipdoc.Element("ShipData").Element("Cargo"); this.manifest = new List<cargoDesc>(); this.sold = new List<cargoDesc>(); try { foreach (XElement cargoitem in manifestNode.Elements()) { cargoDesc cd = new cargoDesc(); int.TryParse(cargoitem.Attribute("cargoID").Value, out cd.cargoID); cd.origCode = cargoitem.Attribute("origCode").Value; cd.dtons = Convert.ToInt32(cargoitem.Attribute("dTons").Value); cd.desc = cargoitem.Element("desc").Value; XElement purchaseNode = cargoitem.Element("purchase"); cd.purchasePrice = Convert.ToInt32(purchaseNode.Element("finalprice").Value); cd.origSEC = purchaseNode.Attribute("system").Value; cd.purchaseDate = purchaseNode.Attribute("date").Value; double.TryParse(purchaseNode.Element("avMod").Value, out cd.avBuy); XElement sellNode = cargoitem.Element("selling"); cd.sellingDate = sellNode.Attribute("date").Value; int.TryParse(sellNode.Element("finalprice").Value, out cd.sellingPrice); cd.destSEC = sellNode.Attribute("system").Value; if (cargoitem.Attribute("sold").Value == "false") { this.manifest.Add(cd); } else { this.sold.Add(cd); } } } catch (Exception ex) { this.errMsg = "Error in cargo loading: " + ex.Message; } XElement logNode = shipdoc.Element("ShipData").Element("Travelogue"); this.log = new List<travelDesc>(); try { foreach (XElement log in logNode.Elements()) { travelDesc td = new travelDesc(); td.system = log.Attribute("system").Value; td.date = log.Attribute("date").Value; td.notes = new List<string>(); foreach (XElement note in log.Elements()) { td.notes.Add(note.Value); } this.log.Add(td); } } catch (Exception ex) { this.errMsg += " Error in travelogue loading: " + ex.Message; } // system info (version, date, current location (SEC), sec file & last cargo ID XElement systemNode = shipdoc.Element("ShipData").Element("system"); this.version = systemNode.Attribute("version").Value; XElement dayNode = systemNode.Element("day"); this.day = Convert.ToInt32(dayNode.Value); XElement yearNode = systemNode.Element("year"); this.year = Convert.ToInt32(yearNode.Value); XElement secNode = systemNode.Element("sec"); this.sec = secNode.Value; XElement secfileNode = systemNode.Element("secfile"); this.secfile = secfileNode.Value; XElement cargoIDNode = systemNode.Element("cargoID"); int.TryParse(cargoIDNode.Value, out this.cargoid); XElement tradeDMNode = systemNode.Element("tradeDM"); int.TryParse(cargoIDNode.Value, out this.tradedm); this.sectorname = systemNode.Element("sectorName").Value; // see if there is an illegals allowed flag; if not, add it // new feature added 1/5/2010 try { this.illegals = Convert.ToBoolean(systemNode.Element("illegals").Value); } catch { this.illegals = false; systemNode.Add(new XElement("illegals", false)); shipdoc.Save(filename); } } catch (Exception ex) { this.errMsg = ex.Message; } }
/// <summary> /// add a cargo to the ship's manifest /// </summary> /// <param name="cargo">cargoDesc - cargo to add</param> public void addCargo(cargoDesc cargo) { try { XDocument xmlDoc = XDocument.Load(this.filename); xmlDoc.Element("ShipData").SetElementValue("cargoID", ++cargoid); xmlDoc.Root.Element("Cargo").Add( new XElement("lot", new XAttribute("cargoID", this.cargoid.ToString()), new XAttribute("origCode", cargo.origCode), new XAttribute("dTons", cargo.dtons), new XAttribute("sold", false), new XElement("desc", cargo.desc), new XElement("purchase", new XAttribute("date", cargo.purchaseDate), new XAttribute("system", cargo.origSEC), new XElement("baseprice", cargo.basecostbuy), new XElement("finalprice", cargo.purchasePrice), new XElement("avMod", cargo.avBuy)), new XElement("selling", new XAttribute("date", " "), new XAttribute("system", " "), new XElement("baseprice", " "), new XElement("finalprice", ""), new XElement("avMod", "")))); xmlDoc.Save(this.filename); } catch (Exception ex) { this.errMsg = ex.Message; } }