public ShopDTO Insert(ShopDTO shop) { using (var context = DataAccessHelper.CreateContext()) { if (context.shop.SingleOrDefault(c => c.NpcId.Equals(shop.NpcId)) == null) { Shop entity = Mapper.Map<Shop>(shop); context.shop.Add(entity); context.SaveChanges(); return Mapper.Map<ShopDTO>(entity); } else return new ShopDTO(); } }
public void importShops() { string filePacket = $"{_folder}\\packet.txt"; StreamReader Packet = new StreamReader(filePacket, Encoding.GetEncoding(1252)); string line; short lastMap = 0; short map = 0; int i = 0; Dictionary<int, int> dictionaryId = new Dictionary<int, int>(); while ((line = Packet.ReadLine()) != null) { string[] linesave = line.Split(' '); if (linesave.Count() > 5 && linesave[0] == "at") { lastMap = map; map = short.Parse(linesave[2]); } if (linesave.Count() > 7 && linesave[0] == "in" && linesave[1] == "2") { if (long.Parse(linesave[3]) < 10000) { NpcDTO npc = DAOFactory.NpcDAO.LoadFromMap(map).FirstOrDefault(s => s.MapId.Equals(map) && s.Vnum.Equals(short.Parse(linesave[2]))); if (npc != null) { if (!dictionaryId.ContainsKey(short.Parse(linesave[3]))) dictionaryId[short.Parse(linesave[3])] = npc.NpcId; } } } if (linesave.Count() > 6 && linesave[0] == "shop" && linesave[1] == "2") { if (dictionaryId.ContainsKey(short.Parse(linesave[2]))) { string named = ""; for (int j = 6; j < linesave.Count(); j++) { named += $"{linesave[j]} "; } named.TrimEnd(' '); ShopDTO shop = new ShopDTO { Name = named, NpcId = (short)dictionaryId[short.Parse(linesave[2])], MenuType = short.Parse(linesave[4]), ShopType = short.Parse(linesave[5]), }; if (DAOFactory.ShopDAO.LoadByNpc(shop.NpcId) == null) { DAOFactory.ShopDAO.Insert(shop); i++; } } } } Logger.Log.Info(String.Format(Language.Instance.GetMessageFromKey("SHOPS_PARSED"), i)); }
public void ImportShops() { int shopCounter = 0; List<ShopDTO> shops = new List<ShopDTO>(); foreach (string[] currentPacket in _packetList.Where(o => o.Length > 6 && o[0].Equals("shop") && o[1].Equals("2"))) { MapNpcDTO npc = DAOFactory.MapNpcDAO.LoadById(short.Parse(currentPacket[2])); if (npc == null) { continue; } string name = String.Empty; for (int j = 6; j < currentPacket.Length; j++) { name += $"{currentPacket[j]} "; } name = name.Trim(); ShopDTO shop = new ShopDTO { Name = name, MapNpcId = npc.MapNpcId, MenuType = byte.Parse(currentPacket[4]), ShopType = byte.Parse(currentPacket[5]) }; if (DAOFactory.ShopDAO.LoadByNpc(npc.MapNpcId) == null && shops.All(s => s.MapNpcId != npc.MapNpcId)) { shops.Add(shop); shopCounter++; } } DAOFactory.ShopDAO.Insert(shops); Logger.Log.Info(String.Format(Language.Instance.GetMessageFromKey("SHOPS_PARSED"), shopCounter)); }