private void FillProductSupplyList(List <MarketDetail> sourcelist) { Dictionary <string, string> StockLevels = new Dictionary <string, string>(); StockLevels.Add("4", "High"); StockLevels.Add("2", "Med"); StockLevels.Add("1", "Low"); StockLevels.Add("0", ""); using (var db = new EDTSQLEntities()) { StarSystem currsystem = db.StarSystems.Single(n => n.SystemName == CurrSystem); // Get Current System Station currstation = db.Stations.Single(n => n.StationName == lblStation.Text); // Get Current Station List <TopDemands> SuppliesWithDemand = new List <TopDemands>(); int itemsfound = 0; foreach (var item in sourcelist) { //Find Systems-Stations that need (have Demand for) the product on sale List <MarketDetail> ItemLocationsWithDemand = db.MarketDetails.Where(n => n.CommodityName == item.CommodityName && n.DemandStatus > 0).ToList(); List <DemandList> DemandItemList = new List <DemandList>(); // List of Demand Locations for Supply Item with calculated profit foreach (MarketDetail DemandItem in ItemLocationsWithDemand) { StarSystem starsystem = db.StarSystems.Single(n => n.SystemID == DemandItem.SystemID); Station station = db.Stations.Single(n => n.StationID == DemandItem.StationID); // Calculate the distance between current location and possible selling system double distance = Program.SystemDistance(currsystem.SpaceX, currsystem.SpaceY, currsystem.SpaceZ, starsystem.SpaceX, starsystem.SpaceY, starsystem.SpaceZ); // Calculate estimated amount of profit per unit int profit = Convert.ToInt16((DemandItem.SellPrice ?? 0) - (item.BuyPrice ?? 0)); int sellingprice = Convert.ToInt16(DemandItem.SellPrice ?? 0); DemandItemList.Add(new DemandList { ProductName = DemandItem.CommodityName, SellSystem = starsystem.SystemName, SellStation = station.StationName, Distance = distance, SellPrice = sellingprice, Profit = profit }); } // Sort list by amount of profit , distance DemandItemList = DemandItemList.OrderByDescending(s => s.Profit).ThenBy(s => s.Distance).ToList(); // Return the best profit item if one exists if (DemandItemList.Count() > 0) { var TopDemandItem = DemandItemList.First(); if (TopDemandItem.Profit > 0) { TopDemands TopItem = new TopDemands() { CommodityName = item.CommodityName, SupplyStatus = item.SupplyStatus.ToString(), BuyPrice = item.BuyPrice.ToString(), SellSystem = TopDemandItem.SellSystem, SellSysDist = TopDemandItem.Distance, SellStation = TopDemandItem.SellStation, SellPrice = TopDemandItem.SellPrice.ToString(), Profit = TopDemandItem.Profit }; itemsfound++; lblMonitoredEvent.Text = "Found " + itemsfound + " items"; lblMonitoredEvent.Refresh(); SuppliesWithDemand.Add(TopItem); } } } // Sort the List by Supply Status then Profit per unit SuppliesWithDemand = SuppliesWithDemand.OrderByDescending(s => s.Profit).ToList(); // Add the Top 10 to the Supply List int i = 1; foreach (var prod in SuppliesWithDemand) { if (i <= 10) { var listitem = new ListViewItem(new[] { prod.CommodityName, StockLevels[prod.SupplyStatus], prod.BuyPrice, prod.SellSystem, prod.SellSysDist.ToString(), prod.SellStation, prod.SellPrice, prod.Profit.ToString() }); lvSupply.Items.Add(listitem); i++; } } db.Dispose(); } }
private void DisplayMission() { // Current date & time as seconds DateTimeOffset dto = new DateTimeOffset(DateTime.Now); var currseconds = dto.ToUnixTimeSeconds(); using (var db = new EDTSQLEntities()) { if (db.StarSystems.Count() == 0) { return; } if (CurrSystem != null) { if (CurrSystem.Contains("=") == true) { int index = CurrSystem.IndexOf("="); CurrSystem = (index > 1 ? CurrSystem.Substring(0, index - 1) : null); } } if (CurrSystem != null) { StarSystem currsystem = new StarSystem(); if (CurrSystem == "" | db.StarSystems.Count(n => n.SystemName == CurrSystem) == 0) { currsystem = new StarSystem() { SpaceX = 0, SpaceY = 0, SpaceZ = 0 }; } else { currsystem = db.StarSystems.Single(n => n.SystemName == CurrSystem); // Get Current System } lvMissions.Items.Clear(); if (db.ActiveMissions.Count() > 0) { foreach (ActiveMission amitem in db.ActiveMissions) { var remainingtime = amitem.Expiry.Value - currseconds; var timeSpan = TimeSpan.FromSeconds(remainingtime); int dy = timeSpan.Days; int hr = timeSpan.Hours; int mn = timeSpan.Minutes; int sec = timeSpan.Seconds; string expiresIn = "D:" + dy + " H:" + hr + " M:" + mn + " S:" + sec; if (db.StarSystems.Any(o => o.SystemName == amitem.DestinationSystem)) { StarSystem starsystem = db.StarSystems.Single(n => n.SystemName == amitem.DestinationSystem); // Calculate the distance between current location and possible selling system double distance = Program.SystemDistance(currsystem.SpaceX, currsystem.SpaceY, currsystem.SpaceZ, starsystem.SpaceX, starsystem.SpaceY, starsystem.SpaceZ); var amlistitem = new ListViewItem(new[] { amitem.MissionID.ToString(), amitem.MissionType, amitem.MissionCargo, amitem.DestinationSystem, distance.ToString(), amitem.DestinationStation, expiresIn }); lvMissions.Items.Add(amlistitem); } else { var amlistitem = new ListViewItem(new[] { amitem.MissionID.ToString(), amitem.MissionType, amitem.MissionCargo, amitem.DestinationSystem, "", amitem.DestinationStation, expiresIn }); lvMissions.Items.Add(amlistitem); } } } lvMissions.Sort(); lvMissions.Refresh(); } } }
private void DisplayCargo() { using (var db = new EDTSQLEntities()) { if (db.StarSystems.Count() == 0) { return; } if (CurrSystem != null) { if (CurrSystem.Contains("=") == true) { int index = CurrSystem.IndexOf("="); CurrSystem = (index > 1 ? CurrSystem.Substring(0, index - 1) : null); } } if (CurrSystem != null) { StarSystem currsystem = new StarSystem(); if (CurrSystem == "" | db.StarSystems.Count(n => n.SystemName == CurrSystem) == 0) { currsystem = new StarSystem() { SpaceX = 0, SpaceY = 0, SpaceZ = 0 }; } else { currsystem = db.StarSystems.Single(n => n.SystemName == CurrSystem); // Get Current System } lvCargo.Items.Clear(); if (db.CargoHolds.Count() > 0) { foreach (CargoHold chitem in db.CargoHolds) { //Find Systems-Stations that need (have Demand for) the product on sale List <MarketDetail> possdemand = db.MarketDetails.Where(n => n.CommodityName == chitem.CommodityName && n.DemandStatus > 0).ToList(); List <DemandList> demandlist = new List <DemandList>(); foreach (MarketDetail possitem in possdemand) { StarSystem starsystem = db.StarSystems.Single(n => n.SystemID == possitem.SystemID); Station station = db.Stations.Single(n => n.StationID == possitem.StationID); // Calculate the distance between current location and possible selling system double distance = Program.SystemDistance(currsystem.SpaceX, currsystem.SpaceY, currsystem.SpaceZ, starsystem.SpaceX, starsystem.SpaceY, starsystem.SpaceZ); // Calculate estimated amount of profit per unit int profit = Convert.ToInt16((possitem.SellPrice ?? 0) - (chitem.AvgPurchasePrice ?? 0)); int sellingprice = Convert.ToInt16(possitem.SellPrice ?? 0); demandlist.Add(new DemandList { ProductName = possitem.CommodityName, SellSystem = starsystem.SystemName, SellStation = station.StationName, Distance = distance, SellPrice = sellingprice, Profit = profit }); } // Sort list by amount of profit , distance demandlist = demandlist.OrderByDescending(s => s.Profit).ThenBy(s => s.Distance).ToList(); // Return the best profit item var cargostatus = "Tradable"; if (chitem.Stolen == true) { cargostatus = "Stolen"; } else { if (chitem.MissionCargo == true) { cargostatus = "Mission"; } } if (demandlist.Count() > 0) { var topdemand = demandlist.First(); // var chlistitem = new ListViewItem(new[] { chitem.CommodityName, chitem.Qty.ToString(), chitem.AvgPurchasePrice.ToString(), chitem.Stolen.ToString(), topdemand.SellSystem, topdemand.Distance.ToString(), topdemand.SellStation, topdemand.Profit.ToString() }); var chlistitem = new ListViewItem(new[] { chitem.CommodityName, chitem.Qty.ToString(), chitem.AvgPurchasePrice.ToString(), cargostatus, topdemand.SellSystem, topdemand.Distance.ToString(), topdemand.SellStation, topdemand.Profit.ToString() }); lvCargo.Items.Add(chlistitem); } else { // var chlistitem = new ListViewItem(new[] { chitem.CommodityName, chitem.Qty.ToString(), chitem.AvgPurchasePrice.ToString(), chitem.Stolen.ToString(), "", "0.0", "", "0" }); var chlistitem = new ListViewItem(new[] { chitem.CommodityName, chitem.Qty.ToString(), chitem.AvgPurchasePrice.ToString(), cargostatus, "", "0.0", "", "0" }); lvCargo.Items.Add(chlistitem); } } } lvCargo.Refresh(); } } }
} // ReadJournalLines() End private void ReadMarketLines() { Dictionary <string, string> CommodityInGroup = new Dictionary <string, string>(); Dictionary <string, string> RareInGroup = new Dictionary <string, string>(); using (var db = new EDTSQLEntities()) { foreach (var commod in db.Commodities) { CommodityInGroup.Add(commod.CommodityName.ToUpper(), commod.CommodGroupName); } foreach (var rare in db.RareCommodities) { RareInGroup.Add(rare.CommodityName.ToUpper(), rare.CommodGroupName); } db.Dispose(); } Dictionary <string, int> StockLevels = new Dictionary <string, int>(); StockLevels.Add("High", 4); StockLevels.Add("Med", 2); StockLevels.Add("Low", 1); StockLevels.Add("", 0); using (var sr = new StreamReader(MarketDump)) { var reader = new CsvReader(sr); reader.Configuration.HasHeaderRecord = true; reader.Configuration.Delimiter = ";"; List <MarketInfo> records = new List <MarketInfo>(); bool setdate = false; DateTime rectime = DateTime.UtcNow; // string strrectime = rectime.ToString(); int xxx = 0; while (reader.Read()) { if (setdate == false) { string strdate = reader.GetField <string>(9).Substring(0, 10); string strtime = reader.GetField <string>(9).Substring(11, 8); string strdatetime = strdate + " " + strtime; rectime = DateTime.ParseExact(strdatetime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); // strrectime = rectime.ToString(); setdate = true; } MarketInfo addrec = new MarketInfo(); addrec.StarSystem = reader.GetField <string>(0); addrec.Station = reader.GetField <string>(1); addrec.Commodity = reader.GetField <string>(2); if (int.TryParse(reader.GetField <string>(3), out xxx)) { addrec.SellPrice = int.Parse(reader.GetField <string>(3)); } else { addrec.SellPrice = xxx; } if (int.TryParse(reader.GetField <string>(4), out xxx)) { addrec.BuyPrice = int.Parse(reader.GetField <string>(4)); } else { addrec.BuyPrice = xxx; } addrec.DemandLevel = reader.GetField <string>(6); addrec.SupplyLevel = reader.GetField <string>(8); // addrec.LogDate = strrectime; addrec.LogDate = rectime; records.Add(addrec); } //Read System & Station so previous market entries can be removed if (records.Count > 0) { MarketInfo getLocation = records.First(); using (var db = new EDTSQLEntities()) { StarSystem starsystem = db.StarSystems.SingleOrDefault(p => p.SystemName == getLocation.StarSystem); Station station = db.Stations.SingleOrDefault(o => o.StationName == getLocation.Station); UpdateMonitor("Market Data - Update found"); if (station == null) { UpdateMonitor("Market Data - Station not yet known"); db.Dispose(); } else { db.MarketDetails.RemoveRange(db.MarketDetails.Where(x => x.SystemID == starsystem.SystemID && x.StationID == station.StationID)); db.SaveChanges(); foreach (MarketInfo marketcommodity in records) { if (CommodityInGroup.ContainsKey(marketcommodity.Commodity.ToUpper())) { //Add new Market detail lines db.MarketDetails.Add(new MarketDetail() { SystemID = starsystem.SystemID, StationID = station.StationID, CommodGroupName = CommodityInGroup[marketcommodity.Commodity.ToUpper()], CommodityName = marketcommodity.Commodity, SellPrice = marketcommodity.SellPrice, BuyPrice = marketcommodity.BuyPrice, DemandStatus = StockLevels[marketcommodity.DemandLevel], SupplyStatus = StockLevels[marketcommodity.SupplyLevel], EntryDate = marketcommodity.LogDate }); } else { //Add new Market detail lines db.MarketDetails.Add(new MarketDetail() { SystemID = starsystem.SystemID, StationID = station.StationID, CommodGroupName = RareInGroup[marketcommodity.Commodity.ToUpper()], CommodityName = marketcommodity.Commodity, SellPrice = marketcommodity.SellPrice, BuyPrice = marketcommodity.BuyPrice, DemandStatus = StockLevels[marketcommodity.DemandLevel], SupplyStatus = StockLevels[marketcommodity.SupplyLevel], EntryDate = marketcommodity.LogDate }); } } UpdateMonitor("Market Data - Updated"); db.SaveChanges(); db.Dispose(); MarketDirty = false; btnMDirty.BackColor = Color.Red; btnMDirty.Refresh(); //File.Delete(MarketDump); } } sr.Dispose(); ObtainTopProductsToBuy(); ObtainTopProductsDemanded(); } DisplayCargo(); } } // ReadMarketLines() End
// 4.9 Location private void JournalLocation(string jstr) { LocationEvent location = JsonConvert.DeserializeObject <LocationEvent>(jstr); //Test for in Training if (location.StarSystem == "Training") { return; } //Display System & Station from event lblStarSystem.Text = location.StarSystem; CurrSystem = location.StarSystem; lblStation.Text = location.StationName; //Update System Faction and display using (var db = new EDTSQLEntities()) { lblFaction.Text = location.SystemFaction; try { try { StarSystem ssystem = db.StarSystems.Single(p => p.SystemName == location.StarSystem); } catch { //Add System record StarSystem systemAdd = new StarSystem(); systemAdd.SystemID = 0; systemAdd.SystemName = location.StarSystem; systemAdd.SpaceX = location.StarPos[0]; systemAdd.SpaceY = location.StarPos[1]; systemAdd.SpaceZ = location.StarPos[2]; systemAdd.Allegiance = location.SystemAllegiance; systemAdd.Economy = location.SystemEconomy_Localised; systemAdd.Government = location.SystemGovernment_Localised; systemAdd.SystemFaction = location.SystemFaction; db.StarSystems.Add(systemAdd); db.SaveChanges(); lblDBSystems.Text = db.StarSystems.Count().ToString(); } StarSystem starsystem = db.StarSystems.SingleOrDefault(p => p.SystemName == location.StarSystem); if (starsystem.SystemFaction != location.SystemFaction) { starsystem.SystemFaction = location.SystemFaction; db.SaveChanges(); } //Delete all existing Factions in System if (location.Factions != null) { db.Factions.RemoveRange(db.Factions.Where(x => x.StarSystemID == starsystem.SystemID)); db.SaveChanges(); //Store array of Factions present in System foreach (var item in location.Factions) { Faction addFact = new Faction(); addFact.SSIDFaction = starsystem.SystemID.ToString() + "-" + item.Name; addFact.StarSystemID = starsystem.SystemID; addFact.FactionName = item.Name; db.Factions.Add(addFact); } db.SaveChanges(); } //Display current System Faction lblFaction.Text = starsystem.SystemFaction; } catch { lblStarSystem.Text = lblStarSystem.Text + " = not in DataBase"; lblStation.Text = ""; lblFaction.Text = ""; } db.Dispose(); } }
//4.7 FSDJump private void JournalFSDJump(string jstr) { FSDJumpEvent fsdjump = JsonConvert.DeserializeObject <FSDJumpEvent>(jstr); lblStarSystem.Text = fsdjump.StarSystem; CurrSystem = fsdjump.StarSystem; lblStation.Text = "IN SPACE"; lblFaction.Text = fsdjump.SystemFaction; lblEconomy.Text = ""; // Check for System in Database using (var db = new EDTSQLEntities()) { if (db.StarSystems.Any(o => o.SystemName == fsdjump.StarSystem)) { StarSystem starsystem = db.StarSystems.SingleOrDefault(p => p.SystemName == fsdjump.StarSystem); //Delete all existing Factions in System if (fsdjump.Factions != null) { db.Factions.RemoveRange(db.Factions.Where(x => x.StarSystemID == starsystem.SystemID)); db.SaveChanges(); //Store array of Factions present in System foreach (var item in fsdjump.Factions) { Faction addFact = new Faction(); addFact.SSIDFaction = starsystem.SystemID.ToString() + "-" + item.Name; addFact.StarSystemID = starsystem.SystemID; addFact.FactionName = item.Name; db.Factions.Add(addFact); } db.SaveChanges(); } //Update System record StarSystem systemUpdate = db.StarSystems.Where(p => p.SystemName == fsdjump.StarSystem).First(); systemUpdate.Allegiance = fsdjump.SystemAllegiance; systemUpdate.Economy = fsdjump.SystemEconomy_Localised; systemUpdate.Government = fsdjump.SystemGovernment_Localised; systemUpdate.SystemFaction = fsdjump.SystemFaction; systemUpdate.TimesVisited = systemUpdate.TimesVisited + 1; db.SaveChanges(); lblStarSystem.Text = lblStarSystem.Text + " = Visits (" + systemUpdate.TimesVisited.ToString() + ")"; } else { //Add System record StarSystem systemAdd = new StarSystem(); systemAdd.SystemID = 0; systemAdd.SystemName = fsdjump.StarSystem; systemAdd.SpaceX = fsdjump.StarPos[0]; systemAdd.SpaceY = fsdjump.StarPos[1]; systemAdd.SpaceZ = fsdjump.StarPos[2]; systemAdd.Allegiance = fsdjump.SystemAllegiance; systemAdd.Economy = fsdjump.SystemEconomy_Localised; systemAdd.Government = fsdjump.SystemGovernment_Localised; systemAdd.SystemFaction = fsdjump.SystemFaction; systemAdd.TimesVisited = 1; db.StarSystems.Add(systemAdd); db.SaveChanges(); lblDBSystems.Text = db.StarSystems.Count().ToString(); lblStarSystem.Text = lblStarSystem.Text + " = Visits (1)"; StarSystem starsystem = db.StarSystems.SingleOrDefault(p => p.SystemName == fsdjump.StarSystem); if (fsdjump.Factions != null) { //Store array of Factions present in System foreach (var item in fsdjump.Factions) { Faction addFact = new Faction(); addFact.SSIDFaction = starsystem.SystemID.ToString() + "-" + item.Name; addFact.StarSystemID = starsystem.SystemID; addFact.FactionName = item.Name; db.Factions.Add(addFact); } db.SaveChanges(); } } db.Dispose(); } }