mudlet机器人脚本开发基础:事件系统

Mudlet客户端提供了事件引擎,可以在特定事件发生时自动触发,方便脚本开发。

事件包括Mudlet内置系统事件和用户自定义事件,系统事件自动触发,自定义事件需要调用触发,但触发后怎么处理,需要通过registerAnonymousEventHandler指定事件处理程序。

我们先看看事件处理相关函数:

registerAnonymousEventHandler

registerAnonymousEventHandler(event name, functionReference, [one shot])

注册匿名事件处理程序,事件可以是系统事件也可以是自定义事件。如果event name是星号*,代码可以捕获所有事件,这个可以用来做开发调试和事件追踪。

如下示例,在调整Mudlet窗口大小时会自动触发系统事件sysWindowResizeEvent调用keepStaticSize函数设置窗口大小为1600x900

function keepStaticSize()
  setMainWindowSize(1600,900)
end -- keepStaticSize

registerAnonymousEventHandler("sysWindowResizeEvent", "keepStaticSize", false)

killAnonymousEventHandler

killAnonymousEventHandler(handler id)

注销注册的匿名事件处理程序,如在扩展包中注册的事件,当玩家卸载扩展包时,需要调用这个函数注销事件。

如下示例:

map = map or {}

map.registeredEvents = {
registerAnonymousEventHandler("sysDownloadDone", "map.eventHandler"),
registerAnonymousEventHandler("sysLoadEvent", "map.eventHandler"),
registerAnonymousEventHandler("sysConnectionEvent", "map.eventHandler"),
registerAnonymousEventHandler("sysInstall", "map.eventHandler"),
registerAnonymousEventHandler("sysDataSendRequest", "map.eventHandler"),
registerAnonymousEventHandler("mapOpenEvent", "map.eventHandler"),
registerAnonymousEventHandler("sysManualLocationSetEvent", "map.eventHandler"),
registerAnonymousEventHandler("sysUninstallPackage", "map.eventHandler")
}

function map.eventHandler(event, ...)
    if event == "sysDataSendRequest" then
        -- todo
    elseif event == "sysDownloadDone" and downloading then
        -- todo
    elseif event == "sysLoadEvent" or event == "sysInstall" then
        -- todo
    elseif event == "mapOpenEvent" then
        -- todo
    elseif event == "sysManualLocationSetEvent" then
        -- todo
    elseif event == "sysUninstallPackage" and arg[1] == "generic_mapper" then
        for _,id in ipairs(map.registeredEvents) do
            killAnonymousEventHandler(id)
        end
    end
end

raiseEvent

raiseEvent(name, [arguments...])

触发事件

raiseGlobalEvent

raiseGlobalEvent(event_name, [arguments...])

触发事件,和raiseEvent不同的时这个会把事件发送给所有连线的用户。

如下示例:

-- This is the function that will be called when the event is raised.
-- "event" is set to the event name.
-- "arg" is the argument(s) provided with raiseEvent/rasieGlobalEvent.
-- "profile" - Is the profile name from where the raiseGlobalEvent where triggered from
--             It is 'nil' if raiseEvent() was used.
function onMyEvent(event, arg, profile)
  echo("Event: " .. event .. "\n")
  echo("Arg  : " .. arg .. "\n")
  -- If the event is not raised with raiseGlobalEvent() profile will be 'nil'
  echo("Profile: " .. (profile or "Local") .. "\n")
end

-- Register the event handler.
registerAnonymousEventHandler("my_event", "onMyEvent")

-- Trigger only in the current profile:
raiseEvent("my_event", "Hello world!")

-- Trigger the event in all OTHER profiles:
raiseGlobalEvent("my_event", "Hello World!")

-- To review, count and extract from an unknown number of arguments received by an event:
function eventArgs(...)
  local args = {...}
  local amount = #args
  local first = args[1]
  echo("Number of arguments: " .. amount)
  echo("\nTable of all arguments: ")
  display(args)
  echo("First argument: " .. first)
  echo("\n\n")
end

Mudlet内置事件列表

  1. AdjustableContainerReposition
  2. AdjustableContainerRepositionFinish
  3. channel102Message
  4. mapModeChangeEvent 当在地图组件上切换模式(查看/编辑)时会触发此事件
  5. mapOpenEvent 当地图组件mapper被打开时会触发此事件
  6. sysAppStyleSheetChange
  7. sysConnectionEvent 当玩家连线时会触发此事件
  8. sysCustomHttpDone
  9. sysCustomHttpError
  10. sysDataSendRequest 在send(),sendAll()函数或命令行的命令发送到游戏之前触发
  11. sysDeleteHttpDone
  12. sysDeleteHttpError
  13. sysDisconnectionEvent 在玩家断线时触发
  14. sysDownloadDone 在文件下载成功后触发
  15. sysDownloadError 在文件下载失败后触发
  16. sysDownloadFileProgress 正在下载文件时触发,可以用来指示下载进度
  17. sysDropEvent
  18. sysDropUrlEvent
  19. sysExitEvent 在关闭连线配置时触发
  20. sysGetHttpDone
  21. sysGetHttpError
  22. sysInstall 在扩展包或模块安装后触发
  23. sysInstallModule 在通过模块管理器安装模块后触发
  24. sysInstallPackage 在扩展包安装后触发
  25. sysIrcMessage
  26. sysLabelDeleted
  27. sysLoadEvent
  28. sysLuaInstallModule 在通过installModule()函数安装模块后触发
  29. sysLuaUninstallModule 在通过uninstallModule()函数卸载模块后触发
  30. sysManualLocationSetEvent
  31. sysMapDownloadEvent 在地图下载并加载后触发
  32. sysPathChanged
  33. sysPostHttpDone
  34. sysPostHttpError
  35. sysProtocolDisabled
  36. sysProtocolEnabled
  37. sysPutHttpDone
  38. sysPutHttpError
  39. sysSoundFinished 在音乐播放结束后触发,嗯,可以用来做播放器自动连续放歌
  40. sysSyncInstallModule
  41. sysSyncUninstallModule
  42. sysTelnetEvent 在遇到不支持的Telnet协商时触发,可以用来自定义协商处理
  43. sysUninstall 在模块或扩展包卸载后触发
  44. sysUninstallModule 在通过模拟管理器卸载模块后触发
  45. sysUninstallPackage 在卸载扩展包后触发
  46. sysUnzipDone
  47. sysUnzipError
  48. sysUserWindowResizeEvent 在用户界面窗口大小调整后触发
  49. sysWindowMousePressEvent 在用户在主界面中点击鼠标后触发
  50. sysWindowMouseReleaseEvent 在用户在主界面中松开鼠标后触发
  51. sysWindowResizeEvent 在客户端主界面窗口大小调整后触发

事件示例:

让玩家自动断线重连

registerAnonymousEventHandler("sysDisconnectionEvent", "reconnect")

GMCP事件

file

京ICP备13031296号-4