/// <summary> /// Check if the slot is valid /// </summary> /// <param name="page">Zero-based page number</param> /// <param name="slot">SlotPosition to check</param> /// <returns>the slot if it's valid or eMerchantWindowSlot.Invalid if not</returns> public virtual eMerchantWindowSlot GetValidSlot(int page, eMerchantWindowSlot slot) { if (page < 0 || page >= MAX_PAGES_IN_TRADEWINDOWS) { return(eMerchantWindowSlot.Invalid); } if (slot == eMerchantWindowSlot.FirstEmptyInPage) { IDictionary itemsInPage = GetItemsInPage(page); for (int i = (int)eMerchantWindowSlot.FirstInPage; i < (int)eMerchantWindowSlot.LastInPage; i++) { if (!itemsInPage.Contains(i)) { return((eMerchantWindowSlot)i); } } return(eMerchantWindowSlot.Invalid); } if (slot < eMerchantWindowSlot.FirstInPage || slot > eMerchantWindowSlot.LastInPage) { return(eMerchantWindowSlot.Invalid); } return(slot); }
/// <summary> /// Removes an item from trade window /// </summary> /// <param name="page">Zero-based page number</param> /// <param name="slot">Zero-based slot number</param> /// <returns>true if removed</returns> public virtual bool RemoveTradeItem(int page, eMerchantWindowSlot slot) { lock (m_usedItemsTemplates.SyncRoot) { slot = GetValidSlot(page, slot); if (slot == eMerchantWindowSlot.Invalid) { return(false); } if (!m_usedItemsTemplates.Contains((page * MAX_ITEM_IN_TRADEWINDOWS) + (int)slot)) { return(false); } m_usedItemsTemplates.Remove((page * MAX_ITEM_IN_TRADEWINDOWS) + (int)slot); return(true); } }
/// <summary> /// Get the item in the specified page and slot /// </summary> /// <param name="page">The item page</param> /// <param name="slot">The item slot</param> /// <returns>Item template or null</returns> public virtual ItemTemplate GetItem(int page, eMerchantWindowSlot slot) { try { slot = GetValidSlot(page, slot); if (slot == eMerchantWindowSlot.Invalid) { return(null); } ItemTemplate item; lock (m_usedItemsTemplates.SyncRoot) { item = m_usedItemsTemplates[(int)slot + (page * MAX_ITEM_IN_TRADEWINDOWS)] as ItemTemplate; if (item != null) { return(item); } } if (m_itemsListID != null && m_itemsListID.Length > 0) { var itemToFind = GameServer.Database.SelectObjects <MerchantItem>( "`ItemListID` = @ItemListID AND `PageNumber` = @PageNumber AND `SlotPosition` = @SlotPosition", new[] { new QueryParameter("@ItemListID", m_itemsListID), new QueryParameter("@PageNumber", page), new QueryParameter("@SlotPosition", (int)slot) }).FirstOrDefault(); if (itemToFind != null) { item = GameServer.Database.FindObjectByKey <ItemTemplate>(itemToFind.ItemTemplateID); } } return(item); } catch (Exception e) { if (log.IsErrorEnabled) { log.Error("Loading merchant items list (" + m_itemsListID + ") page (" + page + ") slot (" + slot + "): ", e); } return(null); } }
/// <summary> /// Get the item in the specified page and slot /// </summary> /// <param name="page">The item page</param> /// <param name="slot">The item slot</param> /// <returns>Item template or null</returns> public virtual ItemTemplate GetItem(int page, eMerchantWindowSlot slot) { try { slot = GetValidSlot(page, slot); if (slot == eMerchantWindowSlot.Invalid) { return(null); } ItemTemplate item; lock (m_usedItemsTemplates.SyncRoot) { item = m_usedItemsTemplates[(int)slot + (page * MAX_ITEM_IN_TRADEWINDOWS)] as ItemTemplate; if (item != null) { return(item); } } if (m_itemsListID != null && m_itemsListID.Length > 0) { var itemToFind = DOLDB <MerchantItem> .SelectObject(DB.Column("ItemListID").IsEqualTo(m_itemsListID).And(DB.Column("PageNumber").IsEqualTo(page)).And(DB.Column("SlotPosition").IsEqualTo((int)slot))); if (itemToFind != null) { item = GameServer.Database.FindObjectByKey <ItemTemplate>(itemToFind.ItemTemplateID); } } return(item); } catch (Exception e) { if (log.IsErrorEnabled) { log.Error("Loading merchant items list (" + m_itemsListID + ") page (" + page + ") slot (" + slot + "): ", e); } return(null); } }
/// <summary> /// Adds an item to the merchant item list /// </summary> /// <param name="page">Zero-based page number</param> /// <param name="slot">Zero-based slot number</param> /// <param name="item">The item template to add</param> public virtual bool AddTradeItem(int page, eMerchantWindowSlot slot, ItemTemplate item) { lock (m_usedItemsTemplates.SyncRoot) { if (item == null) { return(false); } eMerchantWindowSlot pageSlot = GetValidSlot(page, slot); if (pageSlot == eMerchantWindowSlot.Invalid) { log.ErrorFormat("Invalid slot {0} specified for page {1} of TradeItemList {2}", slot, page, ItemsListID); return(false); } m_usedItemsTemplates[(page * MAX_ITEM_IN_TRADEWINDOWS) + (int)pageSlot] = item; } return(true); }
public void OnCommand(GameClient client, string[] args) { if (args.Length == 1) { DisplaySyntax(client); return; } string param = ""; if (args.Length > 2) { param = String.Join(" ", args, 2, args.Length - 2); } GameMerchant targetMerchant = null; if (client.Player.TargetObject != null && client.Player.TargetObject is GameMerchant) { targetMerchant = (GameMerchant)client.Player.TargetObject; } if (args[1].ToLower() != "create" && targetMerchant == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } switch (args[1].ToLower()) { #region Create case "create": { string theType = "DOL.GS.GameMerchant"; if (args.Length > 2) { theType = args[2]; } //Create a new merchant GameMerchant merchant = null; ArrayList asms = new ArrayList(ScriptMgr.Scripts); asms.Add(typeof(GameServer).Assembly); foreach (Assembly script in asms) { try { client.Out.SendDebugMessage(script.FullName); merchant = (GameMerchant)script.CreateInstance(theType, false); if (merchant != null) { break; } } catch (Exception e) { client.Out.SendMessage(e.ToString(), eChatType.CT_System, eChatLoc.CL_PopupWindow); } } if (merchant == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.ErrorCreateInstance", theType)); return; } //Fill the object variables merchant.X = client.Player.X; merchant.Y = client.Player.Y; merchant.Z = client.Player.Z; merchant.CurrentRegion = client.Player.CurrentRegion; merchant.Heading = client.Player.Heading; merchant.Level = 1; merchant.Realm = client.Player.Realm; merchant.Name = LanguageMgr.GetTranslation(ServerProperties.Properties.SERV_LANGUAGE, "GMCommands.Merchant.NewName"); merchant.Model = 9; //Fill the living variables merchant.CurrentSpeed = 0; merchant.MaxSpeedBase = 200; merchant.GuildName = LanguageMgr.GetTranslation(ServerProperties.Properties.SERV_LANGUAGE, "GMCommands.Merchant.NewGuildName"); merchant.Size = 50; merchant.AddToWorld(); merchant.SaveIntoDatabase(); DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Create.Created", merchant.ObjectID)); break; } #endregion Create #region Info case "info": { if (args.Length == 2) { if (targetMerchant.TradeItems == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Info.ArtListIsEmpty")); } else { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Info.ArtList", targetMerchant.TradeItems.ItemsListID)); } } break; } #endregion Info #region Save case "save": { targetMerchant.SaveIntoDatabase(); DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Save.SavedInDB")); break; } #endregion Save #region SaveList case "savelist": { string currentID = targetMerchant.TradeItems.ItemsListID; var itemList = GameServer.Database.SelectObjects <MerchantItem>("ItemListID = '" + currentID + "'"); foreach (MerchantItem merchantItem in itemList) { MerchantItem item = new MerchantItem(); item.ItemListID = GameServer.Database.Escape(args[2]); item.ItemTemplateID = merchantItem.ItemTemplateID; item.PageNumber = merchantItem.PageNumber; item.SlotPosition = merchantItem.SlotPosition; GameServer.Database.AddObject(item); } DisplayMessage(client, "New MerchantItems list saved as '" + GameServer.Database.Escape(args[2]) + "'"); break; } #endregion SaveList #region Remove case "remove": { targetMerchant.DeleteFromDatabase(); targetMerchant.Delete(); DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Remove.RemovedFromDB")); break; } #endregion Remove #region Sell case "sell": { switch (args[2].ToLower()) { #region Add case "add": { if (args.Length == 4) { try { string templateID = args[3]; targetMerchant.TradeItems = new MerchantTradeItems(templateID); targetMerchant.SaveIntoDatabase(); DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Sell.Add.Loaded")); } catch (Exception) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } } break; } #endregion Add #region Remove case "remove": { if (args.Length == 3) { targetMerchant.TradeItems = null; targetMerchant.SaveIntoDatabase(); DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Sell.Remove.Removed")); } break; } #endregion Remove #region Default default: { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } #endregion Default } break; } #endregion Sell #region Articles case "articles": { if (args.Length < 3) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } switch (args[2].ToLower()) { #region Add case "add": { if (args.Length <= 6) { try { string templateID = args[3]; int page = Convert.ToInt32(args[4]); eMerchantWindowSlot slot = eMerchantWindowSlot.FirstEmptyInPage; if (targetMerchant.TradeItems == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.ListNoFound")); return; } ItemTemplate template = GameServer.Database.FindObjectByKey <ItemTemplate>(templateID); if (template == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Add.ItemTemplateNoFound", templateID)); return; } if (args.Length == 6) { slot = (eMerchantWindowSlot)Convert.ToInt32(args[5]); } slot = targetMerchant.TradeItems.GetValidSlot(page, slot); if (slot == eMerchantWindowSlot.Invalid) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Add.PageAndSlotInvalid", page, (MerchantTradeItems.MAX_PAGES_IN_TRADEWINDOWS - 1), slot, (MerchantTradeItems.MAX_ITEM_IN_TRADEWINDOWS - 1))); return; } MerchantItem item = GameServer.Database.SelectObject <MerchantItem>("ItemListID = '" + GameServer.Database.Escape(targetMerchant.TradeItems.ItemsListID) + "' AND PageNumber = '" + page + "' AND SlotPosition = '" + slot + "'"); if (item == null) { item = new MerchantItem(); item.ItemListID = targetMerchant.TradeItems.ItemsListID; item.ItemTemplateID = templateID; item.SlotPosition = (int)slot; item.PageNumber = page; GameServer.Database.AddObject(item); } else { item.ItemTemplateID = templateID; GameServer.Database.SaveObject(item); } DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Add.ItemAdded")); } catch (Exception) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } } else { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); } break; } #endregion Add #region Remove case "remove": { if (args.Length == 5) { try { int page = Convert.ToInt32(args[3]); int slot = Convert.ToInt32(args[4]); if (page < 0 || page >= MerchantTradeItems.MAX_PAGES_IN_TRADEWINDOWS) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Remove.PageInvalid", page, (MerchantTradeItems.MAX_PAGES_IN_TRADEWINDOWS - 1))); return; } if (slot < 0 || slot >= MerchantTradeItems.MAX_ITEM_IN_TRADEWINDOWS) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Remove.SlotInvalid", slot, (MerchantTradeItems.MAX_ITEM_IN_TRADEWINDOWS - 1))); return; } if (targetMerchant.TradeItems == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.ListNoFound")); return; } MerchantItem item = GameServer.Database.SelectObject <MerchantItem>("ItemListID = '" + GameServer.Database.Escape(targetMerchant.TradeItems.ItemsListID) + "' AND PageNumber = '" + page + "' AND SlotPosition = '" + slot + "'"); if (item == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Remove.SlotInPageIsAEmpty", slot, page)); return; } GameServer.Database.DeleteObject(item); DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Remove.SlotInPageCleaned", slot, page)); } catch (Exception) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } } else { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); } break; } #endregion Remove #region Delete case "delete": { if (args.Length == 3) { try { if (targetMerchant.TradeItems == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.ListNoFound")); return; } DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Delete.DeletingListTemp")); var merchantitems = GameServer.Database.SelectObjects <MerchantItem>("ItemsListID = '" + GameServer.Database.Escape(targetMerchant.TradeItems.ItemsListID) + "'"); if (merchantitems.Count > 0) { foreach (MerchantItem item in merchantitems) { GameServer.Database.DeleteObject(item); } } DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Articles.Delete.ListDeleted")); } catch (Exception) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } } break; } #endregion Delete #region Default default: { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.CommandOverview")); return; } #endregion Default } break; } #endregion Articles #region Type case "type": { string theType = param; if (args.Length > 2) { theType = args[2]; } //Create a new merchant GameMerchant merchant = null; ArrayList asms = new ArrayList(ScriptMgr.Scripts); asms.Add(typeof(GameServer).Assembly); foreach (Assembly script in asms) { try { client.Out.SendDebugMessage(script.FullName); merchant = (GameMerchant)script.CreateInstance(theType, false); if (merchant != null) { break; } } catch (Exception e) { client.Out.SendMessage(e.ToString(), eChatType.CT_System, eChatLoc.CL_PopupWindow); } } if (merchant == null) { DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.ErrorCreateInstance", theType)); return; } //Fill the object variables merchant.X = targetMerchant.X; merchant.Y = targetMerchant.Y; merchant.Z = targetMerchant.Z; merchant.CurrentRegion = targetMerchant.CurrentRegion; merchant.Heading = targetMerchant.Heading; merchant.Level = targetMerchant.Level; merchant.Realm = targetMerchant.Realm; merchant.Name = targetMerchant.Name; merchant.Model = targetMerchant.Model; //Fill the living variables merchant.CurrentSpeed = targetMerchant.CurrentSpeed;; merchant.MaxSpeedBase = targetMerchant.MaxSpeedBase;; merchant.GuildName = targetMerchant.GuildName; merchant.Size = targetMerchant.Size; merchant.Inventory = targetMerchant.Inventory; merchant.EquipmentTemplateID = targetMerchant.EquipmentTemplateID; merchant.TradeItems = targetMerchant.TradeItems; merchant.AddToWorld(); merchant.SaveIntoDatabase(); targetMerchant.Delete(); targetMerchant.DeleteFromDatabase(); DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.Merchant.Type.Changed", param)); break; } #endregion Type } }
/// <summary> /// Check if the slot is valid /// </summary> /// <param name="page">Zero-based page number</param> /// <param name="slot">SlotPosition to check</param> /// <returns>the slot if it's valid or eMerchantWindowSlot.Invalid if not</returns> public virtual eMerchantWindowSlot GetValidSlot(int page, eMerchantWindowSlot slot) { if (page < 0 || page >= MAX_PAGES_IN_TRADEWINDOWS) return eMerchantWindowSlot.Invalid; if (slot == eMerchantWindowSlot.FirstEmptyInPage) { IDictionary itemsInPage = GetItemsInPage(page); for (int i = (int)eMerchantWindowSlot.FirstInPage; i < (int)eMerchantWindowSlot.LastInPage; i++) { if (!itemsInPage.Contains(i)) return ((eMerchantWindowSlot)i); } return eMerchantWindowSlot.Invalid; } if (slot < eMerchantWindowSlot.FirstInPage || slot > eMerchantWindowSlot.LastInPage) return eMerchantWindowSlot.Invalid; return slot; }
/// <summary> /// Get the item in the specified page and slot /// </summary> /// <param name="page">The item page</param> /// <param name="slot">The item slot</param> /// <returns>Item template or null</returns> public virtual ItemTemplate GetItem(int page, eMerchantWindowSlot slot) { try { slot = GetValidSlot(page, slot); if (slot == eMerchantWindowSlot.Invalid) return null; ItemTemplate item; lock (m_usedItemsTemplates.SyncRoot) { item = m_usedItemsTemplates[(int)slot] as ItemTemplate; if (item != null) return item; } if (m_itemsListID != null && m_itemsListID.Length > 0) { var itemToFind = GameServer.Database.SelectObject<MerchantItem>("ItemListID = '" + GameServer.Database.Escape(m_itemsListID) + "' AND PageNumber = '" + page + "' AND SlotPosition = '" + (int)slot + "'"); if (itemToFind != null) { item = GameServer.Database.FindObjectByKey<ItemTemplate>(itemToFind.ItemTemplateID); } } return item; } catch (Exception e) { if (log.IsErrorEnabled) log.Error("Loading merchant items list (" + m_itemsListID + ") page (" + page + ") slot (" + slot + "): ", e); return null; } }
/// <summary> /// Removes an item from trade window /// </summary> /// <param name="page">Zero-based page number</param> /// <param name="slot">Zero-based slot number</param> /// <returns>true if removed</returns> public virtual bool RemoveTradeItem(int page, eMerchantWindowSlot slot) { lock (m_usedItemsTemplates.SyncRoot) { slot = GetValidSlot(page, slot); if (slot == eMerchantWindowSlot.Invalid) return false; if (!m_usedItemsTemplates.Contains((int)slot)) return false; m_usedItemsTemplates.Remove((int)slot); return true; } }
/// <summary> /// Adds an item to the merchant item list /// </summary> /// <param name="page">Zero-based page number</param> /// <param name="slot">Zero-based slot number</param> /// <param name="item">The item template to add</param> public virtual bool AddTradeItem(int page, eMerchantWindowSlot slot, ItemTemplate item) { lock (m_usedItemsTemplates.SyncRoot) { if (item == null) { return false; } eMerchantWindowSlot pageSlot = GetValidSlot(page, slot); if (pageSlot == eMerchantWindowSlot.Invalid) { log.ErrorFormat("Invalid slot {0} specified for page {1} of TradeItemList {2}", slot, page, ItemsListID); return false; } m_usedItemsTemplates[(int)pageSlot] = item; } return true; }
/// <summary> /// Get the item in the specified page and slot /// </summary> /// <param name="page">The item page</param> /// <param name="slot">The item slot</param> /// <returns>Item template or null</returns> public virtual ItemTemplate GetItem(int page, eMerchantWindowSlot slot) { try { slot = GetValidSlot(page, slot); if (slot == eMerchantWindowSlot.Invalid) return null; ItemTemplate item; lock (m_usedItemsTemplates.SyncRoot) { item = m_usedItemsTemplates[(int)slot+(page*MAX_ITEM_IN_TRADEWINDOWS)] as ItemTemplate; if (item != null) return item; } if (m_itemsListID != null && m_itemsListID.Length > 0) { var itemToFind = GameServer.Database.SelectObjects<MerchantItem>("`ItemListID` = @ItemListID AND `PageNumber` = @PageNumber AND `SlotPosition` = @SlotPosition", new[] { new QueryParameter("@ItemListID", m_itemsListID), new QueryParameter("@PageNumber", page), new QueryParameter("@SlotPosition", (int)slot) }).FirstOrDefault(); if (itemToFind != null) { item = GameServer.Database.FindObjectByKey<ItemTemplate>(itemToFind.ItemTemplateID); } } return item; } catch (Exception e) { if (log.IsErrorEnabled) log.Error("Loading merchant items list (" + m_itemsListID + ") page (" + page + ") slot (" + slot + "): ", e); return null; } }