View Single Post
08-19-22, 01:57 PM   #13
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
PriceAnswer is working, much and many thanks to the help in this forumn and on the WowDev Discord. I just got a request to accept the following syntax. The person making the request changed the trigger from "price" to "?", which is fine.
Code:
?[ItemLink] (doesn't work)
? [ItemLink] (works)
The question is about removing the space for itemLinks, not itemIDs or item names, in a non-breaking way. If I can figure out how to support some or all of these without breaking the match pattern, that would be great.

The best idea I had was adding trim() to the end of the pattern.
Code:
local pattern = "^" .. (L[db.trigger]:gsub("(%W)", "%%%1")) .. ("%s*(%d*)%s*(.*)$"):trim()
Full function code as it stands without further changes:
Lua Code:
  1. function PriceAnswer:GetOutgoingMessage(incomingMessage)
  2.     -- pattern for "price N item" incoming chat messages
  3.     local pattern = "^" .. (L[db.trigger]:gsub("(%W)", "%%%1")) .. "%s*(%d*)%s*(.*)$"
  4.  
  5.     local itemCount, tail = strmatch(incomingMessage, pattern)
  6.  
  7.     itemCount = itemCount and itemCount:trim()
  8.     tail = tail and tail:trim()
  9.  
  10.     -- get the itemID
  11.     local itemID, retOK, ret1 -- use pcall() to validate GetItemInfoInstant()
  12.     if not itemID then
  13.         retOK, ret1 = pcall(GetItemInfoInstant, tail)
  14.         if retOK then
  15.             itemID = ret1
  16.         else
  17.             retOK, ret1 = pcall(GetItemInfoInstant, tonumber(tail))
  18.             if retOK then
  19.                 itemID = ret1
  20.             end
  21.         end
  22.     end
  23.  
  24.     -- the above did not get an itemID
  25.     if not itemID then
  26.         retOK, ret1 = pcall(GetItemInfoInstant, itemCount)
  27.         if retOK then
  28.             itemID = ret1
  29.         else
  30.             retOK, ret1 = pcall(GetItemInfoInstant, tonumber(itemCount))
  31.             if retOK then
  32.                 itemID = ret1
  33.             end
  34.         end
  35.     end -- at this point it does not matter if there is no itemID
  36.  
  37.     -- convert to a TSM item string "i:12345"
  38.     local itemString
  39.     if TSM_API and TSM_API.ToItemString then
  40.         itemString = TSM_API.ToItemString(tostring(tail))
  41.         if not itemString then
  42.             itemString = TSM_API.ToItemString(tostring(itemCount))
  43.             if itemString then
  44.                 itemCount = 1
  45.             end
  46.         end
  47.         if not itemString then
  48.             if itemID then -- check if there is an itemID from the pcall()
  49.                 itemString = TSM_API.ToItemString(tostring(itemID))
  50.                 if not itemString then
  51.                     itemString = "i:" .. tostring(itemID)
  52.                 end
  53.             end
  54.         end
  55.     end
  56.  
  57.     itemCount = tonumber(itemCount) or 1
  58.     if not itemCount or itemCount < 1 then
  59.         itemCount = 1
  60.     end
  61.  
  62.     -- unrelated rest of the function code continues
  63. end

Last edited by myrroddin : 08-19-22 at 02:03 PM. Reason: adjusted code paste
  Reply With Quote