Quantcast

Skeleton slash command implementation

James Whitehead II [01-09-11 - 16:48]
Skeleton slash command implementation
Filename
TomTomLite.lua
diff --git a/TomTomLite.lua b/TomTomLite.lua
index ca2a832..37cb0d7 100644
--- a/TomTomLite.lua
+++ b/TomTomLite.lua
@@ -383,3 +383,59 @@ function addon:WORLD_MAP_UPDATE()
         end
     end
 end
+
+--[[-------------------------------------------------------------------------
+--  Slash command registration
+-------------------------------------------------------------------------]]--
+
+SLASH_TOMTOMLITE1 = "/ttl"
+SLASH_TOMTOMLITE2 = "/tomtomlite"
+SLASH_TOMTOMLITE3 = "/tt"
+
+local wrongseparator = "(%d)" .. (tonumber("1.1") and "," or ".") .. "(%d)"
+local rightseparator =   "%1" .. (tonumber("1.1") and "." or ",") .. "%2"
+
+SlashCmdList["TOMTOMLITE"] = function(msg, editbox)
+    -- Attempt to fix any pairs of coordinates in any form so they work. This
+    -- should correctly handle cases where the user uses a number format that
+    -- is different than their current locale, i.e. someone using 34,45 when
+    -- their numeric locale expects 34.45 (for the number 35 and 45 hundreths).
+    -- Additionally, if the two numbers are separaed by a comma, i.e.
+    -- 45.34, 54.13 then this comma will be removed.
+    --
+    -- Thanks to Phanx for working out the best way to do this
+    local msgfix = msg:gsub("(%d)[%.,] (%d)", "%1 %2"):gsub(wrongseparator, rightseparator)
+
+    local tokens = {}
+    for token in msgfix:gmatch("%S+") do
+        table.insert(tokens, token)
+    end
+
+    local verb = tokens[1] and tokens[1]:lower()
+
+    if verb == "set" then
+        if not tonumber(tokens[2]) then
+            -- A zone has been specified as the first argument so find the boundary
+            local zoneEnd = 2
+            for idx, token in ipairs(tokens) do
+                if tonumber(token) then
+                    zoneEnd = idx - 1
+                    break
+                end
+            end
+
+            local zone = table.concat(tokens, " ", 2, zoneEnd)
+            local x, y, desc = unpack(tokens, zoneEnd + 1)
+
+            -- The description may be multiple tokens as well
+            if desc then
+                desc = table.concat(tokens, " ", zoneEnd + 3)
+            end
+
+            -- TODO: Try to find a match for the zone/map name
+        else
+            self:Printf(L["Usage for /ttl:"])
+            self:Printf(L["  * set [zone] <x> <y> [desc] - sets a waypoint"])
+        end
+	end
+end