Skip to content

Instantly share code, notes, and snippets.

@demonnic
Last active November 12, 2022 03:03
Show Gist options
  • Save demonnic/a71b5b0c545ddb8c4c90c8d24d14e636 to your computer and use it in GitHub Desktop.
Save demonnic/a71b5b0c545ddb8c4c90c8d24d14e636 to your computer and use it in GitHub Desktop.
A function to simulate a gmcp event in Mudlet by name, with a given payload
--- a naive implementation which should have some type checks put in place almost certainly
-- @param eventName the event name, IE "gmcp.Char.Vitals"
-- @param payload the payload to place at the endpoint. IE { name = "TestName", hp = 1388 }
-- @param skipEvent if set to true, will create the table space but not raise the event.
-- @usage simGMCP("gmcp.Char.Vitals", {name = "Demonnic", hp = 123, maxhp = 123})
-- -- the next one will not raise an event.
-- simGMCP("gmcp.Room.Info", {name = "TestRoom", vnum = 1000, x = 0, y = 0, z = 0, area = "TestArea"}, true)
function simGMCP(eventName, payload, skipEvent)
eventName = eventName:gsub("^gmcp.", "")
local eventTokens = eventName:split("%.")
local current = gmcp
for index, token in ipairs(eventTokens) do
if index ~= #eventTokens then
if not current[token] then
current[token] = {}
end
current = current[token]
end
end
current[eventTokens[#eventTokens]] = payload
if not skipEvent then
raiseEvent("gmcp." .. eventName)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment