View Single Post
04/05/14, 05:54 AM   #11
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
This is the first step for slash commands. Either have this line in your initialising function or call the function there. In essence it is saying, watch for /xrga input and process it with the function ProcessSlashCommands.

Lua Code:
  1. -- Initialise slash Commands
  2. local function InitSlashCommands(...)
  3.     SLASH_COMMANDS["/xrga"] = ProcessSlashCommands
  4. end

option is anything that appears after /xrga. So first we need to extract what is typed afterwards. This extraction code extracts up to 2 phrases from the command line so to speak. For example /xrga alert is one phrase 'alert' which becomes option[1]. /xrga zone kenarthi's roost is treated as two phrases 'zone' as option[1] and 'kenarthi's roost' as option[2].

It next tests to see if it finds no phrases so that #options equate to 0 or you type 'help' it lists the available options on screen.

It then processes the options it found and carries out the code to process them.

The two examples I have listed shows the 1 phrase option and 2 phrases option in action. /xrga update to generate the harvest data into a simple table for easy access bypassing the account information not needed for the addon to work. /xrga zone [zonename] uses that simplified harvest data to extract the information related to the zone requested, the current zone if none is requested and if somehow it thinks you are not in a zone it will list everything

Lua Code:
  1. -- Process the slash commands
  2. local function ProcessSlashCommands(option)
  3.  
  4.     -- Extract Options
  5.     local options = {}
  6.     local searchResult = { string.match(option,"^(%S*)%s*(.-)$") }
  7.     for i,v in pairs(searchResult) do
  8.         if (v ~= nil and v ~= "") then
  9.             options[i] = string.lower(v)
  10.         end
  11.     end
  12.  
  13.     -- Show what slash commands are available
  14.     if #options == 0 or options[1] == "help" then
  15.         CHAT_SYSTEM:AddMessage("XrysGatherer Slash Commands")
  16.         CHAT_SYSTEM:AddMessage("/xrga alert - toggle the harvest alert window")
  17.         return
  18.     end
  19.  
  20.     -- Execute functionality based on user requests
  21.  
  22.     -- Refresh the Harvest History table
  23.     if options[1] == "update" then
  24.         UpdateHarvestHistory()
  25.  
  26.     -- Display items gatherered in the selected zone or the current zone
  27.     elseif options[1] == "zone" then
  28.         local testItem = options[2] or GetUnitZone("player")
  29.         if testItem then
  30.             ChatMsg:AddMessage(string.format("Listing history for zone: %s",testItem))
  31.         else
  32.             ChatMsg:AddMessage("Listing history for all zones")
  33.         end
  34.         for historyIndex,historyData in pairs(harvestHistory) do
  35.             for zoneIndex,zoneData in pairs(historyData) do
  36.                 if testItem and string.lower(zoneIndex) == string.lower(testItem) or not testItem then
  37.                     for itemIndex,itemData in pairs(zoneData) do
  38.                         for indexIndex,indexData in pairs(itemData) do
  39.                             ChatMsg:AddMessage('Display the appropriate information here')
  40.                         end
  41.                     end
  42.                 end
  43.             end
  44.         end
  45. etc...

I hope that helps you figure that part out.
  Reply With Quote